CanGrow Firmware WIP

This commit is contained in:
Marcus 2024-04-13 02:38:39 +02:00
parent 33192130ba
commit 0d97eacd76

View file

@ -21,11 +21,96 @@
* *
*/ */
// external Libraries
#include <SPI.h> #include <SPI.h>
#include <Wire.h> #include <Wire.h>
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
#include "DHT.h" #include "DHT.h"
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>
#include <EEPROM.h>
/*
*
* Variables
*
*/
// When D4 is HIGH at start, WIPE is true and EEPROM get cleared
bool WIPE;
/*
* EEPROM variables
*/
bool configured;
char WIFIssid[32];
char WIFIpassword[64];
/*
*
* Constants
*
*/
/*
* WiFi
*/
const char *APssid = "CanGrow-unconfigured";
const char *APpass = "CanGrow";
IPAddress ip(192,168,4,20);
IPAddress netmask(255,255,255,0);
IPAddress gateway(192,168,4,254);
/*
*
* Webserver
*
*/
ESP8266WebServer webserver(80);
/*
* HTML pages
*/
// Template: const char HTMLexamplepage[] PROGMEM = R"EOF()EOF";
const char HTMLheader[] PROGMEM = R"EOF(
<html>
<body>
)EOF";
const char HTMLfooter[] PROGMEM = R"EOF(
</body>
</html>
)EOF";
/*
* Pin assignments
*
*/
// D0 is HIGH at boot, no PWM
const uint8_t PINfan = D0;
// If D3 is pulled to LOW, boot fails
const uint8_t PINdht = D3;
// D4 is HIGH at boot, boot fail if pulled to LOW
// PIN_WIPE set to HIGH at start erases the EEPROM saved data
const uint8_t PIN_WIPE = D4;
const uint8_t PINpump = D5;
const uint8_t PINled = D6; //
const uint8_t PINwaterlevel = D7;
const uint8_t PINsoilmoisture = D8;
const uint8_t PINanalog = A0;
/* /*
* millis timer * millis timer
@ -40,20 +125,7 @@ unsigned long outputPrevTime = 0;
*/ */
int D6status = false; int D6status = false;
/*
* Pin assignments
*
*/
// D0 is HIGH at boot, no PWM
uint8_t PINfan = D0;
// If D3 is pulled to LOW, boot fails
uint8_t PINdht = D3;
// D4 is HIGH at boot, boot fail if pulled to LOW
uint8_t PINpump = D5;
uint8_t PINled = D6; //
uint8_t PINwaterlevel = D7;
uint8_t PINsoilmoisture = D8;
uint8_t PINanalog = A0;
/* I2C Stuff /* I2C Stuff
* *
@ -61,14 +133,15 @@ uint8_t PINanalog = A0;
#define WIRE Wire #define WIRE Wire
/* DHT Stuff /*
* DHT Stuff
* *
*/ */
#define DHTTYPE DHT11 #define DHTTYPE DHT11
DHT dht(PINdht, DHTTYPE); DHT dht(PINdht, DHTTYPE);
/* Display Stuff /*
* * Display Stuff
*/ */
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &WIRE); Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &WIRE);
// 'CanGrow_Logo', 128x32px // 'CanGrow_Logo', 128x32px
@ -115,10 +188,16 @@ const unsigned char* bmpallArray[1] = {
/* /*
*
*
* Functions * Functions
* *
*
*/ */
/*
* Chirp functions
*/
void writeI2CRegister8bit(int addr, int value) { void writeI2CRegister8bit(int addr, int value) {
Wire.beginTransmission(addr); Wire.beginTransmission(addr);
Wire.write(value); Wire.write(value);
@ -136,6 +215,12 @@ unsigned int readI2CRegister16bit(int addr, int reg) {
return t; return t;
} }
/*
*
* Sensor functions
*
*/
int getWaterlevel() { int getWaterlevel() {
/* /*
@ -160,6 +245,7 @@ int getWaterlevel() {
// enable Vcc for water level sensor // enable Vcc for water level sensor
digitalWrite(PINwaterlevel, HIGH); digitalWrite(PINwaterlevel, HIGH);
// wait a bit to let the circuit stabilize // wait a bit to let the circuit stabilize
// TODO: replace delay() with millis()
delay(100); delay(100);
// get the value // get the value
waterlevelRAW = analogRead(PINanalog); waterlevelRAW = analogRead(PINanalog);
@ -250,6 +336,27 @@ int getLightchirp() {
} }
bool loadEEPROM() {
// read var configured from Byte 0
EEPROM.get(0, configured);
// read var WIFIssid, 32 byte long
EEPROM.get(1, WIFIssid);
// read var WIFIpassword, 64 byte long
EEPROM.get(33, WIFIpassword);
// read var ip, 16 byte long
EEPROM.get(113, ip);
// read var netmask, 16 byte long
EEPROM.get(129, netmask);
// read var gateway, 16 byte long
EEPROM.get(145, gateway);
Serial.print("EEPROM loaded, CanGrow configured is: ");
Serial.println(configured);
return(configured);
}
/* /*
* Setup * Setup
* *
@ -262,6 +369,7 @@ void setup() {
pinMode(PINsoilmoisture, OUTPUT); pinMode(PINsoilmoisture, OUTPUT);
pinMode(PINled, OUTPUT); pinMode(PINled, OUTPUT);
pinMode(PINpump, OUTPUT); pinMode(PINpump, OUTPUT);
pinMode(PIN_WIPE, INPUT);
// set all OUTPUT to low // set all OUTPUT to low
digitalWrite(PINfan, LOW); digitalWrite(PINfan, LOW);
@ -269,24 +377,21 @@ void setup() {
digitalWrite(PINsoilmoisture, LOW); digitalWrite(PINsoilmoisture, LOW);
digitalWrite(PINled, LOW); digitalWrite(PINled, LOW);
digitalWrite(PINpump, LOW); digitalWrite(PINpump, LOW);
// read status from PIN_WIPE to WIPE
WIPE = digitalRead(PIN_WIPE);
// Start Serial
Serial.begin(115200);
// Start EEPROM
EEPROM.begin(512);
// initialise Wire for I2C // initialise Wire for I2C
Wire.begin(); Wire.begin();
// initialise Serial output
Serial.begin(115200);
//Serial.println("Test123");
// reset chirp
writeI2CRegister8bit(0x20, 6); //reset
// initialise I2C display // initialise I2C display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32
// initialise DHT11
dht.begin();
display.clearDisplay(); display.clearDisplay();
display.display(); display.display();
@ -297,20 +402,114 @@ void setup() {
// display Logo // display Logo
display.drawBitmap(0, 0, bmpCanGrow_Logo, 128, 32, WHITE); display.drawBitmap(0, 0, bmpCanGrow_Logo, 128, 32, WHITE);
display.display(); display.display();
delay(2500);
// clear display // reset chirp
display.clearDisplay(); writeI2CRegister8bit(0x20, 6); //TODO: Do only, when configured
display.display();
// initialise DHT11
dht.begin(); //TODO: Do only, when configured
WiFi.softAPConfig(ip, gateway, netmask);
WiFi.softAP(APssid);
Serial.print("AP IP address: ");
Serial.println(WiFi.softAPIP());
//
// Webserver handlers
//
webserver.on("/", HTTP_GET, webRoot);
// server.onNotFound(handleNotFound);
// webserver.on("/save", HTTP_POST, handleSave);
// webserver.on("/control", HTTP_GET, handleControl);
webserver.begin();
loadEEPROM();
Serial.print("WIPE is ");
Serial.println(WIPE);
delay(2000); //TODO: replace with millis()
} }
/* /*
*
*
* Loop * Loop
* *
*
*/ */
void loop() { void loop() {
//Serial.println("yolo");
webserver.handleClient();
}
/*
*
* Web pages
*
*/
void webRoot() {
String body = FPSTR(HTMLheader);
body += "<h1>configured!</h1>";
body += FPSTR(HTMLfooter);
webserver.send(200, "text/html", body);
}
void webRootAP() {
String body = FPSTR(HTMLheader);
body += "<h1>unconfigured!</h1>";
body += FPSTR(HTMLfooter);
webserver.send(200, "text/html", body);
}
/*
*
*
* PLAYGROUND / TRASH
*
*
*/
/*
unsigned long currentTime = millis(); unsigned long currentTime = millis();
int valSoilmoisture0 = getSoilmoisture(0); int valSoilmoisture0 = getSoilmoisture(0);
@ -344,9 +543,7 @@ void loop() {
} }
/* // OUTPUT
* Output
*/
if(currentTime - outputPrevTime >= 1000) { if(currentTime - outputPrevTime >= 1000) {
@ -394,7 +591,7 @@ void loop() {
Serial.println("Test"); Serial.println("Test");
outputPrevTime = currentTime; outputPrevTime = currentTime;
} */
/* if(D6status == true) { /* if(D6status == true) {
@ -428,4 +625,4 @@ void loop() {
*/ */
}