Skip to content

Instantly share code, notes, and snippets.

@skandragon
Created December 6, 2015 18:55
Show Gist options
  • Save skandragon/a50cf03ac518092b21be to your computer and use it in GitHub Desktop.
Save skandragon/a50cf03ac518092b21be to your computer and use it in GitHub Desktop.
failing update code
#include <ESP8266WiFi.h>
#include <ESP8266httpUpdate.h>
const char* host = "holiday.home.flame.org";
const char* ssid = "home.flame.org";
const char* pass = "xxx";
const uint16_t app_port = 4201;
const char *UPDATE_HOST = "update-hostname-here";
const char *UPDATE_PATH = "path-to-bin-here";
WiFiServer server(app_port);
const char *version = "0.1.21";
void setupWifi()
{
Serial.printf("Version: %s\n", version);
Serial.printf("Sketch size: %u\n", ESP.getSketchSize());
Serial.printf("Free size: %u\n", ESP.getFreeSketchSpace());
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
if (WiFi.waitForConnectResult() == WL_CONNECTED) {
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
server.begin();
}
void setup()
{
Serial.begin(115200);
delay(100);
//Serial.setDebugOutput(true);
setupWifi();
}
void doUpdate()
{
auto ret = ESPhttpUpdate.update(UPDATE_HOST, 80, UPDATE_PATH, version);
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.println("[update] Update failed.");
Update.printError(Serial);
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("[update] Update no Update.");
break;
case HTTP_UPDATE_OK:
Serial.println("[update] Update ok."); // may not called we reboot the ESP
break;
}
}
WiFiClient client;
uint8_t stringId;
void wifiLoop()
{
if (!client.connected()) {
if (!server.hasClient())
return;
client = server.available();
}
if (!client.connected()) {
client.stop();
return;
}
int command = client.read();
if (command == -1)
return;
if (command == 'S') {
stringId = client.read();
} else if (command == 'L') {
uint8_t buffer[4];
int readlen = client.readBytes(buffer, sizeof buffer);
if (readlen != sizeof(buffer)) {
Serial.printf("Only read %d bytes\n", readlen);
return;
}
uint8_t lightId = buffer[0];
uint8_t brightness = buffer[1];
uint8_t rg = buffer[2];
uint8_t b = buffer[3];
} else if (command == 'R') {
client.stop();
delay(1000);
ESP.reset();
} else if (command == 'O') {
Serial.println("Attempting firmware upgrade");
client.stop();
delay(1000);
doUpdate();
}
}
void loop()
{
wifiLoop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment