Skip to content

Instantly share code, notes, and snippets.

@mahees
Created November 17, 2017 04:04
Show Gist options
  • Save mahees/bfd83c4230b5152d013f242d8e72f917 to your computer and use it in GitHub Desktop.
Save mahees/bfd83c4230b5152d013f242d8e72f917 to your computer and use it in GitHub Desktop.
#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);
}
@chaseTfreeman
Copy link

Hello there. Do you by chance still have the code for the Lambda you used on this, and if so, would you mind sharing?
More specifically, the iot-update-handler (https://xxxx.execute-api.us-east-1.amazonaws.com/prod/iot-update-handler)

@mahees
Copy link
Author

mahees commented Apr 11, 2020

hi, ive just uploaded it here

https://gist.github.com/mahees/4f7a4454edfe4e5552ae09ee5d8534a2

however, ive not tested it, or used it in the last 4 years. it may need some changes as at the time esp8266/Arduino#3811 was blocking me.

good luck, let me know if you have any questions.

@chaseTfreeman
Copy link

chaseTfreeman commented Apr 11, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment