Bosch BME280 driver

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
warren
Posts: 74
Joined: Tue Jul 12, 2016 5:47 pm

Re: Bosch BME280 driver

Post by warren » Thu Jul 21, 2016 4:52 pm

pythoncoder wrote:Assuming you've double checked that bme280.py on the ESP8266 is correct, I suspect you have run out of RAM. Are you running it from webrepl? It might be worth not running webrepl and using a direct connection. Executing the following after the error may be informative:

Code: Select all

import micropython
micropython.mem_info()
Thanks for that tip. I tried it with only the above code in main.py, it works.

I added code to make it report memory at key points:

Code: Select all

Memory BEFORE BME import: 22272
Memory AFTER BME import: 18624
BME values: ('24.72C', '1009.63hPa', '0.00%')
Memory now: 17360
Memory AFTER gc.collect(): 19008
(Obviously the humidity value - the last one - is wrong - I need to poke around the driver a bit).

Question: If instead of having bme280.py as a separate python script like this, I added it to the build folders and compiled it into the firmware, what difference would you expect it to make in terms of memory usage..?

Would this approach materially improve the ability to load several modules without running out of memory like this?

Unfortunately for me, my compiler toolchain is still broken, and it will be a hassle to keep recompiling modules into the full firmware bin ...

Could I compile the module concerned to bytecode:

Code: Select all

 'python -m py_compile_files bme280.py'  ==>  bme280.pyc...
Will micropython use bytecode.pyc files if it cannot find the py file?

That at least would be significantly faster for me to make and test changes - but would this make much difference to these shortage of memory errors?

(I am guessing that compiling it into the bin will make the greatest difference because that is in flash. The bytecode .pyc files would still take ram but would be maybe 60% of the original in size...)

Thoughts?

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

Re: Bosch BME280 driver

Post by pythoncoder » Thu Jul 21, 2016 7:29 pm

Based mainly on experience with the Pyboard, frozen bytecode works wonders in reducing RAM usage. To get round the problems building the ESP8266 toolchain I use the nightly toolchain builds from http://www.kaltpost.de/~wendlers/micropython/. On the ESP I freeze the stable code, ideally working on a single Python module to enable a fast turn round.
Peter Hinch
Index to my micropython libraries.

warren
Posts: 74
Joined: Tue Jul 12, 2016 5:47 pm

Re: Bosch BME280 driver

Post by warren » Thu Jul 21, 2016 8:33 pm

pythoncoder wrote:Based mainly on experience with the Pyboard, frozen bytecode works wonders in reducing RAM usage. To get round the problems building the ESP8266 toolchain I use the nightly toolchain builds from http://www.kaltpost.de/~wendlers/micropython/.
I tried flashing tonight's build, it seems faster and with less glitchy chartacters when it starts up. Thanks for pointing that out.

To be honest, I don't understand what you mean:
On the ESP I freeze the stable code, ideally working on a single Python module to enable a fast turn round
Is there somewhere in the docs where it explains this in greater detail? I sense that this is important for any like me who struggle with needing to use several modules in a RAM-constrained environment!

Many thanks

catdog2
Posts: 2
Joined: Sat May 21, 2016 11:17 pm

Re: Bosch BME280 driver

Post by catdog2 » Thu Jul 21, 2016 11:43 pm

Code: Select all

Memory BEFORE BME import: 22272
Memory AFTER BME import: 18624
BME values: ('24.72C', '1009.63hPa', '0.00%')
Memory now: 17360
Memory AFTER gc.collect(): 19008
(Obviously the humidity value - the last one - is wrong - I need to poke around the driver a bit).
You said that you have a BMP280 which does not report humidity, only the BME280 does.
Last edited by catdog2 on Fri Jul 22, 2016 8:41 am, edited 1 time in total.

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

Re: Bosch BME280 driver

Post by pythoncoder » Fri Jul 22, 2016 5:49 am

@warren Frozen bytecode has been around on the Pyboard for some time, but is new to the ESP8266. Just put the Python source modules in the build tree esp8266/modules, recompile the firmware and flash it. The makefile compiles the modules to bytecode and stores the result in flash memory along with the firmware.

