1Programming the mcModule to check soil moisture
After working out the soil moisture variables (see YouTube video at the bottom) using the wireless debugging feature in mcStudio, we loaded the code onto the module and put into a planter. We programmed the module to check the soil moisture every 30 seconds as well as the temperature every minute and then send that information via MQTT to our broker, CloudMQTT.
The module is also programmed to check the temperature every minute and also relay that information via MQTT to our broker.
We also programmed the module to check its battery level every 2 hours and send a notification to a smartphone using IFTTT by setting up an applet to receive the information from the module and then alert on Android (using the IFTTT Android app) if the low-battery message was received.
*Note - we programmed to check the soil every 30 seconds, the battery voltage every 2 hours and temperature every 60 seconds for the purposes of this example but in real-life and for a long battery life, you could easily set the intervals to only a couple of times per day or mix and match your intervals with the different variables you are measuring to tailor the information to your needs
Define PinMode Pin0 As AnalogInput Alias MoistureLevel Define PinMode Pin6 As DigitalOutput Alias enableMoistureLevel Class RainSensor Shared Event measureMoisture() RaiseEvent Every 30 Seconds enableMoistureLevel = True 'turn on voltage divider Thread.Sleep(40000) 'sleep 40ms for voltage to stabilize Dim voltage As Short = MoistureLevel Dim payload As ListOfByte = New ListOfByte Dim payString As String = "" If voltage > 1500 Then payString = "Dry - I NEED WATER" ElseIf voltage <= 1499 And voltage >= 600 Then payString = "Fairly Moist" ElseIf voltage <= 599 And voltage >= 335 Then payString = "Moist" ElseIf voltage < 355 Then payString = "OVERWATERED" Else End If enableMoistureLevel = False 'turn off voltage divider payload.Add(payString) Lplan.Publish("mcThings/MoistureLevel", payload) End Event Shared Event sendMQTT() RaiseEvent Every 1 Minutes Dim payload As ListOfByte = New ListOfByte() Dim TempC As Float = TempSensor.GetTemp Dim TempString As String = TempC.ToString() payload.Add(TempString) Lplan.Publish("mcThings/GardenTemp", payload) End Event Shared Event measureVoltage() RaiseEvent Every 2 Hours Dim BattVolt As Short = Device.BatteryVoltage If BattVolt < 2200 Then Lplan.IFTTT("YOURIFTTTKEYHERE", "MoistureSensorBatt") Else End If End Event End Class
2Viewing the incoming information on Android
Using an Android MQTT App called IoT MQTT Dashboard we are able to see the incoming information from this device. There are many other great MQTT apps in the Google Play store that you should check out!
Note - We had other mcModules reporting variables into the app including a rain sensor device!
4Sending soil moisture information to other applications
Thanks for reading!
5Connecting the Moisture sensor to the mcModule
We acquired some inexpensive soil moisture sensors and then connected them to a couple of mcModules (Note - The modules shown below are mcMod110, end of life now, but this project is the exact same using the new mcMod120).
We started by attaching and sealing the moisture sensor into an enclosure. We made sure to make it a tight fit for the sensor and then sealed it nicely as it would be exposed to the elements. After the sensor was in in place, we then connected two flexible and short female jumpers:
We then soldered a couple of pins into the mcModule along with a 100k resistor for the voltage divider. You could easily connect the sensor directly to the module using male to female jumpers as well. We added pins to the module so that you can 'unplug' and remove the module from the enclosure if need be.
- Moisture Sensor: PIN 0 & GND
- Resistor: PIN 6 & PIN 0
Backside of the module. Watch the Youtube video for a closer and better view of the wiring
We then connected the sensor to the mcModule, added a battery and closed it up!
We suggest putting the UID of the module on the back of the enclosure for easy identification