当前位置:网站首页>Esp8266wifi development board collects temperature and humidity data and uploads them to the Internet of things platform

Esp8266wifi development board collects temperature and humidity data and uploads them to the Internet of things platform

2022-06-12 01:00:00 ThingsPanel

This time, I will mainly explain how to use open source WIFI Internet of things development board ESP-12FWIFI collection DHT11 The data of digital temperature and humidity sensor passes through OLED Exhibition , And pass MQTT Use the collected temperature and humidity data JSON Send the format to the open source Internet of things platform ThingsPanel. And pass ThingsPanel The free temperature and humidity plug-in of the open source platform displays relevant data .
One 、 Hardware preparation
The hardware of this time adopts the new technology ESP-12FWIFI The Internet of things development board is implemented as hardware . Design to all hardware as follows :
ESP-12FWIFI Internet of things development board :
ESP-12F Development board
OLED display :
OLED display
DHT11 Temperature and humidity sensor :
DHT11 sensor
USB cable :
USB cable
Two 、 Hardware connection
take DHT11 The temperature and humidity sensor is plugged into ESP-12FWIFI Peripheral interface of Internet of things development board , As shown in the figure below :
WIFI Internet of things development board
take OLED The display is plugged into ESP-12FWIFI Internet of things development board 4PIN On port , As shown in the figure below :
WIFI Internet of things development board
Finally, put ESP-12FWIFI The Internet of things development board passes USB Connect the cable to the computer USB On the interface , As shown in the figure below :
WIFI Internet of things development board
3、 ... and 、 Code
Anxinco WINDOWS Construction and use instructions of development environment , Please refer to the installation instructions downloaded from the official website of Anxin :
http://aiclouds3.0-docs.aithinker.com/#/2.device/configuration_windows
After the development environment is built , Through Anxin, you can WINDOWS development environment AiThinkerIDE Open the sample code provided .
The complete code is self fetched :
link :https://pan.baidu.com/s/11arto289Rckv3zEBQ_zn2Q
Extraction code :f2dv
If you have any questions, please consult bloggers or add groups :QQ260150504
Or forum questions :
http://forum.thingspanel.cn/
Hardware Taobao purchasing address ( Jiyi Internet of things ):
https://item.taobao.com/item.htm?spm=a21dvs.23580594.0.0.3bc13d0dnUfTY4&ft=t&id=668712619774
DHT11 Temperature and humidity acquisition part code :

u8 ICACHE_FLASH_ATTR DHT11_Read_Data_Complete(void)
{
    
	u8 C_delay_time = 0;	//  Time delay 
	//  start-up DHT11 transmission _ success 
	//-----------------------------------------------------------
	if(DHT11_Start_Signal_JX() == 0)	// DHT11: Output start signal -> Receive response signal 
	{
    
		DHT11_Data_Array[0] = DHT11_Read_Byte();	//  humidity _ Integers _ part 
		DHT11_Data_Array[1] = DHT11_Read_Byte();	//  humidity _ decimal _ part 
		DHT11_Data_Array[2] = DHT11_Read_Byte();	//  temperature _ Integers _ part 
		DHT11_Data_Array[3] = DHT11_Read_Byte();	//  temperature _ decimal _ part 
		DHT11_Data_Array[4] = DHT11_Read_Byte();	//  Check byte 
		//  If this is the high level of the last bit of data , Then wait for it to end 
		//-----------------------------------------------------------
		while(GPIO_INPUT_GET(GPIO_ID_PIN(5))==1 && C_delay_time<100)
		{
    
			os_delay_us(1);		// 1us timing 
			C_delay_time++;
		}
		C_delay_time = 0 ;		//  Low level timing starts 
		//  The low level of the end signal is timed for a long time 
		//-------------------------------------------------------
		while(GPIO_INPUT_GET(GPIO_ID_PIN(5))==0 && C_delay_time<100)
		{
    
			os_delay_us(1);		// 1us timing 
			C_delay_time++;
		}
		//-----------------------------------------------------------
		if(C_delay_time >= 100)
			return 1;		//  return 1, Express : The low level of the end signal will last for a long time 
		//  data verification 
		//-----------------------------------------------
		if(	DHT11_Data_Array[4] ==
			DHT11_Data_Array[0] + DHT11_Data_Array[1] +
			DHT11_Data_Array[2] + DHT11_Data_Array[3] )
		{
    
			//  Read DHT11 End of data ,ESP8266 To take over DHT11 The signal line 
			//-----------------------------------------------------------
			//  Determine whether the temperature is  0℃ above 
			//----------------------------------------------
			if((DHT11_Data_Array[3]&0x80) == 0)
			{
    
				DHT11_Data_Array[5] = 1;		// >=0℃
			}
			else
			{
    
				DHT11_Data_Array[5] = 0;		// <0℃
				DHT11_Data_Array[3] &= 0x7F;	//  Correct the decimal part of the temperature 
			}
			return 0;	//  return 0, Express : Temperature and humidity read successfully 
		}
		else return 3;		//  return 3, Express : Check error 
	}
	//-----------------------------------------------------
	else return 2;		//  return 2, Express : start-up DHT11 transmission , Failure 
}

