CanGrow/CanGrow.ino

216 lines
6.2 KiB
C++

/*
*
* CanGrow - an OpenSource growcontroller firmware (for cannabis)
*
*
* MIT License
*
* Copyright (c) 2024 DeltaLima
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
/*
* Libraries include
*/
#include "Arduino.h"
// * ESP8266 *
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#endif
// * ESP32 *
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#include <Update.h>
#include <HTTPClient.h>
#endif
#include <WiFiUdp.h>
// https://github.com/thijse/Arduino-Log/
#include <ArduinoLog.h>
// https://github.com/mathieucarbou/ESPAsyncWebServer
#include <ESPAsyncWebServer.h>
// LittleFS filesystem
#include "FS.h"
// arduino-core for esp8266 and esp32
#include "LittleFS.h"
//#include <SPI.h>
#include <Wire.h>
// https://github.com/bblanchon/ArduinoJson
#include <ArduinoJson.h>
#include "AsyncJson.h"
// https://github.com/PaulStoffregen/Time
#include <TimeLib.h>
// https://github.com/arduino-libraries/NTPClient/
#include <NTPClient.h>
// https://github.com/nusabot-iot/NusabotSimpleTimer/
#include <NusabotSimpleTimer.h>
// https://github.com/adafruit/RTClib/
#include "RTClib.h"
/*
* CanGrow includes
*/
/* main header file, where all variables, consts and structs get defined */
#include "include/CanGrow.h"
/* CanGrow platform specific includes */
#include "include/Architecture/ESP8266.h"
#include "include/Architecture/ESP32.h"
#include "include/Architecture/ESP32_LOLIN_S2_MINI.h"
#include "include/Architecture/ESP32_MAKERGO_C3_SUPERMINI.h"
/* CanGrow header with all functions
* order is important - I need to learn how to do it right, so order is not important */
#include "include/CanGrow_ConfigHelper.h"
#include "include/CanGrow_Sensor.h"
#include "include/CanGrow_Output.h"
#include "include/CanGrow_Core.h"
#include "include/CanGrow_Wifi.h"
#include "include/CanGrow_LittleFS.h"
#include "include/CanGrow_Control.h"
#include "include/CanGrow_Timer.h"
#include "include/CanGrow_Webserver.h"
void setup() {
/* Measure start up time */
unsigned long millisFinish;
// define output for onboard LED/WIPE pin
pinMode(PinWIPE, OUTPUT);
// Start Serial
Serial.begin(115200);
// Write a line before doing serious output, because before there is some garbage in serial
// whats get the cursor somewhere over the place
Serial.println("420");
// initiate ArduinoLog
Log.setPrefix(LogPrefix);
Log.begin(LOG_LEVEL_VERBOSE, &Serial);
// disable show loglevel, we do it in Prefix
Log.setShowLevel(false);
// set Log Location, to tell user at which part of the code we are
const char LogLoc[] = "[SETUP]";
//Serial.printf(".:: CanGrow firmware v%s build %s starting ::.\n", CANGROW_VER, CANGROW_BUILD);
Log.notice(F("CanGrow firmware v%s build %s starting ::" CR), CANGROW_VER, CANGROW_BUILD);
Log.warning(F("%s To format / factory reset LittleFS, pull GPIO %d (PinWIPE) to %d - NOW! (2 seconds left)" CR), LogLoc, PinWIPE, 1 - PinWIPE_default );
// blink with the onboard LED on D4/GPIO2 (PinWIPE)
for(byte i = 0; i <= 6 ; i++) {
if(i % 2) {
digitalWrite(PinWIPE, 1 - PinWIPE_default);
} else {
digitalWrite(PinWIPE, PinWIPE_default);
}
delay(333);
}
// set PinWIPE back to its default
digitalWrite(PinWIPE, PinWIPE_default);
// read status from PinWIPE to WIPE
// when PinWIPE is set to LOW, format LittleFS
if(digitalRead(PinWIPE) != PinWIPE_default) {
LFS_Format();
Restart();
}
/* for ESP32-C3 supermini board compatibility, we initiate I2C here and not at the beginning
* ESP32-C3 supermini board shares GPIO 8 Internal LED with I2C SDA */
/* I2C init*/
Wire.begin(Pin_I2C_SDA, Pin_I2C_SCL);
LFS_Init();
LoadConfig();
Wifi_Init();
Webserver_Init();
Log.notice(F("%s Usable Pins: %d" CR), LogLoc, GPIOindex_length);
// List all available pins
for(byte i = 1; i <= GPIOindex_length; i++) {
Log.notice(F("%s Pin Index: %d, GPIO: %d, Notes: %s" CR), LogLoc, i , GPIOindex[i].gpio, GPIO_Index_note_descr[GPIOindex[i].note]);
}
// time init
Time_Init();
TimeR_Init();
#ifdef ESP8266
/* set pwm frequency global for ESP8266.
* ESP32 pwm frequency setting is done withing CanGrow_Output / Init */
analogWriteFreq(config.system.pwmFreq);
#endif
Output_Init();
Sensor_Init();
Log.notice(F("%s Done. Startup took : %u ms" CR), LogLoc, millis());
}
bool alrdySaved = false;
void loop() {
const char LogLoc[] = "[LOOP]";
/* Execute main timer, runs Timer_1s, Timer_3s, Timer_5s by default */
timer.run();
// if global var doRestart is true, perform a restart
if(doRestart == true) {
/* wait 100ms after Restart got triggered. This should workaround some crash problems with AsyncWebserver stuff
* for example when updating the firmware by web upload */
Log.verbose(F("%s Restart got triggered. Waiting 100ms before doing it" CR), LogLoc);
timer.setTimeout(100, Restart);
//Restart();
}
// does ntp offset need an update?
if(updateNtpOffset) {
/* doing ntp offset update here, because when doing it in the webserver:system function
* where the new value gets entered, it sometimes crashed */
NTP_OffsetUpdate();
updateNtpOffset = false;
}
}