Programming esp8266 on the Internet

Hello.
Is it possible to program a large number of ESP8266 via the Internet (outside the local network)?

Yes it is.

Check examples related to the library included in the main core <ESP8266httpUpdate.h>

Thank you so much. Can you please explain a bit about how it works?
thank you

I installed the HFS software and uploaded the file and uploaded the server address and file to nodemcu. The request to download the file from the server is sent by nodemcu, but the download operation is not performed.

HI @navidh91
I've just tested with this software and it works as expected. Maybe your firewall is blocking the request?

Anyway, that's fine as a starting point, but if you put everything online I don't think downloading all the firmware without any kind of control is an efficient way to do this.

In the example of one of my libraries, I had done this: the MCU downloads a JSON file where there is the current version and the complete address of the new firmware inside.

If you want take a look, you can find here.
The library in question allows to host a webserver within the ESP with a web page through which the user can send the command to verify / update the new firmware.

Thank you for your help
Can you please change the code so that esp32 connects directly to wifi in STA mode and frequently checks the version-esp32.json file and automatically updates if a new version arrives?

What do you mean exactly? This sketch start in AP mode only if no known SSID are found.
In that case it provide a WiFiManager landing page in order to setup your own WiFi network.

The example is intended to perfom check and update only after a "manual trigger" from user within the included webpage.
In order to do what you are asking for, you need to call the remote url where your JSON is stored and then parse it from ESP side using a WiFi client.

It's quite simple... something like this for example (off course it's just an how-to, not a complete sketch):

amazing. Thank you for your help

In the version-esp32.json file you specified the firmware path:

https://raw.githubusercontent.com/cotestatnt/esp-fs-webserver/main/examples/remoteOTA/fw-esp32/firmware.bin

How can I create a raw path from the firmware .bin file in GitHub?

Upload your compiled firmware in your own repository and then copy&paste the download link

It detects the "version-esp32.json" file and changes the version to 1.01 but does not upload new firmware.

As i've wrote before, this is not a complete sketch!
You need to add code related to firmware update as you can see in the proveded core examples.

Thanks for your help my friend. The problem was solved.

My problem was solved for esp32. It gives me this error when I want to change the code for esp8266. I do not understand where the problem is?
Thank you for your help

ESP32 Code:


#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <HTTPUpdate.h>
#include <WiFiClientSecure.h>

#define WIFI_SSID "Ang-IoT"
#define WIFI_PASS "qwerty2580"
const char* ssid = WIFI_SSID;
const char* password = WIFI_PASS;

const char* fimwareInfo = "https://raw.githubusercontent.com/navid-h/SelfUpdate/main/version-esp32.json";
#define URL_fw_Bin "https://raw.githubusercontent.com/navid-h/SelfUpdate/main/firmware.bin"

const char* actualVersion = "1.0.6";
//void firmwareUpdate();

void setup() {
  Serial.begin(115200);
  Serial.print("Connecting to WiFi");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
  Serial.println(" Connected!");
}

void loop() {
  delay(10);
  static uint32_t checkTime;
  if (millis() - checkTime > 10000) {
    checkTime = millis();
    WiFiClientSecure *client = new WiFiClientSecure;  
    //firmwareUpdate();  
    HTTPClient http;
    client->setInsecure();

    Serial.print("[HTTP] begin...\n");    
    http.begin(*client, fimwareInfo); //HTTP
    
    Serial.print("[HTTP] GET...\n");
    // start connection and send HTTP header
    int httpCode = http.GET();
      delay(500);
    // httpCode will be negative on error
    if (httpCode > 0) {
      // HTTP header has been send and Server response header has been handled
      Serial.printf("[HTTP] GET... code: %d\n", httpCode);
      // file found at server
      if (httpCode == HTTP_CODE_OK) {
        String payload = http.getString();

        // Parse the payload in order to get version and url        
        DynamicJsonDocument doc(512);
        DeserializationError err = deserializeJson(doc, payload);
//        if (err) {
//          Serial.print(F("deserializeJson() failed with code "));
//          Serial.println(err.f_str());
//        }
        
        String version = doc["version"].as<String>();
        Serial.println(version);
        if(!version.equals(actualVersion)) {
          Serial.println("DO UPDATE!!!");
          firmwareUpdate();
          Serial.println("New Version Updated!");
        }
        else{
          Serial.print("Device already on latest firmware version");
        }
      }
    } else {
      Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
    }

    http.end();
  }
}

  void firmwareUpdate(void) {
   WiFiClientSecure client;
  client.setInsecure();
  httpUpdate.setLedPin(LED_BUILTIN, LOW);
  t_httpUpdate_return ret = httpUpdate.update(client, URL_fw_Bin);

  switch (ret) {
    case HTTP_UPDATE_FAILED:
      Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
      break;

    case HTTP_UPDATE_NO_UPDATES:
      Serial.println("HTTP_UPDATE_NO_UPDATES");
      break;

    case HTTP_UPDATE_OK:
      Serial.println("HTTP_UPDATE_OK");
      break;
  }
}



