Use micropython to do dead simple "over-the-air" code updates

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
WhiteHare
Posts: 129
Joined: Thu Oct 04, 2018 4:00 am

Use micropython to do dead simple "over-the-air" code updates

Post by WhiteHare » Fri Oct 05, 2018 9:52 pm

The idea for doing OTA code updates is simple enough: you transmit each line of code and the target assembles it into a string. Then the target executes the string, which updates the code on the target. QED

Here's a notional example of what I mean:

Code: Select all

>>> simpleCode="def j(x): return x**2"
>>> simpleCode
'def j(x): return x**2'
>>> exec(simpleCode)
>>> j(5)
25
>>>
You can use whatever radio transport you want, and I'm planning to use Nordic's nRF52 "proprietary radio" because it's the simplest. With this approach, there's no reliance on bluetooth or Nordic's DFU. :D

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Use micropython to do dead simple "over-the-air" code updates

Post by jickster » Fri Oct 05, 2018 10:06 pm

Are you asking for help?


Sent from my iPhone using Tapatalk Pro

User avatar
WhiteHare
Posts: 129
Joined: Thu Oct 04, 2018 4:00 am

Re: Use micropython to do dead simple "over-the-air" code updates

Post by WhiteHare » Fri Oct 05, 2018 10:24 pm

Not per se, unless this is old news and somebody has already done it. For instance, if there's already a library, I'd love to know what it is so that I don't re-invent the wheel.

Honestly, I was a bit shocked that no one had developed a way for doing OTA micropython updates on the BBC micro:bit. This technique would, of course, also work especially well for the BBC micro:bit, because in that case, there's not enough room for both miropython and the bluetooth stack. Or so I've read, and that also appears to be the excuse for why there is no OTA update mechanism for micropython on the BBC micro:bit. Nonetheless, OTA updates should be fairly easy to accomplish on the BBC micro:bit using Nordic's "proprietary" radio mode and direct manipulation of nRF51822's radio's registers. I say "proprietary" in quotation marks, because it's Nordic's term for it, but it really just means "non-standardized". We're all free to use it. I imagine the micropython code required to do OTA updates would be compact enough to fit within the space limitations.

Personally, I'm more interested in the nRF52840, which is capable of a lot more oomph. So, with the OP post here, I'm just trying to provide the motivation for why others should also be interested in the nRF5 series too.

I would, of course, appreciate any answers to the question I posted earlier in another thread, though: viewtopic.php?f=2&t=5350

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Use micropython to do dead simple "over-the-air" code updates

Post by jickster » Fri Oct 05, 2018 10:51 pm

WhiteHare wrote:Then the target executes the string, which updates the code on the target.
Which code on target do you want to update?
FROZEN_STR?
FROZEN_MPY?
Micropython firmware?


Sent from my iPhone using Tapatalk Pro

User avatar
WhiteHare
Posts: 129
Joined: Thu Oct 04, 2018 4:00 am

Re: Use micropython to do dead simple

Post by WhiteHare » Fri Oct 05, 2018 11:57 pm

jickster wrote:
Fri Oct 05, 2018 10:51 pm
WhiteHare wrote:Then the target executes the string, which updates the code on the target.
Which code on target do you want to update?
The goal would be whatever code needs updating.

Is there a gotcha that I'm not seeing?

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Use micropython to do dead simple "over-the-air" code updates

Post by jickster » Sat Oct 06, 2018 12:40 am

WhiteHare wrote:
jickster wrote:
Fri Oct 05, 2018 10:51 pm
WhiteHare wrote:Then the target executes the string, which updates the code on the target.
Which code on target do you want to update?
The goal would be whatever code needs updating.

Is there a gotcha that I'm not seeing?
Are you a C/C++ programmer?


Sent from my iPhone using Tapatalk Pro

User avatar
WhiteHare
Posts: 129
Joined: Thu Oct 04, 2018 4:00 am

Re: Use micropython to do dead simple "over-the-air" code updates

Post by WhiteHare » Sat Oct 06, 2018 1:47 am

Well, for microcontrollers, that's the language that I've been using.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Use micropython to do dead simple "over-the-air" code updates

Post by jickster » Sat Oct 06, 2018 5:26 am

WhiteHare wrote:Well, for microcontrollers, that's the language that I've been using.
Updating the firmware of micropython cannot be done while running micropython.


Sent from my iPhone using Tapatalk Pro

loboris
Posts: 344
Joined: Fri Oct 02, 2015 6:19 pm

Re: Use micropython to do dead simple

Post by loboris » Sat Oct 06, 2018 8:21 am

jickster wrote:
Sat Oct 06, 2018 5:26 am
Updating the firmware of micropython cannot be done while running micropython.
On ESP32 boards it is an easy process and it is implemented in my MicroPython port.

User avatar
WhiteHare
Posts: 129
Joined: Thu Oct 04, 2018 4:00 am

Re: Use micropython to do dead simple "over-the-air" code updates

Post by WhiteHare » Sun Oct 07, 2018 10:05 pm

Here's a better proof-of-concept example than what I posted earlier above:

Code: Select all

from microbit import *
import radio

print ("Starting...");
radio.on()

txCode = "def j(x): return x**2"

codeReceived = False
while (not codeReceived):
    radio.send(txCode)
    sleep(1000)
    newCode = radio.receive()


    if (newCode != None) :
        print(newCode)
        exec(newCode)
        codeReceived = True
Install it on two BBC micro:bit's. The result is this:

Code: Select all

Starting...
def j(x): return x**2
MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit v1.0.0 with nRF51822
Type "help()" for more information.
>>> j(4)
16
>>>
i.e. "def j(x): return x**2" was transmitted from one micro:bit to the other, and it is now a part of the code base that's running on the micro:bit that received it. :D

Post Reply