Tuesday, May 9, 2017

Wemos ESP8266 - mDNS and OTA firmware update

Windows 10
Ubuntu Linux 16.04
Arduino IDE 1.8.1


Goal:

  • Test Multicast DNS (mDNS) and Over The Air (OTA) firmware update for using with IoT devices.

Status:

2017.05.09 - It's working.

ToDo:
  • Test OTA functions onStart, onEnd, onProgress and onError.

Code:

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <Hash.h>
// OTA firmware update
#include <ESP8266HTTPUpdateServer.h>
const String DEVICE_ID  = "Test_OTA_mDNS_v0.0.1";
const char*  ssid       = "SSID";
const char*  password   = "pass";
const int    httpPort   = 8080; // OTA firmware update
#define      DEBUG        true
#define ARRAY_SIZE 6
String activeClients[ARRAY_SIZE];
ESP8266WiFiMulti WiFiMulti;
// OTA firmware update
ESP8266WebServer httpServer(httpPort);
ESP8266HTTPUpdateServer httpUpdater;
void setup() {
    Serial.begin(115200);
    if (DEBUG) Serial.println(DEVICE_ID);
    for(uint8_t t = 4; t > 0; t--) {
        if (DEBUG) Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
        Serial.flush();
        delay(1000);
    }
    // ESP Chip Id
    if (DEBUG) Serial.println(ESP.getChipId());
    WiFiMulti.addAP(ssid, password);
    while(WiFiMulti.run() != WL_CONNECTED) {
        delay(100);
    }
    // ESP client IP
    if (DEBUG) Serial.println(WiFi.localIP());
    // OTA
    httpUpdater.setup(&httpServer);
    httpServer.begin();
    if(MDNS.begin("OTASom4r")) {
        if (DEBUG) Serial.println("MDNS responder started");
    }
    // Add service to MDNS
    MDNS.addService("http", "tcp", httpPort);
}
void loop() {
  httpServer.handleClient();  
}
/*
void onStart(OTA_CALLBACK(fn)) {
  Serial.println("onStart");
}
void onEnd(OTA_CALLBACK(fn)) {
  Serial.println("onEnd");
}
void onProgress(OTA_CALLBACK_PROGRESS(fn)) {
  Serial.println("onProgress");
}
void onError(OTA_CALLBACK_ERROR (fn)) {
  Serial.println("onError");

}
*/

Installing mDNS:

  • Windows:
    • Install Bonjour (included with Safari Browser for Windows).
  • Linux:
    • sudo apt-get update
    • sudo upt-get install libnss-mdns

Testing:


Site to upload new firmware.
Serial log while device is rebooting.


References:
If you like this content, feel free to