Developing a IOT device: humidity sensor for plants using Arduino
This is the first of a collection of posts that will share along with the experiences to develop project with the Internet of Things (IOT), using different methods and modules in many applications, testing and exploring ways to take the advantage of the different approaches, with real cases to bring this technologies for our every day lives.
What about the real project?
I will start by apply IOT in an area that I like it a lot, that was a thesis in my unfinished study for a master degree in informatics. I will talk about internet of things for little indoor garden measure with sensors.
Why this project is internet of things?
The Internet of things (IoT) is the network of devices, vehicles, and home appliances that contain electronics, software, actuators, and connectivity which allows these things to connect, interact and exchange data
This is a project that I’ve been looking for a long times, with a lot of tries to end with a good quality measure for soil moisture for my plants. But there’s a lot of projects around in different ways that take care of that. Is perfectly fine to look other ones like the Intructables. There’s some consolidated projects on the market:
There’s a lot of projects that taked advantage from crowdfunding platform to try to put these IOT projects for indoor gardens.
So we can see there’s a lot of different designs and and interfaces and the marketing around these new Gadgets, but a lot of these example of IOT devices on the market is not something that really add for us some value in a long term.
Considering this, each IOT project that you need to do needs, as any other software project, a really good design, usability, security and accessibility to be useful for your target users. So it’s really important to prototype and test to build something that will be sustainable.
When we think about automated indoor garden, there’s a lot of variables involved, and when we consider the biologic world as our context, is really essencial to make experiments for a while in different situations, and this case I will focus in this post about sensors and I will show different possibilities around the use in a practical world to validate this project.
I will use a Wifi connection to connect to a server and send data about the humidity to send to an e-mail (I could tweet as well) in a 1 hour interval. Then we could use in different situations. So it’s a great start to feel how the sensors behave in a real environment.
Why develop projects with IOT? What I need to know to start?
Another important consideration about the IOT prototypes that you decided to build, is before proceed by solding components, is think about how to build in large scale and for this there’s a lot of variants to be considered, like the price of the components and their use in real world, because not everything will work as we expected. So is important to test the prototype in different situations and by real. I think is the same with software development, but when we are dealing with hardware, there’s new dimensions and variables to be considered, and the software is embedded to a hardware, so this can change everything.
How to develop an IOT project
I will use this project as example to show a possible architecture and you use as reference to create a project in the same field as well in other contexts.
Which components I will use?
We need two components for this: the humidity moiusture sensor and the little NodeMCU, a powerful microcontroller with Wifi integrated.
The sensor sends a tension to the soil to measure humidity. When the values increase it indicates that is dry
- You will not need to develop with the Arduino language, and you can develop using Lua or a Python variation called MicroPython
- MicroUSB as source
- For some reason, is not supported in a 5.4GHz network
How to connect the components
Schematic view between an Arduino NODEMCU and a sensor
Parte 1: Developing and embedded software with MicroPython
I decided to use this microcontroller with Python, and there’s a little initial effort to setup. We have to change the firmware, doing a proper download and set the initial system from scratch, that is different that counts with the embedded Lua platform. But once you upload the firmware you can upload files and run on the device and access the libraries that is possible to handle the Wifi connection.
The required steps to setup the firmware is not required if you wish to develop using Lua, that is the embeded system in this model of Arduinos, the ESP8266, that is the included firmware from factory.
If you want to try to build system upon MicroPython and customize your device, you need to follow these steps:
- Intall the required drivers on mac, and then your operational system will recognize the MicroUSB connection:
- Download the firmware
- Install Esptool
- Erase the flash and reset the device
- esptool.py — port /dev/cu.wchusbserial1420 erase_flash
- Upload a new firmware
- esptool.py — port /dev/cu.wchusbserial1420 — baud 460800 write_flash — flash_size=detect 0 ~/Downloads/esp8266–20180511-v1.9.4.bin
- Connect the device
- picocom /dev/cu.wchusbserial1420 -b115200
Connecting the sensor
You can read now the sensor values by doing:
from machine import ADC
>>> adc = ADC(0)
Part 2: Developing the server that will receive the requests
We need a server that will receive the requests, so for this purpose I created an Open Source project called Ahorta, that will receive the data send by the sensor through the wifi connected.
- A server receives the requests to an API rest that has endpoints that sends the sensor value about humidity
- Activates the requests to send e-mail, using the Heroku stack with Sendgrid to send e-mail, that could easily be any other integration.
For security reasons, we need to create a token that needs to be present on the request, and in our example we had a environment variable that holds the key, that should match with the key on the server, that is set with the environment variable as well. This token will be stored on the device, source of request, that validates to not handle any other unknown request.
As a result of this scripts and the Node on the server, we send a e-mail hour by hour to the destination e-mail.
Important to know
- The range of support of this microcontroller is 2.4ghz network, and lucky me that I have both frequencies in my home network
- Use the best cables you have, cause I had a lot of issues with malfunction cables that it was hard to find the source
- A velocidade de conexão deve funcionar bem na faixa de 115200 (No meu tem escrito no chip as instruções, dizendo que na criação do flash usar a faixa de 9600)
- The connection speed of transfer should work at 115200 (On my device has 9600 specified to create the flash install)
I hope this post was helpful to understand real world IOT projects.
With these devices we can do great projects with different architectures for home automation.
Are you interested to contribute with this project? Follow this project on Github