//int FirmwareVersionCheck(void) {
//  String payload;
//  int httpCode;
//  String fwurl = "";
//  fwurl += URL_fw_Version;
//  fwurl += "?";
//  fwurl += String(rand());
//  Serial.println(fwurl);
//  WiFiClientSecure * client = new WiFiClientSecure;
//
//  if (client) 
//  {
//    // Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is 
//    HTTPClient https;
//
//    if (https.begin( * client, fwurl)) 
//    { // HTTPS      
//      Serial.print("[HTTPS] GET...\n");
//      // start connection and send HTTP header
//      delay(100);
//      httpCode = https.GET();
//      delay(100);
//      if (httpCode == HTTP_CODE_OK) // if version received
//      {
//        payload = https.getString(); // save received version
//      } else {
//        Serial.print("error in downloading version file:");
//        Serial.println(httpCode);
//      }
//      https.end();
//    }
//    delete client;
//  }
//      
//  if (httpCode == HTTP_CODE_OK) // if version received
//  {
//    payload.trim();
//    if (payload.equals(FirmwareVer)) {
//      Serial.printf("\nDevice already on latest firmware version:%s\n", FirmwareVer);
//      return 0;
//    } 
//    else 
//    {
//      Serial.println(payload);
//      Serial.println("New firmware detected");
//      return 1;
//    }
//  } 
//  return 0;  
//}

ESP8266 Code:


//If the firmware file could not be downloaded by esp, first create a cpp file and upload it to github.
//Then create a raw path from the cpp file. Copy the path but upload the new fimrware with the .bin extension.

#include <WiFi.h>
#include <ArduinoJson.h>

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#include <WiFiClientSecure.h>



#define WIFI_SSID "Ang-IoT"
#define WIFI_PASS "qwerty2580"
char* ssid = WIFI_SSID;
char* password = WIFI_PASS;

const char* fimwareInfo = "https://raw.githubusercontent.com/navid-h/SelfUpdate/main/version-esp32.json";
#define URL_fw_Bin "https://raw.githubusercontent.com/navid-h/SelfUpdate/main/firmware.bin"

const char* actualVersion = "1.0.6";
//void firmwareUpdate();

void setup() {
  Serial.begin(115200);
  Serial.print("Connecting to WiFi");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
  Serial.println(" Connected!");
}

void loop() {
  delay(10);
  static uint32_t checkTime;
  if (millis() - checkTime > 10000) {
    checkTime = millis();
    WiFiClientSecure *client = new WiFiClientSecure;  
    //firmwareUpdate();  
    HTTPClient http;
    client->setInsecure();

    Serial.print("[HTTP] begin...\n");    
    http.begin(*client, fimwareInfo); //HTTP
    
    Serial.print("[HTTP] GET...\n");
    // start connection and send HTTP header
    int httpCode = http.GET();
      delay(500);
    // httpCode will be negative on error
    if (httpCode > 0) {
      // HTTP header has been send and Server response header has been handled
      Serial.printf("[HTTP] GET... code: %d\n", httpCode);
      // file found at server
      if (httpCode == HTTP_CODE_OK) {
        String payload = http.getString();

        // Parse the payload in order to get version and url        
        DynamicJsonDocument doc(512);
        DeserializationError err = deserializeJson(doc, payload);
//        if (err) {
//          Serial.print(F("deserializeJson() failed with code "));
//          Serial.println(err.f_str());
//        }
        
        String version = doc["version"].as<String>();
        Serial.println(version);
        if(!version.equals(actualVersion)) {
          Serial.println("DO UPDATE!!!");
          firmwareUpdate();
          Serial.println("New Version Updated!");
        }
        else{
          Serial.print("Device already on latest firmware version");
        }
      }
    } else {
      Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
    }

    http.end();
  }
}

  void firmwareUpdate(void) {
   WiFiClientSecure client;
  client.setInsecure();
  ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW);
  t_httpUpdate_return ret = ESPhttpUpdate.update(client, URL_fw_Bin);

  switch (ret) {
    case HTTP_UPDATE_FAILED:
      Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
      break;

    case HTTP_UPDATE_NO_UPDATES:
      Serial.println("HTTP_UPDATE_NO_UPDATES");
      break;

    case HTTP_UPDATE_OK:
      Serial.println("HTTP_UPDATE_OK");
      break;
  }
}



