---
title: "My LORA remote relay system?"
url: "https://maker.wiznet.io/teju/projects/My-LORA-remote-relay-system/"
markdown_url: "https://maker.wiznet.io/teju/projects/My-LORA-remote-relay-system/md"
type: "UCC: User Created Content"
author: "teju"
original_author: "teju"
original_url: "https://sites.google.com/site/marcobotprojects/my-projects/my-lora-remote-relay-system"
published: "2021-06-17"
language: "en"
hardware: ["220V Relay", "WIZnet W5500 Ethernet Shield", "RFM95 LORA", "Aduino mega 2560"]
likes: 0
views: 280
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# My LORA remote relay system?

> My LORA remote relay system?

Original author: teju (source: https://sites.google.com/site/marcobotprojects/my-projects/my-lora-remote-relay-system)

## Components

- **220V Relay** x 4
- **WIZnet W5500 Ethernet Shield** x 1
- **RFM95 LORA** x 1
- **Aduino mega 2560** x 1: Main MCU

## Article

## Description

This is the core of the system acting as gateway between the Lora relay module, the human interface and the network.

It is composed of:

- ?Ethernet module to connect to MQTT server and host a simple webserver

- ?RTC module to save time and the time is synchronized on regular bases to an NTP server.

- ?RF95 900Mhz?module to communicate via lora radio to the remote lora relay modules

- LCD screen with 4 buttons and 1 rotary encoder and 1 buzer as human interface

- Aduino mega 2560 as main MCU

- Aduino nano as MCU for the RF95 module

- 5Vdc buck converter

- 3.3Vdc buck converter

The Arduino?mega 2560 is the brain of the system. The Ethernet?module W5500 is connected to the SPI interface. The RF95 module use SPI as communication port too but the Arduino?mega offer only 1 SPI port and because the 2 modules must act as server i can't share the SPI port between them. I have then connected the RF95 module to an Arduino?nano connected to Serial 2 of the Arduino?nano. The RTC module use the ic2 port of the Arduino?mega. The system is powered by a generic 2 amps 12Vdc with 2 buck converter, one 5v for the arduino mega,the aruino nanoand the LCD screen and one 3.3v for the rf95 module. i could have used the 3.3 v from the mega but because it was my first project with lora, i had no idea of the power needed for the lora module in when transmiting. That's why the RF95 got it's dedicaded buck converter. it is little bit over killed but at least the power is stable.

Hardware button debouncing: To minimize the load of the MCU i try to do an hardware button debouncing based on RC filter and Hex inverter schmit trigger like the 74HC14N as described in the following paper:?<https://my.eng.utah.edu/~cs5780/debouncing.pdf> I connect everything on perfboard instead of making a PCB because it is going to be a one unit production only :)

## Component

| ?Arduino Mega 2560 | [![](https://sites.google.com/site/marcobotprojects/_/rsrc/1592294167343/my-projects/my-lora-remote-relay-system/main-monitor/arduinomega.jpg?height=200&width=150)](https://sites.google.com/site/marcobotprojects/my-projects/my-lora-remote-relay-system/main-monitor/arduinomega.jpg?attredirects=0) | ??Ethernet W5500 | [![](https://sites.google.com/site/marcobotprojects/_/rsrc/1592294167343/my-projects/my-lora-remote-relay-system/main-monitor/ethernetModule.jpg?height=200&width=200)](https://sites.google.com/site/marcobotprojects/my-projects/my-lora-remote-relay-system/main-monitor/ethernetModule.jpg?attredirects=0) |
| --- | --- | --- | --- |
| ?Arduino nano | [![](https://sites.google.com/site/marcobotprojects/_/rsrc/1592294167343/my-projects/my-lora-remote-relay-system/main-monitor/ArduinoNano.jpg)](https://sites.google.com/site/marcobotprojects/my-projects/my-lora-remote-relay-system/main-monitor/ArduinoNano.jpg?attredirects=0) | ?RF95 module | [![](https://sites.google.com/site/marcobotprojects/_/rsrc/1592294167343/my-projects/my-lora-remote-relay-system/main-monitor/rfm95Module.jpg)](https://sites.google.com/site/marcobotprojects/my-projects/my-lora-remote-relay-system/main-monitor/rfm95Module.jpg?attredirects=0) |
| ?RTC module | ?![Uzinb RTC DS1302 Real Time Clock Module for Arduino AVR ARM PIC ...](https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSKlgi0Kj45qnthDmwLH7zB8nI1_dyUItmMUv4sgoMOy4a10aCE&usqp=CAU) | ?LCD 4x20 | [![](https://sites.google.com/site/marcobotprojects/_/rsrc/1592294167343/my-projects/my-lora-remote-relay-system/main-monitor/LCD4x20.jpg)](https://sites.google.com/site/marcobotprojects/my-projects/my-lora-remote-relay-system/main-monitor/LCD4x20.jpg?attredirects=0) |
| ?LM 2596 Buck Converter | ?![](https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRJUzfqJWIK9gokq7aQDUFp-R2SuD77F77tEsPd_c_4lcMKE4rVRPeEdRCRtkmTSAmUN_Jr058&usqp=CAc) | ?Rotary encoder | [![](https://sites.google.com/site/marcobotprojects/_/rsrc/1592294167343/my-projects/my-lora-remote-relay-system/main-monitor/Rotaryencoder.jpg)](https://sites.google.com/site/marcobotprojects/my-projects/my-lora-remote-relay-system/main-monitor/Rotaryencoder.jpg?attredirects=0) |
| Omron Push button | [![](https://sites.google.com/site/marcobotprojects/_/rsrc/1592294167343/my-projects/my-lora-remote-relay-system/main-monitor/OmronPushButton.jpg?height=200&width=200)](https://sites.google.com/site/marcobotprojects/my-projects/my-lora-remote-relay-system/main-monitor/OmronPushButton.jpg?attredirects=0) | ?5v buzzer | [![](https://sites.google.com/site/marcobotprojects/_/rsrc/1592294167343/my-projects/my-lora-remote-relay-system/main-monitor/buzzer.jpg?height=200&width=200)](https://sites.google.com/site/marcobotprojects/my-projects/my-lora-remote-relay-system/main-monitor/buzzer.jpg?attredirects=0) |
| ?74HC14N | [![](https://sites.google.com/site/marcobotprojects/_/rsrc/1592294167343/my-projects/my-lora-remote-relay-system/main-monitor/74HC14N.jpg?height=200&width=200)](https://sites.google.com/site/marcobotprojects/my-projects/my-lora-remote-relay-system/main-monitor/74HC14N.jpg?attredirects=0) | ?Other passive component as Resistor, capacitor, trim pot jumpers etc... | ?Check diagram below |

## Schematic

check below to download eagle schematic source file?ETH_LORA_Transceiver.sch

[![](https://sites.google.com/site/marcobotprojects/_/rsrc/1592294167343/my-projects/my-lora-remote-relay-system/main-monitor/ETH_LoraTranceiver.png)](https://sites.google.com/site/marcobotprojects/my-projects/my-lora-remote-relay-system/main-monitor/ETH_LoraTranceiver.png?attredirects=0)

## Communication protocol:

The radiohead Reliable datagram use an int as address for each device

I made my own address table like this: (the one in bold are the one actually used, the other one are for future development)

| ?Name | ?Lora Address | ?Name | Lora Address |
| --- | --- | --- | --- |
| **?MONITOR1** | **1** | ?**COTTAGE12** | **12?** |
| MONITOR2 | ?2 | ?**COTTAGE14** | **14** |
| ?**PROBE** | ?**3** | ?**COTTAGE11** | **11** |
| ?**DIVING1?** | ?**4** | ?**COTTAGE10** | **10** |
| ?RESTAURANT1 | ?17 | ?**COTTAGE9** | **9** |
| ?RESTAURANT2 | ?18 | ?**COTTAGE8** | **8?** |
| ?BAR1 | ?19 | ?**COTTAGE7** | **7** |
| ?BAR2 | ?20 | ?**COTTAGE6** | **6** |
| ?PIER1 | ?13 | ?**LORA** | Internal no address |

Special address:?**RH_BROADCAST_ADDRESS=255**

my communication protocol was defined as follow:

**[FROM][TO][CDE][RLY]**

Where?**FROM**?is a char array of the Sender,?**TO**?the char array of the Receiver?**CDE**?is a char array of the command and?**RLY**?and integer representing a payload. The separator are

"**[**" and "**]**"

This are the packet send and received via lora radio

The lora receiver on the monitor is managed by an arduino nano communicating to the main MCU via serial port.

The nano receiver add the following data to the packet:?**[SNR][RSSI]**?two integer representing the Signal Noise Ratio and the Received Signal Strength of the lora sender.

The available commands are:

| **?Command** | ?**Description** |  | **?Reply** | ?Description |
| --- | --- | --- | --- | --- |
| RST | ?Reset the module |  | ?OK | ?The message have been received and acknowledge |
| PING | ?Send a ping command and wait for reply |  | PONG | ?Reply to a ping command the RLY state is transmitted as payload |
| SETRELAY | ?Set the Relay to the int RLY |  | ?FAILED | ?The transmission of the message failed |
| GARDENON | ?Switch on the Garden relay |  | ?LORAFAILED | ?The LORA module on the nano failed to initialize |
| GARDENOFF | ?Switch off the Garden relay |  | ?LORASTARTED | ?The LORA module on the nano has started |
| WALLON | ?Switch on the wall relay |  |  |  |
| WALLOFF | ?Switch off the wall relay |  |  |  |
| NIGHTON | ?Switch on the Garden? and wall relay |  |  |  |
| NIGHTOFF | ??Switch on the Garden? and wall relay |  |  |  |
| COTTAGESON | ?Switch on the cottage relay |  |  |  |
| COTTAGESOFF | ?Switch off the cottage relay |  |  |  |
| FRIDGESON | ?Switch on the fridge relay |  |  |  |
| FRIDGESOFF | ?Switch off the cottage relay |  |  |  |
| ?LORARESET | ?REset the nano Lora module |  |  |  |

All the commands transmitted to a specific address wait for a reply from the receiver

All the commands send to?**RH_BROADCAST_ADDRESS?**are received simultaneously by all the modules and don need a reply.

**Example1:?[MONITOR1][DIVING1][SETRELAY][8]**

Send the command SETRELAY to DIVING1 from Monitor with tthe payload RLY=8

The expected reply from DIVING1is:

**[DIVING1][MONITOR1][OK[8]**

The payload is send back to the sender eith the rerply OK

**Example2:[MONITOR1][LORA][LORARESET][0]**

The command LORARESET is send to the nano lora module from MONITOR1

The expected reply are:

**[LORA][MONITOR1][LORASTARTED]?**or?**[LORA][MONITOR1][LORAFAILED]??Example3:[MONITOR1][COTTAGE12][PING][15]**

MONITOR1 is sending a PING command to COTTAGE12 with the RLYSTATE as payload

The expected reply are:

**[COTTAGE12]MONITOR1][PONG][15]**?or?**[LORA][MONITOR1][FAILED][15]Example4:[MONITOR1][RH_BROADCAST_ADDRESS?][NIGHTON][0]**

MONITOR1 is asking to all the module to switch on the GARDEN and WALL relays.

No reply expected.

ETC.....

## The relay state and Relay mask:

each module is associated with RELAYSTATE and RELAYMASK those data are store in the Arduino Mega EEPROM

The RELAY STATE is an integer where each bit represent the state of one relay.

RELAYSTATE=15 = 1111000000000000 the 4 first relays are on and the rest off

RELAYSTATE=2? =? 0100000000000000 only the 2nd relay is ON

I could address 16 relays per module. i have only 4 relay to address so my relay state goes from 0 to 15.

The RELAYMASK allow the program to calculate the new RELAYSTATE for the NIGHT timer

This is made by applying a logical OR between the RELAYSTATE and the RELAYMASK

let say that every night a 18:00 i want to switch ON the garden light and the wall light of a certain cottage my RELAYMASK for this cottage should be 3 (the 1st 2 relays).

by applying the mask to the relay state i would not change the state of the other relay but only the state of the 2 first relay etc...

If the RELAYMASK is set to 0 the relay state would not be affected by the night timer. Doing that i can select which wall and garden light are on during the night timer.

Timer

A nightOn and NightOff time are stored in the EEPROM for the Night light timer.

The timer automatically switch ON/OFF simultaneously the wall and garden light of the selected cottages selected by they respective RELAYMASK

The time of the device is synchronized with the RTC module which is synchronized hourly to an NTP server.

The NTP synchronization accure first during startup and then is schedule every hour.

The Ping Command:

Every 3 minutes the monitor is sending a ping command to each device to check if the device is still alive and to synchronize the last recorded relay state in case one of the devices didn't received the last SETRELAY command. The syntax is?**[MONITOR1][***TO***][PING][***RLY***]** The monitor is expecting a PONG reply from each device and the loara module is adding the SNR and RSSI to the received packet. The expected packet will be like:**?[***FROM***][MONITOR1][PONG][***RLY***][***RSSI***][***SNR***]** Once a reply has been received, the monitor is publishing the relay state, the SNR and RSSI to mqtt

## MQTT:

The monitor is listening to an MQTT mosquito server hosted on the local network

The monitor is reading the following MQTT datas:

| ?**Topic?? ?? ???** | **?Description** | **Type?** | **?Topic** | **Description** | **Type?** |
| --- | --- | --- | --- | --- | --- |
| ?batteryvolts | ?From the inverter | ?Float | ?humidity | ?from the weather station | ?float |
| ?SOC | ?Battery State Of Charge from the inverter | int | ?temp | ?from the weather station | float |
| ?loadwatts | ?System Load from the inverter | long int | ?MONITOR1CMD | ?general command which apply either to the monitor itself or all the buildings. | Text |
| ?gridmode | ?power source from generator or battery | bool | ?node-RED/SETMSK | ?set mask command from the nod red interface | Json |
| ?dust | dust in ug/m2 from the dust sensor | float | ?/*"Building"*/RELAY*X* | ?Set the relay of a specific bungalow | ?int |
|  |  |  | ?/*"Building"*/CMD | ?send a command to a specific bungalow. | text |

**MONITOR1CMD are:**

- PING: Send a ping command to all the devices

- RST: send a reset command to all the building

- GARDENON:Switch ON all thegarden lights on all the devices

- GARDENOFF: Switch OFF all the garden lights on all the devices

- WALLON:Switch ON all the wall lights on all the devices

- WALLOFF: Switch OFF all the wall lights on all the devices

- NIGHTON:Switch ON all the garden and wall lights on all the devices

- NIGHTOFF: Switch OFF all the garden and wall lights on all the devices

- ALLON: Put the RELAYSTATE to 15 to all the devices

- ALLOFF:??Put the RELAYSTATE to 0 to all the devices

- COTTAGESON:Switch ON the cottage relays on all the devices

- COTTAGESOFF: Switch OFF the cottage relays on all the devices

- FRIDGESON:Switch ON all the fridge relay on all the devices

- FRIDGESOFF: Switch OFF all the fridge relay on all the devices

- LORARST: rEset the Lora module on the monitor

/*"Building"*/RELAY*X*?is for example: /DIVING1/RELAY1/0 set the RELAY1 of the Building DIVING1 to 1

/*"Building"*/CMD is for example: /DIVING1/CMD/RST to reset the device or /DIVING1/CMD/PING to sen a ping to a specific device.

the node-RED/SETMSK is Json string to set the mas of all the relays for all the devices at the same time:

**{D1:8,C12:1,C14:3, C11:3,C10:3,C9:0,C8:0,C7:0,C60}**?where D1 is the device DIVING1, C12 is the device COTTAGE12 etc, followed by the int representing the mask.

## Web Server:

A web server is listening on the port 80 of the Ethernet module.The home page of the server is serving the actual date and time of the system. The following commands are available:

- **/**?home page display time and date of the day.

- **/?data=DIVING1&RELAY1&0**? Set the Relay1 of Diving1 to OFF ex:**/?data=COTTAGE6&RELAY4&1**

- **/?rqst=MONITOR1RST?**Reset monitor1

- **/?rqst=GETALARM**?Get time for night lights On/Off.

- **/?rqstSETLARMOFF&Time=66000000**?set time for alarn nightOn/Off time is in ms (1h=3600000ms).

- **/?rqstSETLARMOFF&Time=79200000**

- **/?rqst=PINGALL**?send a ping command to all lora devices.

- **/?rqst=RSTALL?**Reset all lora devices.

- **/?rqst=ALLON?**Switch On all relays on all lora devices.

- **/?rqst=ALLOFF**?Switch Off all relay on all lora devices.

- **/?rqst=GARDENON?**Switch On all garden lights on all lora devices.

- **/?rqst=GARDENOFF**?Switch Off all garden lights on all lora devices.

- **/?rqst=WALLON?**Switch On all wall lights on all lora devices.

- **/?rqst=WALLOFF**?Switch Off all wall lights on all lora devices.

- **/?rqst=NIGHTON**?Switch On all Night lights (Garden/Wall) on all lora devices.

- **/?rqst=NIGHTOFF**?Switch Off all Night lights (Garden/Wall) on all lora devices.

- **/?rqst=COTTAGESON**?Switch On all Cottages

- **/?rqst=COTTAGESOFF**?Switch Off all Cottages.

- **/?rqst=FRIDGESON**?Switch On all Fridges

- **/?rqst=FRIDGESOFF**?Switch Off all Fridges.

## Serial Commands:

The monitor is listening on the USB Serial port for some plain text commands.

The commands available are:

| **sendTO:**?send data packet to LORA **ping:**?send ping command **NTPtime:**?set RTC time **RTCtime:**?set RTC time **printTime:**?Get time from RTC module **printDate:?**Get date from RTC module **setAlarm:**?Set ON/OFF time garden lights **printAlarm:**?Get night lights alarm **loraRst:?**Reset lora module? ?module **RESET:?**Reset this device **PingAll:**?send Ping to all devices **rstAll:**?Reset all the Lora devices **allOn:**?All the realy ON on all lora devices | **allOFF:**?All the realy OFF on all lora device **gardenOn:**?all garden lights ON **gardenOff:**?all garden lights OFF **wallOn:?**all wall lights ON **wallOff:**?all wall lights OFF **nightOn:**?all night lights ON **nightOff:**?all night lights OFF **cottagesOn:**?all cottages lights ON **cottagesOff:**?all cottages lights OFF **fridgesOn:**?all fridges ON **fridgesOff:**?all fridges OFF **printMenu:?**print this menu |
| --- | --- |

The command?**sendTO**?is expecting the following syntax:?**[FROM][TO][CDE][RLY]**

Where FROM is the sender, TO the recipient, CDE the command and RLY the payload.

---

Source: https://maker.wiznet.io/teju/projects/My-LORA-remote-relay-system/
