CanGrow/Arduino/CanGrow/CanGrow.ino

165 lines
4.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>
#endif
// * ESP32 *
#ifdef ESP32
#include <WiFi.h>
#include <AsyncTCP.h>
#endif
// https://github.com/mathieucarbou/ESPAsyncWebServer
#include <ESPAsyncWebServer.h>
// LittleFS filesystem
#include "FS.h"
// arduino-core for esp8266 and esp32
#include "LittleFS.h"
// https://github.com/arduino/ArduinoCore-avr/tree/master/libraries/SPI
#include <SPI.h>
// https://github.com/arduino/ArduinoCore-avr/tree/master/libraries/Wire
#include <Wire.h>
// https://github.com/bblanchon/ArduinoJson
#include <ArduinoJson.h>
// https://github.com/PaulStoffregen/Time
#include <TimeLib.h>
/*
* CanGrow platform specific includes
*/
#ifdef ESP8266
#include "include/CanGrow_ESP8266.h"
#endif
#ifdef ESP32
#include "include/CanGrow_ESP32.h"
#endif
/*
* CanGrow includes
*/
#include "include/CanGrow.h"
#include "include/CanGrow_Core.h"
#include "include/CanGrow_LittleFS.h"
#include "include/CanGrow_Webserver.h"
void setup() {
// 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");
Serial.printf(".:: CanGrow firmware v%s build %s starting ::.\n", CANGROW_VER, CANGROW_BUILD);
Serial.print("II To format / factory reset LittleFS, pull GPIO ");
Serial.print(PinWIPE);
Serial.print(" (PinWIPE) to ");
// we need to invert the default to tell that user the state for an action
Serial.print(1 - PinWIPE_default);
Serial.println(" - NOW! (2 seconds left) II");
// blink with the onboard LED on D4/GPIO2 (PinWIPE)
for(byte i = 0; i <= 6 ; i++) {
if(i % 2) {
digitalWrite(PinWIPE, LOW);
} else {
digitalWrite(PinWIPE, HIGH);
}
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();
Panic();
}
LFS_init();
LoadConfig();
if(strlen(configWifi.ssid) == 0) {
Serial.println(":: [SETUP] configWifi.ssid is empty, creating access point");
WifiAP();
} else {
Serial.printf(":: [SETUP] configWifi.ssid is set, connecting to ssid: %s\n", configWifi.ssid);
WifiConnect();
}
SetupWebserver();
}
bool alrdySaved = false;
void loop() {
if((digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == false)) {
Serial.println(":: [LOOP] PinWIPE is triggered, saving config.json and set configSystem.httpLogSerial to true");
configSystem.httpLogSerial = true;
// save config to littlefs as json
SaveConfig();
// only print json to serial
SaveConfig(true);
alrdySaved = true;
} else if( (digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == true) ) {
alrdySaved = true;
} else {
alrdySaved = false;
}
}