3.Main Monitor Code

Libraries:

This is the list of the libraries used tn the code:

    • SPI.h Generic arduino library to communicate via SPI. Used by the ethernet module and the RF95 module.

    • ethernet.h Generic ethenet arduino library to manage the W5500 ethernet module

    • ethenetUDP.h Used to connect to NTP server via the ethenet w5500 module.

    • PubSubClient.h MQTT protocole library by Knolleary to communicate with my MQTT server

    • EEPROM.h generic arduino library to read and write to the EEPROM

    • ds1307.h by paul Stoffrengen library to communicate with the RTC module

    • wire.h library generic i2c arduino library to communicate with the RTC module via i2c

    • time.h and timeLib.h by paul Stoffrengen libraries used to translate time in different format

  • LiquidCrystal.h Generic arduino library for the LCD module.

  • RH_RF95.h by airspayce to communicate with the RF95 lora module

  • RHReliableDatagram.h by airspayce to make an adressable network via the RF95 library.

The monitor code:

Check files at the bottom of the page for the source code.

The code is divided in 8 different modules or filed:

SolarEnergyMonitorMQTT.ino

This is the main heap of the program,

it is composed of:

    • the global variable definition

    • the callbackMQTTT() function,

    • the setup() loop

    • the main() loop

network.ino

it composed of the :

    • sendNTPpacket() function and the received NTPpacket

    • Functions to send and received UDP packets to synchronised time with the RTC module

  • webserverClient() function

  • where it is written all the webserver mechanism.

MQTT.ino

    • reconnect)_ function:

    • Subscrition to the MQTT topics

    • postEmoncms():

    • Publishing the relay states, SNR and RSSI of one device after a PONG reply

    • SendRelyStatus()

    • Publish the relay status of a specific device

    • SendAllRelayStatus():

    • publishing the state relay per relay of a spcific device

    • sendMonitor1()

    • publishing monitor text message (loar message, system message...)

    • sendAlarm()

    • Publishing the timer nightON nightOFF

    • sendMSK()

    • Publishing the RELAYMASK of all the devices at once with a JSON string{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.

lora.ino

    • sendMessage()

    • Sending message to lora radio [FORM][TO][CDE][RLY]

    • decodeAdress()

    • decoding the text form adress of a device to its lora adress integer.

    • decodePacket()

    • decoding the Lora packet [FROM][TO][CDE][RLY][RSSI][SNR]

    • sendPing()

    • Preparing the execution of the ping command device per device

    • loraRX()

    • Decoding the received packet from Lora

    • loraRESET

    • sending a reset command to the lora module

    • pingALL()

    • Preparing the ping timer command

    • allOn(),AllOff(),gardenOn()gardenOff(),wallOn()wallOff(),nightOn()nightOff(),cottagesOn()cottagesOff(),fridgesOn()fridgesOff(),rstAll()

    • sending specific command to all the devices

time.ino

  • setTimeRTC()

  • Set time to the RTC module via the serial console

  • setDateRTC()

  • Set date to the RTC module via the serial console

  • dayOfTheWeek()

  • Convert the day of the date to "Monday",Tuesday... form

  • printDate(),printTime(),printAlarm()

  • print date, time and night alarm to the serial consol

  • setAlarm()

  • Set the nightON nightOff timer from the serial consol

  • NTPtimer()

  • Timout timer if the NTP server doesn't reply

  • dayNightTimer()

  • send the setrelay command to each device when timerNightOn or OFF is trigger

  • sendPingtimer()

  • Send the ping command to ech device every 3 minutes

LCD.ino

it is where all the LCD screen are defined it is composed off:

  • screen1()

  • sendLcd()

  • replyLcd()

  • screenAlarmBat()

  • NTPscreen()

  • switches()

  • menuAlarm()

  • checkButton()

  • switchCottage()

Menu.ino

This where the LCD menu are defined, it is composed of:

  • mainMenu()

  • menuMonitor()

  • menuSwitches()

  • menuNightLights()

  • menuSelectLights()

  • menuCottages()

  • menuGenerator()

Serial.ino

it is composed of:

  • printMenu()

  • print on the serial port all the serial command available

  • sendTO()

  • read the [FROM][TO][CDE][RLY] sequence from the serial port

  • menuSerial()

  • Read the serial pors and interpret the command to run the subsquent function.

The Lora module code for the Arduino nano module:

The RF95 module is connected to ist own arduino nano MCU. The arduino nano is using it's serial port to communicate with the serial port 2 of the arduino mega.

there is a sotware serial for debugging.

the code is store in the following file:

SolarEnergyMonitorLoraTX

the folowing libraies are used:

  • RHReliableDatagram.h

  • RH_RF95.h

  • SPI.h

  • SoftwareSerial.h

The global variable definition are on the top of the file,

  • setup() function is initialising the lora module and the serial ports

  • main loop() fuction is listening the serial port for any packet to send or command to interpret from the monitor module.

  • sendMessage()

  • is sending any meesage from Monitor1 to anay device via the lora module and forward to the monitor module via the serial port any messages recieved from the lora module

  • decodeAddress()

  • Decode the test form address to the lora integer form address

  • decodePacket()

  • decode the packet [FROM][TO][CDE][RLY] to interpret any command directly send to the LORA module (RST etc...)

NEXT: 4 Relays Switch Remote Module >>>>>>>