• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

"readtemperaturehumidity" was not declared

Q&A Forum › Category: Home Automation › "readtemperaturehumidity" was not declared
0 Vote Up Vote Down
fredmoss asked 6 years ago

I’m receiving the following compile error in this section of the code
 
“SmokeDetector:133: error: ‘readTemperatureHumidity’ was not declared in this scope
readTemperatureHumidity();”
 
 
WiFiClient client = server.available();
// When a client is available
if(client){
Serial.println(“New client”);
// Reads temperature and humidity
readTemperatureHumidity();
// Bolean to locate when the http request ends
boolean blank_line = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
header += c;
if (c == ‘\n’ && blank_line) {

Question Tags: compile error
6 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

Hi Fred.

Are you sure you’ve copied the entire code?

The readTemperatureHumidity() function is defined at the end of the code at line 300: https://gist.github.com/RuiSantosdotme/f021ba834a4db70d7313605d89d34e6e#file-esp8266_build_a_powerful_web_server-ino-L300

That error means that the code can’t find that function. Probably because you haven’t copied the entire code to your Arduino IDE. You can copy the code from here if it is easier:

Let me know if this solves your problem.

Regards,
Sara

0 Vote Up Vote Down
fredmoss answered 6 years ago

The entire code is loaded into the IDE, this call to the subroutine around line 120
// Listenning for new clients
WiFiClient client = server.available();
// When a client is available
if(client){
Serial.println(“New client”);
// Reads temperature and humidity
readTemperatureHumidity();
// Bolean to locate when the http request ends
 
And the actual subroutine itself around line 300.
void readTemperatureHumidity(){
// Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(“Failed to read from DHT sensor!”);
return;
}
 
Missing code doesn’t seem to be the problem.  Could this be an issue with the library?
 
Thanks!

0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

Hi fred.
What is the version of Arduino IDE that you are using?
I’ve tried to compile the sketch you refer and it works fine.
Regards,
Sara

0 Vote Up Vote Down
fredmoss answered 6 years ago

Thanks, Sara…

I’m using IDE 1.6.9.  I’ll cut and past the entire code and the error message to see if that makes it easier to see what might be going wrong.

When I printed out the code you supplied and compared it to what is in my IDE, I could find no differences (except where I commented out the calls to subroutines to allow the compile to complete.

Thanks! for all your help.

Fred

/*********
Rui Santos
Complete project details at http://randomnerdtutorials.com and http://rntlab.com
*********/
// Loading libraries
#include <ESP8266WiFi.h>
#include "DHT.h"
// Uncomment one of the lines below for whatever DHT sensor type you're using
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
// REPLACE THOSE VARIABLES WITH YOUR SSID and PASSWORD
const char* ssid = "";
const char* password = "";
// Web server runs on port 8888
WiFiServer server(8888);
// Header page
String header;
// GPIOs variables
String output1State = "Off";
String output2State = "Off";
int output1 = 16;
int output2 = 2;
// DHT sensor pin
const int DHTPin = 5;
// Initialize DHT sensor.
DHT dht(DHTPin, DHTTYPE);
// Temporary variables to store DHT data
static char celsiusTemp[7];
static char fahrenheitTemp[7];
static char humidityTemp[7];
// Smoke sensor pin
int smokePin = A0;
// Smoke threshold, you can adjust this value for your own readings
int smokeThres = 60;
// PIR mtion sensor
const int motionSensor = 4;
// Control variables for smoke and motion sensors
boolean armSmoke = false;
boolean smokeTriggered = false;
boolean armMotion = false;
boolean motionTriggered = false;
// LEDs for smoke and motion status
const int smokeLED = 13;
const int motionLED = 12;
// Buzzer pin
const int buzzerPin = 14;
// Timers auxiliar variables
long now = millis();
long lastSmokeCheck = 0;
// Only runs once
void setup() {
// Preparing GPIOs
pinMode(output1, OUTPUT);
digitalWrite(output1, LOW);
pinMode(output2, OUTPUT);
digitalWrite(output2, LOW);
pinMode(smokeLED, OUTPUT);
digitalWrite(smokeLED, LOW);
pinMode(motionLED, OUTPUT);
digitalWrite(motionLED, LOW);
pinMode(buzzerPin, OUTPUT);
pinMode(smokePin, INPUT);
// Setting motionSensor as an interrupt
// attachInterrupt(digitalPinToInterrupt(motionSensor), detectsMovement, RISING);
// Initializing DHT sensor 
dht.begin();

// Initializing serial port for debugging purposes
Serial.begin(115200);
delay(10);

// Connecting to WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password); 
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");

// Starting the web server
server.begin();
Serial.println("Web server running. Waiting for the ESP IP...");
delay(10000);

// Printing the ESP IP address in the serial monitor
Serial.println(WiFi.localIP());
}
// Runs over and over again
void loop() {
now = millis();
// Checks current smoke value every 500 milliseconds 
if (now - lastSmokeCheck > 500) {
lastSmokeCheck = now; 
int smokeValue = analogRead(smokePin);
// If the smoke sensor is armed and smoke is detected. It sends a notification saying "Smoke Detected" that is printed in the dashboard
if (smokeValue > smokeThres && armSmoke){
Serial.print("Pin A0: ");
Serial.println(smokeValue);
tone(buzzerPin, 1000, 200); 
if(!smokeTriggered){
Serial.println("SMOKE DETECTED!");
smokeTriggered = true;
}
}
}
// Listenning for new clients
WiFiClient client = server.available();
// When a client is available
if(client){
Serial.println("New client");
// Reads temperature and humidity
readTemperatureHumidity();
// Bolean to locate when the http request ends
boolean blank_line = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
header += c;
if (c == '\n' && blank_line) {
Serial.print(header);
// Checking if header is valid
// You can use this website https://www.base64decode.org/ to generate a new username and password (select the Encode tab) 
// Enter with the following format: yourusername:yourpassword is encoded as eW91cnVzZXJuYW1lOnlvdXJwYXNzd29yZA==
// In this case, I've used the username user and the password pass, so the encoded string is dXNlcjpwYXNz = 'user:pass' (user:pass) base64 encode
if(header.indexOf("dXNlcjpwYXNz") >= 0) {
//successful login
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
// Checks the current URL that you opened in your web server
// For example the URL /output1off turns the output 1 off
// The /armSmoke URL arms the smoke sensor and so on...
// These URL are opened when you press the buttons in your dashboard
if(header.indexOf("GET / HTTP/1.1") >= 0) {
Serial.println("Main Web Page");
} 
else if(header.indexOf("GET /output1on HTTP/1.1") >= 0){
Serial.println("Output 1 On");
output1State = "On";
digitalWrite(output1, HIGH);
}
else if(header.indexOf("GET /output1off HTTP/1.1") >= 0){
Serial.println("Output 1 Off");
output1State = "Off";
digitalWrite(output1, LOW);
}
else if(header.indexOf("GET /output2on HTTP/1.1") >= 0){
Serial.println("Output 2 On");
output2State = "On";
digitalWrite(output2, HIGH);
}
else if(header.indexOf("GET /output2off HTTP/1.1") >= 0){
Serial.println("Output 2 Off");
output2State = "Off";
digitalWrite(output2, LOW);
}
else if(header.indexOf("GET /armSmoke HTTP/1.1") >= 0){
Serial.println("Smoke sensor is: Armed");
digitalWrite(smokeLED, HIGH);
armSmoke = true;
smokeTriggered = false;
}
else if(header.indexOf("GET /disarmSmoke HTTP/1.1") >= 0){
Serial.println("Smoke sensor is: Disarmed");
digitalWrite(smokeLED, LOW);
armSmoke = false;
smokeTriggered = false;
}
else if(header.indexOf("GET /armMotion HTTP/1.1") >= 0){
Serial.println("Motion sensor is: Armed");
digitalWrite(motionLED, HIGH);
armMotion = true;
motionTriggered = false;
}
else if(header.indexOf("GET /disarmMotion HTTP/1.1") >= 0){
Serial.println("Motion sensor is: Disarmed");
digitalWrite(motionLED, LOW);
armMotion = false;
motionTriggered = false;
}
// Displays your html web page
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head>");
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
// Loads Bootstrap framework to give you the mobile reponsive web page and the buttons look
client.println("<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css\">"); 
client.println("<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js\"></script>"); 
client.println("</head><div class=\"container\">");
client.println("<h1>ESP8266 Web Server - <a href=\"/\">Refresh</a></h1>");
// Generates buttons to control the outputs
client.println("<h2>Output 1 - State: " + output1State + "</h2><div class=\"row\">");
// If LED is off, it shows the button to turn the output 1 on 
if(output1State == "Off"){
client.println("<div class=\"col-md-2\"><a href=\"/output1on\" class=\"btn btn-block btn-lg btn-success\" role=\"button\">ON</a></div></div>");
}
// If LED is on, it shows the button to turn the output 1 off 
else if(output1State == "On"){
client.println("<div class=\"col-md-2\"><a href=\"/output1off\" class=\"btn btn-block btn-lg btn-danger\" role=\"button\">OFF</a></div></div>"); 
}
client.println("<h2>Output 2 - State: " + output2State + "</h2><div class=\"row\">");
// If LED is off, it shows the button to turn the output 2 on 
if(output2State == "Off"){
client.println("<div class=\"col-md-2\"><a href=\"/output2on\" class=\"btn btn-block btn-lg btn-success\" role=\"button\">ON</a></div></div>"); 
}
// If LED is on, it shows the button to turn the output 2 off 
else if(output2State == "On"){
client.println("<div class=\"col-md-2\"><a href=\"/output2off\" class=\"btn btn-block btn-lg btn-danger\" role=\"button\">OFF</a></div></div>");
}
// Checks if there was smoke detected in your room and prints a message according to the current state (smoke detected or no smoke) 
if(smokeTriggered){
client.println("<h2>Smoke Sensor - State: Smoke detected</h2><div class=\"row\">"); 
}
else {
client.println("<h2>Smoke Sensor - State: No smoke</h2><div class=\"row\">");
}
// If the smoke sensor is armed, it shows the button to disarm the sensor 
if(armSmoke){
client.println("<div class=\"col-md-2\"><a href=\"/disarmSmoke\" class=\"btn btn-block btn-lg btn-default\" role=\"button\">Disarm</a></div></div>"); 
}
// If the smoke sensor is disarmed, it shows the button to arm the sensor 
else {
client.println("<div class=\"col-md-2\"><a href=\"/armSmoke\" class=\"btn btn-block btn-lg btn-primary\" role=\"button\">Arm</a></div></div>");
}
// Checks if there was motion detected in your room and prints a message according to the current state (motion detected or no motion) 
if(motionTriggered){
client.println("<h2>Motion Sensor - State: Motion detected</h2><div class=\"row\">"); 
}
else {
client.println("<h2>Motion Sensor - State: No motion</h2><div class=\"row\">");
}
// If the motion sensor is armed, it shows the button to disarm the sensor 
if(armMotion){
client.println("<div class=\"col-md-2\"><a href=\"/disarmMotion\" class=\"btn btn-block btn-lg btn-default\" role=\"button\">Disarm</a></div></div>"); 
}
// If the motion sensor is disarmed, it shows the button to arm the sensor 
else {
client.println("<div class=\"col-md-2\"><a href=\"/armMotion\" class=\"btn btn-block btn-lg btn-primary\" role=\"button\">Arm</a></div></div>");
}
// Prints the latest temperatuer and humidity readings 
client.println("<div class=\"row\"><h3>Temperature: "); 
client.println(celsiusTemp);
client.println("*C</h3><h3>Temperature: ");
client.println(fahrenheitTemp);
client.println("*F</h3><h3>Humidity: ");
client.println(humidityTemp);
client.println("%</h3></div>"); 
client.println("</div></div></html>"); 
} 
// If you enter wrong user or password, the http request fails... 
else { 
client.println("HTTP/1.1 401 Unauthorized");
client.println("WWW-Authenticate: Basic realm=\"Secure\"");
client.println("Content-Type: text/html");
client.println();
client.println("<html>Authentication failed</html>");
} 
header = "";
break;
}
if (c == '\n') {
// When starts reading a new line
blank_line = true;
}
else if (c != '\r') {
// When finds a character on the current line
blank_line = false;
}
}
} 
// Closing the client connection
delay(1);
client.stop();
Serial.println("Client disconnected.");
}
}
void readTemperatureHumidity(){
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);

// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Computes temperature values in Celsius
float hic = dht.computeHeatIndex(t, h, false); 
dtostrf(hic, 6, 2, celsiusTemp);
// Computes temperature values in Fahrenheit
float hif = dht.computeHeatIndex(f, h);
dtostrf(hif, 6, 2, fahrenheitTemp);
// Stores humidity
dtostrf(h, 6, 2, humidityTemp);
// Serial prints that are used for debugging purposes
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t Heat index: ");
Serial.print(hic);
Serial.println(" *C ");
Serial.print(hif);
Serial.println(" *F");
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t Heat index: ");
Serial.print(hic);
Serial.println(" *C ");
Serial.print(hif);
Serial.println(" *F");
return;
}
// Detects motion 
void detectsMovement() {
Serial.println("MOTION!");
// If the motion sensor is armed and motion is detected. It sends a notification saying "Motion Detected" that is printed in the dashboard
if (armMotion && !motionTriggered) {
Serial.println("MOTION DETECTED!");
motionTriggered = true;
}
}

Here is the entire compile report from IDE, the error message in question  is at the bottom

Arduino: 1.6.9 (Windows 10), Board: "NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Disabled, All SSL ciphers (most compatible), 4M (no SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"

C:\Program Files (x86)\Arduino\arduino-builder -dump-prefs -logger=machine -hardware "C:\Program Files (x86)\Arduino\hardware" -hardware "C:\Users\Fred\AppData\Local\Arduino15\packages" -tools "C:\Program Files (x86)\Arduino\tools-builder" -tools "C:\Program Files (x86)\Arduino\hardware\tools\avr" -tools IC:\Users\Fred\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\variants\nodemcu" "-IC:\Users\Fred\Documents\Arduino\libraries\DHT" "-IC:\Users\Fred\Documents\Arduino\libraries\Adafruit_Sensor-master" "-IC:\Users\Fred\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\libraries\ESP8266WiFi\src" "C:\Users\Fred\AppData\Local\Temp\buildba969838db15f0d0e67de17ee15bcd27.tmp\sketch\SmokeDetector.ino.cpp" -o "C:\Users\Fred\AppData\Local\Temp\buildba969838db15f0d0e67de17ee15bcd27.tmp\sketch\SmokeDetector.ino.cpp.o"
C:\Users\Fred\Desktop\SmokeDetector\SmokeDetector.ino: In function 'void loop()':
SmokeDetector:133: error: 'readTemperatureHumidity' was not declared in this scope
readTemperatureHumidity();
^
Multiple libraries were found for "DHT.h"
Used: C:\Users\Fred\Documents\Arduino\libraries\DHT
Not used: C:\Users\Fred\Documents\Arduino\libraries\DHT_sensor_library-1.3.6
Not used: C:\Users\Fred\Documents\Arduino\libraries\DHT-sensor-library-master
Using library ESP8266WiFi at version 1.0 in folder: C:\Users\Fred\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.2\libraries\ESP8266WiFi 
Using library DHT at version 1.3.6 in folder: C:\Users\Fred\Documents\Arduino\libraries\DHT 
Using library Adafruit_Sensor-master at version 1.0.3 in folder: C:\Users\Fred\Documents\Arduino\libraries\Adafruit_Sensor-master 
exit status 1

‘readTemperatureHumidity’ was not declared in this scope

0 Vote Up Vote Down
Sara Santos Staff answered 6 years ago

Hi again.
I think that is related with the version of the Arduino IDE you’re using.
You can either update to the latest version of Arduino IDE, or copy the readTemperatureHumidity() function declaration to the top of the sketch (before the setup).
Can you try one of these suggestions and tell me if it works?
Regards,
Sara

0 Vote Up Vote Down
fredmoss answered 6 years ago

Sara,
 
Updating to 1.8.21 did the trick.  Thanks!
 
Fred

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.