//int FirmwareVersionCheck(void) {
//  String payload;
//  int httpCode;
//  String fwurl = "";
//  fwurl += URL_fw_Version;
//  fwurl += "?";
//  fwurl += String(rand());
//  Serial.println(fwurl);
//  WiFiClientSecure * client = new WiFiClientSecure;
//
//  if (client) 
//  {
//    // Add a scoping block for HTTPClient https to make sure it is destroyed before WiFiClientSecure *client is 
//    HTTPClient https;
//
//    if (https.begin( * client, fwurl)) 
//    { // HTTPS      
//      Serial.print("[HTTPS] GET...\n");
//      // start connection and send HTTP header
//      delay(100);
//      httpCode = https.GET();
//      delay(100);
//      if (httpCode == HTTP_CODE_OK) // if version received
//      {
//        payload = https.getString(); // save received version
//      } else {
//        Serial.print("error in downloading version file:");
//        Serial.println(httpCode);
//      }
//      https.end();
//    }
//    delete client;
//  }
//      
//  if (httpCode == HTTP_CODE_OK) // if version received
//  {
//    payload.trim();
//    if (payload.equals(FirmwareVer)) {
//      Serial.printf("\nDevice already on latest firmware version:%s\n", FirmwareVer);
//      return 0;
//    } 
//    else 
//    {
//      Serial.println(payload);
//      Serial.println("New firmware detected");
//      return 1;
//    }
//  } 
//  return 0;  
//}

And here is the error:

@navidh91

You have leaved this file included which is not present in the ESP8266 Arduino Core

#include <WiFi.h>

Thanks for your help but the problem was not solved.
Someone did this in the following link.

The problem with esp8266 with github is that it does not have an ssl certificate. Do you know how to solve this problem?
I got the certificate from this website GRC | SSL TLS HTTPS Web Server Certificate Fingerprints   but I do not know how to put it in the source code.
Thank you for your help.

You can use the instruction client.setInsecure() to skip SSL certificate verfication,
or if you want to use Github certificate check this example.

You will find both certificate as byte array, and the way to use it with ESP8266

Arduino/HTTPSRequest.ino at master · esp8266/Arduino (github.com)

Thank you my friend. My problem was solved by adding client.setInsecure ().
thanks a lot

How can I implement this firmware comparison with the .json file on esp8266?
I tested it gives httpcode -11.

#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>

const char* wifiName = "Ang-IoT";
const char* wifiPass = "qwerty2580";

//Web Server address to read/write from 
const char *host = "https://raw.githubusercontent.com/navid-h/SelfUpdate/main/version-esp32.json";

void setup() {
  
  Serial.begin(115200);
  delay(10);
  Serial.println();
  
  Serial.print("Connecting to ");
  Serial.println(wifiName);

  WiFi.begin(wifiName, wifiPass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());   //You can get IP address assigned to ESP
}

void loop() {
  HTTPClient http;    //Declare object of class HTTPClient
  Serial.print("Request Link:");
  Serial.println(host);
  WiFiClient wclient;
  http.begin(wclient,host);     //Specify request destination
  
  int httpCode = http.GET();            //Send the request
  String payload = http.getString();    //Get the response payload from server

  Serial.print("Response Code:"); //200 is OK
  Serial.println(httpCode);   //Print HTTP return code

  Serial.print("Returned data from Server:");
  Serial.println(payload);    //Print request response payload
  
  if(httpCode == 200)
  {
    // Allocate JsonBuffer
    // Use arduinojson.org/assistant to compute the capacity.
    
    Serial.println(F("Response:"));
   
  }
  else
  {
    Serial.println("Error in response");
  }

  http.end();  //Close connection
  
  delay(5000);  //GET Data at every 5 seconds
}

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