Publishing MQTT messages from a NodeMCU – Soil Moisture Sensor

In this article I’d like to share the experience of building a soil moisture sensor built on NodeMCU which publishes MQTT messages (payload) to a cloud service.

This article is also a soft launch of a preview of cloud.iotalot.com service. We use it for our DIY projects to register json payload and visualise it is a very simple way. This allows us to “debug” our sensors. If you wish to get access to cloud.iotalot.com let us know by sending an email to admin@iotalot.com. We will provide you with the user account and instruct on how to start your project.DISCLAIMER: We are not responsible for any data privacy or availability of the service at the moment.

Now back to the “experience” …

The set up

The sensor is mounted on the flower pot

Every 10 minutes is sends MQTT payload to cloud.iotalot.com

I can check the sensor data records using the telegram bot

This is how the components look like

The code

I assume you have a nodeMCU with the firmware burnt like explained in the post earlier. The firmware version that is used in this example is the following: nodemcu_float_0.9.6-dev_20150704.bin

This is the code for init.lua and moisturesensor.lua . As a best practice, I don’t put the main code to int.lua file so that I can play with the NodeMCU module when the main code has some loops or timers. Before going to “production” I just put dofile("moisture sensor.lua") to init.lua to make sure the main code runs when the device is powered.