ChipChop Support Forum
Log in
Log out
Join the forum
My Details
REPLIES: 8
VIEWS: 286
BACK
REPLY TO THIS POST
kennyFx
20 Oct 2023
OTA using ChipChop
Hi all,

Is it possible to do OTA (over the air) updates somehow using ChipChop?

I've come across some screenshot that someone has posted of their phone and it shows bit blurred at the bottom just about readable something like "ota version". Is this something available or I've imagined it?
viva amigo
21 Oct 2023

that would be nice but I can't see it mentioned anywhere
Son1cb00m
21 Oct 2023

undocd api?
DaraganGut0vic
23 Oct 2023

no answer, anyone? It would be helpful
pixie dust
24 Oct 2023

Common Gizmonster we know it exists, stop hiding stuff!🤡👀
Gizmo
24 Oct 2023

@pixie_dust

Gizmonster!? Been called all sorts but that's a first! Is that a scary clown emoji...wtf?

Sorry all for late response been busy.

lol... is this by any chance that screenshot? ;-)


uh, OTA is nothing special really I thought everyone has figured that out so I didn't bother writing anything about it :-p

I mean, I use predominantly ESPs so it's literally few lines of code. The only thing you really need is somewhere to temporarily upload the bin file and I trigger the ota manually from the ChipChop WebApp

I have made a simple class "ChipChop_ota" for ESPs using esp-idf "HTTPUpdate.h" and I'll post the code at the end but you can make your own it's really simple

Here is what I do:

Steps

1. In the Dev Console I add to every device I want to be able to do an OTA an extra component called ota | type text | mode interactive

2. Here's all I add in the main Arduino file


#include <chipchop_ota.h>
ChipChop_OTA ChipChop_ota;

String _OTA_VERSION = "1.0"; //change this every time you are creating a new ota update

    void ChipChop_onCommandReceived(String target, String value, String source, int command_age){
         
        //blah blah some commands actions here

        if(target == "ota"){ //that's the component I've made in the Dev Console
            ChipChop_ota.downloadUpdate(value);
        }
    }

    void setup(){
        
        ....whatever setup stuff you need

        // only needs to happen once when the device wakes up and it's really only to see in the WebApp/Dev Console which version it's running
        ChipChop.updateStatus("ota", _OTA_VERSION);

    }


3. Generate your update .bin file (I use PlatformIO) and make sure to change the _OTA_VERSION in the code so you can tell it's been updated

4. Upload the bin file somewhere. A little caveat though, I couldn't get this to work with https:// in like 30 seconds (needed it in a rush) so the little library only works with files accessible through plain old http://
This may sound like not a problem but if you are using basic/free hosting many servers will try and redirect all traffic to https:// automatically.

5. Open the ChipChop WebApp (can do it in the browser) https://my.chipchop.io, select the device and click on the "Send Command" button next to the "ota" component and type the full url to the .bin file
something like: http://someserver.com/somefolder/your_bin_file.bin and press send.

6. The WebApp will send a command to ChipChop library on your device which will trigger the callback on the "ota" component with the value of the url and then pass that value to the little ota library which will start the download & update process automatically.

You should see straight away in the WebApp that the update has started or if there are any errors and once the update is complete the ESP will restart automatically and once it connects back to ChipChop (be patient) the ota component will show whatever you've specified as _OTA_VERSION in your code.

That's it pretty much it, I'll add the library code in the next post


Attached images
Gizmo
24 Oct 2023

Here is the code for the chipchop_ota little library for ESP32 & ESP8266

chipchop_ota.h


    #ifndef CHIPCHOP_OTA
    #define CHIPCHOP_OTA
    #include <Arduino.h>


    class ChipChop_OTA{
        private:
        public:

        ChipChop_OTA();

        void downloadUpdate(String uri);

    };

    extern ChipChop_OTA ChipChop_ota;

    #endif


chipchop_ota.cpp


    #include <Arduino.h>
    #include <chipchop_ota.h>

    #ifdef ESP32
        #include <WiFi.h>
    #include <HTTPClient.h>
    #include <HTTPUpdate.h>

     #else //for ESP8266

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

    #include <ChipChopManager.h>
    extern ChipChopManager ChipChop;

    void ChipChop_OTA_update_started(){
        ChipChop.triggerEvent("ota","update started");
    }

    void ChipChop_OTA_update_finished() {
        // only useful for serial monitor output, ChipChop can not be reached at this point
    }

    void ChipChop_OTA_update_progress(int cur, int total) {
        float percent = cur * 100 / total;
        // only useful for serial monitor output
    }

    void ChipChop_OTA_update_error(int err) {
        
        ChipChop.triggerEvent("ota","error"); // inform immediately the WebApp that the OTA didn't work

    }

    ChipChop_OTA::ChipChop_OTA(){

        #ifdef ESP32
         httpUpdate.onStart(ChipChop_OTA_update_started);
         httpUpdate.onEnd(ChipChop_OTA_update_finished);
         httpUpdate.onProgress(ChipChop_OTA_update_progress);
         httpUpdate.onError(ChipChop_OTA_update_error);
        #else //for ESP8266
        ESPhttpUpdate.setClientTimeout(2000);
        ESPhttpUpdate.onStart(ChipChop_OTA_update_started);
        ESPhttpUpdate.onEnd(ChipChop_OTA_update_finished);
        ESPhttpUpdate.onProgress(ChipChop_OTA_update_progress);
        ESPhttpUpdate.onError(ChipChop_OTA_update_error);
        #endif

    }


    void ChipChop_OTA::downloadUpdate(String uri){

        WiFiClient wifi_client;
         #ifdef ESP32
        t_httpUpdate_return ret = httpUpdate.update(wifi_client, uri);
        #else
        t_httpUpdate_return ret = ESPhttpUpdate.update(wifi_client, uri);
        #endif


    switch (ret) {
    #ifdef ESP32
    case HTTP_UPDATE_FAILED: Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s\n", httpUpdate.getLastError(), httpUpdate.getLastErrorString().c_str());
    #else //for ESP8266
        case HTTP_UPDATE_FAILED: Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
    #endif

    break;

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

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

}


Put the two files in some folder and on PlatformIO put that folder in the "lib" folder of your project. On Arduino IDE (not tested) I guess put that folder in the "documents/arduino/libraries/"


That's it, enjoy and hope this helps
pixie dust
25 Oct 2023

Hey thanks, would that work if I use GitHub to upload the .bin file? or any suggestions where I can get some free space for uploads?
Gizmo
25 Oct 2023

No idea, don't use GitHub ;-)