├── ESP32_Thingspeak_BME280_Uploader_02.ino ├── ESP32_Thingspeak_Deep_Sleep_BME280.ino ├── ESP32_Thingspeak_Upload_Deep_Sleep.ino ├── ESP8266_Thingspeak_Upload_Deep_Sleep.ino ├── Licence.txt └── README.md /ESP32_Thingspeak_BME280_Uploader_02.ino: -------------------------------------------------------------------------------- 1 | // Weather station V2 board sample 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | const char* ssid = "your_SSID"; 8 | const char* password = "your_PASSWORD"; 9 | 10 | // ThingSpeak Settings 11 | char thingSpeakAddress[] = "api.thingspeak.com"; 12 | String writeAPIKey = "32CCT6D9GBIDV0NW"; // Get the key for your channel to approve writing 13 | const int UpdateThingSpeakInterval = 10 * 60; // e.g. 10 * 60 for a 10-Min update interval (10-mins x 60-secs) 14 | #define pressure_offset 3.95 // Adjusts BME280 pressure value for my location at 40M asl 15 | //#define sda 4 16 | //#define scl 5 17 | int timer; 18 | 19 | Adafruit_BME280 bme; 20 | 21 | void setup() { 22 | Serial.begin(115200); 23 | timer = millis(); 24 | Serial.println("\n Start:" + String(timer)); 25 | WiFi.begin(ssid, password); 26 | Serial.println("Time to connect to WiFI:" + String(millis() - timer)); 27 | while (WiFi.status() != WL_CONNECTED) { 28 | delay(500); 29 | Serial.print("."); 30 | } 31 | Wire.begin(); // Defaults but can use Wire.begin(SDA,SCL) e.g. Wire.begin(22,21) or any other pins for the I2C bus 32 | // if (!bme.begin(0x76)) Serial.println("Could not find a sensor, check wiring!"); 33 | // else 34 | // { 35 | // Serial.println("Found a sensor continuing"); 36 | // while (isnan(bme.readTemperature())) { Serial.println(bme.readTemperature()); } // The serial print of sensor value is not needed, but helps in debugging 37 | // } 38 | delay(500); 39 | float current_temperature = 22; //bme.readTemperature(); 40 | float current_humidity = 51; //bme.readHumidity(); 41 | float current_pressure = 1001;// bme.readPressure()/100.0F + pressure_offset; 42 | Serial.println(String(current_temperature,1)+"'c"); 43 | Serial.println(String(current_humidity,0)+"%"); 44 | Serial.println(String(current_pressure,1)+"hPa"); 45 | UpdateThingSpeak(current_temperature, current_humidity, current_pressure); 46 | Serial.println("Finished:" + String(millis() - timer)); 47 | Serial.println("Going to sleep..."); 48 | esp_sleep_enable_timer_wakeup(UpdateThingSpeakInterval * 1000000LL); 49 | esp_deep_sleep_start(); // Sleep for e.g. 30 minutes 50 | } 51 | 52 | void loop() { 53 | } 54 | 55 | void UpdateThingSpeak(float current_temperature, float current_humidity, float current_pressure) { 56 | WiFiClient client; 57 | //GET http://api.thingspeak.com/update?api_key=32CCT6D9GBIDV0NW&field1=22&field2=23&field3=1028 58 | String url = "GET /update?api_key=" + writeAPIKey + "&field1=" + String(current_temperature,1) + 59 | "&field2=" + String(current_humidity,1) + 60 | "&field3=" + String(current_pressure,1) + 61 | " HTTP/1.1"; 62 | if (client.connect(thingSpeakAddress, 80)) 63 | { 64 | client.println(url); 65 | client.println("Host: api.thingspeak.com"); 66 | client.println("Connection: close\n"); 67 | Serial.println("Upload of data complete"); 68 | if (client.connected()) Serial.println("Connecting to ThingSpeak..."); 69 | else Serial.println("Connection to ThingSpeak failed"); 70 | } 71 | else 72 | { 73 | Serial.println("Connection to ThingSpeak Failed"); 74 | } 75 | delay(1000); // Essential to enable upload to complete 76 | client.stop(); // close the connection 77 | } 78 | -------------------------------------------------------------------------------- /ESP32_Thingspeak_Deep_Sleep_BME280.ino: -------------------------------------------------------------------------------- 1 | /* Last update: 22-Aug-17 with improved sensor startup checks 2 | * ESP32 and a BME280 sensor that updates a Thingspeak channel, then goes to Deep Sleep 3 | * During Deep Sleep the ESP32 consumes ~5uA 4 | * The 'MIT License (MIT) Copyright (c) 2016 by David Bird'. Permission is hereby 5 | * granted, free of charge, to any person obtaining a copy of this software and associated 6 | * documentation files (the "Software"), to deal in the Software without restriction, including 7 | * without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the Software is furnished 9 | * to do so, subject to the following conditions: 10 | * The above copyright ('as annotated') notice and this permission notice shall be included 11 | * in all copies or substantial portions of the Software and where the software use is visible 12 | * to an end-user. 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 17 | * FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 18 | * See more at http://dsbird.org.uk 19 | * Also please refer to Adafruit for their Licences 20 | */ 21 | #include 22 | #include 23 | #include 24 | #include "esp_deep_sleep.h" //Library needed for ESP32 Sleep Functions 25 | WiFiClient client; // wifi client object 26 | 27 | const char *ssid = "your_SSID"; 28 | const char *password = "your_PASSWORD"; 29 | 30 | char ThingSpeakAddress[] = "api.thingspeak.com"; // Thingspeak address 31 | String api_key = "P1CS1NBUJXRAUSPS"; // Thingspeak API WRITE Key for your channel 32 | const int UpdateInterval = 0.33 * 60 * 1000000; // e.g. 0.33 * 60 * 1000000; //20-Sec update interval for development tests, to fast for practical purposes and Thingspeak! 33 | //const int UpdateInterval = 15 * 60 * 1000000; // e.g. 15 * 60 * 1000000; for a 15-Min update interval (15-mins x 60-secs * 1000000uS) 34 | 35 | #define pressure_offset 3.9 // Compensates for this location being 40M asl 36 | Adafruit_BME280 bme; 37 | 38 | void setup() { 39 | Serial.begin(115200); 40 | WiFi.begin(ssid, password); 41 | Serial.println("Start WiFi"); 42 | while (WiFi.status() != WL_CONNECTED ) {Serial.print("."); delay(500); } 43 | Wire.begin(17,16); // (sda,scl) 44 | // These pins are for ESP32 EH-ET LIVE board as the sensor is VCC-Gnd-SCL-SDA and the Wemos D1 variant of the ESP32 has Vcc-Gnd-SCL_SDA so just plugs in, no wiring 45 | 46 | if (!bme.begin()) { 47 | Serial.println("Could not find a sensor, check wiring!"); 48 | } 49 | else 50 | { 51 | Serial.println("Found a sensor continuing"); 52 | while (isnan(bme.readPressure())) { 53 | Serial.println(bme.readPressure()); 54 | } 55 | } 56 | 57 | float temperature = bme.readTemperature(); // 3 example variables, ideally supplied by a sensor, see my examples for the BMP180, BME280 or DS18B20 58 | float humidity = bme.readHumidity(); 59 | float pressure = bme.readPressure() / 100.0F + pressure_offset; 60 | 61 | UpdateThingSpeak("field1="+String(temperature)+"&field2="+String(humidity)+"&field3="+String(pressure)); //Send the data as text 62 | esp_deep_sleep_enable_timer_wakeup(UpdateInterval); 63 | Serial.println("Going to sleep now..."); 64 | esp_deep_sleep_start(); 65 | } 66 | 67 | void loop() { 68 | //Do nothing as it will never get here! 69 | } 70 | 71 | void UpdateThingSpeak(String DataForUpload) { 72 | WiFiClient client; 73 | if (!client.connect(ThingSpeakAddress, 80)) { 74 | Serial.println("connection failed"); 75 | return; 76 | } 77 | else 78 | { 79 | Serial.println(DataForUpload); 80 | client.print("POST /update HTTP/1.1\n"); 81 | client.print("Host: api.thingspeak.com\n"); 82 | client.print("Connection: close\n"); 83 | client.print("X-THINGSPEAKAPIKEY: " + api_key + "\n"); 84 | client.print("Content-Type: application/x-www-form-urlencoded\n"); 85 | client.print("Content-Length: "); 86 | client.print(DataForUpload.length()); 87 | client.print("\n\n"); 88 | client.print(DataForUpload); 89 | } 90 | client.stop(); 91 | } 92 | 93 | -------------------------------------------------------------------------------- /ESP32_Thingspeak_Upload_Deep_Sleep.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include "esp_deep_sleep.h" //Library needed for ESP32 Sleep Functions 3 | WiFiClient client; // wifi client object 4 | 5 | const char *ssid = "your_SSID"; 6 | const char *password = "your_PASSWORD"; 7 | 8 | char ThingSpeakAddress[] = "api.thingspeak.com"; // Thingspeak address 9 | String api_key = "P1CS1NBUJXRAUSPS"; // Thingspeak API WRITE Key for your channel 10 | const int UpdateInterval = 15 * 60 * 1000000; // e.g. 15 * 60 * 1000000; for a 15-Min update interval (15-mins x 60-secs * 1000000uS) 11 | 12 | void setup() { 13 | Serial.begin(115200); 14 | WiFi.begin(ssid, password); 15 | Serial.println("Start WiFi"); 16 | while (WiFi.status() != WL_CONNECTED ) {Serial.print("."); delay(500); } 17 | 18 | float temp = 20.5; // 3 example variables, ideally supplied by a sensor, see my examples for the BMP180, BME280 or DS18B20 19 | float humi = 60; 20 | float pres = 1015.6; 21 | 22 | UpdateThingSpeak("field1="+String(temp)+"&field2="+String(humi)+"&field3="+String(pres)); //Send the data as text 23 | esp_deep_sleep_enable_timer_wakeup(UpdateInterval); 24 | Serial.println("Going to sleep now..."); 25 | esp_deep_sleep_start(); 26 | } 27 | 28 | void loop() { 29 | //Do nothing as it will never get here! 30 | } 31 | 32 | void UpdateThingSpeak(String DataForUpload) { 33 | WiFiClient client; 34 | if (!client.connect(ThingSpeakAddress, 80)) { 35 | Serial.println("connection failed"); 36 | return; 37 | } 38 | else 39 | { 40 | Serial.println(DataForUpload); 41 | client.print("POST /update HTTP/1.1\n"); 42 | client.print("Host: api.thingspeak.com\n"); 43 | client.print("Connection: close\n"); 44 | client.print("X-THINGSPEAKAPIKEY: " + api_key + "\n"); 45 | client.print("Content-Type: application/x-www-form-urlencoded\n"); 46 | client.print("Content-Length: "); 47 | client.print(DataForUpload.length()); 48 | client.print("\n\n"); 49 | client.print(DataForUpload); 50 | } 51 | client.stop(); 52 | } 53 | 54 | -------------------------------------------------------------------------------- /ESP8266_Thingspeak_Upload_Deep_Sleep.ino: -------------------------------------------------------------------------------- 1 | #include 2 | WiFiServer server(80); 3 | // ************************************************************************************************************* 4 | // **** NOTE: The ESP8266 only supports deep sleep mode by connecting GPIO 16 to Reset 5 | // Wiring: ESP8266 16 -> ESP8266 RST (or D0 for GPIO16 on most ESP8266 Dev Boards 6 | // 7 | // !!!!! REMEMBER TO REMOVE THE GPIO16 to RST link for (re) programming, otherwise it remains in a RESET state.. 8 | // 9 | // ************************************************************************************************************* 10 | const char *ssid = "your_SSID"; 11 | const char *password = "your_PASSWORD"; 12 | 13 | char ThingSpeakAddress[] = "api.thingspeak.com"; // ThingSpeak address 14 | String writeAPIKey = "P1CS1NBUJXRAUSPS"; // Thingspeak Write Key to approve channel writing 15 | const int UpdateInterval = 0.33 * 60 * 1000000; // e.g. 15 * 60 * 1000000; for a 15-Min update interval (15-mins x 60-secs * 1000000uS) 16 | 17 | void setup() { 18 | Serial.begin(115200); // Or use 74480 to see Boot messages too 19 | WiFi.begin(ssid, password); 20 | Serial.println("Start WiFi"); 21 | while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print(".");} // Wait for a WiFi connection 22 | Serial.println(); 23 | server.begin(); 24 | 25 | float temp = 20.1; // 3 example variables, ideally supplied by a sensor, see examples for the BMP180, BME280 or DS18B20 26 | float humi = 65; 27 | float pres = 1010.5; 28 | 29 | UpdateThingSpeak("field1="+String(temp)+"&field2="+String(humi)+"&field3="+String(pres)); // Send the data as text 30 | Serial.println("Going to sleep now..."); 31 | ESP.deepSleep(UpdateInterval, WAKE_RF_DEFAULT); // Sleep for the time set by 'UpdateInterval' 32 | } 33 | 34 | void loop(){ 35 | // Do nothing as it will never get here! 36 | } 37 | 38 | void UpdateThingSpeak(String DataForUpload){ // takes ~ 2.5Secs to complete an update, from power-up to entering sleep 39 | Serial.println(DataForUpload); // Diagnostic print so you can see what data is being sent to the server 40 | WiFiClient client = server.available(); 41 | if (!client.connect(ThingSpeakAddress, 80)) { 42 | Serial.println("connection failed"); 43 | return; 44 | } 45 | else 46 | { 47 | Serial.println(DataForUpload); 48 | client.print("POST /update HTTP/1.1\n"); 49 | client.print("Host: api.thingspeak.com\n"); 50 | client.print("Connection: close\n"); 51 | client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n"); 52 | client.print("Content-Type: application/x-www-form-urlencoded\n"); 53 | client.print("Content-Length: "); 54 | client.print(DataForUpload.length()); 55 | client.print("\n\n"); 56 | client.print(DataForUpload); 57 | } 58 | client.stop(); // close the connection 59 | } 60 | 61 | -------------------------------------------------------------------------------- /Licence.txt: -------------------------------------------------------------------------------- 1 | This software, the ideas and concepts is Copyright (c) David Bird 2014 and beyond. 2 | 3 | All rights to this software are reserved. 4 | 5 | It is prohibited to redistribute or reproduce of any part or all of the software contents in any form other than the following: 6 | 7 | 1. You may print or download to a local hard disk extracts for your personal and non-commercial use only. 8 | 9 | 2. You may copy the content to individual third parties for their personal use, but only if you acknowledge the author David Bird as the source of the material. 10 | 11 | 3. You may not, except with my express written permission, distribute or commercially exploit the content. 12 | 13 | 4. You may not transmit it or store it in any other website or other form of electronic retrieval system for commercial purposes. 14 | 15 | 5. You MUST include all of this copyright and permission notice ('as annotated') and this shall be included in all copies or substantial portions of the software and where the software use is visible to an end-user. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS" FOR PRIVATE USE ONLY, IT IS NOT FOR COMMERCIAL USE IN WHOLE OR PART OR CONCEPT. 18 | 19 | FOR PERSONAL USE IT IS SUPPLIED WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 | 21 | IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESP32-8266-Thingspeak-Deep-Sleep-Examples 2 | 3 | Two versions that demonstrate how to upload data to Thingspeak.com and then to enter Deep Sleep. 4 | 5 | The ESP8266 consumes around 75mA in normal operation (WiFI on) and in Deep Sleep about 19uA. The ESP8266 takes ~ 2.5secs to upload data. 6 | 7 | The ESP32 consumes around 75mA in normal operation (WiFi on) and in Deep Sleep about 5uA. The ESP32 takes ~ 2.5secs to upload data. 8 | 9 | Battery Life calculations. Assume a 3.6v (3x1.2 NiMHd) battery of capacity 2200mAH and a time-on duration of 2.5-secs and 10 | then 9mins 57.5secs (595-secs) of sleep (6x per hour): 11 | 12 | For the ESP8266: Total mAH consumption per hour 13 | (ON time) = 6 x 75mA * 2.5 / 3600 = 0.313mAH 14 | (DS time) = 6 x 19uA * 597.5 / 3600 = 0.114mAH 15 | Total time in hours on battery = 2200/(0.0313+0.114) = 5159Hrs 16 | Total days on battery = 5159/24 = 214 Days 17 | 18 | For the ESP32: Total mAH consumption per hour 19 | (ON time) = 6 x 75mA * 2.5 / 3600 = 0.313mAH 20 | (DS time) = 6 x 5uA * 597.5 / 3600 = 0.03mAH 21 | Total time in hours on battery = 2200/(0.313+0.03) = 6423Hrs 22 | Total days on battery = 7092/24 = 267 Days 23 | 24 | 3-samples/hour extends battery life to: ESP8266 = 429Days or 1.2years and ESP32 = 535Days or 1.5years 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | --------------------------------------------------------------------------------