In: Physics
this is code to Temperature and Humidity Monitoring over ThingSpeak using Arduino UNO and ESP8266 i need someone explain the steps for me
#include <stdlib.h>
#include <DHT.h>
#define DHTPIN 5 // DHT data pin connected to Arduino pin 5
#define DHTTYPE DHT11 // DHT11 (DHT Sensor Type )
DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor
#define SSID "WiFi Name" // "WiFi Name"
#define PASS "WiFi Password" // "Password"
#define IP "184.106.153.149"// thingspeak.com ip
String msg = "GET /update?key=Your API Key"; //change it with your key...
float temp;
int hum;
String tempC;
int error;
void setup()
{
Serial.begin(115200); // use default 115200.
Serial.println("AT");
delay(5000);
if(Serial.find("OK")){
connectWiFi();
}
}
void loop(){
start:
error=0;
temp = dht.readTemperature();
hum = dht.readHumidity();
char buffer[10];
tempC = dtostrf(temp, 4, 1, buffer);
updateTemp();
if (error==1){
goto start;
}
delay(5000);
}
void updateTemp(){
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
Serial.println(cmd);
delay(2000);
if(Serial.find("Error")){
return;
}
cmd = msg ;
cmd += "&field1=";
cmd += tempC;
cmd += "&field2=";
cmd += String(hum);
cmd += "\r\n";
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
if(Serial.find(">")){
Serial.print(cmd);
}
else{
Serial.println("AT+CIPCLOSE");
//Resend...
error=1;
}
}
boolean connectWiFi(){
Serial.println("AT+CWMODE=1");
delay(2000);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
Serial.println(cmd);
delay(5000);
if(Serial.find("OK")){
return true;
}else{
return false;
}
}
The explanation is given with comment (//) mark in the right side: (in bold)
#include <stdlib.h> //header file containing the library definitions, inside variables, commands etc.
#include <DHT.h> //same (.h file)
#define DHTPIN 5 // DHT data pin connected to Arduino pin 5
#define DHTTYPE DHT11 // DHT11 (DHT Sensor Type )
DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor
#define SSID "WiFi Name" // "WiFi Name"
#define PASS "WiFi Password" // "Password"
#define IP "184.106.153.149" // thingspeak.com ip
String msg = "GET /update?key=Your API Key"; //change it with your key...
float temp; \\setting up the temperature sensing via WiFi channeling
int hum;
String tempC;
int error; //error measurement, accuracy of the sensor
void setup()
{
Serial.begin(115200); // use default 115200.
Serial.println("AT");
delay(5000); \\delay time
if(Serial.find("OK")){
connectWiFi(); //connection to the wifi
}
}
void loop(){
start: \\starting the system after connecting with wifi
error=0;
temp = dht.readTemperature(); //sensor reading the temperature
hum = dht.readHumidity();
char buffer[10]; //strings for temperature mesaurement
tempC = dtostrf(temp, 4, 1, buffer);
updateTemp();
if (error==1){ //if sensor doesn't read any input, start it again
goto start;
}
delay(5000); //reading delay of 5 sec
}
void updateTemp(){
String cmd = "AT+CIPSTART=\"TCP\",\""; //procedure to give input commands to temp sensor
cmd += IP;
cmd += "\",80";
Serial.println(cmd);
delay(2000);
if(Serial.find("Error")){
return;
}
cmd = msg ;
cmd += "&field1=";
cmd += tempC;
cmd += "&field2=";
cmd += String(hum);
cmd += "\r\n";
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
if(Serial.find(">")){ \\after successful measurement print/output the temp. value to the display or screen
Serial.print(cmd);
}
else{
Serial.println("AT+CIPCLOSE");
//Resend...
error=1;
}
}
boolean connectWiFi(){
Serial.println("AT+CWMODE=1");
delay(2000);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
Serial.println(cmd);
delay(5000);
if(Serial.find("OK")){
return true;
}else{
return false;
}
}