Updating the code of esp8266 nodemcu from personal website websocket connection setup

Hi! i'm creating a website to practise what i learn online and bring my hobby project to life. I'm using:"node.js", "express", "mongodb-mongoose", "ws(websockets)" on server side(i know these are irrelevant for arduino forum but im mentioning them just in case it helps?). The purpose of this website is managing iot devices. I'm using charts to display live sensor data, and i'm sending commands(like turn on/off something) to my esp8266 nodemcu devices via websocket connection. My current setup(which im sharing parts of the codes below) works fine and does its job. Reason why i'm writing here is; i want to add the functionality to update my esp8266 nodemcu devices codes from my webpage. Cuz at some point these devices will be somewhere i cant reach easily to physically connect them to a pc or in some time i will share this webpage with others so its good to allow them to program their microcontrollers from the webpage. I've done some search online and i know i can send binary data via websocket connection and recieve it from nodemcu(using <WebSocketsClient.h>) and i know i can let web clients to send file from frontend to backend.

But the part that i dont know is how do i flash that binary file to nodemcu and reboot on that when i recieve it ?

**i tried the ArduinoOTA and it works in local network but i need the ability to recieve the bin file from my webpage and flash that.

esp8266 code example that i use for my current websocket setup:

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <WebSocketsClient.h>

#include <Hash.h>

#include <Arduino_JSON.h>
#include <assert.h>

ESP8266WiFiMulti WiFiMulti;
WebSocketsClient webSocket;

#define ADRESS "local-ip"
#define PORT 3000
#define URL "/"

#define SSID "wifi ssid"
#define PASSWRD "wifi pass"

#define boardID "to let the backend know which nodemcu is this"

void setup() {
Serial.begin(115200);
  // put your setup code here, to run once:
  pinMode(LED_BUILTIN,OUTPUT);

    for(uint8_t t = 4; t > 0; t--) {
    digitalWrite(LED_BUILTIN,!open);
        delay(1000);
    digitalWrite(LED_BUILTIN,open);
    }

    WiFiMulti.addAP(SSID, PASSWRD);

    //WiFi.disconnect();
    while(WiFiMulti.run() != WL_CONNECTED) {
    digitalWrite(LED_BUILTIN,!open);
        delay(50);
    digitalWrite(LED_BUILTIN,open);
    }

    // server address, port and URL
    webSocket.begin(ADRESS, PORT, URL);

    // event handler
    webSocket.onEvent(webSocketEvent);

    // try ever 5000 again if connection has failed
    webSocket.setReconnectInterval(5000);

}

void loop() {
  // put your main code here, to run repeatedly:
    webSocket.loop();
}

void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {

  if(type==WStype_DISCONNECTED){
    Serial.printf("[WSc] Disconnected!\n");
  }

  if(type==WStype_CONNECTED){
    String dataPack = "{\"event\":\"nodemcu\",\"clientType\":\"nodemcu\",\"nodemcuID\":\"";
    dataPack += boardID;
    dataPack += "\"}";
    webSocket.sendTXT(dataPack);
  }

  if(type==WStype_TEXT){ //using this section to handle on/off commands from web/server but this part in my original code has a lot of switch-case structures and its too long so im not including them here
    Serial.printf("[WSc] get text: %s\n", payload);

    char dataToMem[length];
    memcpy(dataToMem,payload,length+1);
    Serial.println(dataToMem);

    JSONVar myObject = JSON.parse(dataToMem);

    // JSON.typeof(jsonVar) can be used to get the type of the variable
    if (JSON.typeof(myObject) == "undefined") {
      Serial.println("Parsing input failed!");
      return;
    }


    Serial.print("JSON.typeof(myObject) = ");
    Serial.println(JSON.typeof(myObject)); // prints: object    
  }

//section below is where i can get the binary file from server. i wanna update the firmware here with the recieved binary data.
  if(type==WStype_BIN){
    Serial.printf("[WSc] get binary length: %u\n", length);
    hexdump(payload, length);//this line came from the library example code so i dont know what exactly it does and i never used it...
  }

  if(type==WStype_PING){
    // pong will be send automatically
  }

  if(type==WStype_PONG){
    // answer to a ping we send
  }
}

use the Updater object. the examples are the ArduinoOTA, ESP8266HTTPUpdate and ESP8266HTTPUpdateServer libraries

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.