Reducing wastes is important to minimize the energy footprint from human activities. Compost is a great way to reduce organic waste since it shrinks wastes in controlled conditions by the action of bacteria, micro-organisms and macro-organisms. These creatures decompose organic materials through oxygen in order to create heat. ♨️
One of the key factors to make good compost is monitoring regularly the internal conditions such as temperature to calculate the decomposition rate. The temperature also helps to predict when the pile needs to be active to introduce more oxygen. More information about the effects of the temperature can be found here.
Monitor process is not always a simple task since the confined conditions of the composts reduce accessibility. Besides that, the operation of opening the compost box affects internal conditions as well.
Therefore, I decided to use Internet of Things (IoT) to monitor temperature of a compost. My idea is to build an IoT device to retrieve compost conditions in a regular basis and send them to the cloud in order to easily monitor them via a web page.
Existing solutions are focused mainly on locally recording temperatures, and don’t provide transmission of data to the cloud. Then, my solution provides a low-power consumption autonomous device, which sends information regularly to the cloud in order to monitor temperature in real-time.
SolutionMy project is based on the following guidelines:
- a low-cost solution adaptable by anyone in the world ;
- low-power consumption solution based on built-in batteries for long discharge periods of time ;
- no WiFi access for data transmission is required.
Following these conditions, I chose the ESP8266 microcontroller. It’s a very popular IoT device, which costs me 2.69€ and it has a deep sleep mode that allows a reduced power consumption when the device is idle. In addition, this device can easily interact with a BRKWS01 module in order to send messages using Sigfox LPWAN protocol. Consequently, this solves my last requirement because it removes the need to have the device connected to a fix internet access point in order to transmit the messages.
Project planningThe ESP8266 has 30 pins. The detail of the function of each pin can be found here. I use the following pins:
- The GPIO16 and RST to wake up the device from idle ;
- The GPIO5 to control temperature from the DS18B20 ;
- The GPIO1 and GPIO3 to communicate with the BRKWS01 device.
Notice that pins GPIO1 and GPIO3 are used for serial communication with the USB cable and so with your PC, so serial connections like rshell
or picocom
won’t work. So I have found another way to communicate with the device from my PC.
The following ESP8266 ports can be used for serial communication:
- GPIO1 and GPIO3 for transmission and reception ;
- GPIO2 for transmission only ;
- GPIO13 and GPIO15 for transmission and reception.
MicroPython provides a UART class which implements the UART protocol for serial communication. This class is initialized using only ids 0 or 1. UART0 corresponds to ports to the serial communication of ports GPIO1 and GPIO3, while UART1 corresponds to the connection of pin GPIO2. It is not possible to initialize it to use pins GPIO13 and GPIO15. For this reason, I was forced to use GPIO1 and GPIO3 (UART0) to communicate with the BRKWS01 device.
Luckily MicroPython provides WebREPL, which relies only on web socket connection, so that I could use UART0 for data transmission and still have access to the REPL console for debugging purposes.
Hardware buildingI started by soldering the pins of the BRKWS01 device. I only used the power supply, and the transmission and reception pins. Then, I soldered the edge of the device that contains those pins, as show in the photo below.
The DS18B20 probe must be soldered to the device because I chose the version that comes with cables, which cannot be attached easily to the breadboard.
I put the components in the breadboard and connected them using wire cables following the Frizing schematic, which was presented before.
I connected a charger via a USB port to the ESP8266 and embedded them in a plastic bottle in order to keep safe from harsh conditions of a compost box. The only thing set as output is the DS18B20 probe to measure the temperature correctly.
Every fifteen minutes, the ESP8266 measures temperature and sends information through the Sigfox network. When the information reaches Sigfox cloud, a callback is executed, which is configured to send information to a Nodejs application. This application saves the information in a Posgresql database and queries the temperature evolution in a dashboard.
Micropython provides access to most of the python 3.5 directives, which allows to any programmer to write a high-level Python code to program this microcontroller. Rather than Arduino, I used Micropython for the following reasons:
- It has an Interactive REPL (read-evaluate-print loop). This allows you to connect to a board and execute the code without any need for compiling and flashing. It is suitable for python debugging as show in the image below.
- Extensive software library: Like the ordinary Python programming language, MicroPython has standard built-in library for decoding/encoding strings, parsing JSON files, searching text using regular extension or through network sockets.
- Extensibility: For advanced users, MicroPython is extensible with low-level C/C++ functions. Then, you can mix expressive high-level MicroPython code with faster low-level code when required.
In order to use Micropython you need to configure your board first. The configuration is done through serial connection, so remember to disconnect temporally the cables from GPIO1 and GPIO3, as explained in the project planing section. Then, proceed with the following commands:
- Erase flash memory of the ESP8266, considering that it's available in the ttyUSB0 port:
esptool.py --port /dev/ttyUSB0 erase_flash
- Flash Micropython binaries, which can be found here:
esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect -fm dio 0 esp8266-20190928-v1.11-361-g4ba0aff47.binrshell
Then, you can access the serial shell using the following command:
rshell -p /dev/ttyUSB0
rshell
allows you to access the REPL, copy and/or edit files in the micro controller. More information about rshell
can be found here.
The webREPL can be enabled using the following command in the REPL:
>>>import webrepl_setup
This commands will help you configure the webREPL. Then you can use a code like the one show here to connect your ESP8266 to your WiFi network. Finally enter the IP address of your device in the webREPL to connect to the device.
Send information using Sigfox BRKWS01 device and MicropythonFirst of all, you have to configure your BRKWS01 with the Sigfox subscription. This step has been already been documented, as shown here. After that, you will be able to send messages to your BRKWS01 device, and see them in your Sigfox development account.
My main contribution was the code itself, that enables the communication between the ESP8266 and the BRKWS01. I will explain the Python class used to send the messages, but you can find the entire code in the github page of the project.
The Sigfox class contains the following methods:
test_connection:
sends an ‘AT’ message to the BRKWS01 and waits for a successful ‘OK’ response.send_message:
sends a message using Sigfox network and waits for a ‘OK’ response. Messages are sent using hexadecimal encoding, in order to optimize message size, I removed the dot punctuation in the message.wait_for
: waits for a response after sending a message. This function is used in the previous methods.
class Sigfox(object):
def __init__(self):
uos.dupterm(None, 1)
self.uart = UART(0, 9600)
self.uart.init(9600, bits=8, parity=None, stop=1)
def wait_for(self, success, failure, timeout):
"""Wait for a response
Args:
success: success message
failure: failure message
timeout: timeout in seconds
"""
iter_count = timeout / 0.1
success_h = ubinascii.hexlify(success).decode('utf-8')
failure_h = ubinascii.hexlify(failure).decode('utf-8')
message = ""
while (iter_count >= 0 and
success_h not in message and
failure_h not in message):
if self.uart.any() > 0:
c = self.uart.read()
hex_str = ubinascii.hexlify(c).decode("utf-8")
message += hex_str
iter_count -= 1
sleep(0.1)
if success_h in message:
return message
elif failure_h in message:
print("Failure ({})".format(message.replace('\r\n', '')))
else:
print("Received timeout ({})".format(message.replace('\r\n', '')))
return ''
def test_connection(self, timeout=3):
print('testing connection')
self.uart.write("AT\r")
if self.wait_for('OK', 'ERROR', timeout):
return 'connection ok'
else:
return ''
def send_message(self, message):
res = ''
while not res:
res = self.test_connection(timeout=1)
print('connection ok, sending message')
self.uart.write("AT$SF={}\r".format(message))
if self.wait_for('OK', 'ERROR', 15):
print('Message send')
BackendThe backend of my application uses a simple express
nodejs library to build a web application with the following endpoints:
/data
: to show raw data in a json format ;/insert
: to receive data coming from Sigfox callback and save it in the posgresql database ;/
: to serve the static html used for the frontend of the application.
The entire code is available in the github of the project.
Please notice that you have to configure the callback in the Sigfox account to transmit data to the Heroku application as follows:
https://temp-sigfox.herokuapp.com/insert?token=secret_token&seconds={time}&data={data}
The token parameters allow to secure the communication between the Sigfox callback and the nodejs application. So, you need to have the right token if you want to insert data.
FrontendThe advantage of using a custom application is that alerts can be sent if the temperature goes up above a threshold. In addition, It is also possible to make simple static analysis to predict when the temperature will raise above a certain level, and program compost interventions.
The frontend of my application contains a simple html with a line chart, created with chart.js.
I have deployed my application in Heroku (the link to the dashboard can be found here) because it’s free of charge, and also because I think it’s very simple to employ. I have just to create a Procfile
to indicate Heroku what command to execute. Then, by pushing the code to Github or Heroku git, the deployment is online automatically.
Recommended temperature range for the compost varies between 32°C and 60°C. Above 70°C, most microorganism cannot survive. The compost pile should be turned around when the temperature is above 60°C to provide oxygen and release excess heat. In each pile turn, the temperature decreases and rises depending on a given number of cycles. When the pile fails to reheat, the compost is ready to cure.
During my tests, I put the device outside the compost and insert the temperature sensor inside the compost pile. The plastic bottle serves as protection for environmental conditions. The device continues sending messages even in remote conditions. My results prove my solution works, while there is a lot of room for improvements.
Conclusions
I'm globally satisfied with my project 🎉because:
- My device has low-power consumption. For my case, I used a regular 1000mAh battery to power on the device. During my tests I measured temperature every 15 minutes and I obtained the following results: ~20 hours when the device is always on and 5 days with deep sleep mode between measurements.
- Sigfox LPWAN protocol allows to take measurements in remote places, which are not always close to an internet connection.
- Micropython is a very popular solution to write software for small microcontrollers. Even if it may not be as fast or light as a compiled language such as C or C++, it has a simple and accessible syntax which facilitates development, debugging, and maintenance.
- The materials used in this project are very cheap. I spent 7.72€ in the ESP8266, the breadboard, the cables, the waterproof temperature sensor, and the USB battery. Even if the BRKWS01 device is the most expensive part in the project, it is still cheaper than a device like the SyPy, when it is used in a combination of the ESP8266 + BRKWS01.
This project contributes to the Sustainable Development Goal 12, for a responsible consumption and production. Composting is a clean way to reduce organic wastes, this kind of project increases the interest of people about doing this activity and also allows to do it in a very efficient way.
Perspectives
In different conditions, I would use a different battery, because batteries based on USB ports consume a lot of energy due to their brights leds, which indicate when they are on.
There is still room to ameliorate the web interface by adding more functionalities such as sending email alerts, when temperatures reaches a certain value.
There has been scientific publications about this kind of applications, which prove that there is a rising interest in the field.
Comments