Using the pre-built toolchain makes this process very straightforward.
Peter Hinch
Index to my micropython libraries.

warren
Posts: 74
Joined: Tue Jul 12, 2016 5:47 pm

Re: Bosch BME280 driver

Post by warren » Fri Jul 22, 2016 7:39 am

catdog2 wrote: You said that you have a BMP280 which does not report humidity, only the BME280 does.
Doh!

My mistake. I have now ordered a BME280 - thanks!

warren
Posts: 74
Joined: Tue Jul 12, 2016 5:47 pm

Re: Bosch BME280 driver

Post by warren » Fri Jul 22, 2016 8:13 am

pythoncoder wrote: Using the pre-built toolchain makes this process very straightforward.
Thanks for the extra info.

I do understand the issue of frozen bytecode and the potential benefits. It was the relevance of the link you sent ( http://www.kaltpost.de/~wendlers/micropython/ ) that I need to clarify.

Question 1: As I see it, that just contains someone's nightly builds of the firmware - how does this differ from the publically released firmware binaries ( https://micropython.org/download/#esp8266 )??

Question 2:
Regarding making bytecode versions of modules, you said:
Just put the Python source modules in the build tree esp8266/modules, recompile the firmware and flash it.
In order to do this, I had assumed that I had to install esp-open-sdk in order to compile my own firmware.
I had started to follow pfalcon's guide here: https://github.com/pfalcon/esp-open-sdk .

Are you saying there is a simpler way?

Sorry to ask what will possibly be a dumb questions, but one of the big issues facing newcomers to this project is that the sources of relevant information are spread out. The developers are clearly stretched with actually developing and fixing (which is good) - but it means that people like me have to do a lot of detective work to get the picture.

Many many thanks...

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

Re: Bosch BME280 driver

Post by pythoncoder » Fri Jul 22, 2016 8:27 am

warren wrote:... It was the relevance of the link you sent ( http://www.kaltpost.de/~wendlers/micropython/ ) that I need to clarify.... As I see it, that just contains someone's nightly builds of the firmware... Are you saying there is a simpler way?...
The link, in addition to a firmware build, has xtensa-lx106-elf_linux64_latest.tar.bz2. This is a nightly build of the toolchain and is worth its weight in gold as building the toolchain is a) nontrivial and b) a bit of a moving target.

Download the file, unzip it somewhere, and make sure your PATH includes the path to xtensa-lx106-elf/bin

You should then be able to build your firmware by following the instructions.
Peter Hinch
Index to my micropython libraries.

warren
Posts: 74
Joined: Tue Jul 12, 2016 5:47 pm

Re: Bosch BME280 driver

Post by warren » Fri Jul 22, 2016 9:20 am

pythoncoder wrote:You should then be able to build your firmware by following the instructions.
That is seriously helpful! Thanks so much.

I'll try it today..

warren
Posts: 74
Joined: Tue Jul 12, 2016 5:47 pm

Re: Bosch BME280 driver

Post by warren » Fri Jul 22, 2016 9:45 am

pythoncoder wrote:Download the file, unzip it somewhere, and make sure your PATH includes the path to xtensa-lx106-elf/bin
You should then be able to build your firmware by following the instructions.
I downloaded and unzipped it. Unfortunately no instructions are in the unzipped folder:

Code: Select all

drwx------ 2 voldermort voldermort   4096 Jul 21 18:42 bin
-rw------- 1 voldermort voldermort 726176 Jul 21 18:42 build.log.bz2
drwx------ 3 voldermort voldermort   4096 Jul 21 18:41 include
drwx------ 4 voldermort voldermort   4096 Jul 21 18:36 lib
drwx------ 3 voldermort voldermort   4096 Jul 21 18:36 libexec
drwx------ 4 voldermort voldermort   4096 Jul 21 18:42 share
drwx------ 6 voldermort voldermort   4096 Jul 21 18:22 xtensa-lx106-elf
There is a README in the github folder from whence I downloaded this. But the links there just take me back to the sdk install instructions which is where i am stalling previously.. I am sure I am being dumb, but it's just not obvious what to do next - is it me?

Post Reply