Capacitive Soil Moisture with LoRaWAN (English)

Somsak Lima
13 min readAug 11, 2020

--

Making a sensor node to measure soil moisture. To be able to send the measured data via LoRaWAN to TheThingsNetwork. We can view the Sensor data on TheThingsNetwork and forward it to Cayenne.mydevices.com. Sensor Data is displayed as a Dashboard in a graph where you can see comparative and historical data.

Equipment needed

  1. Hardware
  • ESP32 Development Board
  • OLED 0.96 inch 128X64
  • USB to TTL UART CP2102
  • LoRa module Maxiiot DL7612-AS923-TH with PCB Adapter. Orders at click. (Thailand Freq Version)
  • Capacitive Soil Moisture Sensor
  • Multi Channel LoRaWAN Gateway (Indoor)
  • Dupont wires

2. Software

  • Thonny can Download version 3.2.7 from click.
  • SSCOM V5.13.1 can download by this click.
  • Firmware MicroPython can be downloaded by clicking here.

You may choose to download a stable generic Firmware built with ESP-IDF v4.x, with support for BLE, but no LAN or PPP, such as esp32-idf4–20191220-v1.12.bin.

3. Library Language MicroPython

  • cayennelpp.py
  • font.py
  • Fssd1306.py
    Note: The original library that was included in Flash of MicroPython ESP32 was named ssd1306.py, but it could not display a large font, so I switched to this library because of the same name, so I changed this big font to the name Fssd1306. py instead.

Library or MicroPython program used in this article can be downloaded from Github.

4. LoRaWAN Multichannel Gateway

If the area where we are in does not have TheThingsNetwork LoRaWAN Gateway that is affiliated with TheThingsNetwork, we will need to set up a LoRaWAN Gateway by purchase and install it. LoRaWAN Gateway may be purchased as a Multi Channel Gateway, indoor or outdoor. Where an indoor Gateway in the building is often much cheaper. In Thailand, you should buy a LoRaWAN Gateway certified by NBTC, which will have a Garuda Sticker NBTC displayed on the gateway.

LoRaWAN Gateway generally, if using a 9 cm antenna and installed indoor on the 1st floor, data can be received from Sensor Node in a radius of 1–3 km around the Gateway.

Hardware connection

  • ESP32 Dev Board — — Capacitive Soil Moisture Sensor
3V3 - - VCC 
GND - - GND
P34 - - AOUT
  • ESP32 Dev Board — — OLED
GND - - GND 
3V3 - - VDD
P22 - - SCK
P21 - - SDA
  • ESP32 Dev Board — MAXIIOT DL7612-AS923-TH
3V3 - - 3.3V 
GND - - GND
P16 - - TX
P17 - - RX

Upload the firmware MicroPython.

Flashing firmware MicroPython into ESP32 Dev Board is done via MicroUSB Port on ESP32 board, use MicroUSB cable to plug into ESP32 and the other side to USB port of PC.

Open Thonny program, go to the Tool / Option menu.

Select MicroPython (ESP32) and Port Silicon Laps CP210x USB to UART Bridge.

Click Open the dialog for installing or upgrading MicroPython on your device.

Select Port again as Silicon Laps CP210x USB to UART Bridge, click browse field after Firmware, select your downloaded Firmware file .

Then click Install. The program will begin deleting the original firmware and installing a new one. If finished, click OK and close the window with Close and OK buttons.

Tip!

If Flash process cannot be done and you an error occurs, it may be that the ESP32 Dev Board does not enter Bootloader mode, press and hold the Boot button, press EN once, then release the Boot button while Thonny starts Flashing.

Or it may be modified by inserting a capacitor (no polarity) to cover the pin EN-GND using a 1–10UF capacitor.

Viewing or configuring the Maxiiot DL7612-AS923-TH module

1.Viewing Config Module Maxiiot DL7612-AS923-TH

There are two simple ways to do this: using a PC with a USB to Serial Port device, then using a program to read through Serial Port and use AT Command directly.

Before using the module DL7612-AS923-TH. It should be soldered onto the Adapter which converts the pin as needed, TX RX 3V3 GND as shown.

Connect the cables to USB to TTL as in the picture, use 4 dupont wires.

