Host pushing JSON config file to Pyboard

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
doublevee
Posts: 4
Joined: Mon Jul 02, 2018 11:09 pm

Host pushing JSON config file to Pyboard

Post by doublevee » Mon Jul 02, 2018 11:24 pm

Hi - I have never posted in a forum before so if this is in the wrong place, or against the rules, please be gentle with me.

I have been using Micropython now for about 6 months and am a total novice at best.

I would like to use uPy in a project I am working on (my background is hardware) to display data on multiple I2C screens. I have tried a few different approaches, but the best architecture vs performance would be to have a host 'push' a JSON config file to the Pyboard. The Pyboard will then read, parse and display the necessary data across multiple displays.

The area I am concerned with is file management. Is there a preferred or recommended method for pushing a file to the Pyboard? The Pyboard will only ever read the file, never write.

I need a robust mechanism to transfer the file to the board without the risk of corruption whilst the code on the Pyboard is running. I envisage the host will be a RPi connected via USB. I would like to run 4 Pyboards from 1 RPi using this approach.

My sincere apologies if this is a trivial question!!

I would also like to thank the experts on here who have replied to so many threads and whose comments have helped me immensely to date as I have learnt how to start with uPy.

User avatar
dhylands
Posts: 2790
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, BC, Canada
Contact:

Re: Host pushing JSON config file to Pyboard

Post by dhylands » Tue Jul 03, 2018 1:53 am

One simple way would be to write a python script which writes the .json file and use pyboard.py to execute that.

Something along the lines of this:

Code: Select all

with open('/flash/foo.json', 'w') as f:
    f.write('''{
  "a": "field a",
  "b": "field b",
  "c": 42,
  "d": false
}''')

doublevee
Posts: 4
Joined: Mon Jul 02, 2018 11:09 pm

Re: Host pushing JSON config file to Pyboard

Post by doublevee » Tue Jul 03, 2018 6:11 am

Thanks Dave - that is very simple and neat.

I do have a use case where the Pyboard could also be used in isolation ie no connection to a host, once an initial .JSON file has been transferred. I think your approach would still work though as after a power cycle, the Pyboard would run main.py to process the last .JSON file in flash?

I also want the system to perform as fast as possible, as I may wish to send lots of .JSON files, maybe 10 per second. Would running a remote pyboard.py script have any performance issues?

Finally, what pyb.usb_mode() would you assume for the connection to the host? 4 Pyboards on one host would be my maximum.

chrismas9
Posts: 63
Joined: Wed Jun 25, 2014 10:07 am

Re: Host pushing JSON config file to Pyboard

Post by chrismas9 » Thu Jul 05, 2018 4:42 pm

If you are going to write files at high frequency (you say up to 10 per second) I strongly suggest using an SD card on the Pyboard. The STM32 flash does not have wear levelling. The largest SD card will have the longest life due to there between more sectors to share the writes.

There are warnings on the forum about mounting the Pyboard flash drive on the host causing drive corruption. This is due to upy and the host both writing to the drive, caching writes and simultaneously writing the FAT.

If the Pyboard script never writes to the SD card it should be safe for the host to mount the 4 Pyboard drives and write to them directly. Others may have different opinions based on their experience. Any comments?

Chris Mason

User avatar
dhylands
Posts: 2790
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, BC, Canada
Contact:

Re: Host pushing JSON config file to Pyboard

Post by dhylands » Thu Jul 05, 2018 5:26 pm

I wrote up a simple IPC mechanism which is based on JSON messages: https://github.com/dhylands/json-ipc

You may find that interesting to look at and use rather than using pyboard.py. It really depends on your application.

doublevee
Posts: 4
Joined: Mon Jul 02, 2018 11:09 pm

Re: Host pushing JSON config file to Pyboard

Post by doublevee » Sat Jul 07, 2018 5:11 am

Thank you so much guys - very appreciated. The IPC approach looks very interesting and I will follow that up.

I had done some reading on using an SD card, but this will add considerable expense to my solution so I was trying to avoid this route if at all possible. Thanks for the heads up on the built in flash wear levelling - can you buy SD cards as effectively ICs?

chrismas9
Posts: 63
Joined: Wed Jun 25, 2014 10:07 am

Re: Host pushing JSON config file to Pyboard

Post by chrismas9 » Sun Jul 08, 2018 1:59 am

Embedded flash chips with wear levelling are called eMMC memory. They are over $US10 at Digi-Key. Beagle bone uses eMMC. Alternatively you could use an SPI NAND flash chip such as Winbond W25N01GVZEIG $4 100+ at Digi-Key with a wear levelling file system. Damien is porting littlefs from mbed. See #3847.

Make sure you pick SLC flash (single layer cell) as MLC have lower endurance. The W25N01GVZEIG has 100,000 erase cycle endurance and by spreading writes evenly over 1Gb ( 128MB) should last a long time.

chrismas9
Posts: 63
Joined: Wed Jun 25, 2014 10:07 am

Re: Host pushing JSON config file to Pyboard

Post by chrismas9 » Sun Jul 08, 2018 4:32 am

On further reading of data sheet and littlefs DESIGN.md the 128 kB block erase size of the NAND flash may be a problem. I believe most of the negative comments apply to large scale flash with 300 - 10,000 cycle endurance. If you want to use SPI NOR flash use the largest affordable part and wear levelling ( littlefs or spiffs). For example with wear levelling an 8MB (64mb) flash should last at least 10 times longer than the on board MCU flash.

chrismas9
Posts: 63
Joined: Wed Jun 25, 2014 10:07 am

Re: Host pushing JSON config file to Pyboard

Post by chrismas9 » Sun Jul 08, 2018 7:17 am

You can get eMMC chips for under $5 but stock is limited. Search eMMC at octopart.com then search by price.

User avatar
pythoncoder
Posts: 2708
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Host pushing JSON config file to Pyboard

Post by pythoncoder » Sun Jul 08, 2018 7:40 am

Another option is ferroelectric RAM (FRAM) which has lifetime on the order of 10**14 writes. Nonvolatile and easy to interface as it's bit addressable with no erase cycle. However it's only economical if data volumes are small - say <= 32KB.
Peter Hinch

Post Reply