Arduino temperature, humidity and smoke monitoring upload ThingSpeak_Esp8266

1. Project introduction

This project uses Arduino as the main control board, uses the ESP-01 (ESP8266) module for network communication, and uploads the data to the Thing Speak website for real-time monitoring and viewing. Thing Speak will display it as a line graph (visual interface).
Thing Speak Display Section
insert image description here

Finally, three parts of data are uploaded and displayed at the same time
insert image description here

2. Preparations

1. Hardware part

Arduino UNO (factory new)
esp8266-01
MQ smoke sensor DHT11
temperature and humidity sensor

1.2 ESP8266-01 configuration (emphasis)

Esp-01 schematic diagram
insert image description here
(CH_PD = EN in different PS boards)
Before this, we need to flash the ESP-01 into the AT firmware , and we need to call the AT firmware
for the subsequent code. For the tutorial on flashing the Esp8266-01 into the AT firmware, please refer to My other blog has a detailed tutorial

1.3 Wiring

3. Software part

First, we need to register on the Thing Speak official website . After registration
insert image description here
, enter the personal channel
in Channels-My Channels
insert image description here
and then NEW a Channels above
insert image description here
. Note: When selecting the field, you must select three, because we have to upload the data of three sensors
and enter the API keys after creation.
Write API Keys (we need to upload the sensor data need to be Write )
insert image description here

After that, we have to set the permission of public View to public to facilitate the access of the outside world to us, and then add visual components and components in Add Visualizations (Add Widgets is to add small components, if necessary, you can perform visual operations when adding components) To add components, we choose a line chart. Here, you can set the title name in the Title , and the Date Min/Max on the right can set the upper and lower limits of the monitored values. The rest of the settings can be customized by yourself.

insert image description here

insert image description here

3. Code

code analysis

esp8266-01AT firmware part

Connect to Wi-Fi section:

String AP = "Wi-Fi名称";       // Wi-Fi名称
String PASS = "Wi-Fi密码"; // Wi-Fi密码
String API = "Write-API——KEY";   // 频道的API KEY
String HOST = "api.thingspeak.com"; //连接到Thing Speak官网
String PORT = "80";	//端口80
int countTrueCommand;
int countTimeCommand; 
boolean found = false; 
int valSensor = 1;

Use AT firmware to connect Wi-Fi commands
The following commands use AT firmware commands. For details, please refer to the AT firmware documentation on Espressif's official website.

sendCommand("AT",5,"OK"); //发送AT返回OK确定AT固件正常
sendCommand("AT+CWMODE=1",5,"OK");//发送设置模式1
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");

full code

#include <SoftwareSerial.h> //调用
#include <dht11.h> //调用温湿度库
#include <Arduino.h> //调用

#define RX 2 //定义Arduino的RX引脚为2
#define TX 3	//定义Arduino的TX引脚为3

#define Sensor_A0 A0	//定义烟雾传感器的AO引脚
#define Sensor_D0 7		//定义烟雾传感器的DO引脚
unsigned int sensorValue = 0;


#define dht_apin 11 // 温湿度传感器连接模拟引脚
dht11 dhtObject;
String AP = "Wi-Fi名称";       // Wi-Fi名称
String PASS = "Wi-Fi密码"; // Wi-Fi密码
String API = "Write-API——KEY";   // 频道的API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
int countTrueCommand;
int countTimeCommand; 
boolean found = false; 
int valSensor = 1;
  
SoftwareSerial esp8266(RX,TX); 
  
void setup() {
    
    
  pinMode(Sensor_D0, INPUT);
  Serial.begin(9600);
  esp8266.begin(115200);
  sendCommand("AT",5,"OK");
  sendCommand("AT+CWMODE=1",5,"OK");
  sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}

void loop() {
    
    
  
 String getData = "GET /update?api_key="+ API +"&field1="+getTemperatureValue()+"&field2="+getHumidityValue()+"&field3="+SmokeValue();
 sendCommand("AT+CIPMUX=1",5,"OK");
 sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
 sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
 esp8266.println(getData);delay(1500);countTrueCommand++;
 sendCommand("AT+CIPCLOSE=0",5,"OK");
}


String getTemperatureValue(){
    
    

   dhtObject.read(dht_apin);
   Serial.print(" Temperature(C)= ");
   int temp = dhtObject.temperature;
   Serial.println(temp); 
   delay(50);
   return String(temp); 
  
}


String getHumidityValue(){
    
    

   dhtObject.read(dht_apin);
   Serial.print(" Humidity in %= ");
   int humidity = dhtObject.humidity;
   Serial.println(humidity);
   delay(50);
   return String(humidity); 
  
}

String SmokeValue(){
    
    
   sensorValue = analogRead(Sensor_A0);
   Serial.print(" Somke : ");
   Serial.print(sensorValue);
   int smoke = sensorValue;
   Serial.println(smoke); 
   delay(50);
   return String(smoke); 
  
}

void sendCommand(String command, int maxTime, char readReplay[]) {
    
    
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    
    
    esp8266.println(command);//at+cipsend
    if(esp8266.find(readReplay))//ok
    {
    
    
      found = true;
      break;
    }
  
    countTimeCommand++;
  }
  
  if(found == true)
  {
    
    
    Serial.println("OYI");
    countTrueCommand++;
    countTimeCommand = 0;
  }
  
  if(found == false)
  {
    
    
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
  
  found = false;
 }

Guess you like

Origin blog.csdn.net/weixin_50679163/article/details/116564399