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_Nano_Aero_NoTank.ino
Go to the documentation of this file.
1
10#include "Arduino.h"
11#include "avr/wdt.h" // Watchdog timer for detecting a crash and automatically resetting the board
12#include "avr/boot.h" // Watchdog timer related bug fix
13#include "printf.h"
14#include "Thread.h" // Splitting functions to threads for timing
15#include "StaticThreadController.h" // Grouping threads
16#include "SPI.h" // allows you to communicate with SPI devices, with the Arduino as the master device
17#include "RF24.h" // https://github.com/maniacbug/RF24
18#include "SerialLog.h" // Logging debug messages to Serial
19#include "Settings.h" // Contains settings for every component
20#include "src/Modules/AeroModule_NoTank.h"
21#include "src/WirelessCommands_Aero.h" // Structs for wireless communication via the nRF24L01 chip, defines the messages exchanged with the main modul
22
23// Global variable initialization
24bool &Debug = *new bool;
25bool &Metric = *new bool;
26char LongMessage[MaxLongTextLength] = ""; // Temp storage for assembling long messages (REST API - Google Sheets reporting)
27char ShortMessage[MaxShotTextLength] = ""; // Temp storage for assembling short messages (Log entries, Error messages)
28char CurrentTime[MaxWordLength] = ""; // Buffer for storing current time in text format
29void *ReceivedMessage = malloc(WirelessPayloadSize); // Stores a pointer to the latest received data. A void pointer is a pointer that has no associated data type with it. A void pointer can hold address of any type and can be typcasted to any type. Malloc allocates a fixed size memory section and returns the address of it.
30uint32_t ReceivedMessageTimestamp = millis(); // Stores the timestamp when the last wireless package was received
31
33HardwareSerial &ArduinoSerial = Serial;
35AeroModule *AeroMod1;
36RF24 Wireless(WirelessCEPin, WirelessCSNPin);
37
39Thread TimeCriticalThread = Thread();
40Thread OneSecThread = Thread();
41Thread FiveSecThread = Thread();
42Thread MinuteThread = Thread();
43StaticThreadController<4> ThreadControl(&TimeCriticalThread, &OneSecThread, &FiveSecThread, &MinuteThread);
44
45void setup()
46{
47 ArduinoSerial.begin(115200);
48 pinMode(13, OUTPUT);
49 printf_begin();
50 logToSerials(F(""), true, 0);
51 logToSerials(F("Aeroponics module initializing"), true, 0);
52 wdt_enable(WDTO_8S);
53 boot_rww_enable();
54 struct AeroModuleCommand BlankCommand = {AeroMessages::AeroModuleCommand1};
55 memcpy(ReceivedMessage, &BlankCommand, sizeof(struct AeroModuleCommand));
56 setSyncProvider(updateTime);
57 setSyncInterval(3600); //Sync time every hour with the main module
58
63
66
68 TimeCriticalThread.setInterval(100);
70 OneSecThread.setInterval(1000);
71 OneSecThread.onRun(run1sec);
72 FiveSecThread.setInterval(5000);
74 MinuteThread.setInterval(60000);
75 MinuteThread.onRun(run1min);
76
78 AeroMod1 = new AeroModule(F("Aero1"), &ModuleSettings->Aero1);
79
80 logToSerials(F("Setup ready, starting loops:"), true, 0);
81}
82
84{
85 logToSerials(F("(re)Initializing wireless transceiver"), false, 0);
86 pinMode(WirelessCSNPin, OUTPUT);
87 digitalWrite(WirelessCSNPin, HIGH);
88 pinMode(WirelessCEPin, OUTPUT);
89 digitalWrite(WirelessCEPin, HIGH);
90 Wireless.begin();
91 Wireless.powerDown();
92 Wireless.setDataRate(RF24_250KBPS);
93 Wireless.setCRCLength(RF24_CRC_16);
94 Wireless.setPALevel(RF24_PA_MAX);
95 Wireless.setPayloadSize(WirelessPayloadSize);
96 Wireless.enableDynamicPayloads();
97 Wireless.enableAckPayload();
98 Wireless.openReadingPipe(1, WirelessChannel);
99 Wireless.startListening();
100 Wireless.powerUp();
101 Wireless.flush_tx();
102 Wireless.flush_rx();
103 logToSerials(F("done"), true, 3);
104 ReceivedMessageTimestamp = millis();
105}
106
107void loop()
108{
109 ThreadControl.run();
112}
113
115
117{
118 AeroMod1->processTimeCriticalStuff();
119}
120
122{
123 wdt_reset();
124 heartBeat();
125 AeroMod1->run1sec();
126}
127
129{
130 wdt_reset();
131 AeroMod1->run5sec();
132}
133
135{
136 wdt_reset();
137 AeroMod1->run1min();
139}
140
142{
143 static bool ledStatus;
144 ledStatus = !ledStatus;
145 digitalWrite(LED_BUILTIN, ledStatus);
146}
147
149
151{
152 if (Wireless.available())
153 {
155 if (timeStatus() != timeSet && ((AeroCommonTemplate *)ReceivedMessage)->SequenceID == AeroMessages::AeroModuleCommand1)
156 {
157 updateTime();
158 }
159 if (AeroMod1->processCommand(ReceivedMessage))
160 {
161 ReceivedMessageTimestamp = millis(); //< Reset the timer after the last message was exchanged
162 }
163 }
165 {
167 }
168}
169
171{
172 if (Debug)
173 {
174 logToSerials(F("Wireless report:"), true, 0);
175 Wireless.printPrettyDetails();
176 logToSerials(F(""), true, 0);
177 }
178}
179
181{
182 time_t ReceivedTime = ((AeroModuleCommand *)ReceivedMessage)->Time;
183 if (ReceivedTime > 0)
184 {
185 setTime(ReceivedTime);
186 logToSerials(F("Clock synced"), true, 0);
187 }
188 else
189 {
190 logToSerials(F("Clock out of sync"), true, 0);
191 }
192 return ReceivedTime;
193}
void logToSerials(const __FlashStringHelper *ToPrint, bool BreakLine, uint8_t Indent)
< Logging
Definition SerialLog.cpp:5
Settings * loadSettings(bool ResetEEPROM)
Load settings from EEPROM.
Definition Settings.cpp:20
void getWirelessData()
Thread TimeCriticalThread
RF24 Wireless(WirelessCEPin, WirelessCSNPin)
Initialize the NRF24L01 wireless chip (CE, CSN pins are hard wired on the Arduino Nano RF)
char CurrentTime[MaxWordLength]
Buffer for storing current time in text format.
char LongMessage[MaxLongTextLength]
Temp storage for assembling long messages (REST API, MQTT reporting)
Thread MinuteThread
void processTimeCriticalStuff()
uint32_t ReceivedMessageTimestamp
Thread FiveSecThread
char ShortMessage[MaxShotTextLength]
Temp storage for assembling short text messages (Log entries, Error messages,etc)
time_t updateTime()
Thread OneSecThread
void InitializeWireless()
Settings * ModuleSettings
settings loaded from the EEPROM. Persistent between reboots, defaults are in Settings....
AeroModule * AeroMod1
Represents a Aeroponics tote with solenoid,pressure pump..etc.
HardwareSerial & ArduinoSerial
< Component initialization
void heartBeat()
Wireless communication.
void * ReceivedMessage
void getWirelessStatus()
void loop()
Threads.
constexpr uint8_t WirelessPayloadSize
Size of the wireless packages exchanged with the Main module. Max 32 bytes are supported on nRF24L01+...
Definition Settings.h:31
constexpr uint8_t WirelessCEPin
nRF24l01+ wireless transmitter CE pin - Pre-connected on RF-Nano
Definition Settings.h:29
constexpr uint8_t MaxShotTextLength
Default char * buffer length for storing mutiple words. Memory intense!
Definition Settings.h:18
constexpr uint8_t MaxWordLength
Default char * buffer length for storing a word + null terminator. Memory intense!
Definition Settings.h:17
constexpr uint16_t MaxLongTextLength
Default char * buffer length for storing a long text. Memory intense!
Definition Settings.h:19
constexpr uint16_t WirelessReceiveTimeout
(ms) If no packages are received from the Main module over this limit, try reseting the nRF24L01+ wir...
Definition Settings.h:33
constexpr uint8_t WirelessCSNPin
< nRF24L01+ wireless receiver
Definition Settings.h:28
constexpr uint8_t WirelessChannel[6]
This needs to be unique and match with the Name of the HempyModule_Web object in the MainModule_Web....
Definition Settings.h:30
struct AeroponicsModuleSettings Aero1
Definition Settings.h:54
bool Metric
Switch between Imperial/Metric units. If changed update the default temp and pressure values below to...
Definition Settings.h:61
bool Debug
Logs debug messages to serial and web outputs.
Definition Settings.h:60