nodemcu+DHT11上传数据到自己的远程服务器

前言:近期做了一个物联网项目,我是做硬件部分的,任务是把DHT11测得的温湿度数据上传到远程服务器,因为以前没有接触过这些,互联网知识什么也不懂,前期做的很艰难,后来也是翻遍csdn,找到了些别人的资料,才把这个项目做完。发这篇博客的目的是怕代码丢了,以免以后忘记,有什么错误之处。还请批评指正

所需物料:家里的路由器、一台能连WIFI的电脑、一个nodemcu开发板、一根安卓数据线、两个DHT11模块、杜邦线若干

代码如下,我怕解释不好,就不做解释了

#include   
#include 
#include "DHT11.h" 
#include "stdlib.h"
WiFiClient client;
 
DHT11 my1DHT11(D4);
DHT11 my2DHT11(D5);

const char *ssid     = "Tenda_1C50E8";//
const char *password = "12345678";//
const char *host = "115.29.142.200";//服务器IP
const int httpPort =89;//端口号
String line ="";
int m=0; 
int i=0;

float wendu1,wendu2,shidu1,shidu2;

void setup() {
  Serial.begin(115200);
  delay(10);
  pinMode(relay1,OUTPUT);
  // We start by connecting to a WiFi network
 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  while (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    //return;
    delay(500);
  }
  Serial.print("connecting to ");
  Serial.println(host);
 // delay(1000);

}
 

void loop() 
{  
    
    my1DHT11.DHT11_Read();                //读取温湿度值
    
   String wendu1=String(my1DHT11.TEM_Buffer_Int);
   String shidu1=String(my1DHT11.HUMI_Buffer_Int);
    my2DHT11.DHT11_Read();                //读取温湿度值
   String wendu2=String(my2DHT11.TEM_Buffer_Int);
   String shidu2=String(my2DHT11.HUMI_Buffer_Int);
    //Serial.println(wendu1);
    //Serial.println(shidu1);
    //Serial.println(wendu2);
   // Serial.println(shidu2); 

       String a1=wendu1;
         String b1=shidu1;
           Serial.println("a1==="+a1);
             Serial.println("b1======"+b1);
          client.println("GET /device?number=S001&wendu="+wendu1+"&shidu="+shidu1+" HTTP/1.1");
            client.println("Host:115.29.142.200");
               client.println();
           Serial.println("S001发送成功======");


       String a2=wendu2;
         String b2=shidu2;
           Serial.println("a2==="+a2);
             Serial.println("b2======"+b2);
          client.println("GET /device?number=S002&wendu="+wendu2+"&shidu="+shidu2+" HTTP/1.1");
            client.println("Host:115.29.142.200");
               client.println();
           Serial.println("S002发送成功======");
     delay(1000);

你可能感兴趣的:(Arduino,nodemcu)