CanGrow/Arduino/CanGrow/CanGrow.ino

163 lines
4.1 KiB
Arduino
Raw Normal View History

2024-03-30 03:04:48 +01:00
/*
*
* 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.
*
2024-04-13 02:38:39 +02:00
*/
2024-10-16 21:48:49 +02:00
/*
* Libraries include
*/
2024-10-19 22:52:19 +02:00
#include "Arduino.h"
// * ESP8266 *
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#endif
// * ESP32 *
#ifdef ESP32
2024-10-19 22:52:19 +02:00
#include <WiFi.h>
#include <AsyncTCP.h>
#endif
2024-10-19 22:52:19 +02:00
// 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>
2024-10-19 22:52:19 +02:00
// https://github.com/bblanchon/ArduinoJson
#include <ArduinoJson.h>
// https://github.com/PaulStoffregen/Time
#include <TimeLib.h>
2024-10-16 21:48:49 +02:00
/*
* 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"
2024-10-19 22:52:19 +02:00
#include "include/CanGrow_Webserver.h"
2024-10-16 21:48:49 +02:00
void setup() {
2024-10-17 02:15:15 +02:00
// define output for onboard LED/WIPE pin
pinMode(PinWIPE, OUTPUT);
2024-10-17 01:10:01 +02:00
// 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");
2024-10-19 01:29:45 +02:00
Serial.printf(".:: CanGrow firmware v%s build %s starting ::.\n", CANGROW_VER, CANGROW_BUILD);
2024-10-20 01:44:55 +02:00
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");
2024-10-17 02:15:15 +02:00
2024-10-19 02:52:17 +02:00
// blink with the onboard LED on D4/GPIO2 (PinWIPE)
2024-10-17 02:15:15 +02:00
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);
2024-10-17 02:15:15 +02:00
// read status from PinWIPE to WIPE
// when PinWIPE is set to LOW, format LittleFS
if(digitalRead(PinWIPE) != PinWIPE_default) {
LFS_format();
2024-10-17 02:35:04 +02:00
Panic();
2024-10-17 02:15:15 +02:00
}
LFS_init();
2024-10-19 22:52:19 +02:00
LoadConfig();
if(strlen(configWifi.ssid) == 0) {
Serial.println(":: [SETUP] configWifi.ssid is empty, creating access point");
WifiAP();
2024-10-17 22:20:25 +02:00
} else {
Serial.printf(":: [SETUP] configWifi.ssid is set, connecting to ssid: %s\n", configWifi.ssid);
WifiConnect();
2024-10-17 22:20:25 +02:00
}
2024-10-17 01:42:34 +02:00
SetupWebserver();
2024-10-16 21:48:49 +02:00
}
bool alrdySaved = false;
2024-10-16 21:48:49 +02:00
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;
SaveConfig(true);
alrdySaved = true;
} else if( (digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == true) ) {
alrdySaved = true;
} else {
alrdySaved = false;
}
2024-10-16 21:48:49 +02:00
}