Wireless Temperature and Humidity Monitor With ESP8266

Hi. anyone, need help for my Wireless Temperature and Humidity Monitor With ESP8266 project,

help @ program code: connection completed and all lights on but no data output on serial monitor,

see code:

#include “ESP8266WiFi.h”
#include “WiFiClient.h”
#include “DHT.h”
#include “ESP8266WebServer.h”

#define SSID “<…>”//your network name
#define PASS “<…>”//your network password
#define IP “52.3.135.28” // thingspeak.com // nslookup domain name // 25.203.165.232
#define DHTPIN 7 // what pin the DHT11 sensor is connected to
#define DHTTYPE DHT11 // DHT 11
#define DHT11 // can Change to DHT22
#define Baud_Rate 115200 //Another common value is 9600
#define GREEN_LED 3 //optional LED’s for debugging
#define RED_LED 4 //optional LED’s for debugging
#define DELAY_TIME 5000 //time in mili-secound(ms) between posting data to ThingSpeak

//Can use a post also
String GET = “GET /update?key=<thingspeak.com/Channel/208139>&field1=”; //<your_thingspeak_chanelID_key>&field1=";
String FIELD2 = “&field2=”;
//String FIELD3 = “&field3=”;

//Adding to ThingSpeak fields 3
String FIELD3 = “&field3=”;

bool updated; // To update

ESP8266WebServer server(80);

DHT dht(DHTPIN, 11); //type of DHT you are using
// To Initialize DHT senroe above
// NOTE: working with a faster ATmega328p 16MHz Arduino chip,
// you need to increase the considering the thresholding cycle counts of 1 or 0,
// doing this you are passing a 3rd parameter for a fsater CPU with higher value.
// The default for a 16MHz AVR is about value of 6 for an Arduino due to this it,
// runs at about 84MHz of a value of 30 which works very well
// The ESP8266 processor on ESP-01, the DHT dht(DHTPIN, DHTTYPE, 11) works foe ESP8266

float humidity, temp_f; //this will read the value for the ESP8266
String webString=""; // this will display the string
// You should use “unsigned log” variable that hold time
unsigned long previousMillis = 0; // this will store the last temperature that was raed
const long interval = 2000; // the interval at whcih to read sensor

//the sevrial start “runs once”
void handle_root(){
server.send(200, “text/plain”, “From Networking Studio, read Room /temp and /humidity”);
delay(100);
}
void setup(void)
{
// Open up the Arduino IDE seriral monitor window to see reading code
// to see the serial conne-CONNECTED){ction to ESP-01 via 3.3v console cable
Serial.begin(Baud_Rate); //(-39,5 +44,7 or -40,60 +45,80); //this where your "Baud Rate will come in (115200 or 9600)
Serial.begin(115200);
dht.begin(); // To initialize temperature sensor

// Connecting to WiFi network
WiFi.begin();
Serial.print("\n\rconnect");
// To wait for the connection
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.print("");
Serial.print(“DHT Reading Server Data”);
Serial.print("Connected to ");
//Serial.print(ssid);
Serial.print("IP address: ");
Serial.print(WiFi.localIP());

server.on("/", handle_root);

server.on("/temp", { // Adding to WebServer subdirectory
gettemperature(); // To read the temperature
webString="Temperature: "+String((int)temp_f)+“F”;
server.send(200, “text/plain”, webString); // To send information to thingspeak browser
});

server.on("/humidity", { // Adding to Webserver subdirectory
gettemperature(); // to read the sensor values
webString=“Humidity: “+String((int)humidity)+”%”;
server.send(200, “text/plain”, webString); // To send data to thingspeak browser
});

server.begin();
Serial.print(“HTTP Networking Studio server started”);
}

void loop(void)
{
server.handleClient();
}

