Created
January 5, 2018 12:34
-
-
Save sourceperl/de973709615b53e9ecde475f6c968b48 to your computer and use it in GitHub Desktop.
ESP8266 periodic upload of a counter to thingspeak with HTTP or MQTT (use ESP deep sleep, RTC memory)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
ESP8266 periodic upload an integer to thingspeak, set ESP to deep sleep between sends | |
and use RTC memory for variable persistence. Use http and mqtt update. | |
For leave deep sleep mode we need a jumper between RST and D0 (GPIO16/WAKE) so don't uses LED_BUILTIN here. | |
Test on nodemcu v2. | |
This example code is in the public domain. | |
*/ | |
extern "C" { | |
#include "user_interface.h" // ESP RTC memory read/write functions | |
} | |
#include <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
#include <PubSubClient.h> | |
// some consts | |
#define SLEEP_TIME 30e6 | |
// ESP RTC memory is 4 bytes aligned, here 64 is block number: byte address is 64*4 = 256 | |
#define RTC_MEM_START 64 | |
// wifi credentials. | |
#define WIFI_SSID "myssid" | |
#define WIFI_PASS "mypwd" | |
// thingspeak | |
#define HTTP_SERVER "api.thingspeak.com" | |
#define MQTT_SERVER "mqtt.thingspeak.com" | |
#define MQTT_PORT 1883 | |
#define MQTT_USER "" | |
#define MQTT_PWD "DEADBEEF" | |
#define API_KEY "DEADBEEF" | |
#define CHANNEL_ID 396356 | |
// misc | |
static const char alphanum[] ="0123456789" | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
"abcdefghijklmnopqrstuvwxyz"; // For random generation of client ID. | |
// some vars | |
WiFiClient espClient; | |
PubSubClient mqttClient(espClient); | |
uint32_t counter; | |
//HTTPClient http; | |
// since we use ESP deep sleep mode: all stuff is in setup() | |
void setup() { | |
Serial.begin(115200); | |
Serial.setTimeout(2000); | |
Serial.println(); | |
// check reset type | |
rst_info *rsti; | |
rsti = ESP.getResetInfoPtr(); | |
switch (rsti->reason) { | |
case 5: | |
Serial.println("RTC-RESET (ResetInfo.reason = 5)"); | |
system_rtc_mem_read(RTC_MEM_START, &counter, sizeof(counter)); | |
break; | |
case 6: | |
Serial.println("POWER-UP (ResetInfo.reason = 6)"); | |
counter = 0; | |
break; | |
} | |
system_get_rst_info(); | |
// update var | |
counter++; | |
// Connect to Wifi. | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(WIFI_SSID); | |
WiFi.begin(WIFI_SSID, WIFI_PASS); | |
// WiFi fix: https://github.com/esp8266/Arduino/issues/2186 | |
WiFi.persistent(false); | |
WiFi.mode(WIFI_OFF); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(WIFI_SSID, WIFI_PASS); | |
unsigned long wifiConnectStart = millis(); | |
while (WiFi.status() != WL_CONNECTED) { | |
// Check to see if | |
if (WiFi.status() == WL_CONNECT_FAILED) { | |
Serial.println("Failed to connect to WiFi. Please verify credentials: "); | |
delay(10000); | |
} | |
delay(500); | |
Serial.println("..."); | |
// Only try for 5 seconds. | |
if (millis() - wifiConnectStart > 15000) { | |
Serial.println("Failed to connect to WiFi"); | |
return; | |
} | |
} | |
Serial.println(""); | |
Serial.print("WiFi connected (IP: "); | |
Serial.print(WiFi.localIP()); | |
Serial.println(")"); | |
// update thingspeak with http | |
/* | |
http.begin("http://"+ String(HTTP_SERVER) + "/update?api_key=" + String(API_KEY) + "&field1=" + String(counter)); | |
http.GET(); | |
http.end(); | |
*/ | |
// update thingspeak with mqtt | |
mqttClient.setServer(MQTT_SERVER, MQTT_PORT); | |
// Generate ClientID | |
char clientID[10]; | |
for (int i = 0; i < 8; i++) { | |
clientID[i] = alphanum[random(51)]; | |
} | |
if (mqttClient.connect(clientID, MQTT_USER, MQTT_PWD)) { | |
Serial.println("MQTT connected"); | |
// topic buffer | |
String topicString = "channels/" + String(CHANNEL_ID) + "/publish/" + String(API_KEY); | |
int length = topicString.length(); | |
char topicBuffer[length]; | |
topicString.toCharArray(topicBuffer,length+1); | |
Serial.println(topicBuffer); | |
// payload buffer | |
String data = String("field1=" + String(counter)); | |
length = data.length(); | |
char msgBuffer[length]; | |
data.toCharArray(msgBuffer,length+1); | |
Serial.println(msgBuffer); | |
// publish (wait to ensure update) | |
mqttClient.publish(topicBuffer, msgBuffer); | |
mqttClient.disconnect(); | |
// allow ESP time to process network stuff | |
delay(10); | |
} | |
// store current value to RTC memory for next wakeup | |
system_rtc_mem_write(RTC_MEM_START, &counter, sizeof(counter)); | |
// go to deep sleep | |
Serial.println("Go to deep sleep"); | |
ESP.deepSleep(SLEEP_TIME, WAKE_NO_RFCAL); | |
} | |
// do nothing here | |
void loop() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment