Gbox 4.20
Grow box automation and monitoring - <a href='https://sites.google.com/site/growboxguy/'>https://sites.google.com/site/growboxguy/</a>
 
Loading...
Searching...
No Matches
Gbox420_Mega_Main.ino File Reference

Main module for Mega2560 - Grow tent monitoring and controlling sketch. More...

#include "Arduino.h"
#include "avr/wdt.h"
#include "avr/boot.h"
#include "printf.h"
#include "TimerThree.h"
#include "ELClient.h"
#include "ELClientWebServer.h"
#include "ELClientCmd.h"
#include "ELClientRest.h"
#include "ELClientMqtt.h"
#include "Thread.h"
#include "StaticThreadController.h"
#include "SerialLog.h"
#include "Settings.h"
#include "src/Modules_Web/MainModule_Web.h"
#include "SPI.h"
#include "RF24.h"
Include dependency graph for Gbox420_Mega_Main.ino:

Go to the source code of this file.

Functions

RF24 Wireless (WirelessCEPin, WirelessCSNPin)
 Wireless communication with Modules over nRF24L01+.
 
void setup ()
 
void loop ()
 
void processTimeCriticalStuff ()
 
void run1sec ()
 
void run5sec ()
 
void run1min ()
 
void heartBeat ()
 Turns the integrated LED on the Arduino board ON/OFF.
 
void resetWebServer ()
 (re-)Initialize the ESP-link connection
 
void setupMqtt ()
 Sets up the MQTT relay.
 
void mqttConnected (__attribute__((unused)) void *response)
 
void mqttDisconnected (__attribute__((unused)) void *response)
 
void mqttPublished (__attribute__((unused)) void *response)
 
void mqttReceived (void *response)
 
time_t getNtpTime ()
 Update the time over ESP-link using NTP (Network Time Protocol)
 
void loadCallback (__attribute__((unused)) char *Url)
 Called when a website is loading on the ESP-link webserver.
 
void refreshCallback (__attribute__((unused)) char *Url)
 Called when a website is refreshing on the ESP-link webserver.
 
void buttonCallback (char *Button)
 Called when a button is pressed.
 
void fieldCallback (char *Field)
 Called when a field on the website is submitted.
 
void settingsLoadCallback (__attribute__((unused)) char *Url)
 Called when the /Settings.html website is loading on the ESP-link webserver.
 
void settingsRefreshCallback (__attribute__((unused)) char *Url)
 Called when the /Settings.html website is refreshing on the ESP-link webserver.
 
void settingsButtonCallback (char *Button)
 Called when a button is pressed on the /Settings.html web page.
 
void settingsFieldCallback (char *Field)
 Called when a field on the /Settings.html website is submitted.
 
void getWirelessStatus ()
 Prints the nRF24L01+ wireless transceiver's status to the Serial log.
 
void ignoreCallback (__attribute__((unused)) char *Url)
 Ignores the incoming load/refresh event. Used when embedding another module's web interface that already handles the event.
 

Variables

char LongMessage [MaxLongTextLength] = ""
 Temp storage for assembling long messages (REST API, MQTT reporting)
 
char ShortMessage [MaxShotTextLength] = ""
 Temp storage for assembling short text messages (Log entries, Error messages,etc)
 
char CurrentTime [MaxWordLength] = ""
 Buffer for storing current time in text format.
 
HardwareSerial & ArduinoSerial = Serial
 Reference to the Arduino Serial output : Mega 2560 Rev3 Serial:0(RX) 1(TX) also showed on USB, Serial1:19(RX1) 18(TX1), Serial2:17(RX2), 16(TX2), Serial4:15(RX3) 14(TX3)
 
HardwareSerial & ESPSerial = Serial3
 Reference to the ESP Link Serial output : Mega 2560 Rev3 Serial:0(RX) 1(TX), Serial1:19(RX1) 18(TX1), Serial2:17(RX2), 16(TX2), Serial4:15(RX3) 14(TX3)
 
ELClientWebServer WebServer & ESPLink
 ESP-link - WebServer API.
 
