Asynchronous Temperature Sensing with Mongoose OS and Wemos D1
Checkout the code repo for a library to use the DS1820/MAX31820 in Mongoose OS asynchronously without using the Arduio compatibility mode.
Mongoose OS is an open source operating system for embedded devices using
ESP32, ESP8266, STM32, or TI CC3200 chips. Apps can either be written in
loop() or asynchronously. It includes the
Mongoose embedded web server as well as utilities to configure the WiFi
Wemos D1 mini
The Wemos D1 Mini is a $4 development board based on ESP8266. There are many other development boards that use the ESP8266 chip that will also work.
The MAX31820 is a 1-wire temperature sensor. There are a few versions of the sensor, any sensor compatible with the DallasTemperature library will work.
Mongoose OS has an implementation of the 1-wire protocol already included. However, it did not have a wrapper for the temperature sensors. The DallasTemperature library for Arduino (and compatible) give a rather nice API for interfacing with the sensors. However since it is written with Arduino in mind, it’s API uses polling to prevent blocking.
In Mongoose OS callbacks are used extensively to provide async features. In addition libraries are commonly written in C vs C++. A relatively simple port of the logic in the DallasTemperature library to C and callback based async temperature readings
The API for most functions will accept a
dallastemp struct which holds most
of the same information as the DallasTemperature class:
The implementation of the API is a relatively straight forward port of the Arduino logic, varyingly slightly to make using it in an async manner helpful:
The library also supports passing a callback function into the
dallastemp_temp_all functions. They return the
timer id from Mongoose OS’s
mgos_set_timer so the calling application
can manage the timer if needed. But by default it will call the supplied
callback function with the
rom identifier and the raw temperature reading.
dallastemp_temp_all will call the callback function once for every
dallastemp struct knows about. A complete example using the
dallastemp_temp callback api is:
With an all callback async approach, the embedded MCU is freed to perform other tasks, such as managing the WiFi stack or serving http content while waiting for the temperature sensors to preform the conversion.