-
-
Save chaseTfreeman/f21260abbf671e7b6703837f804eda67 to your computer and use it in GitHub Desktop.
This file contains 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
#include <Arduino.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
#include <ESP8266httpUpdate.h> | |
#define USE_SERIAL Serial | |
#define SKETCH_NAME "app222le112122" | |
//#define LAMBDA_URL "https://xxxx.execute-api.us-east-1.amazonaws.com/prod/iot-update-handler" | |
//#define SSL_SIGNATURE "xx xx xx ... xx" | |
//#define S3_SSL_SIGNATURE "xx xx xx ... xx" | |
//#define WIFI_SSID "x...x" | |
//#define WIFI_PASS "x...x" | |
//image describing approach | |
//https://docs.google.com/drawings/d/11_g4em223vnbjms_Kr3ZcWv2Cl0fKIqF2UdagAzVmn4/edit?usp=sharing | |
void setup() { | |
Serial.begin(115200); | |
Serial.setDebugOutput(true); | |
Serial.println(); | |
WiFi.begin(WIFI_SSID, WIFI_PASS); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.println('.'); | |
delay(1000); | |
} | |
Serial.println("Connected, updating"); | |
delay(1000); | |
String macAddr = WiFi.macAddress(); | |
USE_SERIAL.println(macAddr); | |
HTTPClient httpClient; | |
USE_SERIAL.println(LAMBDA_URL + (String)"?mac=" + macAddr + (String)"&sketch=" + SKETCH_NAME); | |
httpClient.begin(LAMBDA_URL + (String)"?mac=" + macAddr + (String)"&sketch=" + SKETCH_NAME, SSL_SIGNATURE); | |
//Using this hack as ESPhttpUpdate.update does not respect 301 or 302 redirects as yet | |
//https://github.com/esp8266/Arduino/issues/3811 <- once resolved we only need to call ESPhttpUpdate.update directly | |
int httpCode = httpClient.GET(); | |
if (httpCode == 304) { | |
USE_SERIAL.println(F("HTTP_UPDATE_NO_UPDATES")); | |
return; | |
} | |
if (httpCode != HTTP_CODE_OK && httpCode != 301 && httpCode != 302) { | |
USE_SERIAL.println(httpCode); | |
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", httpClient.errorToString(httpCode).c_str()); | |
return; | |
} | |
// if (httpClient.hasHeader("Location")) { | |
// String redirectingTo = httpClient.header("Location"); | |
// USE_SERIAL.println("redirectingTo"); | |
// USE_SERIAL.println(redirectingTo); | |
// } | |
String response = httpClient.getString(); | |
USE_SERIAL.println("response"); | |
USE_SERIAL.println(response); | |
httpClient.end(); | |
t_httpUpdate_return ret = ESPhttpUpdate.update(response, SKETCH_NAME, S3_SSL_SIGNATURE); | |
Serial.println("=========="); | |
Serial.println(ret); | |
Serial.println("=========="); | |
switch (ret) { | |
case HTTP_UPDATE_FAILED: | |
USE_SERIAL.printf("HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); | |
break; | |
case HTTP_UPDATE_NO_UPDATES: | |
USE_SERIAL.println(F("HTTP_UPDATE_NO_UPDATES")); | |
break; | |
case HTTP_UPDATE_OK: | |
USE_SERIAL.println(F("HTTP_UPDATE_OK")); | |
break; | |
} | |
pinMode(LED_BUILTIN, OUTPUT); | |
} | |
void loop() { | |
digitalWrite(LED_BUILTIN, LOW); | |
delay(1000); | |
digitalWrite(LED_BUILTIN, HIGH); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment