• 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

Home automation

Q&A Forum › Category: ESP8266 › Home automation
0 Vote Up Vote Down
Larry asked 2 years ago

So you said not to use the project Blynk because it was out of date so I went to the Home automation book and found this project to attempt to learn how to read temperature and humidity. Seems just about every project I try here has issues, probably some of mine but when you  cut and paste and still get problems it makes it very difficult to go on . In the code below you will see bold text that have issues. Trying desperately here to learn something but so far nothing but problems. I am starting to feel like this is just over my head, been able to do some programming with Arduino UNO , small things but this so far has been frustrating. Thanks

#include “DHT.h”
#define DHTPIN 4 // Digital pin connected to the DHT sensor
// Uncomment whatever type you’re using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect
//pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the
//sensor

DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println(F(“DHTxx test!”));
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
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)) { // F IS NOT DEFINED
Serial.println(F(“Failed to read from DHT sensor!”));
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h); // AGAIN F IS NOT DEFINED
// Compute heat index in Celsius (isFahreheit = false)
//float hic = dht.computeHeatIndex(t, h, false);
Serial.print(F(“Humidity: “)); // SERIAL NOT NAME A TYPE
Serial.print(h); // SERIAL NOT NAME A TYPE
Serial.print(F(“% Temperature: “)); // SERIAL NOT NAME A TYPE
Serial.print(t); // SERIAL NOT NAME A TYPE
Serial.print(F(“°C “)); // SERIAL NOT NAME A TYPE
Serial.print(f); // Serial does not name a type
Serial.print(F(“°F Heat index: “)); // SERIAL NOT NAME A TYPE
Serial.print(hic); // SERIAL NOT NAME A TYPE
Serial.print(F(“°C “)); // SERIAL NOT NAME A TYPE
Serial.print(hif); // SERIAL NOT NAME A TYPE
Serial.println(F(“°F”)); // SERIAL NOT NAME A TYPE
}

Question Tags: home automation
4 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 2 years ago

Hi Larry.

I’ve just tried the code from the eBook and it is working as expected. Additionally, that is the sample code from the library. Yours is not working as expected because of messed up formatting.

Where did you copy the code from?
I advise not to copy the code directly from the PDF because, the formatting may be getting messed up.
Next to each code, there is a link to the github page. That’s where we recommend that you copy the code from:https://github.com/RuiSantosdotme/ESP8266-eBook/blob/master/Code/PART1_Arduino/Module3/Unit1/DHT_Example/DHT_Example.ino

You have several errors in your code because of messed up formatting.
For example, you have the f variable declaration in a comment line:

// Read temperature as Fahrenheit (isFahrenheit = true)float f = dht.readTemperature(true);

The f variable is not being initialized because it is in a comment line. That’s why you get the “f is not defined” error.

The same happens for the hic variable. It is commented, so it is not defined. If you don’t want the information in celsius, you need to delete all the lines that reference to the celsius variables.

Try again with the original code and tell me if it works as expected.

Regards,
Sara

0 Vote Up Vote Down
Larry answered 2 years ago

Thanks, I will try it, I kinda thought that maybe the issue but was unsure. I didn’t see or realize that there was a link to that code. I guess I am used to seeing “view raw code” on websites when code is given. 

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

In the eBook, at the end of each code there is a box “source code” with a link to the code on github.
Regards,
Sara

0 Vote Up Vote Down
Larry answered 2 years ago

Thanks

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] SMART HOME with RPi, ESP32, and ESP8266 (version 1.2) March 8, 2023
  • [eBook Updated] ESP32-CAM Projects (version 1.3) January 7, 2023

You must be logged in to view this content.

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

Copyright © 2013-2023 · 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.