SettingsModuleSettings
 This object will store the settings loaded from the EEPROM. Persistent between reboots.
 
bool & Debug = *new bool
 True - Turns on extra debug messages on the Serial Output.
 
bool & Metric = *new bool
 True - Use metric units, False - Use imperial units.
 
bool MqttConnected = false
 Track the connection state to the MQTT broker configured on the ESP-link's REST/MQTT tab.
 
MainModule * Main1
 Represents a Grow Box with all components (Lights, DHT sensors, Power sensor, Aero/Hempy/Reservoir wireless modules,..etc)
 
Thread OneSecThread = Thread()
 
Thread FiveSecThread = Thread()
 
Thread MinuteThread = Thread()
 

Detailed Description

Main module for Mega2560 - Grow tent monitoring and controlling sketch.

To change the default pin layout / startup settings navigate to: Settings.h

Author
GrowBoxGuy - https://sites.google.com/site/growboxguy/
Version
4.20

Definition in file Gbox420_Mega_Main.ino.

Function Documentation

◆ buttonCallback()

void buttonCallback ( char *  Button)

Called when a button is pressed.

Parameters
Button- ID of the button HTML element

Definition at line 315 of file Gbox420_Mega_Main.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fieldCallback()

void fieldCallback ( char *  Field)

Called when a field on the website is submitted.

Parameters
Field- Name of the input HTML element

< Called when any field on the website is updated.

Definition at line 333 of file Gbox420_Mega_Main.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getNtpTime()

time_t getNtpTime ( )

Update the time over ESP-link using NTP (Network Time Protocol)

reset watchdog timeout

Definition at line 267 of file Gbox420_Mega_Main.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getWirelessStatus()

void getWirelessStatus ( )

Prints the nRF24L01+ wireless transceiver's status to the Serial log.

Definition at line 396 of file Gbox420_Mega_Main.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ heartBeat()

void heartBeat ( )

Turns the integrated LED on the Arduino board ON/OFF.

Definition at line 154 of file Gbox420_Mega_Main.ino.

Here is the caller graph for this function:

◆ ignoreCallback()

void ignoreCallback ( __attribute__((unused)) char *  Url)

Ignores the incoming load/refresh event. Used when embedding another module's web interface that already handles the event.

Definition at line 409 of file Gbox420_Mega_Main.ino.

Here is the caller graph for this function:

◆ loadCallback()

void loadCallback ( __attribute__((unused)) char *  Url)

Called when a website is loading on the ESP-link webserver.

Parameters
Url- HTML filename that is getting loaded

Definition at line 297 of file Gbox420_Mega_Main.ino.

Here is the caller graph for this function:

◆ loop()

void loop ( )

< loop only checks if it's time to trigger one of the threads (run1sec(), run5sec(),run1min()..etc)

Definition at line 119 of file Gbox420_Mega_Main.ino.

◆ mqttConnected()

void mqttConnected ( __attribute__((unused)) void *  response)

Definition at line 227 of file Gbox420_Mega_Main.ino.

Here is the caller graph for this function:

◆ mqttDisconnected()

void mqttDisconnected ( __attribute__((unused)) void *  response)

Definition at line 234 of file Gbox420_Mega_Main.ino.

Here is the caller graph for this function:

◆ mqttPublished()

void mqttPublished ( __attribute__((unused)) void *  response)

Definition at line 240 of file Gbox420_Mega_Main.ino.

Here is the caller graph for this function:

◆ mqttReceived()

void mqttReceived ( void *  response)

Definition at line 245 of file Gbox420_Mega_Main.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ processTimeCriticalStuff()

void processTimeCriticalStuff ( )

< Interrupt calls this every 0.5 sec to process any request coming from the ESP-Link hosted webpage. Uses Serial Line Internet Protocol (SLIP)

Definition at line 124 of file Gbox420_Mega_Main.ino.

Here is the caller graph for this function:

◆ refreshCallback()

void refreshCallback ( __attribute__((unused)) char *  Url)

Called when a website is refreshing on the ESP-link webserver.

Parameters
Url- HTML filename that is refreshinging

