Upload real-time temperature and humidity data to LeNetCloud via ESP8266 and DHT11 via http protocol

First music Intranet site
I used is punctual atomic elite board, punctual atoms wifi module, module temperature and humidity, and dht11
Insert picture description here

Fill in the device
Insert picture description here
device settings to
Insert picture description here
add sensors
Insert picture description here

Insert picture description here
Insert picture description here

Insert picture description here

Insert picture description here
The screenshots viewed on the mobile phone of the result image are
Insert picture description here
Insert picture description here
about uploading data every 20 seconds.

Next, I will talk about the use of the WiFi module ESP8266. I just played it recently. If there is something wrong, please correct me.
I use the Punctual Atom WiFi module, but it doesn’t matter if they are all connected.

If you don’t know anything about the wifi module, I suggest you take a look at the basics of Haichuang Electronics. I think it’s very good.
Station B Haichuang Electronics’ video link

STA mode is equivalent to a mobile phone
AP mode without hotspot function is equivalent to a router
Insert picture description here
Insert picture description here

You can use the serial debugging assistant to send data manually at the beginning
Insert picture description here

Note that except for the +++ command to end the transparent transmission, do not send a new line. All other commands must be added to send a new line.
As for the code behind me, \r\n is not added because the function of the punctual atom is encapsulated and it is automatically added. , If not, please note that every AT command must add \r\n

Initializing the wifi connection is a general procedure for data transmission, but when I connected by myself, I found that wifi automatically connects every time, so I modified this step a bit.

1.检测wifi是否是正常         AT
2.关闭wifi自动连接			AT+CWAUTOCONN=0
关闭wifi自动连接 如果不写这个那么后面的wifi连接上的回应就不是OK了而是GOT IP
3.配置WIFI模式   		AT+CIPMODE=1       1为sta模式
3.完成配置重启   		AT+RST
4连接路由器       		AT+CWJAP="IOT205","IOT205iot205."
5.开启单路连接模式		AT+CIPMUX=0
注意:如果要使用透传模式,必须先进入单连接模式
6.开启透传模式		 AT+CIPMODE=1
7.建立TCP连接		AT+CIPSTART="TCP","api.lewei50.com",80
8.进入透传模式                AT+CIPSEND
….进入透传模式后AT指令就没有用了,就可以直接发送报文了
这里使用透传模式有一个好处 就是一般不会轻易断开连接,如果不使用透传模式,TCP连接容易断开,下次发数据还要重新建立连接
还需要注意两次报文的发送最好要大于10秒所以在我的代码中我的代码是延时了10秒的。
结束透传的模式必须         发送+++不加发送新行

HTTP message format

POST /api/V1/gateway/Updatesensors/01 HTTP/1.1
userkey: cc04640c49e847009c0bb227d64ad834 
Host: open.lewei50.com
Content-Length: 28
Connection: close

[{
    
    "Name":"temp","Value":26}]

POST /api/V1/gateway/Updatesensors/01 HTTP/1.1
userkey: cc04640c49e847009c0bb227d64ad834 
Host: open.lewei50.com
Content-Length: 27
Connection: close

[{
    
    "Name":"wet","Value":50}]

There are spaces in some places. Don’t get it wrong.
28 is the length of the entire string [{"Name":"temp","Value":26}] including the symbol. The
userkey is self-generated and copied directly from the
front of HTTP/1.1. That 01 is the logo of your device, you must pay attention

Initialization of wifi module

void esp8266Init(){
    
    

	//监测wifi模块是否能回复指令
 printf("1.AT\r\n");
	while(atk_8266_send_cmd("AT","OK",20)){
    
    
	   
		
	}	
   delay_ms(500);	
	//关闭wifi自动连接 如果不写这个那么后面的wifi连接上的回应就不是OK了而是GOT IP
	printf("2. CWAUTOCONN\r\n");
		while(atk_8266_send_cmd("AT+CWAUTOCONN=0","OK",20)){
    
    
	   
		
	}
		delay_ms(500);
	//配置wifi模式 AT+CWMODE=1是sta模式
  printf("3. CWMODE\r\n");
	while(atk_8266_send_cmd("AT+CWMODE=1","OK",20)){
    
    
	   
		
	}
    delay_ms(500);	
	printf("4. CWJAP\r\n");
	//连接网络
	while(atk_8266_send_cmd(ESP8266_WIFI_INFO,"OK",1000)){
    
    
	   
		
	}
	 delay_ms(1000);
	//开启单连接模式因为透传必须开启单连接模式
	printf("5. CIPMUX\r\n");
	while(atk_8266_send_cmd("AT+CIPMUX=0","OK",20)){
    
    
	   
		
	}
	delay_ms(500);
	//开启透传模式
	printf("6. CIPMODE\r\n");
	
	while(atk_8266_send_cmd("AT+CIPMODE=1","OK",20)){
    
    
	   
		
	}
	delay_ms(500);
	//建立tcp连接
	printf("7. CIPSTART\r\n");
	
	while(atk_8266_send_cmd(ESP8266_LLW_INFO,"OK",50)){
    
    
	      
		
	}
	
	delay_ms(500);
	//进入透传模式
	printf("7. CIPSEND\r\n");
	while(atk_8266_send_cmd("AT+CIPSEND","OK",20)){
    
    
	      
		
	}
	delay_ms(500);
	printf("ESP8266Init ok");


}

Functions for sending messages

	void LLW_send(u8 value1,u8 value2)
{
    
    
	//这里的temp和wet是自己在乐联网上设置的名称
	//下面是http协议的格式,注意有些地方的空格不能省略
    char p[100]="";
	  char p1[100]="";
    sprintf((char*)p,"[{\"Name\":\"temp\",\"Value\":%d}]\r\n",value1);
    u3_printf("POST /api/V1/gateway/Updatesensors/01 HTTP/1.1\r\n");
    u3_printf("userkey: cc04640c49e847009c0bb227d64ad834\r\n");
    u3_printf("Host: open.lewei50.com\r\n");
    u3_printf("Content-Length: %d\r\n",strlen(p)-2); 
    u3_printf("Connection: close\r\n");
    u3_printf("\r\n");
	  atk_8266_send_data(p,"true",20);
	  delay_ms(20);
    sprintf((char*)p1,"[{\"Name\":\"wet\",\"Value\":%d}]\r\n",value2);
     u3_printf("POST /api/V1/gateway/Updatesensors/01 HTTP/1.1\r\n");
    u3_printf("userkey: cc04640c49e847009c0bb227d64ad834\r\n");
    u3_printf("Host: open.lewei50.com\r\n");
    u3_printf("Content-Length: %d\r\n",strlen(p)-2); 
    u3_printf("Connection: close\r\n");
    u3_printf("\r\n");
	 atk_8266_send_data(p1,"true",20);
} 

Source link

Guess you like

Origin blog.csdn.net/qq_44866153/article/details/108884466