Garden environmental telemetry project – part 3

Garden environmental telemetry project – part 1 laid out plans for a simple maker / DIY IoT garden environmental telemetry system.

Earlier experiments highlighted the disadvantage of analogue sensor connections. This article reports tests on sensors connected using digital signals.

Above is the Dragino RS485-LN LoRaWAN end node, about $90 incl shipping. In this application it will be used to read two MODBUS sensors (each with two data channels) and forward the data to The Things Network via LoRaWAN, then after some processing, RESTFUL submission to Thingspeak.

Above is a switchboard enclosure used to host a variety of projects over time.

On the post is a SHT30 air temperature and humidity sensor with MODBUS interface.

Above is a view of the internals of the enclosure after a tidy up to remove expired experiments. The RS485-LN is upside down on the lower right, its antenna projects into the PVC conduit below the box.

Above is the cheap Chinese soil temperature and humidity sensor (~$30 inc shipping) with MODBUS digital connection.

Above is the soil temperature and humidity sensor in place.

The wires are terminated in 3m Scothlok UY2 connectors. UR connectors could be used to daisychain sensors… but they are a bit bigger.

Above, the terminations are pushed up into the conduit and the end wrapped in tape to keep bug out.

Above is a block diagram of the system.

Setup procedure

This is mostly about configuring off the shelf components.

Steps:

  1. Replace firmware with current AU915 firmware;
  2. Create new TTN v3 application;
  3. Add RS485-LN device;
  4. Verify that it connects;
  5. Test MODBUS soil sensor stand alone;
  6. Test MODBUS SHT30 air sensor stand alone;
  7. Change MODBUS SHT30 to 2 for use on shared bus;
  8. Configure RS485-LN for the above two MODBUS sensors;
  9. Check payload report from RS485-LN;
  10. Check payload received by TTN;
  11. Write and test payload formatter;
  12. Write and test Thingspeak webhook;
  13. Configure Thingspeak channel for the four data fields;
  14. Write and test Thingspeak webhook;
  15. Check data correctly received and plotted on Thingspeak.

Some of these steps are fairly straight forward and will not be detailed here, but some are less so and are described.

Replace firmware with current AU915 firmware

The RS485-LN user manual recommends a firmware programming client that is deprecated, and rather than install that, the replacement STMCUBEPROGRAMMER was used.

Dragino supplies a short cable to be used with a USB-RS232-TTL adapter. Above, the supplied 3way plug housing was replaced with a 6way one and the pins inserted to suit the pinout of the common Arduino USB-RS232-TTL adapters. This is one of the fake $3 adapters sold online, whilst they are fakes, they do work in this application.

The latest AU915 firmware was downloaded from Dragino.

Above is a screenshot of STMCUBEPROGRAMMER at completion of the programming

13:50:52 : STM32CubeProgrammer API v2.11.0 | Windows-64Bits 
13:54:17 : RTS low
13:54:17 : DTR low
13:54:17 : Serial Port COM18 is successfully opened.
13:54:17 : Port configuration: parity = even, baudrate = 115200, data-bit = 8,                     stop-bit = 1.0, flow-control = off
13:54:17 : Activating device: OK
13:54:17 : Board       : --
13:54:17 : Chip ID: 0x447 
13:54:17 : BootLoader protocol version: 3.1
13:54:19 : UPLOADING OPTION BYTES DATA ...
13:54:19 :   Bank          : 0x00
13:54:19 :   Address       : 0x1ff80000
13:54:19 :   Size          : 20 Bytes
13:54:20 : UPLOADING ...
13:54:20 :   Size          : 1024 Bytes
13:54:20 :   Address       : 0x8000000
13:54:20 : Read progress:
13:54:22 : Data read successfully
13:54:22 : Time elapsed during the read operation is: 00:00:01.434
13:54:37 : Read File: S:\Dragino\LN\AU915.hex
13:54:37 : Number of segments: 1
13:54:37 : segment[0]: address= 0x8000000, size= 0x150D4
13:54:44 : Memory Programming ...
13:54:44 : Opening and parsing file: AU915.hex
13:54:44 :   File          : AU915.hex
13:54:44 :   Size          : 84.21 KB 
13:54:44 :   Address       : 0x08000000 
13:54:44 : Erasing memory corresponding to segment 0:
13:54:44 : Erasing internal memory sectors [0 673]
13:54:47 : Download in Progress:
13:55:14 : File download complete
13:55:14 : Time elapsed during download operation: 00:00:29.447
13:55:22 : Disconnected from device.

Above is the log from STMCUBEPROGRAMMER.

Change MODBUS SHT30 to 2 for use on shared bus

The configuration command was found from the MODBUS SHT30 documentation. The required command string in hex is 01 06 07 d0 00 02 08 b5.

Configure RS485-LN for the above two MODBUS sensors

It is convenient to batch the configuration commands up, and in the absence of flow control, some method of pacing the commands is needed. A crude method is simply to force a delay after each line is sent. Teraterm can do this, Putty cannot (despite users asking for it for many years).

Above is the dialog found at Menu/Setup/SerialPort… note the msec/line transmit delay value.

123456
AT+DEBUG
AT+BAUDR=4800
AT+DATAUP=0
AT+PAYVER=1
AT+DATACUT1=0,0,0
AT+DATACUT2=0,0,0
AT+MBFUN=1
AT+COMMAND1=01 04 00 00 00 02,1
AT+COMMAND2=02 04 00 00 00 02,1
AT+CMDDL1=0
AT+CMDDL2=0

Above, the script sends the password (which if already unlocked will cause an error which can be ignored) and commands to configure sampling of the two sensors, and packaging of the results in the payload.

CMD1     = 01 04 00 00 00 02 71 cb
RETURN1  = 01 04 04 00 00 00 d8 fb de
CMD2     = 02 04 00 00 00 02 71 f8
RETURN2  = 02 04 04 02 4a 00 e2 68 a3
Payload  = 01 00 00 00 d8 02 4a 00 e2

The log can be captured in-situ with the adapter shown above and OTG cable, a mobile phone and suitable terminal emulator, an example follows.

08:15:45.594 
08:15:45.594 
DRAGINO RS485-LN Device
08:15:45.625 
Image Version: v1.3
08:15:45.641 
LoRaWan Stack: DR-LWS-005
08:15:45.673 
Frequency Band: AU915
08:15:45.689 
DevEui= ...
08:15:45.737 
08:15:45.737 JoinRequest NbTrials= 72
08:15:45.753 
08:15:45.753 
Please use AT+DEBUG to see debug info
08:15:45.801 
08:15:45.801 ***** UpLinkCounter= 0 *****
08:15:45.833 
TX on freq 916.200 MHz at DR 2
08:15:46.057 txDone
08:15:51.042 RX on freq 926.300 MHz at DR 10
08:15:51.073 rxTimeOut
08:15:52.064 RX on freq 923.300 MHz at DR 8
08:15:52.111 rxTimeOut
08:16:22.817 
08:16:22.817 ***** UpLinkCounter= 0 *****
08:16:22.848 
TX on freq 917.200 MHz at DR 2
08:16:23.184 txDone
08:16:28.185 RX on freq 924.500 MHz at DR 10
08:16:28.313 rxDone
08:16:28.617 Rssi= -53
08:16:28.617 JOINED
08:16:28.633 
08:16:28.633 Join Accept:
08:16:28.649 Rx1DrOffset:0
08:16:28.665 Rx2Datarate:8
08:16:28.680 ReceiveDelay1:5000 ms
08:16:28.712 ReceiveDelay2:6000 ms
08:16:30.550 
08:16:30.550 ***** UpLinkCounter= 0 *****
08:16:30.582 
TX on freq 918.200 MHz at DR 2
08:16:30.918 RX on freq 923.300 MHz at DR 8
08:16:30.949 txDone
08:16:35.919 RX on freq 927.500 MHz at DR 10
08:16:36.014 
08:16:36.014 Received: ADR Message
08:16:36.030 
08:16:36.030 rxDone
08:16:36.046 Rssi= -54
08:17:12.028 
08:17:12.028 ***** UpLinkCounter= 1 *****
08:17:12.060 
TX on freq 917.0 MHz at DR 4
08:17:12.156 RX on freq 923.300 MHz at DR 8
08:17:12.188 txDone
08:17:17.141 RX on freq 923.900 MHz at DR 12
08:17:17.173 
08:17:17.173 rxDone
08:17:17.190 Rssi= -58

Write and test payload formatter

The payload shown above contains a 8bit payload version number then four 16bit values for the four sensors. This is parsed by the TNN uplink formatter.

function decodeUplink(input) {
    return {
      data: {
        field3: ((input.bytes[7]<< 8)|input.bytes[8])/10,
        field4: ((input.bytes[5]<< 8)|input.bytes[6])/10,
        field5: ((input.bytes[3]<< 8)|input.bytes[4])/10,
        field6: ((input.bytes[1]<< 8)|input.bytes[2])/10
      },
    warnings: [], // optional
    errors: [] // optional (if set, the decoding failed)
    };
}

Above, is the Custom Javascript formatter which writes the measured values into variables fields3-field6 of the data object.

Configure Thingspeak channel for the four data fields

The four data fields were combined with inside temperature, humidity and barometric pressure from another project using a BME280 sensor.

Write and test Thingspeak webhook

This task is simply collecting the URL and access credentials for write access to the Thingspeak channel.

In-situ log capture

A mobile phone can be used to view and capture the serial log output of the RS485-LN to a file.

Final result

Below is about 24h of data capture.

The soil temperature and humidity plots are quite good, very little jitter and very suitable to the application.

A 5 day history is more informative for some parameters. The soil humidity plot shows moderately heavy rain at about 20:00 on 31/10.

To be continued…