Hi All - I've built a set-up to monitor temperatur and humidity within a room, output the current readings to LCD and log the data to thingspeak. Ultimately it will drive two stepper motors (not included in code / setup yet) to open and close vents.
The problem is as follows:
- The system will work fine for anywhere between 30 mins to 8-10 hours
- After a (what feels like random) period of time, the LCD will eventually display the floats (hum and temp) below as "nan", specifically "T:nanC CCC" in the top row and "H:nan%" in the bottom
- The datalogging in thingspeak / outputting temp / humidity to serialmonitor will not indicate any issues and keep running
Do you have any suggestions on how to approach & resolve this?
Thank you so much!
The circuit is set up as follows:
- Arduino Uno
- DHT22 Temp / Humidity Sensor (5v power)
- Standard 16x2 LCD (5v power)
- ESP8266 (3.3v power)
- Breadboard Power Supply (one rail with 5v and 3.3v)
In case it's relevant, the LCD is hooked up to the analog pins to free up the digital pins for temp / wifi / stepper motors (but the error also occurs even if I switch it over to the digital pins).
Full code here (I've removed wifi name / password / thingspeak key):
#include <DHT.h>
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <stdlib.h>
#define DHTPIN 6
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define SSID "WIFINAME"
#define PASS "PASSWORD"
#define IP "184.106.153.149"
String msg = "GET /update?key=READKEY";
SoftwareSerial esp8266(9,10);
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);
float hum;
float temp;
String tempC;
void setup()
{
dht.begin();
lcd.begin(16, 2);
lcd.clear();
esp8266.begin(9600);
esp8266.println("AT");
delay(5000);
if(esp8266.find("OK"))
{
connectWiFi();
}
}
void loop()
{
hum = dht.readHumidity();
temp = dht.readTemperature();
lcd.setCursor(0,0);
lcd.print("T:");
lcd.print(temp);
lcd.print("C");
lcd.setCursor(0,1);
lcd.print("H:");
lcd.print(hum);
lcd.print("%");
// the following lines will "O-T" or "O-H" if the temp / humidity are over certain thresholds, else CCC and be where I plan on later inserting the stepper code
if (temp >= 30)
{
lcd.setCursor(13,0);
lcd.print("O-T");
}
else
{
if (hum >= 91)
{
lcd.setCursor(13,0);
lcd.print("O-H");
}
else
{
lcd.setCursor(13,0);
lcd.print("CCC");
}
}
char buffer[10];
tempC = dtostrf(temp, 4, 1, buffer);
updateTemp();
delay(5000);
}
void updateTemp()
{
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
esp8266.println(cmd);
delay(2000);
if(esp8266.find("Error"))
{
return;
}
cmd = msg ;
cmd += "&field1=";
cmd += tempC;
cmd += "&field2=";
cmd += String(hum);
cmd += "\r\n";
esp8266.print("AT+CIPSEND=");
esp8266.println(cmd.length());
if(esp8266.find(">"))
{
esp8266.print(cmd);
}
else
{
esp8266.println("AT+CIPCLOSE");
}
}
boolean connectWiFi()
{
esp8266.println("AT+CWMODE=1");
delay(2000);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
esp8266.println(cmd);
delay(5000);
if(esp8266.find("OK"))
{
return true;
}
else
{
return false;
}
}