Porting Software from a Raspberry?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
pengoo
Posts: 2
Joined: Mon Dec 23, 2019 4:29 pm

Porting Software from a Raspberry?

Post by pengoo » Mon Dec 23, 2019 5:05 pm

Hello all,
I have implemented an intelligent underfloor heating control on a Raspberry Pi. The idea is that it controls using outside temperature retrieved from the internet and then switching valves on GPIO using PHP and Python today.

I am really curious if this could be ported to an ESP8266. Anyways there are some challenges ahead:
  1. Webserver: I have seen that something is existing already; but it looks like beeing able to provide static html.
  2. Dynamic web page creation: Today I am using PHP to process the user input and give feedback. It seems to me as if I would need to specifically extend an existing webserver implementation. At least I did not find a generic micropython solution to create dynamic web
  3. XML parsing: I am using a public weather API from the internet to get temperature, wind etc. and use it for calculation in my control. Seems like this parsing would require some manual solution as well
  4. Configuration file: Today I use a file (xml of course;-) to store this and read during program run.
  5. Separation of WebUI and heating control: In order not to make the control of the heating directly depend on the webserver reply, there is a separate service running as cron job in the background that does the control. The communicate via a file in ramdisk today.
  6. For interpolation of a characteristic I am using numpy today: I know that it would not require huge code to implement this routine. But best case of course would be to use something existing.
Things like timers and GPIO handling seem to be covered by micropython, so no issue here I guess.

My overall target is, to prevent coding too many solution specific things for those "generic problems". Feel free to suggest alternative solutions for the bullet points if easy ports are not available.

Cheers Armin
Hoping to find some quiet days now for coding between the years :P

User avatar
MostlyHarmless
Posts: 166
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: Porting Software from a Raspberry?

Post by MostlyHarmless » Mon Dec 23, 2019 7:42 pm

pengoo wrote:
Mon Dec 23, 2019 5:05 pm
Hello all,
Hello and welcome to the forum.
pengoo wrote:
Mon Dec 23, 2019 5:05 pm
I am really curious if this could be ported to an ESP8266. Anyways there are some challenges ahead:
There are challenges indeed. The ESP8266 is very resource constrained. If I were you I would first create a proof of concept on a more powerful chip, like the ESP32, then see if it can be made to work using frozen bytecode and other optimizations on the smaller cousin. Even then, the ESP32 won't be happy with some of the below, but let me go over it point by point.
pengoo wrote:
Mon Dec 23, 2019 5:05 pm
Webserver: I have seen that something is existing already; but it looks like beeing able to provide static html.

Dynamic web page creation: Today I am using PHP to process the user input and give feedback. It seems to me as if I would need to specifically extend an existing webserver implementation. At least I did not find a generic micropython solution to create dynamic web
There are a few solutions out there. Some even mimic Flask behavior. With limited functionality of course, but quite useful. You might want to take a look at picoweb and utemplate.
pengoo wrote:
Mon Dec 23, 2019 5:05 pm
XML parsing: I am using a public weather API from the internet to get temperature, wind etc. and use it for calculation in my control. Seems like this parsing would require some manual solution as well
JSON is well supported. Does the weather service you use offer that as an option?
pengoo wrote:
Mon Dec 23, 2019 5:05 pm
Configuration file: Today I use a file (xml of course;-) to store this and read during program run.
Ditto, replace XML with JSON.
pengoo wrote:
Mon Dec 23, 2019 5:05 pm
Separation of WebUI and heating control: In order not to make the control of the heating directly depend on the webserver reply, there is a separate service running as cron job in the background that does the control. The communicate via a file in ramdisk today.
Sorry the wording, but that ramdisk sounds like one piece of a kludge. It seems to be an inefficient way to communicate between a PHP UI and a Python background service.

In Micropython on a system like these, everything will have to be Python (or some C extension built into the firmware). It all will be running in one application. Even if threading might be better supported some day, it all will still be one Python interpreter. You will have the distinction of modules, but they all will be part of one app in one interpreter.

To multiplex between all the parts I suggest uasyncio. picoweb (suggested above) works well with that and writing coroutines that do your background processing (cron replacement) is rather straight forward. Those parts can then communicate via shared in-memory objects with synchronization primitives (Lock, Event).
pengoo wrote:
Mon Dec 23, 2019 5:05 pm
For interpolation of a characteristic I am using numpy today: I know that it would not require huge code to implement this routine. But best case of course would be to use something existing.
If you can't "embed" numpy but for now want to stick with it, would outsourcing that math to an external server be an option?
pengoo wrote:
Mon Dec 23, 2019 5:05 pm
Hoping to find some quiet days now for coding between the years :P
Don't we all?


Regards, Jan

pengoo
Posts: 2
Joined: Mon Dec 23, 2019 4:29 pm

Re: Porting Software from a Raspberry?

Post by pengoo » Wed Dec 25, 2019 3:59 pm

Thank you, Jan!
Seems like it could be possible :D But some work to do for me.
JSON is supported by my weather API. Apart from that I don't see real blockers. Linear interpolation is the only thing I would need from numpy.

Regarding the dodgy solution of a file between WebUI and python background service: Maybe I exaggerated the "everything is a file philosophy" ;) I am not a pro in that matter. How would you have done that?

User avatar
MostlyHarmless
Posts: 166
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: Porting Software from a Raspberry?

Post by MostlyHarmless » Wed Dec 25, 2019 5:18 pm

pengoo wrote:
Wed Dec 25, 2019 3:59 pm
I am not a pro in that matter. How would you have done that?
Not a pro either. The story usually goes like

Copilot: "Captain, how come you never make any mistakes?"
Captain: "Experience."
Copilot: "How did you get all that experience?"
Captain: "Surviving mistakes!"

Anyhow, in my PHP days the Python background stuff would have been a socket server itself, to which the PHP UI could connect to communicate. They would have been talking some sort of line based protocol. Although back in the days when I used PHP, the background service would have been in Tcl, mostly because Python didn't exist and Perl made the hair in my neck stand straight up.

Today on anything as powerful as a RPi3 or better, it would be a Flask + Jinja2 based web server and the background stuff running in separate threading.Thread()s.

For an embedded system like you describe I would use exactly what I suggested above. picoweb + utemplate + uasyncio.

It all depends on the requirements. If there are hard maximum latency requirements, I wouldn't even consider Java or any other garbage hoarding language and go straight down to C.


Happy Winter Solstice, Jan

User avatar
MostlyHarmless
Posts: 166
Joined: Thu Nov 21, 2019 6:25 pm
Location: Pennsylvania, USA

Re: Porting Software from a Raspberry?

Post by MostlyHarmless » Wed Dec 25, 2019 5:23 pm

In all fairness though I do have to admit that I once implemented something that collects data via UDP socket, then writes the aggregated data into a temp file on ram disk, so that consumers can read it back in if and when needed, which happens seldom. That code is still in place and working many years later now. It is the statistics collector of the PostgreSQL database system.


:)

Post Reply