I'm a noob at this, so hopefully a stupidly obvious one.
I'm trying to create a simple temperature/humidity sensor that takes readings from a DHT22 and uses an ESP8266 to ping them to Thingspeak's API to then graph out/store etc.
I've pasted code below - it worked on Arduino Uno and I'm trying to shrinkify it onto a ESP8266 so I can produce lots of little temperature sensors for around the house.
Symptoms
- It's connecting fine to wifi
- It's generating a correct API string (I've tested by manually cutting and pasting it into browser)
- Temp sensors are generating correct readings also
- It's returning 'Data Fail!' in the serial monitor, suggesting it's this point in the code where the error is occurring
I can't tell if it's something odd about moving from Arduino Uno to ESP8266 that's causing the problem (ie different libraries required, TCP commands being different etc)
Any help from a more seasoned veteran would be much appreciated!
Here's a snippet of the serial monitor output and the code (just passwords/apis etc)
22:16:50.266 -> **************
22:16:57.579 -> Wifi Connection Successful
22:16:57.579 -> The IP Address of the Sensor is:192.168.1.211
22:16:57.579 -> Humidity: 41.50
22:16:57.579 -> Temperature: 21.70
22:16:57.579 -> AT+CIPSTART="TCP","api.thingspeak.com",80
22:17:00.574 -> AT+CIPSEND=63
22:17:01.561 -> AT+CIPCLOSE
22:17:02.577 -> Data Fail!
22:17:02.577 -> GET /update?apikey=<REMOVED>&field1=21.70&field2=41.50
#include<stdlib.h>
#include "DHT.h"
#include <ESP8266WiFi.h>
#define SSID "<REMOVED>" //your network name
#define PASS "<REMOVED>" //your network password
#define API "<REMOVED>" //api string
#define IP "api.thingspeak.com" // thingspeak.com
#define DHTPIN 4 // what pin the DHT sensor is connected to
#define DHTTYPE DHT22 // Change to DHT22 if that's what you have
#define Baud_Rate 115200 //Another common value is 9600
#define DELAY_TIME 300000 //time in ms between posting data to ThingSpeak
//Can use a post also
String GET = String("GET /update?apikey=") + API + "&field1=";
String FIELD2 = "&field2=";
//if you want to add more fields this is how
//String FIELD3 = "&field3=";
bool updated;
DHT dht(DHTPIN, DHTTYPE);
//this runs once
void setup()
{
delay(5000);
Serial.begin(Baud_Rate);
// Connect to WIFI
WiFi.begin(SSID, PASS);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("*");
}
Serial.println("");
Serial.println("Wifi Connection Successful");
Serial.print("The IP Address of the Sensor is:");
Serial.println(WiFi.localIP()); //Print the IP Address
//initalize DHT sensor
dht.begin();
}
//this runs over and over
void loop() {
float h = dht.readHumidity();
Serial.print("Humidity: ");
Serial.println(h);
// Read temperature as Fahrenheit (isFahrenheit = true)
float c = dht.readTemperature();
Serial.print("Temperature: ");
Serial.println(c);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(c)) {
Serial.println("Reading DHT22 Failed, exiting");
return;
}
//update ThingSpeak channel with new values
updated = updateTemp(String(c), String(h));
//wait for delay time before attempting to post again
delay(DELAY_TIME);
}
bool updateTemp(String tempC, 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 += tempC;
cmd += FIELD2;
cmd += humid;
cmd += "\r\n";
//continue to add data here if you have more fields such as a light sensor
//cmd += FIELD3;
//cmd += <field 3 value>
//Serial.println(cmd);
//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.
Serial.println("Data Sent!");
return true;
} else {
Serial.println("Data Fail!");
Serial.println(cmd);
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")) {
Serial.println("WIFI connected");
return true;
} else {
Serial.println("WIFI not connected");
return false;
}
}