Skip to content

Instantly share code, notes, and snippets.

@drbh
Created July 26, 2020 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drbh/b0b3575568e63dd20b99546e5d606632 to your computer and use it in GitHub Desktop.
Save drbh/b0b3575568e63dd20b99546e5d606632 to your computer and use it in GitHub Desktop.
Deep sleep enabled door state tracker that connects to WIFI and sends HTTPS request if state is changed. build target: WEMOS D1 ESP8266
// memory
#include <EEPROM.h>
// networking
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
// variables for eval
int inputVal = 0;
int addr = 0;
int value;
// constants for HTTPS resolution
const char * host = "<LAMBDA_FUNCTION_HOST>";
const char fingerprint[] PROGMEM = "<SHA1_FINGERPRINT_OF_SSL (find using browser)>";
const int httpsPort = 443; //HTTPS= 443 and HTTP = 80
// builds and emits a HTTPS request to Lambda function
// also includes ArduinoJson for semi managable way to
// create the JSON POST body
void sendMessageToLambda(int currentState) {
WiFiClientSecure httpsClient; //Declare object of class WiFiClient
Serial.println(host);
Serial.printf("Using fingerprint '%s'\n", fingerprint);
httpsClient.setFingerprint(fingerprint);
httpsClient.setTimeout(15000); // 15 Seconds
delay(1000);
Serial.print("HTTPS Connecting");
int r = 0; //retry counter
while ((!httpsClient.connect(host, httpsPort)) && (r < 30)) {
delay(100);
Serial.print(".");
r++;
}
if (r == 30) {
Serial.println("Connection failed");
} else {
Serial.println("Connected to web");
}
String getData;
Serial.print("requesting URL: ");
Serial.println(host);
// building request object body
StaticJsonDocument < 200 > doc;
doc["currentstate"] = currentState;
String requestBody;
int contentLength;
serializeJson(doc, requestBody);
contentLength = requestBody.length();
Serial.println(contentLength);
Serial.println(requestBody);
// build the HTTP1.0/1 string
httpsClient.print(String("POST ") + "<PATH_TO_LAMBDA_FUNCTION> HTTP/1.1" + "\r\n" +
"Content-Type: application/json" + "\r\n" +
"Host: <LAMBDA_FUNCTION_HOST>" + "\r\n" +
"Content-Length: " + String(contentLength) + "\r\n\r\n" + requestBody + "\r\n\r\n");
Serial.println("request sent");
while (httpsClient.connected()) {
String line = httpsClient.readStringUntil('\n');
if (line == "\r") {
Serial.println("headers received");
break;
}
}
Serial.println("reply was:");
Serial.println("==========");
String line;
while (httpsClient.available()) {
line = httpsClient.readStringUntil('\n'); //Read Line by Line
Serial.println(line); //Print response
}
Serial.println("==========");
Serial.println("closing connection");
}
void setup() {
Serial.begin(9600);
Serial.setTimeout(2000);
Serial.println("");
// get pins status
pinMode(2, OUTPUT);
inputVal = digitalRead(2);
// init persisted memory for 4 bytes (minimum)
EEPROM.begin(4);
value = EEPROM.read(addr);
// only connect to WIFI if state changed
if (inputVal != value) {
// hardcode IP - could use more sophisticated WIFI connection manager
WiFi.begin("<WIFI_SSID>", "<WIFI_PASS>");
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
sendMessageToLambda(inputVal);
}
// log the state to Serial (for humans only)
if (inputVal > value) {
Serial.println("Turned On");
}
if (value > inputVal) {
Serial.println("Turned Off");
}
// update persisted value
EEPROM.write(addr, inputVal);
EEPROM.commit();
EEPROM.end();
// power down for 10 seconds
// GPIO 16 sends LOW signal to RST after sleep
ESP.deepSleep(10e6);
}
void loop() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment