Categories
Connected Farm

Remote 3G/4G MySensors LoRa Gateway V2 with IOT SIM.

The latest most stable version of my Remote 3G/4G MySensors Gateway. This version is much more stable thanks to the SIM7600G-H 4G HAT (B) for Raspberry Pi, this 4G hat is designed for 24/7 use unlike the HiLink USB dongle used in the previous version.

Component list:

The Build instructions will be very similar to the previous version except for the Modem and VPN setup. I now have 2 of these running on different out farms monitoring 2 different solar water pump setups. One is using the short range NRF24 radios and the other is using long range RFM95 LoRa radios.

The NRF24 or RFM95 radio is connected as per the build instructions on mysensors.org.

Raspberry Pi Zero W has same pin layout as Raspberry Pi 2/3/4

NRF24L01+ Radio

RaspberryNRF24L01+Color
GNDGNDBlack
3.3V*()**VCCRed
22CEOrange
24CSN/CSYellow
23SCKGreen
19COPI/MOSIBlue
21CIPO/MISOViolet
15*(\*)**IRQGray

RFM95 Radio

RaspberryRFM69Color
GNDGNDBlack
3.3VVCCRed
24NSSYellow
23SCKGreen
19COPI/MOSIBlue
21CIPOMISOViolet
22DI00Gray
ANAAntenna*
RSTNot used

* Solder a piece of wire, more info here

At the moment I have just one sensor connected to each of the MySensors gateways, they are liquid level monitors I had made previously and it is monitoring water available from my solar pumps.

The Raspberry Pi Zero, Modem Hat and Radio Module fit together quite neatly. For the Lora radio module I soldered it to a dual in line connector using tri track Vero board for easy connection.

The NRF24 module can be simply connected with Dupont jumper cables.

Raspberry Pi OS Install:

The OS install is the same as the in my previous posts.

First download the Raspberry Pi OS Lite from: https://www.raspberrypi.org/software/operating-systems/#raspberry-pi-os-32-bit and write to an SD card using imager software on your PC such as BalenaEtcher https://www.balena.io/etcher/.

Next remove the SD card from the PC and then re-insert, (don’t click on format if prompted by windows). To enable SSH create a new text file with notepad, in the new file put in one space and nothing more, call the file ssh and save and close it.

WiFi is needed as there is no LAN adapter on the Raspberry Pi Zero, so you need to create a second file called wpa_supplicant.conf and paste in the contents below adjusting for the country code, your WiFi name and password. If using Ethernet you can skip.

  country=IE
  ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
  update_config=1

  network={
      ssid="NETWORK-NAME"
      psk="NETWORK-PASSWORD"
  }

Windows may add .txt to the end of the file names and hide it, which will need to be removed. So click view at the top of the folder window and tick the box for file name extensions. Then right click on each file and rename without the .txt, ignore any warning about changing file name extensions.

With file name extension view on and the files renamed correctly they should look like below. The icons have changed from the Notepad icon to a blank page icon.

Next place both files in the root directory of the boot SD card. The SD card can now be removed from the PC and inserted into the Raspberry Pi. The Raspberry Pi can now be booted up for the first time, there is no need for a keyboard mouse or monitor as we will be using Putty to access the OS.

Install Putty from https://www.putty.org/ on your PC if you don’t already have it. You will now need the IP address of your Raspberry Pi, which you may be able to find through your router by checking for connected clients, but I like an app called Fing available as a phone app and on PC https://www.fing.com/, the free versions are more than enough.

Once you have Putty installed and the IP address entered click open. You should be prompted for a login, the username is pi and password is raspberry.

Next we run a script included with the OS to change some system settings:

sudo raspi-config

Go into System Options and set a new password and change the hostname to RemoteGateway. Next go to Advanced options and expand filesystem to make use of the entire SD card. Then Finish and reboot.

4G modem Configuration:

For the sim card I’ve gone with an IOT sim card from https://1nce.com/, the plan includes 500MB to be used over 10 years for €10 and can be topped up if necessary. The main advantage to an IOT sim over traditional data sims is the included VPN service.

To setup the modem type the following commands:

sudo -i
lsusb

If the 4G modem Hat is connected correctly then the output should be similar to:

Bus 001 Device 003: ID 1e0e:9001 Qualcomm / Option
Bus 001 Device 002: ID 1a40:0101 Terminus Technology Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Next create modem config script using Nano.

nano /bin/modemstart

Paste the following and save and exit Nano.

#!/bin/sh		
	killall ModemManager

	echo "setting operating mode online"
	qmicli -d /dev/cdc-wdm0 --dms-set-operating-mode='online'


	ip link set wwan0 down
	echo "wwan0 down"

	echo 'Y' | sudo tee /sys/class/net/wwan0/qmi/raw_ip
	ip link set wwan0 up
	echo "wwan0 up"

	echo "wds starting network"

