2.Functionality:
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)
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:
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:
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"/RELAYX 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
allOFF: All the realy OFF on all lora device
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
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.
Next: Main monitor Code>>>