Use 3.3V power and be careful not to reverse polarity. If the polarity is reversed, the module can be damaged.

Connect the TX power of UART to RX on Adapter and RX of UART to TX on Adapter.

Then open a program that can read and write Serial Port. The program will allow you to use AT command directly. We recommend sscom V.5.13.1. Program sscom is done by a Chinese developer. So you can see some text in Chinese. First of all, click Change language to English.

The program will display the command menu in English.

Begin connecting to the ESP Dev board by selecting the correct ComNum and clicking OpenCom.

Write AT Command in the space below and click SEND . You can click EXT to open the AT Command menu that has been saved in .ini.

Example AT Command

AT + INFO

MAXIIOT 
DL7612
Revision: DL7612R01A74
Date: 2019--01--23

AT + NCONFIG See the Config value saved in the module.

+ NCONFIG: CLASS, C 
+ NCONFIG: ISMBAND, 6
+ NCONFIG: CHMASK, 00ff, 0000,0000,0000,0000,0000
+ NCONFIG: RXWIN2,923200000,2
+ NCONFIG: ACTIVATE, 1
+ NCONFIG: POWER, 0
+ NCONFIG. : PORT, 2
+ NCONFIG: ADR, 1
+ NCONFIG: DR, 2,10,0,0
+ NCONFIG: CFM, 1
+ NCONFIG: NNMI Enable, 1
+ NCONFIG: PNMI Enable, 1

AT + CHSET

+ CHSET ISM Band: 6 
+ CH0: 923200000,0,5
+ CH1: 923400000,0,5
+ CH2: 922200000,0,5
+ CH3: 922400000,0,5
+ CH4: 922600000,0,5
+ CH5: 922800000 , 0,5
+ CH6: 923000000,0,5
+ CH7: 922000000,0,5

Check the DevEUI, APPKEY, APPEUI values ​​set in the lora module. Write down the values ​​because you need these 3 values to register your Node Device with TTN.

AT + DEVEUI
AT + APPKEY
AT + APPEUI

Examples of values ​​obtained from all three commands

+ DevEUI: 24c5d9e632581195
+ APPKEY: 2b7e151628aed2a6abf7158809cf4f3d
+ APPEUI: 526973696e674848

Another method is to use a MicroPython program via the ESP32’s UART.

By connecting the power cord as in the picture.

And run the following MicroPython program

from machine import UART 
import time
from time import sleep
uart = UART (2, 115200, timeout = 20)def sendATcommand (ATcommand):
print (“{0} \ r \ n” .format (ATcommand))
uart.write (“{0} \ r \ n” .format (ATcommand))
print (uart.read (). decode ('utf-8'))
sendATcommand ('AT + INFO')
sendATcommand ("AT + DEVEUI")
sendATcommand ("AT + APPKEY")
sendATcommand ("AT + APPEUI")

Other frequently used AT commands available by click or read full version by click

2. Setting Config Maxiiot DL7612-AS923-TH Module

It can be run through Serial cable and enter edit mode with command.

AT + DEBUG = 1

And Save with the command

AT + SAVE

Or do it by Run the MicroPython program as follows to set the Config in the module.

This example program will configure the module to function as Node Class C, ISMBAND = 6, use AS1 frequency to communicate with LoRaWAN Gateway. Only RUN once, the values ​​will be saved in the module ready to use.

from machine import UART 
import time
from time import sleep
uart = UART (2, 115200, timeout = 20)def sendATcommand (ATcommand):
print (“{0} \ r \ n” .format (ATcommand))
uart.write (“{0} \ r \ n” .format (ATcommand))
print (uart.read (). decode ('utf-8'))