# Update apn if necessary
	qmicli -p -d /dev/cdc-wdm0 --device-open-net='net-raw-ip|net-no-qos-header' --wds-start-network="apn='iot.1nce.net',ip-type=4" --client-no-release-cid

	udhcpc -i wwan0Code language: JavaScript (javascript)

Enable executable permissions.

chmod 755 /bin/modemstart

Update crontab to start modem on boot.

crontab -e

Paste the following and save

@reboot sudo /bin/modemstartCode language: CSS (css)

MySensors Gateway Configuration:

Enter the following commands one at a time:

git clone https://github.com/mysensors/MySensors.git --branch master

cd MySensors

If using the RFM95 LoRa Radios enter the following:


./configure --my-transport=rfm95 --my-rfm95-frequency=868 --my-gateway=ethernet --my-port=5003

If using the NRF24 radios:

./configure --my-transport=rf24 --my-rf24-channel=91 --my-gateway=ethernet --my-port=5003

To test run:

make
sudo ./bin/mysgw

The following message indicates that communication with the radio module failed:

mysgw: Starting gateway...
mysgw: Protocol version - 2.3.0-beta
mysgw: MCO:BGN:INIT GW,CP=RNNG---,VER=2.3.0-beta
mysgw: TSM:INIT
mysgw: TSF:WUR:MS=0
mysgw: !TSM:INIT:TSP FAIL
mysgw: TSM:FAIL:CNT=1
mysgw: TSM:FAIL:PDTCode language: HTTP (http)

A success message would be:

Jun 21 06:36:58 INFO  Starting gateway...
Jun 21 06:36:58 INFO  Protocol version - 2.3.0
Jun 21 06:36:58 DEBUG MCO:BGN:INIT GW,CP=RPNGL---,VER=2.3.0
Jun 21 06:36:58 DEBUG TSF:LRT:OK
Jun 21 06:36:58 DEBUG TSM:INIT
Jun 21 06:36:58 DEBUG TSF:WUR:MS=0
Jun 21 06:36:58 DEBUG TSM:INIT:TSP OK
Jun 21 06:36:58 DEBUG TSM:INIT:GW MODE
Jun 21 06:36:58 DEBUG TSM:READY:ID=0,PAR=0,DIS=0
Jun 21 06:36:58 DEBUG MCO:REG:NOT NEEDED
Jun 21 06:36:58 DEBUG Listening for connections on 0.0.0.0:5003
Jun 21 06:36:58 DEBUG MCO:BGN:STP
Jun 21 06:36:58 DEBUG MCO:BGN:INIT OK,TSP=1

To install:


sudo make install

To start service automatically when the Raspberry boots:

sudo systemctl enable mysgw.serviceCode language: CSS (css)

Now either reboot the Pi, or run this command to start the gateway:

sudo systemctl start mysgw.service

For more information and troubleshooting see https://www.mysensors.org/build/raspberry

Setup Home Assistant VPN:

To install the VPN service on the device running Home Assistant you will need access to the host OS so I’d recommend one of the non standard HA builds like in my previous post Home Assistant for Farming, Install Guide. This time though instead of using WireGuard, I’ll be using OpenVPN as 1NCE have provided the configuration files and the following instructions.

Setting up OpenVPN on Linux/macOS
  1. Install OpenVPN software (e.g. “apt-get install openvpn”)
  2. Download client.conf and store it on your server in /etc/openvpn
    Download client.conf
  3. Download credentials.txt and store it on your server in /etc/openvpn
    Download credentials.txt
  4. Start openvpn service (e.g. “service openvpn start”) and monitor connection in /var/log/syslog

1NCE only allow 1 VPN connection for a single server, but that is fine once you configure Home Assistant it will be able to see all of your registered IOT sims with no further VPN configuration required. You could still use another VPN like WireGuard and I have tried it, but you will use considerably more of your data allowance keeping the connection alive, but the provided OpenVPN solution dose not cost towards your data allowance. However 1NCE only provide service to business customers and do ask for proof.

Setup Home Assistant MySensors integration:

Log into your Home Assistant via its web browser address, then go to configuration and select integrations then click the add integration button on the bottom right.

Fill in MySensors version and IP address.

If you haven’t taken control of your layout on the overview screen then any sensors picked up by the gateway should start showing up automatically. If you have taken control of the layout then you need to add them manually. First click the three dots on top right hand corner and choose edit dashboard, next click add card on bottom right then add an entity or gauge card. In the entity field you can scroll through all available entities or search using the name of sensor and address set in its sketch during programming with the Arduino IDE.

Any questions on this or any other post please feel free to ask in the comments section.

Facebook Comments