This is a prototype of a device wifi powered connected to a NodeMCU. I bought the baby tree at   https://www.leedens.com and this was used for this project. Picture: Alexandre Magno

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
fonte: Wikipedia

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.

There’s others released already and they are available at Amazon. One of those is the Aero Garden.

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?

If you are new in this IOT world, a warning: you can become addicted and you will wish to reinvent many devices that you see around. It’s more common buy components with different supports and languages like Python, LUA and Javascript. It’s easier to develop prototypes for new devices, but this new ways to develop bring new complexities in existent simple structures. With a lot of options and projects, we sometimes insist to recreate disaster projects from scratch, so it’s always good to have a good references before 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?

Arduino

NodeMCU ESP8266 with Wifi embeded which supports MicroPython and Lua to develop IOT projects. Picture: Alexandre Magno. Where to find: https://www.usinainfo.com.br/nodemcu/nodemcu-v3-esp8266-esp-12e-iot-com-wifi-4420.html

We need two components for this: the humidity moiusture sensor and the little NodeMCU, a powerful microcontroller with Wifi integrated.

Sensor

The sensor sends a tension to the soil to measure humidity. When the values increase it indicates that is dry

Advantages

  • 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

Disadvantages

  • 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:

  1. Intall the required drivers on mac, and then your operational system will recognize the MicroUSB connection:
    https://github.com/adrianmihalko/ch340g-ch34g-ch34x-mac-os-x-driver
  2. Download the firmware
    http://micropython.org/download#esp8266
  3. Install Esptool
  4. Erase the flash and reset the device
     esptool.py --port /dev/cu.wchusbserial1420 erase_flash
  5. 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
  6. 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)
 >>> adc.read()
 402

MicroPython code

[gist id=”52a6b0a059593ff8f0273eea649280a2″]

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.

See the project in Github

[gist id=”0b0264042c7aa93e2d9608affa692e65″]

Basically, is a NodeJS stack with Sendgrid, hosted on  Heroku to send e-mail about the values that the sensor reads. Our server will work as follows;

  1. A server receives the requests to an API rest that has endpoints that sends the sensor value about humidity
  2. Activates the requests to send e-mail, using the Heroku stack with Sendgrid to send e-mail, that could easily be any other integration.

Security

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.

Result

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
  • The connection speed of transfer should work at 115200 (On my device has 9600 specified to create the flash install)

Links

Conclusion

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

[mks_pullquote align=”left” width=”600″ size=”18″ bg_color=”#21759b” txt_color=”#ffffff”]

You can subscribe and know about opportunities to work in these projects and receive bounties for complete issues.


    [/mks_pullquote]

    [mks_separator style=”blank” height=”10″]

    Leave a Reply

    Your email address will not be published. Required fields are marked *