void gettemperature(){
// To wait at leaast 2 seconds between measurements and showing
// how you can change the values on the temp
// if there are different values between the current time and last time
// on sensor are bigger than the initial reading you set, this mins that the sensor worked better than delays.
// you have to unsigned long current Millis
unsigned long currentMillis = millis();

if(currentMillis - previousMillis >= interval){

// To save the last sensor readings
previousMillis = currentMillis;

Serial.println(“AT”); // To see the AT information
delay(5000); //can very e.g 10000=10ms or 5000=5ms

if(Serial.find(“OK”)){
//connect to your wifi netowork
bool connected = connectWiFi ();
}else{
if(!connected){
//failure, need to check your values and try again

Error();

}else{

Error();
}

//initalize DHT sensor
//dht.begin();
}
//this runs over and over
//void loop()
float h = dht.readHumidity();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temp_f)){
Serial.print(“Failed to read DHT sensor!”);
LightRed();
return;
}
}
}
//update ThingSpeak channel with new values
updated = updateTemp(String(f), String(h));

//if update succeeded light up green LED, else light up red LED
if(updated){
LightGreen();
}else{
LightRed();
}
//wait for delay time before attempting to post again
delay(DELAY_TIME);

}
}
bool updateTemp(String tenmpF, String humid){
//initialize your AT command string
String cmd = “AT+CIPSTART=“TCP”,”";
//add IP address and port
cmd += IP;
cmd += “”,80";

//connect
Serial.println(cmd);
delay(2000);
if(Serial.find(“Error”)){
return false;
}
}
//build GET command, ThingSpeak takes Post or Get commands for updates, I use a Get
cmd = GET;
cmd += tenmpF;
cmd += FIELD2;
cmd += humid;

//continue to add data here if you have more fields such as a light sensor
cmd += FIELD3;
cmd += <field 3 value>
cmd += “\r\n”;

