|
| 1 | +/** |
| 2 | + httpUpdate.ino |
| 3 | +
|
| 4 | + Created on: 27.11.2015 |
| 5 | +
|
| 6 | +*/ |
| 7 | + |
| 8 | +#include <Arduino.h> |
| 9 | + |
| 10 | +#include <ESP8266WiFi.h> |
| 11 | +#include <ESP8266WiFiMulti.h> |
| 12 | + |
| 13 | +#include <ESP8266HTTPClient.h> |
| 14 | +#include <ESP8266httpUpdate.h> |
| 15 | + |
| 16 | +#define USE_SERIAL Serial |
| 17 | + |
| 18 | +ESP8266WiFiMulti WiFiMulti; |
| 19 | + |
| 20 | +const char pubkey[] PROGMEM = R"EOF( |
| 21 | +-----BEGIN PUBLIC KEY----- |
| 22 | +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyW5a4OO7xd6pRDTETO7h |
| 23 | +vEMBOr/wCqcTi/gi2/99rPnVvT7IH/qGSiYMxpGFKCXVqS4rU5k2XspALEquyGse |
| 24 | +Uav5hqsgHO6CQFFALqXzUVNCsJA9V6raUFhBaIqqCKmWzmeAkV+avM/zDQR9Wj1Q |
| 25 | +TCmi997sJJ5ICQc8cGSdvrhisUSbfPpKI9Ql4FApOZRABBBuZKhN9ujIzTv3OIAa |
| 26 | +rpQVfACKKuv7a2N2qU0uxRDojeO6odT1c6AZv6BlcF76GQGTo+/oBhqPdbAQuaBy |
| 27 | +cuWNgTnDQd6KUzV0E4it2fNG+cHN4kEvofN6gHx8IbOrXwFttlpAH/o7bcfCnUVh |
| 28 | +TQIDAQAB |
| 29 | +-----END PUBLIC KEY----- |
| 30 | +)EOF"; |
| 31 | +BearSSLPublicKey *signPubKey = nullptr; |
| 32 | +BearSSLHashSHA256 *hash; |
| 33 | +BearSSLVerifier *sign; |
| 34 | + |
| 35 | +void setup() { |
| 36 | + |
| 37 | + USE_SERIAL.begin(115200); |
| 38 | + // USE_SERIAL.setDebugOutput(true); |
| 39 | + |
| 40 | + USE_SERIAL.println(); |
| 41 | + USE_SERIAL.println(); |
| 42 | + USE_SERIAL.println(); |
| 43 | + |
| 44 | + for (uint8_t t = 4; t > 0; t--) { |
| 45 | + USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); |
| 46 | + USE_SERIAL.flush(); |
| 47 | + delay(1000); |
| 48 | + } |
| 49 | + |
| 50 | + WiFi.mode(WIFI_STA); |
| 51 | + WiFiMulti.addAP("SSID", "PASS"); |
| 52 | + |
| 53 | + signPubKey = new BearSSLPublicKey(pubkey); |
| 54 | + hash = new BearSSLHashSHA256(); |
| 55 | + sign = new BearSSLVerifier(signPubKey); |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +void loop() { |
| 60 | + // wait for WiFi connection |
| 61 | + if ((WiFiMulti.run() == WL_CONNECTED)) { |
| 62 | + |
| 63 | + WiFiClient client; |
| 64 | + |
| 65 | + |
| 66 | + Update.installSignature(hash, sign); |
| 67 | + |
| 68 | + // The line below is optional. It can be used to blink the LED on the board during flashing |
| 69 | + // The LED will be on during download of one buffer of data from the network. The LED will |
| 70 | + // be off during writing that buffer to flash |
| 71 | + // On a good connection the LED should flash regularly. On a bad connection the LED will be |
| 72 | + // on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second |
| 73 | + // value is used to put the LED on. If the LED is on with HIGH, that value should be passed |
| 74 | + ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW); |
| 75 | + |
| 76 | + t_httpUpdate_return ret = ESPhttpUpdate.update(client, "http://192.168.1.8/esp8266.bin"); |
| 77 | + // Or: |
| 78 | + //t_httpUpdate_return ret = ESPhttpUpdate.update(client, "server", 80, "file.bin"); |
| 79 | + |
| 80 | + switch (ret) { |
| 81 | + case HTTP_UPDATE_FAILED: |
| 82 | + USE_SERIAL.printf("HTTP_UPDATE_FAILED Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); |
| 83 | + break; |
| 84 | + |
| 85 | + case HTTP_UPDATE_NO_UPDATES: |
| 86 | + USE_SERIAL.println("HTTP_UPDATE_NO_UPDATES"); |
| 87 | + break; |
| 88 | + |
| 89 | + case HTTP_UPDATE_OK: |
| 90 | + USE_SERIAL.println("HTTP_UPDATE_OK"); |
| 91 | + break; |
| 92 | + } |
| 93 | + } |
| 94 | + delay(10000); |
| 95 | +} |
| 96 | + |
0 commit comments