CanGrow/Arduino/CanGrow/CanGrow.ino

828 lines
19 KiB
Arduino
Raw Normal View History

2024-03-30 03:04:48 +01:00
/*
* CanGrow - simply DIY automatic plant grow system (for cannabis).
*
* Pin assignment
* ==============
*
2024-03-30 03:38:31 +01:00
* D0 - MOSFET Fan
2024-03-30 03:04:48 +01:00
* D1, D2 - I2C
2024-03-30 03:38:31 +01:00
* D3 - DHT11
* D5 - MOSFET Pump
* D6 - MOSFET Grow LED, PWM
2024-04-01 23:47:11 +02:00
* D7 - waterlevel (set HIGH to read value)
2024-04-02 02:18:10 +02:00
* D8 - analog soil moisture (set HIGH to read value)
2024-04-01 23:47:11 +02:00
* A0 - analog input for soil moisture and waterlevel readings
2024-03-31 04:41:50 +02:00
*
2024-04-01 23:47:11 +02:00
* D4 and D7 cannot be HIGH at the same time!
2024-03-30 03:04:48 +01:00
*/
/*
* Includes
*
*/
2024-03-30 03:04:48 +01:00
2024-04-13 02:38:39 +02:00
// external Libraries
2024-03-30 01:25:58 +01:00
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
2024-03-30 03:04:48 +01:00
#include "DHT.h"
2024-04-13 02:38:39 +02:00
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <EEPROM.h>
#include <ArduinoJson.h>
2024-03-30 03:04:48 +01:00
/*
2024-04-13 02:38:39 +02:00
*
* Variables
*
*/
2024-04-13 03:24:47 +02:00
// When D4 is LOW at start, WIPE is true and EEPROM get cleared
// D4 hast to be shorted to GND withing the 2 sec delay in setup();
// DO NOT PULL D4 DOWN AT START/POWER ON !!! BOOT WILL FAIL
2024-04-13 02:38:39 +02:00
bool WIPE;
2024-04-13 17:53:47 +02:00
// valSoilmoisture - contains the value of getSoilmoisture()
byte valSoilmoisture;
// valTemperature - contains the value of getTemperature()
float valTemperature;
// valTemperature - contains the value of getHumidity()
float valHumidity;
// valWaterlevel - contains the value of getWaterlevel()
/*
2024-04-13 02:38:39 +02:00
* EEPROM variables
*/
2024-04-13 15:46:40 +02:00
2024-04-13 17:53:47 +02:00
// configured - if true, run setup assistant
2024-04-13 02:38:39 +02:00
bool configured;
char WIFIssid[32];
char WIFIpassword[64];
2024-04-13 17:53:47 +02:00
// GrowStart - contains unix timestamp from date where grow starts (00:00)
unsigned long GrowStart;
// DayOfGrow contains on which day the grow is
byte DayOfGrow;
// DaysVeg - contains how many days to be in vegetation phase
byte DaysVeg;
// DaysBloom - contains how many days to be in bloom phase
byte DaysBloom;
// LighthoursVeg - contains how many hours the Growlight is on in Veg
byte LighthoursVeg;
// LighthoursBloom - contains how many hours the Growlight is on in Bloom
byte LighthoursBloom;
// GrowName - contains the name of the grow/plant. Up to 32 byte
char GrowName[32];
// Sunrise - contains to which hour of day the growlight turns on
byte Sunrise;
// PINled_PWM - contains the PWM value for dimming the grow light
byte PINled_PWM;
// MoistureSensor_Type - contains which moisture sensor to use
// 0: analog capacitive sensor
// 1: I2C chirp sensor from catnip electronics
byte MoistureSensor_Type;
// UsePump - is the pump used? bool
bool UsePump;
// UseFan - is the fan used? bool
bool UseFan
// SoilmoistureLow - contains the value , when soil moisture is assumed to be low,
byte SoilmoistureLow;
2024-04-13 02:38:39 +02:00
/*
*
* Constants
*
*/
2024-04-13 02:38:39 +02:00
/*
* WiFi
*/
const char *APssid = "CanGrow-unconfigured";
const char *APpass = "CanGrow";
2024-04-13 03:24:47 +02:00
IPAddress WIFIip(192,168,4,20);
IPAddress WIFInetmask(255,255,255,0);
IPAddress WIFIgateway(192,168,4,254);
2024-04-13 02:38:39 +02:00
/*
*
* 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
*
*/
2024-04-13 02:38:39 +02:00
2024-04-01 23:47:11 +02:00
// D0 is HIGH at boot, no PWM
2024-04-13 02:38:39 +02:00
const uint8_t PINfan = D0;
2024-04-01 23:47:11 +02:00
// If D3 is pulled to LOW, boot fails
2024-04-13 02:38:39 +02:00
const uint8_t PINdht = D3;
2024-04-01 23:47:11 +02:00
// D4 is HIGH at boot, boot fail if pulled to LOW
2024-04-13 02:38:39 +02:00
// 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
*
*/
unsigned long outputPrevTime = 0;
/*
* Status vars
*
*/
int D6status = false;
2024-03-30 01:25:58 +01:00
/* I2C Stuff
*
*/
#define WIRE Wire
2024-04-07 03:55:17 +02:00
2024-04-13 02:38:39 +02:00
/*
* DHT Stuff
*
*/
2024-03-30 03:04:48 +01:00
#define DHTTYPE DHT11
DHT dht(PINdht, DHTTYPE);
2024-03-30 03:04:48 +01:00
2024-04-13 02:38:39 +02:00
/*
* Display Stuff
*/
2024-03-30 01:25:58 +01:00
Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &WIRE);
// 'CanGrow_Logo', 128x32px
const unsigned char bmpCanGrow_Logo [] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x0f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x07, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe0, 0x00, 0x1f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0e, 0x03, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe0, 0x00, 0x38, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x1c, 0x03, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x70, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x18, 0x03, 0x00, 0x00, 0x00, 0x04, 0x07, 0xe0, 0x20, 0x60, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x18, 0x03, 0x00, 0x00, 0x00, 0x06, 0x07, 0xe0, 0xe0, 0x60, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x18, 0x00, 0x00, 0x00, 0x00, 0x03, 0x87, 0xe1, 0xc0, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x30, 0x00, 0x3f, 0xc3, 0xff, 0x03, 0xc7, 0xe3, 0xc0, 0xcf, 0xf9, 0xff, 0xe3, 0xfc, 0xc1, 0x83,
0x30, 0x00, 0x7f, 0xe3, 0xff, 0x83, 0xe7, 0xe7, 0xc0, 0xcf, 0xfb, 0xff, 0xe7, 0xfe, 0xc3, 0x87,
0x30, 0x00, 0xe0, 0x73, 0x80, 0xc1, 0xf7, 0xef, 0xc0, 0xc0, 0x1b, 0x80, 0x0e, 0x03, 0xc3, 0x86,
0x30, 0x00, 0xc0, 0x33, 0x00, 0xc1, 0xff, 0xff, 0x80, 0xc0, 0x1b, 0x00, 0x0c, 0x03, 0xc7, 0x8e,
0x30, 0x01, 0xc0, 0x37, 0x00, 0xc0, 0xff, 0xff, 0x80, 0xc0, 0x3b, 0x00, 0x1c, 0x03, 0xc7, 0x8c,
0x60, 0x01, 0xc0, 0x37, 0x00, 0xc0, 0xff, 0xff, 0x01, 0x80, 0x3f, 0x00, 0x18, 0x03, 0xcf, 0x9c,
0x60, 0x00, 0x00, 0x37, 0x00, 0xc0, 0x7f, 0xfe, 0x01, 0x80, 0x37, 0x00, 0x18, 0x03, 0xcf, 0x9c,
0x60, 0x00, 0x00, 0x76, 0x01, 0xc0, 0x1f, 0xfc, 0x01, 0x80, 0x36, 0x00, 0x18, 0x06, 0xdf, 0xb8,
0x60, 0x00, 0x7f, 0xe6, 0x01, 0x9f, 0x9f, 0xfc, 0xf9, 0x80, 0x36, 0x00, 0x18, 0x06, 0xdd, 0xb8,
0x60, 0x00, 0xff, 0xe6, 0x01, 0x87, 0xff, 0xff, 0xf1, 0x80, 0x76, 0x00, 0x18, 0x06, 0xdd, 0xb0,
0xc0, 0x01, 0xc0, 0xee, 0x01, 0x83, 0xff, 0xff, 0xc3, 0x00, 0x7e, 0x00, 0x30, 0x06, 0xf9, 0xf0,
0xc0, 0x0b, 0x80, 0x6e, 0x01, 0x81, 0xff, 0xff, 0x83, 0x00, 0x6e, 0x00, 0x30, 0x06, 0xf9, 0xe0,
0xc0, 0x1b, 0x00, 0xec, 0x01, 0x80, 0x1f, 0xf8, 0x03, 0x00, 0x6c, 0x00, 0x30, 0x0e, 0xf1, 0xe0,
0xc0, 0x3b, 0x00, 0xcc, 0x03, 0x80, 0x3f, 0xfc, 0x03, 0x00, 0xec, 0x00, 0x30, 0x0c, 0xf1, 0xc0,
0xc0, 0x7b, 0x01, 0xcc, 0x03, 0x00, 0x7f, 0xfe, 0x03, 0x01, 0xec, 0x00, 0x30, 0x1c, 0xe1, 0xc0,
0x7f, 0xf1, 0xff, 0xdc, 0x03, 0x00, 0xf0, 0x8f, 0x01, 0xff, 0xfc, 0x00, 0x1f, 0xf8, 0xe1, 0xc0,
0x3f, 0xe0, 0xff, 0xcc, 0x03, 0x00, 0x00, 0x80, 0x00, 0xff, 0xcc, 0x00, 0x0f, 0xf0, 0xc1, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 528)
const int bmpallArray_LEN = 1;
const unsigned char* bmpallArray[1] = {
bmpCanGrow_Logo
};
/*
2024-04-13 02:38:39 +02:00
*
*
* Functions
*
2024-04-13 02:38:39 +02:00
*
*/
2024-03-30 01:25:58 +01:00
2024-04-13 02:38:39 +02:00
/*
* Chirp functions
*/
2024-03-30 01:25:58 +01:00
void writeI2CRegister8bit(int addr, int value) {
Wire.beginTransmission(addr);
Wire.write(value);
Wire.endTransmission();
}
unsigned int readI2CRegister16bit(int addr, int reg) {
Wire.beginTransmission(addr);
Wire.write(reg);
Wire.endTransmission();
delay(20);
Wire.requestFrom(addr, 2);
unsigned int t = Wire.read() << 8;
t = t | Wire.read();
return t;
}
2024-04-13 02:38:39 +02:00
/*
*
* Sensor functions
*
*/
2024-03-31 05:47:43 +02:00
int getWaterlevel() {
/*
* waterlevelRAW
* ===========
* 0 - 199 : CRITICAL
* 200 - 399 : WARNING
* >400 : OK
*
* waterlevel
* ==========
* 2 : CRITICAL
* 1 : WARNING
* 0 : OK
*/
int waterlevelWARN = 200;
int waterlevelOK = 400;
int waterlevelRAW = 0;
int waterlevel = 0;
// enable Vcc for water level sensor
2024-04-01 23:47:11 +02:00
digitalWrite(PINwaterlevel, HIGH);
// wait a bit to let the circuit stabilize
2024-04-13 02:38:39 +02:00
// TODO: replace delay() with millis()
2024-04-07 01:18:54 +02:00
delay(100);
2024-03-31 05:47:43 +02:00
// get the value
2024-04-01 23:47:11 +02:00
waterlevelRAW = analogRead(PINanalog);
2024-04-07 01:18:54 +02:00
// disable Vcc for the sensor to prevent electrolysis effect and release analog pin
2024-04-01 23:47:11 +02:00
digitalWrite(PINwaterlevel, LOW);
2024-03-31 05:47:43 +02:00
if( waterlevelRAW >= waterlevelOK) {
waterlevel = 0;
} else if( waterlevelRAW >= waterlevelWARN) {
waterlevel = 1;
} else {
waterlevel = 2;
}
return waterlevel;
}
float getTemperature(bool tempSensor) {
/*
* tempSensor
* ==========
* 0/false : DHT11 temp sensor
* 1/true : chirp I2C temp sensor
*/
float temperature = 0;
if(tempSensor == false ) {
// read temperature from DHT11
temperature = dht.readTemperature();
} else {
// read temperature from chrip I2C
temperature = readI2CRegister16bit(0x20, 5) * 0.10 ;
}
return temperature;
}
float getHumidity() {
float humidity = dht.readHumidity();
return humidity;
}
2024-04-07 01:18:54 +02:00
int getSoilmoisture(bool moistureSensor) {
2024-04-07 02:10:45 +02:00
/*
* moistureSensor
* ==============
* 0/false : analog capacitive moisture sensor
* 1/true : chirp I2C moisture sensor
*/
2024-04-07 01:56:33 +02:00
// value to return
2024-04-07 01:18:54 +02:00
int soilmoisture;
2024-04-07 01:56:33 +02:00
// value for wet
int wet;
// value for dry
int dry;
2024-04-07 01:18:54 +02:00
if(moistureSensor == false ) {
// read analog value from analog moisture sensor
2024-04-07 02:10:45 +02:00
wet = 180;
2024-04-07 01:56:33 +02:00
dry= 590;
2024-04-07 01:18:54 +02:00
digitalWrite(PINsoilmoisture, HIGH);
// wait a bit to let the circuit stabilize
2024-04-07 01:20:30 +02:00
delay(100);
2024-04-07 01:56:33 +02:00
// get analog input value
2024-04-07 01:18:54 +02:00
soilmoisture = analogRead(PINanalog);
// disable Vcc for the sensor to release analog pin
digitalWrite(PINsoilmoisture, LOW);
} else {
// read soil moisture from chrip I2C
2024-04-07 02:10:45 +02:00
wet = 560;
2024-04-07 01:56:33 +02:00
dry= 250;
2024-04-07 02:10:45 +02:00
2024-04-07 01:56:33 +02:00
// get raw value from I2C chirp sensor
2024-04-07 01:18:54 +02:00
soilmoisture = readI2CRegister16bit(0x20, 0);
}
2024-04-07 01:56:33 +02:00
return map(soilmoisture, wet, dry, 100, 0);
2024-03-31 05:47:43 +02:00
}
2024-03-31 05:53:57 +02:00
int getLightchirp() {
// get the "light value" from I2C chirp module
writeI2CRegister8bit(0x20, 3); //request light measurement
int lightchirp = readI2CRegister16bit(0x20, 4);
return lightchirp;
}
2024-04-13 04:27:40 +02:00
2024-04-13 03:24:47 +02:00
void wipeEEPROM() {
// write a 0 to all 512 bytes of the EEPROM
Serial.print("wiping EEPROM... ");
for (int i = 0; i < 512; i++) { EEPROM.write(i, 0); }
2024-04-13 15:46:40 +02:00
// commit everything to EEPROM and end here
EEPROM.end();
Serial.println("DONE");
2024-04-13 15:46:40 +02:00
// set D4 PIN_WIPE internal LED to Output to give feedback WIPE
// was done
pinMode(PIN_WIPE, OUTPUT);
2024-04-13 15:46:40 +02:00
Serial.println("!! Device will restart in 5 seconds !!");
byte i = 0;
while( i <= 10 ) {
if( i % 2 ) {
digitalWrite(PIN_WIPE, LOW);
} else {
digitalWrite(PIN_WIPE, HIGH);
}
delay(500);
i++;
}
ESP.restart();
2024-04-13 03:24:47 +02:00
}
2024-04-13 04:27:40 +02:00
2024-04-13 02:38:39 +02:00
bool loadEEPROM() {
2024-04-13 04:27:40 +02:00
2024-04-13 15:46:40 +02:00
/*
* WIFI data
*/
// read var WIFIssid from address 0, 32 byte long
2024-04-13 04:27:40 +02:00
EEPROM.get(0, WIFIssid);
2024-04-13 15:46:40 +02:00
// read var WIFIpassword from address 32, 64 byte long
2024-04-13 04:27:40 +02:00
EEPROM.get(32, WIFIpassword);
2024-04-13 15:46:40 +02:00
// read var WIFIip from address 96, 16 byte long
EEPROM.get(96, WIFIip);
// read var WIFInetmask from address 112, 16 byte long
EEPROM.get(112, WIFInetmask);
// read var WIFIgateway from address 128, 16 byte long
EEPROM.get(128, WIFIgateway);
2024-04-13 04:27:40 +02:00
/*
2024-04-13 15:46:40 +02:00
* Grow data
*/
2024-04-13 15:46:40 +02:00
//TBD
2024-04-13 15:46:40 +02:00
/*
* configured
*
* read var configured from address 511 - I put this to the end to
* prevent confusion with the 1 byte offset in the address when it
* would be at the beginning - more a cosmetic thing
*/
EEPROM.get(511, configured);
// print values to Serial output
Serial.println(":: EEPROM loaded ::");
2024-04-13 04:27:40 +02:00
Serial.print("WIFIssid: ");
Serial.println(WIFIssid);
Serial.print("WIFIpassword: ");
Serial.println(WIFIpassword);
2024-04-13 15:46:40 +02:00
Serial.print("configured: ");
Serial.println(configured);
2024-04-13 02:38:39 +02:00
2024-04-13 15:46:40 +02:00
return(configured);
2024-04-13 02:38:39 +02:00
}
/*
* Setup
*
*/
2024-03-30 01:25:58 +01:00
void setup() {
// Start EEPROM
EEPROM.begin(512);
2024-03-31 04:41:50 +02:00
// setup pins
pinMode(PINfan, OUTPUT);
pinMode(PINdht, INPUT);
2024-04-01 23:47:11 +02:00
pinMode(PINwaterlevel, OUTPUT);
2024-04-07 01:18:54 +02:00
pinMode(PINsoilmoisture, OUTPUT);
2024-03-31 04:41:50 +02:00
pinMode(PINled, OUTPUT);
pinMode(PINpump, OUTPUT);
2024-04-13 02:38:39 +02:00
pinMode(PIN_WIPE, INPUT);
2024-03-31 04:41:50 +02:00
// set all OUTPUT to low
digitalWrite(PINfan, LOW);
2024-04-01 23:47:11 +02:00
digitalWrite(PINwaterlevel, LOW);
2024-04-07 01:18:54 +02:00
digitalWrite(PINsoilmoisture, LOW);
2024-03-31 04:41:50 +02:00
digitalWrite(PINled, LOW);
digitalWrite(PINpump, LOW);
2024-04-13 02:38:39 +02:00
// Start Serial
Serial.begin(115200);
2024-04-13 15:46:40 +02:00
// Write an empty line, because before there is some garbage in serial
// output
Serial.println("");
Serial.println(".:: CanGrow Start ::.");
2024-04-13 02:38:39 +02:00
2024-03-31 04:41:50 +02:00
// initialise Wire for I2C
2024-03-30 01:25:58 +01:00
Wire.begin();
2024-03-31 04:41:50 +02:00
// initialise I2C display
2024-03-30 01:25:58 +01:00
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32
display.clearDisplay();
display.display();
2024-03-31 04:41:50 +02:00
// set display settings
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
// display Logo
2024-03-30 01:25:58 +01:00
display.drawBitmap(0, 0, bmpCanGrow_Logo, 128, 32, WHITE);
display.display();
2024-04-13 02:38:39 +02:00
// reset chirp
writeI2CRegister8bit(0x20, 6); //TODO: Do only, when configured
2024-03-30 03:04:48 +01:00
2024-04-13 02:38:39 +02:00
// initialise DHT11
dht.begin(); //TODO: Do only, when configured
2024-04-13 03:24:47 +02:00
WiFi.softAPConfig(WIFIip, WIFIgateway, WIFInetmask);
2024-04-13 02:38:39 +02:00
WiFi.softAP(APssid);
Serial.print("AP IP address: ");
Serial.println(WiFi.softAPIP());
2024-04-13 03:24:47 +02:00
// wait a few seconds to let the user pull D4 down to wipe EEPROM
// and we can enjoy the boot screen meanwhile :p
delay(2000);
2024-04-13 15:46:40 +02:00
2024-04-13 03:24:47 +02:00
// read status from PIN_WIPE to WIPE
WIPE = digitalRead(PIN_WIPE);
2024-04-13 15:46:40 +02:00
// when PIN_WIPE is set to LOW, wipe EEPROM
2024-04-13 03:24:47 +02:00
if( WIPE == 0 ) {
2024-04-13 15:46:40 +02:00
Serial.print("PIN_WIPE (D4) is set to LOW, wiping EEPROM now..");
// wipe EEPROM
2024-04-13 03:24:47 +02:00
wipeEEPROM();
2024-04-13 15:46:40 +02:00
} else {
// load stored values from EEPROM
loadEEPROM();
2024-04-13 03:24:47 +02:00
}
2024-04-13 04:27:40 +02:00
2024-04-13 15:46:40 +02:00
/*
* Webserver handlers
*/
2024-04-13 03:24:47 +02:00
// when not configured, webroot is WEBrootAP
// nothing else configured
2024-04-13 04:27:40 +02:00
if(configured == 0) {
2024-04-13 03:24:47 +02:00
webserver.on("/", HTTP_GET, WEBrootAP);
2024-04-13 04:27:40 +02:00
} else {
webserver.on("/", HTTP_GET, WEBroot);
2024-04-13 03:24:47 +02:00
}
// generic handler
webserver.on("/wifiConfig/save", HTTP_POST, POSTwifiConfig);
// 404 handling
//webserver.onNotFound(handleNotFound);
/*
webserver.on("/", HTTP_GET, WEBroot);
webserver.on("/control", HTTP_GET, handleControl);
*/
2024-04-13 02:38:39 +02:00
webserver.begin();
2024-04-13 03:24:47 +02:00
2024-03-30 01:25:58 +01:00
}
/*
2024-04-13 02:38:39 +02:00
*
*
* Loop
*
2024-04-13 02:38:39 +02:00
*
*/
2024-03-30 01:25:58 +01:00
void loop() {
2024-04-13 02:38:39 +02:00
//Serial.println("yolo");
webserver.handleClient();
}
/*
*
* Web pages
*
*/
2024-04-13 03:24:47 +02:00
void WEBroot() {
2024-04-13 02:38:39 +02:00
String body = FPSTR(HTMLheader);
body += "<h1>configured!</h1>";
body += FPSTR(HTMLfooter);
webserver.send(200, "text/html", body);
}
2024-04-13 03:24:47 +02:00
void WEBrootAP() {
2024-04-13 02:38:39 +02:00
String body = FPSTR(HTMLheader);
body += "<h1>unconfigured!</h1>";
2024-04-13 03:24:47 +02:00
body += "<h1>WiFi config</h1>";
body += "<form method='post' action='/wifiConfig/save'>";
body += "SSID: <input type='text' name='WIFIssid'><br>";
body += "Password: <input type='password' name='WIFIpassword'><br>";
body += "IP: <input type='text' name='WIFIip'><br>";
body += "Subnet mask: <input type='text' name='WIFInetmask'><br>";
body += "gateway: <input type='text' name='WIFIgateway'><br>";
body += "<input type='submit' value='Save'>";
body += "</form>";
2024-04-13 02:38:39 +02:00
body += FPSTR(HTMLfooter);
webserver.send(200, "text/html", body);
}
2024-04-13 03:24:47 +02:00
/*
*
* POSTs
*
*/
void POSTwifiConfig() {
String WIFIssid_new = webserver.arg("WIFIssid");
String WIFIpassword_new = webserver.arg("WIFIpassword");
String WIFIip_new = webserver.arg("WIFIip");
String WIFInetmask_new = webserver.arg("WIFInetmask");
String WIFIgateway_new = webserver.arg("WIFIgateway");
WIFIssid_new.toCharArray(WIFIssid, 32);
WIFIpassword_new.toCharArray(WIFIpassword, 64);
WIFIip.fromString(WIFIip_new);
WIFInetmask.fromString(WIFInetmask_new);
WIFIgateway.fromString(WIFIgateway_new);
2024-04-13 15:46:40 +02:00
configured = true;
EEPROM.put(0, WIFIssid);
EEPROM.put(32, WIFIpassword);
2024-04-13 15:46:40 +02:00
EEPROM.put(96, WIFIip);
EEPROM.put(112, WIFInetmask);
EEPROM.put(128, WIFIgateway);
EEPROM.put(511, configured);
2024-04-13 03:24:47 +02:00
EEPROM.commit();
2024-04-13 04:27:40 +02:00
Serial.println(":: POSTwifiConfig ::");
Serial.print("WIFIssid: ");
Serial.println(WIFIssid_new);
Serial.println(WIFIssid);
2024-04-13 04:27:40 +02:00
Serial.print("WIFIpassword: ");
Serial.println(WIFIpassword_new);
Serial.println(WIFIpassword);
2024-04-13 04:27:40 +02:00
Serial.print("WIFIip: ");
Serial.println(WIFIip_new);
Serial.print("WIFInetmask: ");
Serial.println(WIFInetmask_new);
Serial.print("WIFIgateway: ");
Serial.println(WIFIgateway_new);
2024-04-13 15:46:40 +02:00
Serial.print("configured: ");
Serial.println(configured);
2024-04-13 03:24:47 +02:00
webserver.send(200, "text/html", "wifiConfig saved, please restart");
2024-04-13 04:27:40 +02:00
2024-04-13 03:24:47 +02:00
}
2024-04-13 02:38:39 +02:00
2024-04-13 02:38:39 +02:00
/*
*
*
* PLAYGROUND / TRASH
*
*
*/
/*
unsigned long currentTime = millis();
2024-04-07 23:56:17 +02:00
int valSoilmoisture0 = getSoilmoisture(0);
int valSoilmoisture1 = getSoilmoisture(1);
2024-04-08 01:28:15 +02:00
float valTemperature0 = getTemperature(0);
float valTemperature1 = getTemperature(1);
2024-04-07 23:56:17 +02:00
float valHumidity = getHumidity();
int valWaterlevel = getWaterlevel();
switch(valWaterlevel) {
case 0:
digitalWrite(PINled, HIGH);
digitalWrite(PINpump, LOW);
digitalWrite(PINfan, LOW);
break;
case 1:
digitalWrite(PINled, LOW);
digitalWrite(PINpump, HIGH);
digitalWrite(PINfan, LOW);
break;
case 2:
digitalWrite(PINled, LOW);
digitalWrite(PINpump, LOW);
digitalWrite(PINfan, HIGH);
break;
}
2024-04-13 02:38:39 +02:00
// OUTPUT
if(currentTime - outputPrevTime >= 1000) {
2024-03-31 05:53:57 +02:00
// set display cursor to top left
2024-03-30 03:04:48 +01:00
display.setCursor(0,0);
2024-03-31 05:53:57 +02:00
// display text
2024-03-30 03:04:48 +01:00
display.print("I2C: ");
2024-04-07 23:56:17 +02:00
display.print(valSoilmoisture1);
2024-03-30 03:04:48 +01:00
display.print(", ");
2024-04-07 23:56:17 +02:00
display.println(valTemperature1);
2024-03-31 05:53:57 +02:00
Serial.print("I2C: ");
2024-04-07 23:56:17 +02:00
Serial.print(valSoilmoisture1);
Serial.print(", ");
2024-04-07 23:56:17 +02:00
Serial.println(valTemperature1);
2024-03-30 01:25:58 +01:00
2024-03-30 03:04:48 +01:00
display.print("DHT11: ");
2024-04-07 23:56:17 +02:00
display.print(valTemperature0);
2024-03-30 03:04:48 +01:00
display.print(", ");
2024-04-07 23:56:17 +02:00
display.println(valHumidity);
Serial.print("DHT11: ");
2024-04-07 23:56:17 +02:00
Serial.print(valTemperature0);
Serial.print(", ");
2024-04-07 23:56:17 +02:00
Serial.println(valHumidity);
2024-03-30 01:25:58 +01:00
2024-03-31 04:41:50 +02:00
2024-03-31 05:53:57 +02:00
display.print("Water Status: ");
2024-04-07 23:56:17 +02:00
display.println(valWaterlevel);
2024-03-31 05:47:43 +02:00
Serial.print("Water Status: ");
2024-04-07 23:56:17 +02:00
Serial.println(valWaterlevel);
2024-03-31 04:41:50 +02:00
2024-04-07 01:18:54 +02:00
display.print("ASM: ");
2024-04-07 23:56:17 +02:00
display.print(valSoilmoisture0);
2024-04-07 02:10:45 +02:00
display.println(", ");
2024-04-07 01:18:54 +02:00
Serial.print("ASM: ");
2024-04-07 23:56:17 +02:00
Serial.println(valSoilmoisture0);
2024-04-07 01:18:54 +02:00
2024-03-31 05:53:57 +02:00
// print everything on the display
2024-03-31 04:41:50 +02:00
display.display();
Serial.println("Test");
2024-04-07 03:55:17 +02:00
outputPrevTime = currentTime;
2024-04-13 02:38:39 +02:00
*/
2024-04-07 23:56:17 +02:00
2024-04-07 23:56:17 +02:00
/* if(D6status == true) {
2024-04-07 03:55:17 +02:00
digitalWrite(PINled, LOW);
digitalWrite(PINpump, LOW);
digitalWrite(PINfan, LOW);
D6status = false;
Serial.println("D6 is off now");
} else {
digitalWrite(PINled, HIGH);
digitalWrite(PINpump, HIGH);
digitalWrite(PINfan, HIGH);
D6status = true;
Serial.println("D6 is ON now");
}
2024-04-07 23:56:17 +02:00
*/
2024-04-07 23:56:17 +02:00
/*
for(int dutyCycle = 0; dutyCycle < 255; dutyCycle++){
// changing the LED brightness with PWM
analogWrite(PINled, dutyCycle);
2024-04-07 23:56:17 +02:00
delay(1);
}
// decrease the LED brightness
for(int dutyCycle = 255; dutyCycle > 0; dutyCycle--){
// changing the LED brightness with PWM
analogWrite(PINled, dutyCycle);
2024-04-07 23:56:17 +02:00
delay(1);
}
*/
2024-04-13 02:38:39 +02:00