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_Hempy.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"
17#include "RF24.h" // https://github.com/maniacbug/RF24
18#include "SerialLog.h" // Logging messages to Serial
19#include "Settings.h"
20#include "src/Modules/HempyModule.h"
21#include "src/WirelessCommands_Hempy.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; // Placeholder reference, to be initialized in setup()
25bool &Metric = *new bool; // Placeholder reference, to be initialized in setup()
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
29uint8_t ReceivedMessage[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; // Reference to the Arduino Serial
34Settings *ModuleSettings; // settings loaded from the EEPROM. Persistent between reboots, defaults are in Settings.h
35HempyModule *HempyMod1; // Represents a Hempy bucket with weight sensors and pumps
36RF24 Wireless(WirelessCEPin, WirelessCSNPin); // Initialize the NRF24L01 wireless chip (CE, CSN pins are hard wired on the Arduino Nano RF)
37
39Thread OneSecThread = Thread();
40Thread FiveSecThread = Thread();
41Thread MinuteThread = Thread();
42StaticThreadController<3> ThreadControl(&OneSecThread, &FiveSecThread, &MinuteThread);
43
44void setup()
45{ // put your setup code here, to run once:
46 ArduinoSerial.begin(115200); // Nano console output
47 pinMode(LED_BUILTIN, OUTPUT);
48 printf_begin();
49 logToSerials(F(""), true, 0);
50 logToSerials(F("Hempy module initializing"), true, 0);
51 wdt_enable(WDTO_8S);
52 boot_rww_enable();
53 struct HempyModuleCommand BlankCommand = {HempyMessages::HempyModuleCommand1};
54 memcpy(ReceivedMessage, &BlankCommand, sizeof(struct HempyModuleCommand));
55 setSyncProvider(updateTime);
56 setSyncInterval(3600); // Sync time every hour with the main module
61 OneSecThread.setInterval(1000);
62 OneSecThread.onRun(run1sec);
63 FiveSecThread.setInterval(5000);
65 MinuteThread.setInterval(60000);
66 MinuteThread.onRun(run1min);
67 HempyMod1 = new HempyModule(F("Hempy1"), ModuleSettings->Hemp1);
68 getFreeMemory();
69 logToSerials(F("Setup ready, starting loops:"), true, 0);
70}
71
73{
74 if (Debug)
75 {
76 logToSerials(F("(re)Initializing wireless transceiver"), false, 0);
77 }
78 pinMode(WirelessCSNPin, OUTPUT);
79 digitalWrite(WirelessCSNPin, HIGH);
80 pinMode(WirelessCEPin, OUTPUT);
81 digitalWrite(WirelessCEPin, HIGH);
82 Wireless.begin();
83 Wireless.powerDown();
84 Wireless.setDataRate(RF24_250KBPS);
85 Wireless.setCRCLength(RF24_CRC_16);
86 Wireless.setPALevel(RF24_PA_MAX);
87 Wireless.setPayloadSize(WirelessPayloadSize);
88 Wireless.enableDynamicPayloads();
89 Wireless.enableAckPayload();
90 Wireless.openReadingPipe(1, WirelessChannel);
91 Wireless.startListening();
92 Wireless.powerUp();
93 Wireless.flush_tx();
94 Wireless.flush_rx();
95 if (Debug)
96 {
97 logToSerials(F("done"), true, 3);
98 }
99 ReceivedMessageTimestamp = millis();
100}
101
102void loop()
103{
104 ThreadControl.run();
106}
107
109
111{
112 wdt_reset();
113 heartBeat();
114 HempyMod1->run1sec();
115}
116
118{
119 wdt_reset();
120 HempyMod1->run5sec();
121}
122
124{
125 wdt_reset();
126 HempyMod1->run1min();
127 // getWirelessStatus(); ///< USES TOO MUCH MEMORY, causes crash
128}
129
131{
132 static bool ledStatus;
133 ledStatus = !ledStatus;
134 digitalWrite(LED_BUILTIN, ledStatus);
135}
136
138
140{
141 if (Wireless.available())
142 {
144 if (timeStatus() != timeSet && ((HempyCommonTemplate *)ReceivedMessage)->SequenceID == HempyMessages::HempyModuleCommand1)
145 {
146 updateTime();
147 }
148 if (HempyMod1->processCommand(ReceivedMessage))
149 {
150 ReceivedMessageTimestamp = millis(); //< Reset the timer after the last message was exchanged
151 }
152 }
154 {
156 }
157}
158
160{
161 if (Debug)
162 {
163 logToSerials(F("Wireless report:"), true, 0);
164 wdt_reset();
165 Wireless.printPrettyDetails();
166 logToSerials(F(""), true, 0);
167 }
168}
169
171{
172 time_t ReceivedTime = ((HempyModuleCommand *)ReceivedMessage)->Time;
173 if (ReceivedTime > 0)
174 {
175 setTime(ReceivedTime);
176 logToSerials(F("Clock synced"), true, 0);
177 }
178 return ReceivedTime;
179}
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
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
void run5sec()
void getWirelessData()
Thread OneSecThread
< Thread initialization
RF24 Wireless(WirelessCEPin, WirelessCSNPin)
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
uint32_t ReceivedMessageTimestamp
void setup()
Thread FiveSecThread
char ShortMessage[MaxShotTextLength]
Temp storage for assembling short text messages (Log entries, Error messages,etc)
HempyModule * HempyMod1
void run1min()
uint8_t ReceivedMessage[WirelessPayloadSize]
time_t updateTime()
void InitializeWireless()
Settings * ModuleSettings
HardwareSerial & ArduinoSerial
< Component initialization
bool & Debug
< communicate with SPI devices, with the Arduino as the master device
void heartBeat()
Wireless communication.
bool & Metric
void run1sec()
void getWirelessStatus()
void loop()
Threads.
struct HempyModuleSettings Hemp1
Definition Settings.h:52
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