CanGrow/Arduino/CanGrow/CanGrow.ino

163 lines
4.3 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 20:06:54 +02:00
/*
* CanGrow includes
*/
2024-04-07 23:56:17 +02:00
2024-10-16 21:48:49 +02:00
#include "include/CanGrow.h"
2024-10-17 02:35:04 +02:00
#include "include/CanGrow_Core.h"
2024-10-16 21:48:49 +02:00
/*
* Libraries
*/
// * ESP8266 *
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include "ESPAsyncTCP.h"
#endif
// * ESP32 *
#ifdef ESP32
#include <WiFi.h>
#include "AsyncTCP.h"
#endif
// 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/lacamera/ESPAsyncWebServer
#include <ESPAsyncWebServer.h>
// https://github.com/bblanchon/ArduinoJson
#include <ArduinoJson.h>
// https://github.com/PaulStoffregen/Time
#include <TimeLib.h>
#include "FS.h"
2024-10-17 01:42:34 +02:00
// arduino-core for esp8266 and esp32
2024-10-17 01:01:37 +02:00
#include "LittleFS.h"
2024-10-16 21:48:49 +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");
Serial.print(".:: CanGrow firmware v");
Serial.print(CanGrowVer);
Serial.print(" build ");
Serial.print(CanGrowBuild);
Serial.println(" starting ::.");
2024-10-17 02:15:15 +02:00
Serial.print(":: To format LittleFS, pull ");
Serial.print(PinWIPE);
Serial.println("(PinWIPE) to LOW - NOW! (2 seconds left) ::");
// blink with the onboard LED on D4 (PinWIPE)
for(byte i = 0; i <= 6 ; i++) {
if(i % 2) {
digitalWrite(PinWIPE, LOW);
} else {
digitalWrite(PinWIPE, HIGH);
}
delay(333);
}
#ifdef ESP8266
// set back to HIGH because thats the default on esp8266
2024-10-17 02:15:15 +02:00
digitalWrite(PinWIPE, HIGH);
#endif
#ifdef ESP32
// set back to HIGH because thats the default esp32
digitalWrite(PinWIPE, LOW);
#endif
2024-10-17 02:15:15 +02:00
// read status from PinWIPE to WIPE
// when PinWIPE is set to LOW, format LittleFS
#ifdef ESP8266
2024-10-17 02:15:15 +02:00
if(digitalRead(PinWIPE) == LOW) {
#endif
#ifdef ESP32
if(digitalRead(PinWIPE) == HIGH) {
#endif
2024-10-17 02:15:15 +02:00
Serial.println("!! formatting LittleFS !!");
LittleFS.format();
2024-10-17 02:35:04 +02:00
Panic();
2024-10-17 02:15:15 +02:00
}
2024-10-17 01:42:34 +02:00
Serial.println(":: initialise LittleFS ::");
if(LittleFS.begin()) {
2024-10-17 02:40:32 +02:00
Serial.println(":: LittleFS: successfully initialised");
2024-10-17 01:42:34 +02:00
} else {
2024-10-17 02:40:32 +02:00
Serial.println("!! LittleFS: failed initialising !!");
2024-10-17 01:42:34 +02:00
}
// read the configfile from LittleFS
File lfs_configfile = LittleFS.open(configfile, "r");
if(!lfs_configfile) {
Serial.println("!! LittleFS: config.json not found, creating it !!");
lfs_configfile = LittleFS.open(configfile, "w");
if(lfs_configfile) {
2024-10-17 02:40:32 +02:00
Serial.println(":: LittleFS: config.json successfully created");
2024-10-17 01:42:34 +02:00
// write into the file
2024-10-17 02:35:04 +02:00
lfs_configfile.print("{test: }");
2024-10-17 01:42:34 +02:00
// close the file
lfs_configfile.close();
}
} else {
2024-10-17 02:40:32 +02:00
Serial.println(":: LittleFS: config.json successfully opened");
2024-10-17 01:42:34 +02:00
String configfile_content = "";
2024-10-17 02:15:15 +02:00
2024-10-17 01:42:34 +02:00
while (lfs_configfile.available()) {
configfile_content += (char)lfs_configfile.read();
}
Serial.println("-- LittleFS: config.json content --");
Serial.println(configfile_content);
Serial.println("-- LittleFS: config.json end --");
}
2024-10-16 21:48:49 +02:00
}
void loop() {
}