B1i ECMA-Adapter Cookbook (feat. BME280)
Since a while, B1i has released its ECMA-Adapter (available on all hardware platforms) in order to speed up development for concrete adapters or – in the IoT world – sensor interfacing applications. But what is the fastest way in order to come up-to-speed?
This small ECMA-Adapter Cookbook blog (it also could be framed as “assisted cribbing guide”) shall show up a promising path towards fast success. The groundwork for all was laid in the former posts
“B1i GY302-Adapter, Mk I: The Basics” and “B1i GY302-Adapter, Mk II: H[e]a[n]ds on!”
As a concrete example, an implementation for a BME280 humidity-, temperature- and barometric pressure-sensor is supplied that shows up how such a project concretely can look like.
The BME280 is a breakout-board around the same named Bosch-sensor for environmental values (temperature, humidity, barometric pressure), suitable e.g. for usage in the RasPi or Arduino environment. It exposes its functionality via the I2C-Bus protocol.
There comes up the general problem about how to quickly get up-to-speed about using e.g. sensors / making adapters for them in B1i:
The very hard way is scratch-building by reading the datasheet and crafting the necessary code, as e.g. for the BME280.
As you can see (also referring to the attached C-code), this isn’t an easy way at all….
The pre-built alternative facts
So in general for sensor / adapter development in B1i, it would be worth to figure out what already is there in terms of coding:
- If you develop in Arduino, you search in the community (the mainstream; you’d always find multiple options), as e.g. this article.
- If you develop explicitly for Atmel micro-controllers (as in our demo machines), you also search the Arduino community, realizing that Arduino libraries are nothing else than C++ libraries exposing objects and carefully hiding the details to the audience.
So, you transpose the C++ code to C.
- If you’re on the Raspi and Python, you’re as well mainstream and can search the community (also finding multiple options)
- If you’re on B1i, you’re on Java and … pretty out of luck from the first view, as Java on RasPi is not that mainstream as e.g. Python is!
In B1i on the XxxPi, the B1i-exposed Java library PI4J exposes the hardware that in turn relies on the C-API wiringPI that b.t.w. also is the low level foundation for Python there. But that’s the end of the official paved road here…
But the situation is not that poor as it looks like:
But what has a huge community and eco-system (and also has its occurrence on the RasPi) is the node.js framework: There do exist countless (and also sometimes hardware-specific) modules / libraries for sensors and whatever else.
So the very conclusion is:
How to get node.js modules working for the ECMA-Adapter
The basic challenge in doing so is that node.js has another environment / basics than B1i. That concretely means:
- Find an implementation / make a small glue layer for the low level technical basics:
The technical connectivity of sensors always is similar: Using GPIO, I2C, SPI, USB, 1-Wire, RS232, RS485.
So in most cases, it will be necessary to once supply this connectivity emulating in a suitable way a particular node.js module:
Because these higher level modules (as e.g. the following used one for the BME280) typically also rely on such lower level transport protocol modules.
The emulation of such a lower level transport protocol module should not be that hard as they typically are very small and have simple functionality. Once done, there is a big chance for re-use for future projects.
- Potentially retro-fit ECMAScript 6 or better syntax to ECMAScript 5:
node.js is one of the parties that have all of the latest ECMAScript features implemented very fast. On the other hand side, Nashorn implementing V5 always has some feature lag. So if you e.g. face a ‘class’ in your source, there needs to be done a conversion to a form implementing this in another way.
The good message is: There are no showstopper features entirely impossible to do in another way in older ECMAScript versions.
As the last rescue, Java can come to help (what is missing in the original node.js).
- Design a suitable adapter specific XML <-> JSON data passing and potential adapter parameterization. This is a specific (but not rocket-science) task to do.
The supplied demo implementation
This is the i2c ‘class’ that emulates the used node.js i2c module:
This is the adopted high level BME280 module from node.js, using the low level I2C module:
Finally, this is the B1i specific adapter module that needs to do the data-passing and to retrieve the I2C facilities:
In order to get them, a tiny API part of the B1i PI4J exposure (Pi4JResourceDispenser) needs to be used for a usage it in a concurrently compliant way (strange that all such sensor coding thinks that it is the single one and only thing performing on the machine).
In fact, the concurrency compliant execution is accomplished by embedding the whole I2C activities within a before-after pattern supplied by B1i for the I2C bus.
These 3 files should be supplied in the given order as a script-group to the ECMA-Adapter, as the order reflects the established inter-dependencies / stacking.
Finally, if you want to try out this example, find above the wiring diagram for attaching the sensor to the Raspi.
The BME280 is an example for a more complex sensor from the usage point of view as some pos-computation is necessary. For most other sensors, coding is much easier.
Leveraging the node.js community + code is a promising general strategy in order to quickly come up-to-speed. It’s not about to prove to be able to develop the code for such a sensor, but about how quickly it is possible to get reasonable results.
(This blog-post originally appeared on my Da Vinci Space Blog: https://davincispace.wordpress.com/2019/03/08/b1i-ecma-adapter-cookbook-feat-bme280/)