OLED Display part of the code :

void ICACHE_FLASH_ATTR OS_Timer_1_cb(void)
{
    
	if(DHT11_Read_Data_Complete() == 0)		//  Read DHT11 Temperature and humidity value 
	{
    
		//  The temperature exceeds 30℃,LED bright 
		//----------------------------------------------------
		if(DHT11_Data_Array[5]==1 && DHT11_Data_Array[2]>=30)
			GPIO_OUTPUT_SET(GPIO_ID_PIN(4),0);		// LED bright 
		else
			GPIO_OUTPUT_SET(GPIO_ID_PIN(4),1);		// LED destroy 
		//  Serial port output temperature and humidity 
		//---------------------------------------------------------------------------------
		if(DHT11_Data_Array[5] == 1)			//  temperature  >= 0℃
		{
    
			os_printf("\r\n humidity  == %d.%d %RH\r\n",DHT11_Data_Array[0],DHT11_Data_Array[1]);
			os_printf("\r\n temperature  == %d.%d ℃\r\n", DHT11_Data_Array[2],DHT11_Data_Array[3]);
		}
		else // if(DHT11_Data_Array[5] == 0) //  temperature  < 0℃
		{
    
			os_printf("\r\n humidity  == %d.%d %RH\r\n",DHT11_Data_Array[0],DHT11_Data_Array[1]);
			os_printf("\r\n temperature  == -%d.%d ℃\r\n",DHT11_Data_Array[2],DHT11_Data_Array[3]);
		}
		// OLED Display temperature and humidity 
		//---------------------------------------------------------------------------------
		DHT11_NUM_Char();	// DHT11 The data value is converted into a string 

		OLED_ShowString(0,2,DHT11_Data_Char[0]);	// DHT11_Data_Char[0] == 【 Humidity string 】
		OLED_ShowString(0,6,DHT11_Data_Char[1]);	// DHT11_Data_Char[1] == 【 Temperature string 】
		//sprintf(wifi_send_buf,WIFI_SEND_MESSAGE,DEVID,temp,hum);
// MQTT_OnConnected(&mqttClient, mqttConnectedCb); //  Set up 【MQTT Successfully connected 】 Another way to call a function 
	}
}

MQTT Communication upload part code :

