Created
March 17, 2017 13:54
-
-
Save LosantGists/783405ca2bf7380c2aa6fd3855414b73 to your computer and use it in GitHub Desktop.
An example showing how to put a ESP8266 device into Deep-sleep mode while making network requests between sleeps
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
/** | |
An example showing how to put ESP8266 into Deep-sleep mode | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
#include <Losant.h> | |
// WiFi credentials. | |
const char* WIFI_SSID = "wifi-ssid"; | |
const char* WIFI_PASS = "wifi-pass"; | |
// Losant credentials. | |
const char* LOSANT_DEVICE_ID = "device-id"; | |
const char* LOSANT_ACCESS_KEY = "access-key"; | |
const char* LOSANT_ACCESS_SECRET = "access-key"; | |
WiFiClientSecure wifiClient; | |
LosantDevice device(LOSANT_DEVICE_ID); | |
void connect() { | |
// Connect to Wifi. | |
Serial.println(); | |
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.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.println(); | |
Serial.print("Connecting to Losant..."); | |
Serial.print("Authenticating Device..."); | |
HTTPClient http; | |
http.begin("http://api.losant.com/auth/device"); | |
http.addHeader("Content-Type", "application/json"); | |
http.addHeader("Accept", "application/json"); | |
/* Create JSON payload to sent to Losant | |
{ | |
"deviceId": "575ecf887ae143cd83dc4aa2", | |
"key": "this_would_be_the_key", | |
"secret": "this_would_be_the_secret" | |
} | |
*/ | |
StaticJsonBuffer<200> jsonBuffer; | |
JsonObject& root = jsonBuffer.createObject(); | |
root["deviceId"] = LOSANT_DEVICE_ID; | |
root["key"] = LOSANT_ACCESS_KEY; | |
root["secret"] = LOSANT_ACCESS_SECRET; | |
String buffer; | |
root.printTo(buffer); | |
int httpCode = http.POST(buffer); | |
if (httpCode > 0) { | |
if (httpCode == HTTP_CODE_OK) { | |
Serial.println("This device is authorized!"); | |
} else { | |
Serial.println("Failed to authorize device to Losant."); | |
if (httpCode == 400) { | |
Serial.println("Validation error: The device ID, access key, or access secret is not in the proper format."); | |
} else if (httpCode == 401) { | |
Serial.println("Invalid credentials to Losant: Please double-check the device ID, access key, and access secret."); | |
} else { | |
Serial.println("Unknown response from API"); | |
} | |
Serial.println("Current Credentials: "); | |
Serial.println("Device id: "); | |
Serial.println(LOSANT_DEVICE_ID); | |
Serial.println("Access Key: "); | |
Serial.println(LOSANT_ACCESS_KEY); | |
Serial.println("Access Secret: "); | |
Serial.println(LOSANT_ACCESS_SECRET); | |
return; | |
} | |
} else { | |
Serial.println("Failed to connect to Losant API."); | |
return; | |
} | |
http.end(); | |
device.connectSecure(wifiClient, LOSANT_ACCESS_KEY, LOSANT_ACCESS_SECRET); | |
while (!device.connected()) { | |
delay(1000); | |
Serial.print("."); | |
} | |
Serial.println("Connected!"); | |
Serial.println("This device is now ready for use!"); | |
} | |
void reportTemp(double degreesC, double degreesF) { | |
StaticJsonBuffer<200> jsonBuffer; | |
JsonObject& root = jsonBuffer.createObject(); | |
root["tempC"] = degreesC; | |
root["tempF"] = degreesF; | |
device.sendState(root); | |
} | |
void setup() { | |
Serial.begin(115200); | |
Serial.setTimeout(2000); | |
// Wait for serial to initialize. | |
while (!Serial) { } | |
Serial.println("Device Started"); | |
Serial.println("-------------------------------------"); | |
Serial.println("Running Deep Sleep Firmware!"); | |
Serial.println("-------------------------------------"); | |
connect(); | |
int temp = analogRead(A0); | |
double degreesC = (((temp / 1024.0) * 3.2) - 0.5) * 100.0; | |
double degreesF = degreesC * 1.8 + 32; | |
Serial.println(); | |
Serial.print("Temperature C: "); | |
Serial.println(degreesC); | |
Serial.print("Temperature F: "); | |
Serial.println(degreesF); | |
Serial.println(); | |
reportTemp(degreesC, degreesF); | |
Serial.println("Going into deep sleep for 20 seconds"); | |
ESP.deepSleep(20e6); // 20e6 is 20 microseconds | |
} | |
void loop() { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment