I have a HUZZAH ESP8266 logging a DTH sensor to Emon server. The sensors are on USB power for the moment but I want to get the ESPs on battery and to do that I need to save energy, so rely on the DeepSleep function. You'll see from the sketch below that i have inserted the
ESP.deepSleep(sleepTimeS * 1000000);
void setup(void){ }
The problem is that its not logging the data to the Emon server as it appears to go to sleep before it logs the data. The output to serial port is not showing any useful code (of course not enough debug lines I suspect). It does show that its logging onto WiFI and then goes to sleep but when checking access log to Apache web server nothing is showing up (i.e. the sensors are definitely not logging data. As soon as you comment out the DeepSleep line, it logs data to the server. I'm missing something but not sure what. Any suggestions?
I've also put the DeeSleep call in the
void gettemperature() {
void setup(void)
Thanks
/***************** LOAD LIBRARIES ********************************************/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <DHT.h>
/************************* DHT22 Sensor *************************************/
#define DHTTYPE DHT22
#define DHTPIN 5
/************************* WiFi Access Point Settings ***********************/
const char* ssid = "MySSID";
const char* password = "MyPASS";
/**************Initialise the DHT sensor ***********************************/
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266
float humidity, temperature; // Values read from sensor
//String webString=""; // String to display
// Generally, you should use "unsigned long" for variables that hold time
unsigned long previousMillis = 0; // will store last temp was read
const long interval = 2000; // interval at which to read sensor
byte mac[6]; // the MAC address of your Wifi shield
char server[] = "192.168.0.34";
// Time to sleep (in seconds):
const int sleepTimeS = 20;
WiFiClient client;
void setup(void)
{
// You can open the Arduino IDE Serial Monitor window to see what the code is doing
Serial.begin(115200); // Serial connection from ESP-01 via 3.3v console cable
delay(100);
Serial.println(); // We start by connecting to a WiFi network
Serial.println();
Serial.print("Attempting to connect to WPA2 network ...");
Serial.println(ssid);
dht.begin(); // initialize temperature sensor
WiFi.begin(ssid, password); // Connect to WiFi network
Serial.print("\n\r \n\r Working to connect");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
WiFi.macAddress(mac);
Serial.print("MAC: ");
Serial.print(mac[0],HEX);
Serial.print(":");
Serial.print(mac[1],HEX);
Serial.print(":");
Serial.print(mac[2],HEX);
Serial.print(":");
Serial.print(mac[3],HEX);
Serial.print(":");
Serial.print(mac[4],HEX);
Serial.print(":");
Serial.println(mac[5],HEX);
// Sleep
Serial.println("ESP8266 in sleep mode");
ESP.deepSleep(sleepTimeS * 1000000);
//delay(5000);
}
void loop() {
gettemperature(); // Read the sensor
// Connect to the server (your local server on intranet)
if (client.connect(server, 193)) {
client.print("GET /html/emoncms/input/post?"); // This is for emon
client.print("node=THesp04");
client.print("&");
client.print("fulljson={");
client.print("\"temperature\":"); // This is the temperature value to post to the database
client.print(temperature); // Temp is posted
client.print(",");
client.print("\"humidity\":"); //Humidity is posted
client.print(humidity);
client.print("}");
client.print("&");
client.print("apikey=c815a12de82c97re9404fceh9b10c0ed");
//client.println(" HTTP/1.1"); // Part of the GET request
client.println(" "); //
client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
client.println(); // Empty line
client.println(); // Empty line
client.stop(); // Closing connection to server
}
else {
// If Arduino can't connect to the server (your computer or web page)
Serial.println("--> connection failed\n");
}
}
void gettemperature() {
// Wait at least 2 seconds seconds between measurements.
// if the difference between the current time and last time you read
// the sensor is bigger than the interval you set, read the sensor
// Works better than delay for things happening elsewhere also
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
// save the last time you read the sensor
previousMillis = currentMillis;
// Reading temperature for humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
humidity = dht.readHumidity(); // Read humidity (percent)
temperature = dht.readTemperature(); // Read temperature as Fahrenheit
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
}