sendATcommand ('AT + INFO')
sendATcommand ('AT + NCONFIG')
sendATcommand ('AT + DEBUG = 1')
sendATcommand ('AT + ISMBAND = 6')
sendATcommand ('AT + CLASS = C ')
sendATcommand (' AT + SAVE ')
sendATcommand (' AT + CHSET ')
sendATcommand (' AT + NRB ')
sendATcommand (' AT + NCONFIG ')
sendATcommand ("AT + DEVEUI")
sendATcommand ("AT + APPKEY ”)

Adding Multi Channel gateway in TheThingsNetwork website

Please refer to the manual that came with your Multichannel gateway, set the frequency to match what you set in the LoRa module, which could be AS1 or AS2 if you are in Thailand.

If your gateway is working properly, it will show a green dot status and connected message.

Adding Sensor Node to measure soil moisture on TheThingsNetwork web.

What is TheThingsNetwork? First of all, I have to briefly conclude that TTN is the website we use to manage our LoRaWAN devices and data, which we can use for free. But there are some limitations, such as supporting only Node Class A, etc., or limiting the amount of Downlink packet per day (this is probably the policy of TTN, but not programmed Lock). TTN supports us to add both Node and Gateway as many as you want.

To read more about TTN, click here.

Apply and open an account at www.thethingsnetwork.org. After signing up, Login and go to Console.

There are options to manage your Applications or Gateway.

If you want to add a Node, click on Application first, there will be no Application at all, create an additional Application first by clicking on add application.

Application ID, choose any name, for example dl7612

Application EUI let a system to create for us. We will come and fix our further later. Once the Application has been created completely, the line dl7612 will be showed, click on the name of dl7612 to see the Application Overview.

Click on Manage EUIS after the right gear icon.

And enter AppEUI from the module you noted

Click the pencil image in front of the space you want to enter, after clicking Add EUI, there will be an Application EUIS line.

TAB Device shows Register Device is still 0 because you have not added anything yet.

Click Register Device on the right, where we will take the values ​​from the Node or our module to register with TTN here.

Deviceid use lowercase letters.

Device EUI, enter the value obtained from the command AT + DEVEUI.

Click on the pencil in front of the App Key and enter the values ​​obtained from the command AT + APPKEY.

Select the APP EUI that we entered earlier, if it is already there, then do not modify it.

Set the Payload Format of the Device.

Set the Payload Format to Cayenne LPP because the data we send from Node Device is in CanyenneLPP format.

Other CayenneLPP data format are available, click here.

Frame Counter Checks should be removed.

Should go into Setting by removing the check mark in front of Frame Counter Checks, which if checked, LoRaWAN Network Server will receive only sorted data. Counting numbers must be matched both sender and receiver. If our node power off and resumes, the counts will not match. Data will be rejected, this option increases the data security system to be more strict if checked. But this often confuses the user. If you check it, you will have to often Reset Frame Counter to 0 when Node starts sending a new 0th packet.

Once registered, you will see Overview as shown in the picture.

If the node has never sent data, it will see an orange dot and Status unseen on the Device Overview page. Run the MicroPython program as follows.

Program

Download MicroPython programs including libraries from Github.

import machine
import time, ubinascii
import utime as time
from machine import UART, Pin, ADC
from cayennelpp import CayenneLPP
from Fssd1306 import SSD1306_I2Cled = Pin(2, Pin.OUT)
relay1 = Pin(12, Pin.OUT)WaterValue = 1200 # WaterValue = 1680;
AirValue = 3035 # AirValue = 3620;i2c = machine.I2C(scl=machine.Pin(22), sda=machine.Pin(21))
oled = SSD1306_I2C(128, 64, i2c)
uart = UART(2, 115200, timeout=300)
pot = ADC(Pin(34))
pot.atten(ADC.ATTN_11DB) # Full range: 3.3vrstr = “”def sendATcommand(ATcommand):
print(“Command: {0}\r\n”.format(ATcommand))
uart.write(“{0}\r\n”.format(ATcommand))
rstr = uart.read().decode(“utf-8”)
print(rstr)
return rstrdef Oledhello():
oled.show_text(0, 0, “Hello”, 12)
oled.show_text(0, 20, “LoRaWAN Thailand”, 16)
oled.show_text(0, 35, “Start OTA Join”, 16)
oled.show()Oledhello()
sendATcommand(“AT”)
sendATcommand(“AT+INFO”)
sendATcommand(“AT+DEVEUI”
sendATcommand(“AT+APPEUI”)
sendATcommand(“AT+APPKEY”)
sendATcommand(“AT+NCONFIG”)
sendATcommand(“AT+CHSET”)
sendATcommand(“AT+NRB”)while rstr != “+CGATT:1”:
rstr = sendATcommand(“AT+CGATT”)
time.sleep(20.0)
print(“Respond String”)
print(rstr)
if rstr.startswith(“+CGATT:1”):
print(“++++OTAA OK+++++”)
break
print(“Retry OTAA Continue”)
print(“Join Success”)
oled.clear()
oled.show_text(0, 24, “Join Success”, 16)
oled.show()
time.sleep(5.0)count = 1
while True:
print(“\r\n\r\nPacket No #{}”.format(count))
pot_value = pot.read()percent = 100 — (100 * (pot_value — WaterValue) / (AirValue — WaterValue))
print(“ Soil Moisture (%):” + str(percent))
percentT = str(round(percent, 2))
print(“Round Soil Moisture to 2 digid(%):” + percentT)oled.clear()
oled.clear()
oled.show_text(0, 0, "#", 24)
oled.show_text(10, 0, str(count), 24)
oled.show_text(30, 24, percentT, 24)
oled.show_text(90, 24, "% ", 24)
oled.show()
c = CayenneLPP()
c.addAnalogInput(1, round(percent, 2))
b = ubinascii.hexlify(c.getBuffer())
print(“************ Sending Data Status **************”)
led.value(1)
ATresp = sendATcommand(
“AT+NMGS={0},{1}”.format(
int(len(b.decode(“utf-8”)) / 2),
(ubinascii.hexlify(c.getBuffer())).decode(“utf-8”)
)
)
print(“********Finish Sending & Receiving Data Status******”)
led.value(0)
count = count + 1
time.sleep(10.0)

Use Thonny program to help run MicroPython program, entering Tool / Option / Interpreter menu first and then selecting Board as MicroPython (ESP32) and correct USB Port first.

If it is connected properly, Promp is as follows (>>>) in the Shell window.

Upload a program to the ESP32 to test. If finish, rename it as main.py. Every time when the ESP32 reboots, it will call this program to run.

If the data from the Sensor Node that we created is correctly sent to TTN, Status will change to green dot.

And tell the time when the packet was last received, e.g. now, it was just received

If it shows 12 seconds ago, you received your last packet 12 seconds ago. You can switch from Overview to Data view by clicking in the top right corner.

You can see the Counter column, it is the sequence of sensor data. Analog_in_1 is the soil moisture value from the Sensor.

We can forward data from TTN to Cayenne MyDevices to display as Graphs on Dashboard or forwarding to Grafana. You can register as a new MyDevices user at Link.

Adding Integration Mydevices to the Device

In the Device configuration section there is a tab integration so we can pass data to other systems. Which we can choose from several systems for example, according to the picture, we chose to forward it to Mydevices.

Set any Process ID and Access Key to Devices.

Other integration options available include:

Example of use Cayenne.mydevices.com to display Dashboard.

Add our Sensor Node to mydevices.com easily by using DEV EUI alone.

Click Add New / Device Widget.

Expand Option LoRa, find TheThingsNetwork and CayenneLPP.

Then enter DevEUI and select “This device doesn’t move”. Then press Add Device.

You will see the screen

Start running Sensor Node and when the data is sent to mydevices. Dashboard will automatically change as follows.

The screen will have 3 more information on the Dashboard: SNR, RSSI, Analog_input (1), where SNR is the Packet Signal to Noise Ratio that the gateway receives. RSSI is the LoRa signal intensity that the gateway receives, If less negative, it indicates that the node and the gateway are not far apart. Analog_input (1) is the value obtained from the Capacitive Soil Moisture Sensor Node.

We can arrange the screen to make it more beautiful or change the appearance as we want.

Conclusion

This article shows an example of how data can be displayed on DashBoard with LoRaWAN Technology. This article is a basic introduction to make it easy for beginners by sending only one sensor data. Once user is familiar with LoRaWAN foundation, it is not difficult to study each subject in more detail.

--

--

Somsak Lima
Somsak Lima

Written by Somsak Lima

สนับสนุนและส่งเสริมให้ผู้สนใจสามารถใช้งานเทคโนโลยี LoRa และ LoRaWAN ได้ โดยนำความรู้ที่ได้ไปต่อยอดเพื่อใช้งาน

No responses yet