Categories
Connected Farm

MySensors.org LoRa IOT for Farming, Part 1 the Gateway.

It’s been a while since I posted, last month was busy with getting the yard ready for cattle for the winter. I’ve a few ideas for the winter months, I’m going to work on building a range of sensors.

Some Idea’s I have for sensors are:

  • Temperature / Humidity Sensor for Polytunnel
  • Soil Moisture Sensor for Polytunnel
  • Electric Fence monitor (Line Voltage or EMF)
  • Weather Station
  • Security (Motion, Perimeter, Door, Vehicle etc.)
  • Water / Milk tank temperature
  • Water / Fuel tank monitoring.

I’ve already covered some of this in a previous post but I wanted to make this a complete series based on the MySensors LoRa platform only. I’ve also experimented with LoRaWAN technologies like The Things Network and will possibly look at Sigfox in the future.

I’m using the MakerHawk ESP32 LoRa Development Board SX1276 868 915MHZ Wifi Module IoT Board as the Gateway Device, It requires no extra hardware except for a micro USB power supply and some sort of enclosure.

Here is the sketch which is loaded by a USB Cable using the Arduino IDE.

#define MY_RADIO_RFM95
#define MY_DEBUG_VERBOSE_RFM95
#define MY_RFM95_RST_PIN 14
#define MY_RFM95_CS_PIN 18
#define MY_RFM95_IRQ_PIN 26
#define MY_RFM95_IRQ_NUM MY_RFM95_IRQ_PIN
#define MY_SOFT_SPI_MOSI_PIN 27
#define MY_SOFT_SPI_SCK_PIN 5
#define MY_RFM95_FREQUENCY (RFM95_868MHZ)
#define MY_TRANSPORT_STATE_TIMEOUT_MS  (3*1000ul)
#define RFM95_RETRY_TIMEOUT_MS  (3000ul) 
#define MY_RFM95_MODEM_CONFIGRUATION  RFM95_BW125CR48SF4096


#define MY_GATEWAY_ESP32

#define MY_WIFI_SSID "WiFi-SSID"
#define MY_WIFI_PASSWORD "WiFi-Password"

// Set the hostname for the WiFi Client. This is the hostname
// it will pass to the DHCP server if not static.
#define MY_HOSTNAME "ESP32_GW_LoRa"

// The port to keep open on node server mode
#define MY_PORT 5003

// How many clients should be able to connect to this gateway (default 1)
#define MY_GATEWAY_MAX_CLIENTS 2

#include <MySensors.h>
#include "heltec.h"


void setup()
{
	// Setup locally attached sensors
  Heltec.begin(true /*DisplayEnable Enable*/, false /*LoRa Disable*/, true /*Serial Enable*/);
  
  Heltec.display->setContrast(255);

 Heltec.display->clear();
 Heltec.display->setFont(ArialMT_Plain_16);
 Heltec.display->drawString(30, 10, "LoRa GW");
 Heltec.display->setFont(ArialMT_Plain_10);
 Heltec.display->drawString(30, 40, WiFi.localIP().toString());
 Heltec.display->display();

}

void presentation()
{
	// Present locally attached sensors here
}

void loop()
{
	// Send locally attached sensors data here
}Code language: PHP (php)

The sketch is from MySensors.org but modified slightly to suit the MakerHawk board including using its integrated screen to display IP info. The boards internal wiring between LoRa radio and ESP32 is different from MySensors.org so alternate pins were defined in the sketch. All required Library Files came from MySensors.org also.

I’m using this Gateway in combination with Home Assistant running on a Raspberry Pi 3 but it can be used with a multitude of other Automation Controllers also. I’ve included the home assistant configuration to be added to configuration.yaml file. I’ve 2 separate Gateways running, one uses nrf24l01 radios for short range indoor sensors and the other uses RFM95 LoRa Radios for long range outdoor sensors. See https://www.home-assistant.io/integrations/mysensors/ for more details.

mysensors:
  gateways:
    - device: '192.168.1.201'
      persistence_file: 'mysensors/mysensors3.json'
      tcp_port: 5003
    - device: '192.168.1.202'
      persistence_file: 'mysensors/mysensors4.json'
      tcp_port: 5003
  optimistic: false
  persistence: true
  retain: true
  version: '2.0'Code language: JavaScript (javascript)

Any Questions or suggestions please feel free to comment here or on the Facebook page.

Facebook Comments