2024-03-30 03:04:48 +01:00
|
|
|
/*
|
|
|
|
*
|
2024-10-16 19:45:16 +02:00
|
|
|
* CanGrow - an OpenSource growcontroller firmware (for cannabis)
|
2024-04-09 13:45:33 +02:00
|
|
|
*
|
2024-05-01 01:29:28 +02:00
|
|
|
*
|
2024-10-16 19:45:16 +02:00
|
|
|
* MIT License
|
2024-05-09 20:30:25 +02:00
|
|
|
*
|
2024-10-16 19:45:16 +02:00
|
|
|
* Copyright (c) 2024 DeltaLima
|
2024-05-09 20:30:25 +02:00
|
|
|
*
|
2024-10-16 19:47:32 +02:00
|
|
|
* 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:
|
2024-05-09 20:30:25 +02:00
|
|
|
*
|
2024-10-16 19:47:32 +02:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2024-05-09 20:30:25 +02:00
|
|
|
*
|
2024-10-16 19:47:32 +02:00
|
|
|
* 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-28 22:59:26 +02:00
|
|
|
*
|
2024-04-13 02:38:39 +02:00
|
|
|
*/
|
2024-10-18 02:45:34 +02:00
|
|
|
|
2024-10-16 21:48:49 +02:00
|
|
|
|
2024-10-16 22:51:24 +02:00
|
|
|
/*
|
2024-10-17 20:38:46 +02:00
|
|
|
* Libraries include
|
2024-10-16 22:51:24 +02:00
|
|
|
*/
|
|
|
|
|
2024-10-19 22:52:19 +02:00
|
|
|
#include "Arduino.h"
|
|
|
|
|
2024-10-16 22:51:24 +02:00
|
|
|
// * ESP8266 *
|
|
|
|
#ifdef ESP8266
|
|
|
|
#include <ESP8266WiFi.h>
|
2024-10-20 01:29:57 +02:00
|
|
|
#include <ESPAsyncTCP.h>
|
2024-10-16 22:51:24 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// * ESP32 *
|
|
|
|
#ifdef ESP32
|
2024-10-19 22:52:19 +02:00
|
|
|
|
2024-10-16 22:51:24 +02:00
|
|
|
#include <WiFi.h>
|
2024-10-20 01:29:57 +02:00
|
|
|
#include <AsyncTCP.h>
|
2024-10-16 22:51:24 +02:00
|
|
|
#endif
|
|
|
|
|
2024-10-19 22:52:19 +02:00
|
|
|
// https://github.com/mathieucarbou/ESPAsyncWebServer
|
|
|
|
#include <ESPAsyncWebServer.h>
|
|
|
|
|
2024-10-20 01:29:57 +02:00
|
|
|
// LittleFS filesystem
|
|
|
|
#include "FS.h"
|
|
|
|
// arduino-core for esp8266 and esp32
|
|
|
|
#include "LittleFS.h"
|
|
|
|
|
2024-10-16 22:51:24 +02:00
|
|
|
// 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
|
|
|
|
2024-10-16 22:51:24 +02:00
|
|
|
// https://github.com/bblanchon/ArduinoJson
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
// https://github.com/PaulStoffregen/Time
|
|
|
|
#include <TimeLib.h>
|
2024-10-20 01:29:57 +02:00
|
|
|
|
2024-10-16 22:51:24 +02:00
|
|
|
|
2024-10-16 21:48:49 +02:00
|
|
|
|
2024-10-17 22:59:51 +02:00
|
|
|
/*
|
|
|
|
* CanGrow platform specific includes
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef ESP8266
|
|
|
|
#include "include/CanGrow_ESP8266.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ESP32
|
|
|
|
#include "include/CanGrow_ESP32.h"
|
|
|
|
#endif
|
|
|
|
|
2024-10-17 20:38:46 +02:00
|
|
|
/*
|
|
|
|
* 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-17 20:38:46 +02:00
|
|
|
|
2024-10-17 15:28:16 +02:00
|
|
|
|
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-17 22:59:51 +02:00
|
|
|
|
2024-10-20 01:44:55 +02:00
|
|
|
Serial.print("II To format / factory reset LittleFS, pull GPIO ");
|
2024-10-18 00:14:39 +02:00
|
|
|
Serial.print(PinWIPE);
|
|
|
|
Serial.print(" (PinWIPE) to ");
|
2024-10-19 18:28:14 +02:00
|
|
|
// we need to invert the default to tell that user the state for an action
|
|
|
|
Serial.print(1 - PinWIPE_default);
|
2024-10-18 00:14:39 +02:00
|
|
|
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);
|
|
|
|
}
|
2024-10-17 15:28:16 +02:00
|
|
|
|
2024-10-17 22:59:51 +02:00
|
|
|
// set PinWIPE back to its default
|
|
|
|
digitalWrite(PinWIPE, PinWIPE_default);
|
2024-10-17 15:28:16 +02:00
|
|
|
|
2024-10-17 02:15:15 +02:00
|
|
|
|
|
|
|
// read status from PinWIPE to WIPE
|
|
|
|
// when PinWIPE is set to LOW, format LittleFS
|
2024-10-17 22:59:51 +02:00
|
|
|
if(digitalRead(PinWIPE) != PinWIPE_default) {
|
2024-10-17 21:32:04 +02:00
|
|
|
LFS_format();
|
2024-10-17 02:35:04 +02:00
|
|
|
Panic();
|
2024-10-17 02:15:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-17 21:32:04 +02:00
|
|
|
LFS_init();
|
2024-10-19 22:52:19 +02:00
|
|
|
LoadConfig();
|
2024-10-19 18:28:14 +02:00
|
|
|
|
|
|
|
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 {
|
2024-10-19 18:28:14 +02:00
|
|
|
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
|
|
|
|
2024-10-20 00:41:23 +02:00
|
|
|
SetupWebserver();
|
|
|
|
|
2024-10-16 21:48:49 +02:00
|
|
|
}
|
|
|
|
|
2024-10-18 02:45:34 +02:00
|
|
|
bool alrdySaved = false;
|
|
|
|
|
2024-10-16 21:48:49 +02:00
|
|
|
void loop() {
|
2024-10-18 02:45:34 +02:00
|
|
|
if((digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == false)) {
|
2024-10-20 01:29:57 +02:00
|
|
|
Serial.println(":: [LOOP] PinWIPE is triggered, saving config.json and set configSystem.httpLogSerial to true");
|
|
|
|
configSystem.httpLogSerial = true;
|
2024-10-20 04:48:05 +02:00
|
|
|
// save config to littlefs as json
|
|
|
|
SaveConfig();
|
|
|
|
// only print json to serial
|
2024-10-20 01:29:57 +02:00
|
|
|
SaveConfig(true);
|
2024-10-18 02:45:34 +02:00
|
|
|
alrdySaved = true;
|
|
|
|
} else if( (digitalRead(PinWIPE) != PinWIPE_default) && (alrdySaved == true) ) {
|
|
|
|
alrdySaved = true;
|
|
|
|
} else {
|
|
|
|
alrdySaved = false;
|
|
|
|
}
|
2024-10-16 21:48:49 +02:00
|
|
|
}
|