Definition at line 306 of file Gbox420_Mega_Main.ino.

Here is the caller graph for this function:

◆ resetWebServer()

void resetWebServer ( )

(re-)Initialize the ESP-link connection

< Callback made from esp-link to notify that it has just come out of a reset

< line break

< Pre-setup PushingBox relay to Google Sheets (Not actual connection)

< If begin returns a negative number the initialization failed

< setup handling request from GrowBox.html

< GrowBox tab - Called then the website loads initially

< GrowBox tab - Called periodically to refresh website content

< GrowBox tab - Called when a button is pressed on the website

< GrowBox tab - Called when a field is changed on the website

< setup handling request from Settings.html

< Settings tab - Called then the website loads initially

< Settings tab - Called periodically to refresh website content

< Settings tab - Called when a button is pressed on the website

< Settings tab - Called when a field is changed on the website

< setup handling request from Hempy.html (embeds the Stand-alone Hempy module's web interface)

< Ignore event, handled by the Stand-alone Hempy module

< Ignore event, handled by the Stand-alone Hempy module

Definition at line 166 of file Gbox420_Mega_Main.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run1min()

void run1min ( )

Definition at line 144 of file Gbox420_Mega_Main.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run1sec()

void run1sec ( )

< reset watchdog timeout

< Blinks built-in led

Definition at line 131 of file Gbox420_Mega_Main.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run5sec()

void run5sec ( )

Definition at line 138 of file Gbox420_Mega_Main.ino.

Here is the caller graph for this function:

◆ settingsButtonCallback()

void settingsButtonCallback ( char *  Button)

Called when a button is pressed on the /Settings.html web page.

Parameters
Button- ID of the button HTML element

Definition at line 360 of file Gbox420_Mega_Main.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ settingsFieldCallback()

void settingsFieldCallback ( char *  Field)

Called when a field on the /Settings.html website is submitted.

Parameters
Field- Name of the input HTML element

Definition at line 382 of file Gbox420_Mega_Main.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ settingsLoadCallback()

void settingsLoadCallback ( __attribute__((unused)) char *  Url)

Called when the /Settings.html website is loading on the ESP-link webserver.

Definition at line 343 of file Gbox420_Mega_Main.ino.

Here is the caller graph for this function:

◆ settingsRefreshCallback()

void settingsRefreshCallback ( __attribute__((unused)) char *  Url)

Called when the /Settings.html website is refreshing on the ESP-link webserver.

Definition at line 351 of file Gbox420_Mega_Main.ino.

Here is the caller graph for this function:

◆ setup()

void setup ( )

< put your setup code here, to run once:

< 2560mega console output

< ESP WiFi console output

< onboard LED - Heartbeat every second to confirm code is running

< Needed to print wireless status to Serial

< New line

< logs to both Arduino and ESP serials, adds new line after the text (true), and uses no indentation (0). More on why texts are in F(""): https://gist.github.com/sticilface/e54016485fcccd10950e93ddcd4461a3

< Watchdog timeout set to 8 seconds, if watchdog is not reset every 8 seconds it assumes a lockup and resets the sketch

< fix watchdog not loading sketch after a reset error on Mega2560

< Callback subscription: What to do when WiFi reconnects

< reset the WebServer

< Points to method for updating time from NTP server

< Sync time every day

< check every 0.5sec, using a larger interval can cause web requests to time out

< Initialize the nRF24L01+ wireless chip for talking to Modules

< Set the speed to slow - has longer range + No need for faster transmission, Other options: RF24_2MBPS, RF24_1MBPS

< RF24_CRC_8 for 8-bit or RF24_CRC_16 for 16-bit

< RF24_PA_MIN=-18dBm, RF24_PA_LOW=-12dBm, RF24_PA_HIGH=-6dBm, and RF24_PA_MAX=0dBm.

< The number of bytes in the payload. This implementation uses a fixed payload size for all transmissions

< Required for ACK messages

< When sending a wireless package, expect a response confirming the package was received in a custom Acknowledgement package

< Defined in Settings.h. How many retries before giving up sending a single package and How long to wait between each retry

< This is the main object representing an entire Grow Box with all components in it. Receives its name and the settings loaded from the EEPROM as parameters

Definition at line 55 of file Gbox420_Mega_Main.ino.

Here is the call graph for this function:

◆ setupMqtt()

void setupMqtt ( )

Sets up the MQTT relay.

Definition at line 213 of file Gbox420_Mega_Main.ino.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Wireless()

RF24 Wireless ( WirelessCEPin  ,
WirelessCSNPin   
)

Wireless communication with Modules over nRF24L01+.

Here is the caller graph for this function:

Variable Documentation

◆ ArduinoSerial

HardwareSerial& ArduinoSerial = Serial

Reference to the Arduino Serial output : Mega 2560 Rev3 Serial:0(RX) 1(TX) also showed on USB, Serial1:19(RX1) 18(TX1), Serial2:17(RX2), 16(TX2), Serial4:15(RX3) 14(TX3)

Printing serial messages to the Arduino Serial output.

Printing serial messages to the Arduino Serial output and the esp-link Microcontroller Console (uC Console)

Definition at line 33 of file Gbox420_Mega_Main.ino.

◆ CurrentTime

char CurrentTime[MaxWordLength] = ""

Buffer for storing current time in text format.

Definition at line 30 of file Gbox420_Mega_Main.ino.

◆ Debug

bool& Debug = *new bool

True - Turns on extra debug messages on the Serial Output.

Definition at line 42 of file Gbox420_Mega_Main.ino.

◆ ESPLink

ELClientMqtt MqttAPI & ESPLink

ESP-link - WebServer API.

ESP-link - MQTT protocol for sending and receiving messages.

ESP-link - REST API for HomeAssistant.

ESP-link - REST API for PushingBox.

ESP-link - Helps getting the current time from the internet using NTP.

Definition at line 36 of file Gbox420_Mega_Main.ino.

◆ ESPSerial

ELClient ESPLink & ESPSerial = Serial3

Reference to the ESP Link Serial output : Mega 2560 Rev3 Serial:0(RX) 1(TX), Serial1:19(RX1) 18(TX1), Serial2:17(RX2), 16(TX2), Serial4:15(RX3) 14(TX3)

ESP-link. Both SLIP and debug messages are sent to ESP over the ESP Serial link.

Definition at line 34 of file Gbox420_Mega_Main.ino.

◆ FiveSecThread

Thread FiveSecThread = Thread()

Definition at line 51 of file Gbox420_Mega_Main.ino.

◆ LongMessage

char LongMessage[MaxLongTextLength] = ""

Temp storage for assembling long messages (REST API, MQTT reporting)

Definition at line 28 of file Gbox420_Mega_Main.ino.

◆ Main1

MainModule* Main1

Represents a Grow Box with all components (Lights, DHT sensors, Power sensor, Aero/Hempy/Reservoir wireless modules,..etc)

Definition at line 45 of file Gbox420_Mega_Main.ino.

◆ Metric

bool& Metric = *new bool

True - Use metric units, False - Use imperial units.

Definition at line 43 of file Gbox420_Mega_Main.ino.

◆ MinuteThread

Thread MinuteThread = Thread()

Definition at line 52 of file Gbox420_Mega_Main.ino.

◆ ModuleSettings

Settings* ModuleSettings

This object will store the settings loaded from the EEPROM. Persistent between reboots.

Definition at line 41 of file Gbox420_Mega_Main.ino.

◆ MqttConnected

bool MqttConnected = false

Track the connection state to the MQTT broker configured on the ESP-link's REST/MQTT tab.

Definition at line 44 of file Gbox420_Mega_Main.ino.

◆ OneSecThread

StaticThreadController< 3 > ThreadControl & OneSecThread = Thread()

Definition at line 50 of file Gbox420_Mega_Main.ino.

◆ ShortMessage

char ShortMessage[MaxShotTextLength] = ""

Temp storage for assembling short text messages (Log entries, Error messages,etc)

Definition at line 29 of file Gbox420_Mega_Main.ino.