//Use AT commands to send data
Serial.print(“AT+CIPSEND=”);
Serial.println(cmd.length());
if(Serial.find(">")){
//send through command to update values
Serial.print(cmd);
}else{
Serial.println(“AT+CIPCLOSE”);
}
if(Serial.find(“OK”)){
//success! Your most recent values should be online.
}
return true;
}else{
return false;
}
}
}
boolean connectWiFi(){
//set ESP8266 mode with AT commands
Serial.println(“AT+CWMODE=1”);
delay(2000);

//build connection command
String cmd=“AT+CWJAP=”";
cmd+=SSID;
cmd+="","";
cmd+=PASS;
cmd+=""";

//connect to WiFi network and wait 5 seconds
Serial.println(cmd);
delay(5000);

//if connected return true, else false
if(Serial.find(“OK”)){
return true;
}else{
return false;
}
}
void LightGreen(){
digitalWrite(RED_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
}
}
void LightRed(){
digitalWrite(GREEN_LED, LOW);
digitalWrite(RED_LED, HIGH);
}
//if an error has occurred alternate green and red leds
void Error(){
while(true){
LightRed();
delay(2000);
LightGreen();
delay(2000);
}
}
}

Serial.begin(Baud_Rate); //(-39,5 +44,7 or -40,60 +45,80); //this where your "Baud Rate will come in (115200 or 9600)
    Serial.begin(115200);

Why are you calling Serial.begin() twice?

Serial.begin(115200);
    dht.begin(); // To initialize temperature sensor
   
    // Connecting to WiFi network
    WiFi.begin();
    Serial.print("\n\rconnect");
    // To wait for the connection
    while (WiFi.status() != WL_CONNECTED){
      delay(500);
      Serial.print(".");
    }
    Serial.print("");
    Serial.print("DHT Reading Server Data");

If nothing ever appears in the Serial Monitor app (or whatever app you are using to read the serial data from the Arduino), then it would appear that one of the begin() calls had not returned. It's time to figure out which one is blocking.

Here we go, bullet format:

ESP modules will trigger the watchdog reset if they see delays of > ~3000mS.

Thingspeak doesnt like updates more frequently than 20 seconds.

Hardcoding IP's is a bad idea.

You seem to have lots of code lurking that needs removing (eg look at your multiple serial.begin statements

Thanks PaulS and Scrumfled, for your response,

I have taking out Serial.begin(115200); and changed all delays to "20000" and rerun the program again I got this output:

First time

C:\Users\dan\OneDrive\Documents\Arduino\NetworkingStudio\NetworkingStudio.ino: In function 'void gettemperature()':

NetworkingStudio:116: error: 'boolen' was not declared in this scope

boolen connected = connectWiFi();

^

NetworkingStudio:116: error: expected ';' before 'connected'

boolen connected = connectWiFi();

^

NetworkingStudio:118: error: 'connected' was not declared in this scope

if(!connected){

^

NetworkingStudio:121: error: 'Error' was not declared in this scope

Error();

^

NetworkingStudio:125: error: 'Error' was not declared in this scope

Error();

^

NetworkingStudio:139: error: 'LightRed' was not declared in this scope

LightRed();

^

C:\Users\dan\OneDrive\Documents\Arduino\NetworkingStudio\NetworkingStudio.ino: At global scope:

NetworkingStudio:145: error: 'updated' does not name a type

updated = updateTemp(String(f), String(h));

^

NetworkingStudio:148: error: expected unqualified-id before 'if'

if(updated){

^

NetworkingStudio:150: error: expected unqualified-id before 'else'

}else{

^

NetworkingStudio:154: error: expected constructor, destructor, or type conversion before '(' token

delay(DELAY_TIME);

^

NetworkingStudio:156: error: expected declaration before '}' token

}

^

exit status 1
'boolen' was not declared in this scope

second time

C:\Users\dan\OneDrive\Documents\Arduino\NetworkingStudio\NetworkingStudio.ino: In function 'void gettemperature()':

NetworkingStudio:116: error: 'boolen' was not declared in this scope

boolen connected = connectWiFi();

^

NetworkingStudio:116: error: expected ';' before 'connected'

boolen connected = connectWiFi();

^

NetworkingStudio:118: error: 'connected' was not declared in this scope

if(!connected){

^

NetworkingStudio:121: error: 'Error' was not declared in this scope

Error();

^

NetworkingStudio:125: error: 'Error' was not declared in this scope

Error();

^

NetworkingStudio:139: error: 'LightRed' was not declared in this scope

LightRed();

^

C:\Users\dan\OneDrive\Documents\Arduino\NetworkingStudio\NetworkingStudio.ino: At global scope:

NetworkingStudio:145: error: 'updated' does not name a type

updated = updateTemp(String(f), String(h));

^

NetworkingStudio:148: error: expected unqualified-id before 'if'

if(updated){

^

NetworkingStudio:150: error: expected unqualified-id before 'else'

}else{

^

NetworkingStudio:154: error: expected constructor, destructor, or type conversion before '(' token

delay(DELAY_TIME);

^

NetworkingStudio:156: error: expected declaration before '}' token

}

^

exit status 1
'boolen' was not declared in this scope

boolen connected = connectWiFi();

Pick boolean or bool - compiler doesn't know "boolen"

Thanks Dan95,

what should I do? at this point, I have this after changing to “Boolean”,

see output:

C:\Users\dan\OneDrive\Documents\Arduino\NetworkingStudio\NetworkingStudio.ino: In function ‘void gettemperature()’:

NetworkingStudio:116: error: ‘connectWiFi’ was not declared in this scope

boolean connected = connectWiFi();

^

NetworkingStudio:118: error: ‘connected’ was not declared in this scope

if(!connected){

^

NetworkingStudio:121: error: ‘Error’ was not declared in this scope

Error();

^

NetworkingStudio:125: error: ‘Error’ was not declared in this scope

Error();

^

NetworkingStudio:139: error: ‘LightRed’ was not declared in this scope

LightRed();

^

C:\Users\dan\OneDrive\Documents\Arduino\NetworkingStudio\NetworkingStudio.ino: At global scope:

NetworkingStudio:145: error: ‘updated’ does not name a type

updated = updateTemp(String(f), String(h));

^

NetworkingStudio:148: error: expected unqualified-id before ‘if’

if(updated){

^

NetworkingStudio:150: error: expected unqualified-id before ‘else’

}else{

^

NetworkingStudio:154: error: expected constructor, destructor, or type conversion before ‘(’ token

delay(DELAY_TIME);

^

NetworkingStudio:156: error: expected declaration before ‘}’ token

}

^

exit status 1
‘connectWiFi’ was not declared in this scope

Hi,
When you wrote this code, did you do it in stages, getting each stage to work then going onto the next?

What everybody is trying to tell you is;
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Tom... :slight_smile:

Hi. Nick,

Thanks for your advice and information, going through the steps are very helpful,

Apology to everyone for inappropriate use of the forum.

Can you pls help me with my code?

Below is my code, error messages and Image files attached:

 void gettemperature(){
      // To wait at leaast 2 seconds between measurements and showing 
          // you have to unsigned long current Millis
      unsigned long currentMillis = millis();
      
      if(currentMillis - previousMillis >= interval){
        
        //  To save the last sensor readings
        previousMillis = currentMillis;
        
        Serial.println("AT"); // To see the AT information
        delay(20000); //can very e.g 10000=10ms or 5000=5ms
        
        if(Serial.find("OK")){
           //connect to your wifi netowork
            boolean connected = connectWiFi();
            }else{
          if(!connected){
            //failure, need to check your values and try again
            
            Error();
            
        }else{
            
            Error();
    }
    
    //initalize DHT sensor
    //dht.begin(); 
    }
    //this runs over and over 
    //void loop()
    float h = dht.readHumidity(); 
    // Read temperature as Fahrenheit (isFahrenheit = true)
    float f = dht.readTemperature(true);
    // Check if any reads failed and exit early (to try again).
    if (isnan(humidity) || isnan(temp_f)){
      Serial.print("Failed to read DHT sensor!");
      LightRed();
      return;
      }
    }
   }
   //update ThingSpeak channel with new values 
   updated = updateTemp(String(f), String(h));
   
   //if update succeeded light up green LED, else light up red LED 
   if(updated){
    LightGreen(); 
   }else{ 
     LightRed();
   }
   //wait for delay time before attempting to post again
   delay(DELAY_TIME);
   
   }
 }

I guess, I'm doing many things at a time, and want to get things quicker, due to time frame.

I got these error messages: Errors pasted are inside code tags.

C:\Users\dan\OneDrive\Documents\Arduino\NetworkingStudio\NetworkingStudio.ino: In function 'void gettemperature()':

NetworkingStudio:116: error: 'connectWiFi' was not declared in this scope

             boolean connected = connectWiFi();

                                             ^

NetworkingStudio:118: error: 'connected' was not declared in this scope

           if(!connected){

               ^

NetworkingStudio:121: error: 'Error' was not declared in this scope

             Error();

                   ^

NetworkingStudio:125: error: 'Error' was not declared in this scope

             Error();

                   ^

NetworkingStudio:139: error: 'LightRed' was not declared in this scope

       LightRed();

                ^

C:\Users\dan\OneDrive\Documents\Arduino\NetworkingStudio\NetworkingStudio.ino: At global scope:

NetworkingStudio:145: error: 'updated' does not name a type

    updated = updateTemp(String(f), String(h));

    ^

NetworkingStudio:148: error: expected unqualified-id before 'if'

    if(updated){

    ^

NetworkingStudio:150: error: expected unqualified-id before 'else'

    }else{ 

     ^

NetworkingStudio:154: error: expected constructor, destructor, or type conversion before '(' token

    delay(DELAY_TIME);

         ^

NetworkingStudio:156: error: expected declaration before '}' token

    }

    ^

exit status 1
'connectWiFi' was not declared in this scope

Below are wiring Arduino Uno, DHT11 and ESP8266 Image files link attached:

ESP8266.zip (189 KB)

This can't be all of your code.

Below are all my code for this project:

#include "ESP8266WiFi.h"
  #include "WiFiClient.h"
  #include "DHT.h"
  #include "ESP8266WebServer.h"

  #define SSID "<.................>"//your network name
  #define PASS "<.................>"//your network password
  #define IP "52.71.144.164" // thingspeak.com // nslookup domain name // 25.203.165.232//54.152.31.164
  #define DHTPIN 7 // what pin the DHT11 sensor is connected to
  #define DHTTYPE DHT11   // DHT 11 
  #define DHT11 // can Change to DHT22
  #define Baud_Rate 115200 //Another common value is 9600
  #define GREEN_LED 3 //optional LED's for debugging
  #define RED_LED 4 //optional LED's for debugging
  #define DELAY_TIME 5000 //time in mili-secound(ms) between posting data to ThingSpeak

  
  //Can use a post also
  String GET = "GET /update?key=/api.thingspeak.com/Channels/208139/&field1=3"; //<your_thingspeak_chanelID_key>&field1=";
  String FIELD2 = "&field2=";
  String FIELD3 = "&field3=";
  
  //Adding to ThingSpeak fields 3
  //String FIELD3 = "&field3=";
  
  boolean updated; // To update

  ESP8266WebServer server(80);
  
  DHT dht(DHTPIN, 11); //type of DHT you are using
  // To Initialize DHT senroe above
  // NOTE: working with a faster ATmega328p 16MHz Arduino chip,
  // you need to increase the considering the thresholding cycle counts of 1 or 0,
  // doing this you are passing a 3rd parameter for a fsater CPU with higher value.
  // The default for a 16MHz AVR is about value of 6 for an Arduino due to this it,
  // runs at about 84MHz of a value of 30 which works very well
  // The ESP8266 processor on ESP-01, the DHT dht(DHTPIN, DHTTYPE, 11) works foe ESP8266 
  
  
  float humidity, temp_f; //this will read the value for the ESP8266
  String webString=""; // this will display the string
  // You should use "unsigned log" variable that hold time
  unsigned long previousMillis = 0; // this will store the last temperature that was raed
  const long interval = 20000;  // the interval at whcih to read sensor
  
  //the sevrial start "runs once"
  void handle_root(){
    server.send(200, "text/plain", "From Networking Studio, read Room /temp and /humidity");
    delay(20000);
  }
   void setup(void)
    {
    // Open up the Arduino IDE seriral monitor window to see reading code
    // to see the serial conne-CONNECTED){ction to ESP-01 via 3.3v console cable
    Serial.begin(Baud_Rate); //(-39,5 +44,7 or -40,60 +45,80); //this where your "Baud Rate will come in (115200 or 9600)
    //dht.begin(); // To initialize temperature sensor
    
    // Connecting to WiFi network
    WiFi.begin();
    Serial.print("\n\rconnect");
    // To wait for the connection
    while (WiFi.status() != WL_CONNECTED){
      delay(20000);
      Serial.print(".");
    }
    Serial.print("");
    Serial.print("DHT Reading Server Data");
    Serial.print("Connected to ");
    //Serial.print(ssid);
    Serial.print("IP address: ");
    Serial.print(WiFi.localIP());
    
    server.on("/", handle_root);
    
    server.on("/temp", [](){ // Adding to WebServer subdirectory
      gettemperature();  // To read the temperature
      webString="Temperature: "+String((int)temp_f)+"F";
      server.send(200, "text/plain", webString);  // To send information to thingspeak browser
    });

    server.on("/humidity", [](){  // Adding to Webserver subdirectory
      gettemperature();  // to read the sensor values
      webString="Humidity: "+String((int)humidity)+"%";
      server.send(200, "text/plain", webString); // To send data to thingspeak browser
    });

    server.begin();
    Serial.print("Networking Studio server started");
    }
    
    void loop(void)
    {
      server.handleClient();
    }
    
    void gettemperature(){
      // To wait at leaast 2 seconds between measurements and showing 
      // you have to unsigned long current Millis
      unsigned long currentMillis = millis();
      
      if(currentMillis - previousMillis >= interval){
        
        //  To save the last sensor readings
        previousMillis = currentMillis;
        
        Serial.println("AT"); // To see the AT information
        delay(20000); //can very e.g 10000=10ms or 5000=5ms
        
        if(Serial.find("OK")){
           //connect to your wifi netowork
            boolean connected = connectWiFi();
            }else{
          if(!connected){
            //failure, need to check your values and try again
            
            Error();
            
        }else{
            
            Error();
    }
    
    //initalize DHT sensor
    dht.begin(); 
    }
    //this runs over and over 
    //void loop()
    float h = dht.readHumidity(); 
    // Read temperature as Fahrenheit (isFahrenheit = true)
    float f = dht.readTemperature(true);
    // Check if any reads failed and exit early (to try again).
    if (isnan(humidity) || isnan(temp_f)){
      Serial.print("Failed to read DHT sensor!");
      LightRed();
      return;
      }
    }
   }
   //update ThingSpeak channel with new values 
   updated = updateTemp(String(f), String(h));
   
   //if update succeeded light up green LED, else light up red LED 
   if(updated){
    LightGreen(); 
   }else{ 
     LightRed();
   }
   //wait for delay time before attempting to post again
   delay(DELAY_TIME);
   
   }
 }
 boolean updateTemp(String tenmpF, String humid){
  //initialize your AT command string
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  //add IP address and port
  cmd += IP;
  cmd += "\",80";
   
  //connect
  Serial.println(cmd);
  delay(20000);
  if(Serial.find("Error")){
    return false; 
   } 
 } 
 //build GET command, ThingSpeak takes Post or Get commands for updates, I use a Get 
 cmd = GET;
 cmd += tenmpF;
 cmd += FIELD2;
 cmd += humid;
 
 //continue to add data here if you have more fields such as a light sensor 
 cmd += FIELD3; 
 cmd += <field 3 value>
 cmd += "\r\n";
 
 //Use AT commands to send data
 Serial.print("AT+CIPSEND="); 
 Serial.println(cmd.length());
 if(Serial.find(">")){
  //send through command to update values
  Serial.print(cmd);
  }else{
    Serial.println("AT+CIPCLOSE");
  }
  if(Serial.find("OK")){
    //success! Your most recent values should be online.
    }
    return true;
    }else{
      return false;
      }
    }
   }
   boolean connectWiFi(){
    //set ESP8266 mode with AT commands 
    Serial.println("AT+CWMODE=1"); 
    delay(20000);
    
    //build connection command 
    String cmd="AT+CWJAP=\""; 
    cmd+=SSID; 
    cmd+="\",\""; 
    cmd+=PASS; 
    cmd+="\""; 
    
    //connect to WiFi network and wait 5 seconds 
    Serial.println(cmd); 
    delay(20000); 
    
    //if connected return true, else false 
    if(Serial.find("OK")){ 
      return true;
      }else{
        return false;
        }
   }
    void LightGreen(){
      digitalWrite(RED_LED, LOW);
      digitalWrite(GREEN_LED, HIGH);
      }
  }
  void LightRed(){
    digitalWrite(GREEN_LED, LOW);
    digitalWrite(RED_LED, HIGH); 
  }
  //if an error has occurred alternate green and red leds 
  void Error(){
    while(true){
      LightRed();
      delay(20000);
      LightGreen();
      delay(20000);
      }
    }
   }

Below are the Images:

ESP8266 Images.zip (223 KB)

void Error(){
    while(true){
      LightRed();
      delay(20000);
      LightGreen();
      delay(20000);
      }
    }
   }

It looks to me like there are too many } there.

The code blocks delimited with { and } are all over the place - too many and too few.