Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    OTA or not OTA that is the question...

    Scheduled Pinned Locked Moved WiPy 2&3
    86 Posts 11 Posters 52.5k Views 14 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • robert-hhR Offline
      robert-hh @Paco88
      last edited by

      @Paco88 Sorry Sir. Please explain more detailed what you want to achieve and what you tried to get here.
      The FAT file system of a standard WiPy is too small to hold the file appimg.bin. If you want to store appimg.bin in the file system, you have to extend it, e.g. by adding a SD card. And still, at the moment you cannot install that file as a new firmware. One could try to implement a similar mechanism as is used for OTA: if an attempt is made to store a file at /flash/sys, it is accepted as a new firmware stream. But that is not there yet and tricky to implement.

      1 Reply Last reply Reply Quote -1
      • B Offline
        BetterAuto Pybytes Beta
        last edited by BetterAuto

        I had envisioned the devices self-updating by downloading the file to the local file system via a secure manner e.g. HTTPS or MQTT over TLS then copying that via an FTP client Py script to its own internal FTP server for final install. If you're saying the internal file system is too small I could see using the SD card or perhaps even an external I2C flash memory.

        Is this feasible?

        robert-hhR 2 Replies Last reply Reply Quote 0
        • robert-hhR Offline
          robert-hh @BetterAuto
          last edited by

          @BetterAuto If you can have a ftp client connect to the local server, then it should be possible. A small ftp client was provided by spotlightkid here: https://github.com/SpotlightKid/micropython-ftplib

          1 Reply Last reply Reply Quote 1
          • robert-hhR Offline
            robert-hh @BetterAuto
            last edited by

            @betterauto Actually I gave it a try. The ftp client is working, when I connect to a different device. But I could not get a connection to itself.
            Unless there is another option, you could do that: If you have a cluster of devices, then one of them could act as an update server, which pulls the update from a remote hub and distributes that locally.

            robert-hhR 1 Reply Last reply Reply Quote 0
            • B Offline
              BetterAuto Pybytes Beta
              last edited by

              What error did you get? Doesn't make sense it can't connect to itself, it's just another IP right?

              robert-hhR 1 Reply Last reply Reply Quote 0
              • robert-hhR Offline
                robert-hh @BetterAuto
                last edited by

                This post is deleted!
                1 Reply Last reply Reply Quote 0
                • robert-hhR Offline
                  robert-hh @robert-hh
                  last edited by robert-hh

                  @robert-hh By looking into the pycom module I found that an OTA interface for Python scripts already exists. There are three methods:
                  pycom.ota_start()
                  pycom.ota_write(data)
                  pycom.ota_finish()
                  So one can easily use them. Start the update by calling ota_start(). Then receive the image data in arbitrary chunks and call ota_write() with it. Once the data is transferred, call ota_finish().

                  B 1 Reply Last reply Reply Quote 2
                  • B Offline
                    BetterAuto Pybytes Beta @robert-hh
                    last edited by

                    @robert-hh does that only update files or also the firmware? Where do I find more info on this?

                    robert-hhR 2 Replies Last reply Reply Quote 0
                    • robert-hhR Offline
                      robert-hh @BetterAuto
                      last edited by

                      @betterauto It ONLY updates the firmware. There is no documentation yet. I'll give it a try by using ftplib for downloading the file and let you know.

                      1 Reply Last reply Reply Quote 0
                      • robert-hhR Offline
                        robert-hh @BetterAuto
                        last edited by robert-hh

                        @betterauto OK. So here is my very simple sample script, using ftplib:

                        #
                        # OTA update using ftplib and the pycom.ota_* methods
                        #
                        from ftplib import *
                        from pycom import *
                        
                        def do_write(data):
                            ota_write(data)
                            print(".", end="")
                        
                        def ota():
                            ftp = FTP("server_ip", 21, "user", "password")
                            ota_start()
                            ftp.retrbinary("RETR appimg.bin", do_write, 1024)
                            ota_finish()
                            
                        ota()
                        

                        I added an intermediate call to do_write() to have an progress indication.
                        Obviously you can use ftplib to download other files as well.

                        P 1 Reply Last reply Reply Quote 2
                        • B Offline
                          BetterAuto Pybytes Beta
                          last edited by BetterAuto

                          So this code uploads a bin file from the local file system to the local ftp server? Looks like the other way round.

                          Edit: Nevermind, I see you are fetching the file from an ftp server to flash it. Beautiful. I can do the same but fetch the file with MQTT over TLS to the local file system and flash itself in chunks.

                          I hope to try it soon. Nice find.

                          robert-hhR 1 Reply Last reply Reply Quote 0
                          • robert-hhR Offline
                            robert-hh @BetterAuto
                            last edited by robert-hh

                            @betterauto You do not have to store the file in the local files system, if there is no space. You can feed the data into OTA on the fly while fetching.
                            I made a few attempts with:

                            • interrupting an update and then starting a new one.
                            • repeating the update w/o restart in between.

                            All was stable. Not a very exhaustive test, but at least a first check. Since the image itself carries a checksum & signature, which is checked at the call of ota_finish(), transmission errors should not harm.

                            1 Reply Last reply Reply Quote 1
                            • P Offline
                              Paco88 @robert-hh
                              last edited by

                              @robert-hh
                              Have you performed a full OTA write?

                              robert-hhR 1 Reply Last reply Reply Quote 0
                              • robert-hhR Offline
                                robert-hh @Paco88
                                last edited by

                                @paco88 Of course, several times. And I verified that the new image is then excuted, by embedding changing frozen bytecode.
                                The example below is simplified, in that I had to insert a gc.collect() into the do_write() function, epsecially with a larger buffer size.
                                I have sketched another example here, which uses a fixed preallocated buffer. And yes, I tested that variant too.

                                # Firmware update by reading the image from the SD card
                                #
                                from pycom import ota_start, ota_write, ota_finish
                                from os import mount
                                from machine import SD
                                
                                BLOCKSIZE = const(4096)
                                APPIMG = "/sd/appimg.bin"
                                
                                sd = SD()
                                mount(sd, '/sd')
                                
                                with open(APPIMG, "rb") as f:
                                    buffer = bytearray(BLOCKSIZE)
                                    mv = memoryview(buffer)
                                    size=0
                                    ota_start()
                                    while True:
                                        chunk = f.readinto(buffer)
                                        if chunk > 0:
                                            ota_write(mv[:chunk])
                                            size += chunk
                                            print("\r%7d " % size, end="")
                                        else:
                                            break
                                    ota_finish()
                                
                                P 1 Reply Last reply Reply Quote 3
                                • P Offline
                                  Paco88 @robert-hh
                                  last edited by

                                  @robert-hh
                                  I'm also trying. Occasionally I have memory allocation problems, nothing that can't be solved with the garbage collector.
                                  Thanks for the tips

                                  1 Reply Last reply Reply Quote 0
                                  • M Offline
                                    mdnncl01
                                    last edited by

                                    Hi all guys, I'm a beginner in the Pycom and Python world. With my team we decided to start experimenting with these devices (WiPy),
                                    but for us it is absolutely essential to update the OTA of the user application for a real use in the field. From all the discussions
                                    and documentation this aspect is not very clear; It would also be nice to have a possible firmware upgrade of the module; this aspect
                                    is also unclear. Is there currently a stable procedure to update at least the user application? I'm sorry for my english. Thanks

                                    robert-hhR 1 Reply Last reply Reply Quote 0
                                    • sebS Offline
                                      seb
                                      last edited by

                                      The firmware allows the python code to be updated over FTP or UART at the moment and the pycom module include some functions for updating the firmware OTA (https://docs.pycom.io/chapter/firmwareapi/pycom/pycom.html). I am currently working on an example project to demonstrate updating both firmware and python code over WiFi but this will take a couple weeks at least to complete. I will post a reply here once it has been completed.

                                      1 Reply Last reply Reply Quote 1
                                      • robert-hhR Offline
                                        robert-hh @mdnncl01
                                        last edited by robert-hh

                                        @mdnncl01 From what I understand of your question, you want to update both your application and the firmware. So you have to consider two aspects:

                                        a) If your application is a Python source file or precompiled file, you can load that, store it into the file system and reboot.

                                        b) If your application is embedded into the firmware, or if you want to update the firmware, you have to do a firmware update, and that's what is called OTA here. The mechanism itself is implemented and works. Below in the post you see two examples. (i) The first dowloads the new image from a ftp file server and flashes it during the download. That method does not need extra file space on the device. (ii) The second assumes, that the new image is downloaded to the file system and transfers it from the file into the flash. Since the firmware image is pretty large, the internal file system of the old devices cannot handle it. Therefore, in the example a SD card is used. The new pycom devices with 8M flash have sufficient space to store that file and a SD card is not required.

                                        You migth have to implement your own method of downloading the files or images from your server. The given examples just demonstrate the use of the OTA system calls.

                                        1 Reply Last reply Reply Quote 0
                                        • M Offline
                                          mdnncl01
                                          last edited by

                                          @seb and @robert-hh Thank. @seb I'm waiting for your post.

                                          1 Reply Last reply Reply Quote 1
                                          • M Offline
                                            mfallavol @robert-hh
                                            last edited by

                                            @robert-hh I know this is an old thread but does ftplib downloaded from github work for you with the current firmware (1.18.2.r6) or did you have to modify it? I'm having some issues with it. Firstly an error in resolve_addr() at the call to socket.getaddrinfo. I removed the family and now I'm getting an error trying to connect. Just wondering if someone else has already gone through this and can save me some time. Ultimately I want to see if I can get OTA working.
                                            -- Mike

                                            robert-hhR 1 Reply Last reply Reply Quote 0

                                            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                                            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                                            With your input, this post could be even better 💗

                                            Register Login
                                            • 1
                                            • 2
                                            • 3
                                            • 4
                                            • 5
                                            • 5 / 5
                                            • First post
                                              Last post

                                            Pycom on Twitter

                                            Pycom