void ICACHE_FLASH_ATTR mqtt_timer(void *arg)
{
    
	char Buf_Shadow_Report_Data[512] = {
    0};		//  Report data cache 
    MQTT_Client* client = (MQTT_Client*)arg;
    //  If it is currently [MQTT_DATA] state , Then the survival timing operation 
    //--------------------------------------------------------------------------
    if (client->connState == MQTT_DATA)		// MQTT_DATA
    {
    
//#################################################################################################################################
    	C_Report_DHT11_Data++;
    	if(C_Report_DHT11_Data>=5)		// 5 Report data every second 
    	{
    
    		C_Report_DHT11_Data = 0 ;

		if(DHT11_Read_Data_Complete() == 0)		//  Read DHT11 Temperature and humidity value 
		{
    
    	    			os_sprintf( Buf_Shadow_Report_Data,Format_JSON_TH_ESP8266_JX,TOKEN_DEVID,TOKEN_TYPE,//  format JSON character string 
    	    						DHT11_Data_Array[2],DHT11_Data_Array[3],
    								DHT11_Data_Array[0],DHT11_Data_Array[1] );

    	    			os_printf("\r\n------ Shadow_Report_Data:%s -- ",Buf_Shadow_Report_Data);			//  Data reported to the object shadow 
    	    			os_printf("Report_Data_Length:%d ------\r\n\r\n",strlen(Buf_Shadow_Report_Data));	//  Length of reported data 
    	 // MQTT_Publish(client,"thingspaenl.operation",Buf_Shadow_Report_Data,strlen(Buf_Shadow_Report_Data),0,0);
    	    			MQTT_Publish(client,"thingspaenl.operation4444",Buf_Shadow_Report_Data,strlen(Buf_Shadow_Report_Data),0,0);
    	    			system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client);	//  Arrange a task 
    	    		}
    	    	}
    	    	//#################################################################################################################################

        client->keepAliveTick ++;	//  client (ESP8266) Heartbeat count ++
        //  Judge client (ESP8266) Heartbeat count  ?>  Keep connected 1/2 Time 
        //--------------------------------------------------------------------------------------------------------------------------
        if (client->keepAliveTick>(client->mqtt_state.connect_info->keepalive/2))	//【 notes : The official routine is : Determine whether the connection duration is exceeded 】
        {
    
            client->connState = MQTT_KEEPALIVE_SEND;	// MQTT_KEEPALIVE_SEND

            system_os_post(MQTT_TASK_PRIO,0,(os_param_t)client);//  Arrange a task 
        }
    }

    //  Reconnection waiting time : After entering the reconnection request state , Need to wait 5 second , Then reconnect 
    //--------------------------------------------------------------------------
    else if (client->connState == TCP_RECONNECT_REQ)	// TCP Reconnection request ( wait for 5 second )
    {
    
        client->reconnectTick ++;	//  Reconnection timing ++
        if (client->reconnectTick > MQTT_RECONNECT_TIMEOUT)	//  Reconnection request exceeded 5 second 
        {
    
            client->reconnectTick = 0;	//  Reconnection timing  = 0
            client->connState = TCP_RECONNECT;	// TCP Reconnect the 
            system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client);	//  Arrange a task 
            //  Reconnect and wait for the timing to end 
            //-----------------------------------------------------------------
            if (client->timeoutCb)							//  Callback function not created 
                client->timeoutCb((uint32_t*)client);
        }
    }
    // TCP Send successfully / Message sending 5 The second timer is over  =>  End of message sending (sendTimeout=0)
    //----------------------------------------------------------------
    if (client->sendTimeout>0)		//  send out MQTT When the message ,sendTimeout=5
        client->sendTimeout --;		// sendTimeout Decrement per second ( until =0)
}

Four 、OLED Check the temperature and humidity data
OLED Show
5、 ... and 、ThingsPanel Platform creation business
Demo platform 、 For installation and plug-in development and installation, please refer to the official website tutorial :
http://wiki.thingspanel.cn/index.php?title=%E9%A6%96%E9%A1%B5
After successful login, start to create business , Video tutorial reference :
https://www.bilibili.com/video/BV1bu41117cX?spm_id_from=333.999.0.0
1、 Log in to the home page
 home page
2、 Business - newly added - Enter a name - preservation
 newly build
 preservation
3、 Editing business
Click Edit business
 edit
Enter the asset name 、 Asset alias 、 Choose plug-ins
 To configure

原网站

版权声明
本文为[ThingsPanel]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203011429476155.html