995 lines
24 KiB
C++
995 lines
24 KiB
C++
/*
|
|
* CanGrow - simply DIY automatic plant grow system (for cannabis).
|
|
*
|
|
*/
|
|
|
|
/*
|
|
* Includes
|
|
*
|
|
*/
|
|
|
|
// external Libraries
|
|
#include <SPI.h>
|
|
#include <Wire.h>
|
|
#include <Adafruit_GFX.h>
|
|
#include <Adafruit_SSD1306.h>
|
|
#include "DHT.h"
|
|
#include <ESP8266WiFi.h>
|
|
#include <ESP8266WebServer.h>
|
|
#include <EEPROM.h>
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
/*
|
|
*
|
|
* Variables
|
|
*
|
|
*/
|
|
|
|
// 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()
|
|
|
|
|
|
|
|
|
|
/*
|
|
* EEPROM variables
|
|
*/
|
|
|
|
// configured - if true, run setup assistant
|
|
bool configured;
|
|
|
|
char WIFIssid[32];
|
|
char WIFIpassword[64];
|
|
// WIFIuseDHCP - if true, get IP by DHCP
|
|
bool WIFIuseDHCP;
|
|
IPAddress WIFIip(192,168,4,20);
|
|
IPAddress WIFInetmask(255,255,255,0);
|
|
IPAddress WIFIgateway(192,168,4,254);
|
|
IPAddress WIFIdns(0,0,0,0);
|
|
|
|
// GrowName - contains the name of the grow/plant. Up to 32 byte
|
|
char GrowName[32];
|
|
// GrowStart - contains unix timestamp from date where grow starts (00:00)
|
|
// unsigned long is 4 byte
|
|
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;
|
|
|
|
// 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
|
|
byte PumpOnTime;
|
|
bool UseFan;
|
|
// SoilmoistureLow - contains the value , when soil moisture is assumed to be low,
|
|
byte SoilmoistureLow;
|
|
|
|
/*
|
|
*
|
|
* Constants
|
|
*
|
|
*/
|
|
|
|
|
|
/*
|
|
* WiFi
|
|
*/
|
|
|
|
const char* APssid = "CanGrow-unconfigured";
|
|
/*
|
|
* TODO - does not work atm. idk why.
|
|
* const char* APpass = "CanGrow";
|
|
const int APchannel = 6;
|
|
const bool APhidden = false;
|
|
*
|
|
*/
|
|
|
|
|
|
/*
|
|
*
|
|
* Webserver
|
|
*
|
|
*/
|
|
|
|
ESP8266WebServer webserver(80);
|
|
|
|
/*
|
|
* HTML constants for header, footer, css, ...
|
|
*/
|
|
|
|
// Template: const char HTMLexamplepage[] PROGMEM = R"EOF()EOF";
|
|
const char HTMLheader[] PROGMEM = R"EOF(
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>CanGrow</title>
|
|
<link rel="stylesheet" href="./style.css">
|
|
</head>
|
|
<body>
|
|
)EOF";
|
|
|
|
const char HTMLfooter[] PROGMEM = R"EOF(
|
|
</body>
|
|
</html>
|
|
)EOF";
|
|
|
|
const char HTMLstyleCSS[] PROGMEM = R"EOF(
|
|
body {
|
|
font-family: helvetica;
|
|
background-color: #cccccc;
|
|
}
|
|
|
|
a:link, a:visited {
|
|
color: #33ff33;
|
|
}
|
|
|
|
a:hover {
|
|
color: #ffff00;
|
|
}
|
|
|
|
a:active {
|
|
color: #da7a0a;
|
|
}
|
|
|
|
)EOF";
|
|
|
|
/*
|
|
*
|
|
* Pin assignments
|
|
*
|
|
* D0 - MOSFET Fan
|
|
* D1, D2 - I2C
|
|
* D3 - DHT11
|
|
* D4 - PIN_WIPE
|
|
* D5 - MOSFET Pump
|
|
* D6 - MOSFET Grow LED, PWM
|
|
* D7 - waterlevel (set HIGH to read value)
|
|
* D8 - analog soil moisture (set HIGH to read value)
|
|
* A0 - analog input for soil moisture and waterlevel readings
|
|
*
|
|
* D4 and D7 cannot be HIGH at the same time!
|
|
*/
|
|
|
|
// 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
|
|
// During Start Screen you can pull D4 to LOW to wipe saved data in EEPROM
|
|
// DO NOT PULL D4 DOWN AT WHEN POWERING ON !!! BOOT WILL FAIL
|
|
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;
|
|
|
|
|
|
|
|
/* I2C Stuff
|
|
*
|
|
*/
|
|
#define WIRE Wire
|
|
|
|
|
|
/*
|
|
* DHT Stuff
|
|
*
|
|
*/
|
|
#define DHTTYPE DHT11
|
|
DHT dht(PINdht, DHTTYPE);
|
|
|
|
/*
|
|
* Display Stuff
|
|
*/
|
|
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
|
|
};
|
|
|
|
|
|
/*
|
|
*
|
|
*
|
|
* Functions
|
|
*
|
|
*
|
|
*/
|
|
|
|
/*
|
|
* Chirp functions
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/*
|
|
*
|
|
* Sensor functions
|
|
*
|
|
*/
|
|
|
|
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
|
|
digitalWrite(PINwaterlevel, HIGH);
|
|
// wait a bit to let the circuit stabilize
|
|
// TODO: replace delay() with millis()
|
|
delay(100);
|
|
// get the value
|
|
waterlevelRAW = analogRead(PINanalog);
|
|
// disable Vcc for the sensor to prevent electrolysis effect and release analog pin
|
|
digitalWrite(PINwaterlevel, LOW);
|
|
|
|
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;
|
|
}
|
|
|
|
int getSoilmoisture(byte moistureSensor) {
|
|
/*
|
|
* moistureSensor
|
|
* ==============
|
|
* 0 : analog capacitive moisture sensor
|
|
* 1 : chirp I2C moisture sensor
|
|
*/
|
|
|
|
// value to return
|
|
int soilmoisture;
|
|
// value for wet
|
|
int wet;
|
|
// value for dry
|
|
int dry;
|
|
|
|
if(moistureSensor == 0 ) {
|
|
// read analog value from analog moisture sensor
|
|
wet = 180;
|
|
dry= 590;
|
|
|
|
digitalWrite(PINsoilmoisture, HIGH);
|
|
// wait a bit to let the circuit stabilize
|
|
delay(100);
|
|
// get analog input value
|
|
soilmoisture = analogRead(PINanalog);
|
|
// disable Vcc for the sensor to release analog pin
|
|
digitalWrite(PINsoilmoisture, LOW);
|
|
} else {
|
|
// read soil moisture from chrip I2C
|
|
wet = 560;
|
|
dry= 250;
|
|
|
|
// get raw value from I2C chirp sensor
|
|
soilmoisture = readI2CRegister16bit(0x20, 0);
|
|
}
|
|
|
|
return map(soilmoisture, wet, dry, 100, 0);
|
|
}
|
|
|
|
int getLightchirp() {
|
|
// get the "light value" from I2C chirp module
|
|
writeI2CRegister8bit(0x20, 3); //request light measurement
|
|
int lightchirp = readI2CRegister16bit(0x20, 4);
|
|
return lightchirp;
|
|
}
|
|
|
|
|
|
void wipeEEPROM() {
|
|
Serial.println(":: wipe EEPROM ::");
|
|
|
|
// wipeMsg is helper variable to know if the Serial.print Message was
|
|
// already sent
|
|
byte wipeMsg = 0;
|
|
while(digitalRead(PIN_WIPE) == LOW ) {
|
|
// only show the Serial message once
|
|
if(wipeMsg == 0) {
|
|
Serial.println("Please release PIN_WIPE to erase all data saved in EEPROM");
|
|
Serial.println("LAST CHANCE TO KEEP THE DATA BY RESETTING NOW!!");
|
|
// increase i to show the serial message only once
|
|
wipeMsg = 1;
|
|
}
|
|
delay(500);
|
|
}
|
|
|
|
// 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); }
|
|
|
|
// commit everything to EEPROM and end here
|
|
EEPROM.end();
|
|
|
|
Serial.println("DONE");
|
|
|
|
// set D4 PIN_WIPE internal LED to Output to give feedback WIPE
|
|
// was done
|
|
pinMode(PIN_WIPE, OUTPUT);
|
|
|
|
Serial.println("!! Device will restart in 3 seconds !!");
|
|
|
|
// let the internal led blink fast to signalize wipe is done
|
|
for(byte i = 0; i <= 24 ; i++) {
|
|
if(i % 2) {
|
|
digitalWrite(PIN_WIPE, LOW);
|
|
} else {
|
|
digitalWrite(PIN_WIPE, HIGH);
|
|
}
|
|
delay(125);
|
|
}
|
|
ESP.restart();
|
|
}
|
|
|
|
|
|
bool loadEEPROM() {
|
|
Serial.println(":: loading EEPROM ::");
|
|
/*
|
|
* 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
|
|
*
|
|
* All boolean variables are at the end of the EEPROM
|
|
*/
|
|
EEPROM.get(511, configured);
|
|
Serial.print("configured: ");
|
|
Serial.println(configured);
|
|
|
|
// when configured is > 1 (it should == 1) then read EEPROM furher data
|
|
if(configured > 0) {
|
|
/*
|
|
* WIFI data
|
|
*/
|
|
|
|
// read var WIFIssid from address 0, 32 byte long
|
|
EEPROM.get(0, WIFIssid);
|
|
// read var WIFIpassword from address 32, 64 byte long
|
|
EEPROM.get(32, WIFIpassword);
|
|
// 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);
|
|
// read var WIFIgateway from address 128, 16 byte long
|
|
EEPROM.get(144, WIFIdns);
|
|
// read var WIFIuseDHCP from Address 510, 1 byte long
|
|
EEPROM.get(510, WIFIuseDHCP);
|
|
|
|
/*
|
|
* Grow data
|
|
*/
|
|
|
|
//TBD
|
|
|
|
|
|
|
|
// print values to Serial output
|
|
Serial.println(":: EEPROM loaded ::");
|
|
Serial.print("WIFIssid: ");
|
|
Serial.println(WIFIssid);
|
|
Serial.print("WIFIpassword: ");
|
|
Serial.println(WIFIpassword);
|
|
Serial.print("Use DHCP: ");
|
|
Serial.println(WIFIuseDHCP);
|
|
}
|
|
Serial.println(":: EEPROM loaded ::");
|
|
|
|
return(configured);
|
|
}
|
|
|
|
void wifiConnect() {
|
|
Serial.println(":: Connecting to WiFi ::");
|
|
Serial.print("SSID: ");
|
|
Serial.println(WIFIssid);
|
|
|
|
// Start WiFi connection
|
|
WiFi.begin(WIFIssid, WIFIpassword);
|
|
if(WIFIuseDHCP == false) {
|
|
WiFi.config(WIFIip, WIFIdns, WIFIgateway, WIFInetmask);
|
|
}
|
|
|
|
// wait until WiFi connection is established
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println(" CONNECTED!");
|
|
Serial.print("IP: ");
|
|
Serial.println(WiFi.localIP());
|
|
}
|
|
|
|
void wifiAp() {
|
|
Serial.println(":: Creating Accesspoint ::");
|
|
|
|
// configure WiFi Access Point
|
|
WiFi.softAPConfig(WIFIip, WIFIgateway, WIFInetmask);
|
|
// start Access Point
|
|
// TODO make AP with password - does not work atm. idk why.
|
|
WiFi.softAP(APssid);
|
|
Serial.print("SSID: ");
|
|
Serial.println(APssid);
|
|
Serial.print("CanGrow IP address: ");
|
|
Serial.println(WiFi.softAPIP());
|
|
}
|
|
|
|
/*
|
|
* Setup
|
|
*
|
|
*/
|
|
void setup() {
|
|
// Start EEPROM
|
|
EEPROM.begin(512);
|
|
|
|
// setup pins
|
|
pinMode(PINfan, OUTPUT);
|
|
pinMode(PINdht, INPUT);
|
|
pinMode(PINwaterlevel, OUTPUT);
|
|
pinMode(PINsoilmoisture, OUTPUT);
|
|
pinMode(PINled, OUTPUT);
|
|
pinMode(PINpump, OUTPUT);
|
|
pinMode(PIN_WIPE, INPUT);
|
|
|
|
// set all OUTPUT to low
|
|
digitalWrite(PINfan, LOW);
|
|
digitalWrite(PINwaterlevel, LOW);
|
|
digitalWrite(PINsoilmoisture, LOW);
|
|
digitalWrite(PINled, LOW);
|
|
digitalWrite(PINpump, LOW);
|
|
|
|
// Start Serial
|
|
Serial.begin(115200);
|
|
|
|
// Write an empty line, because before there is some garbage in serial
|
|
// output
|
|
Serial.println("");
|
|
Serial.println(".:: CanGrow Start ::.");
|
|
|
|
// initialise Wire for I2C
|
|
Wire.begin();
|
|
|
|
// initialise I2C display
|
|
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32
|
|
display.clearDisplay();
|
|
display.display();
|
|
|
|
// set display settings
|
|
display.setTextSize(1);
|
|
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
|
|
|
|
// display Logo
|
|
display.drawBitmap(0, 0, bmpCanGrow_Logo, 128, 32, WHITE);
|
|
display.display();
|
|
|
|
// reset chirp
|
|
writeI2CRegister8bit(0x20, 6); //TODO: Do only, when configured
|
|
|
|
// initialise DHT11
|
|
dht.begin(); //TODO: Do only, when configured
|
|
|
|
Serial.println("To wipe the EEPROM saved data, set D4 (PIN_WIPE) to LOW - NOW! (2 seconds left)");
|
|
// wait a few seconds to let the user pull D4 down to wipe EEPROM
|
|
// and we can enjoy the boot screen meanwhile :p
|
|
// meanwhile blink with the led onboad :)
|
|
// 333 * 6 =~ 2 seconds
|
|
pinMode(PIN_WIPE, OUTPUT);
|
|
for(byte i = 0; i <= 6 ; i++) {
|
|
if(i % 2) {
|
|
digitalWrite(PIN_WIPE, LOW);
|
|
} else {
|
|
digitalWrite(PIN_WIPE, HIGH);
|
|
}
|
|
delay(333);
|
|
}
|
|
//delay(2000);
|
|
|
|
// read status from PIN_WIPE to WIPE
|
|
|
|
|
|
// when PIN_WIPE is set to LOW, wipe EEPROM
|
|
if(digitalRead(PIN_WIPE) == LOW) {
|
|
// wipe EEPROM
|
|
wipeEEPROM();
|
|
}
|
|
|
|
/*
|
|
* load EEPROM and Setup WiFi
|
|
*
|
|
* call loadEEPROM() which returns a bool
|
|
* When true, CanGrow is already configured and EEPROM values are applied
|
|
* When false, CanGrow is unconfigured and we need to run the setup assistant
|
|
*/
|
|
|
|
|
|
// load stored values from EEPROM and check what var configured is returned
|
|
if(loadEEPROM()) {
|
|
|
|
// connect to wifi
|
|
wifiConnect();
|
|
|
|
// use webhandler for configured state
|
|
WebHandler_configured();
|
|
|
|
// configured is 0, setup Access Point
|
|
} else {
|
|
|
|
// start an wifi accesspoint
|
|
wifiAp();
|
|
// use webhandler for unconfigured state
|
|
WebHandler_unconfigured();
|
|
}
|
|
|
|
/*
|
|
* Webserver handlers
|
|
* here are the generic webserver handlers like 404 not found
|
|
* wificonfig save, ...
|
|
*
|
|
* if you are looking for the single webpages handler, have a look to
|
|
*
|
|
* WebHandler_unconfigured() and WebHandler_configured()
|
|
*/
|
|
|
|
// generic handler
|
|
webserver.on("/wifiConfig/save", HTTP_POST, POSTwifiConfig);
|
|
webserver.on("/style.css", HTTP_GET, WEBstyleCSS);
|
|
// 404 handling
|
|
webserver.onNotFound(WEB404);
|
|
|
|
// start webserver
|
|
webserver.begin();
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
*
|
|
*
|
|
* Loop
|
|
*
|
|
*
|
|
*/
|
|
void loop() {
|
|
//Serial.println("yolo");
|
|
webserver.handleClient();
|
|
}
|
|
|
|
void WebHandler_unconfigured() {
|
|
/*
|
|
* WebHandler_unconfigured()
|
|
*
|
|
* Here all URL routings are defined, in unconfigured state
|
|
*/
|
|
webserver.on("/", HTTP_GET, WEBrootUnconfigured);
|
|
}
|
|
|
|
void WebHandler_configured() {
|
|
/*
|
|
* WebHandler_unconfigured()
|
|
*
|
|
* Here all URL routings are defined, in configured state
|
|
*/
|
|
webserver.on("/", HTTP_GET, WEBroot);
|
|
}
|
|
|
|
/*
|
|
*
|
|
* Web pages
|
|
*
|
|
*/
|
|
|
|
// not really a webpage, but CSS is important for them :p
|
|
void WEBstyleCSS() {
|
|
webserver.send(200, "text/css", HTMLstyleCSS);
|
|
}
|
|
|
|
void WEB404() {
|
|
String body = FPSTR(HTMLheader);
|
|
body += "404 - not found";
|
|
body += FPSTR(HTMLfooter);
|
|
webserver.send(200, "text/html", body);
|
|
}
|
|
|
|
|
|
void WEBrootUnconfigured() {
|
|
byte ssidsAvail = WiFi.scanNetworks();
|
|
String body = FPSTR(HTMLheader);
|
|
body += "<h1>unconfigured!</h1>\n";
|
|
body += "<h1>WiFi config</h1>\n";
|
|
body += "<p>Select your wifi network from the SSID list.<br>To use DHCP leave IP, Subnet, Gateway and DNS fields blank!</p>";
|
|
body += "<form method='post' action='/wifiConfig/save'>\n";
|
|
body += "SSID: <select id='WIFIssid' name='WIFIssid'>\n";
|
|
// build option list for selecting wifi
|
|
Serial.println("Available Wifis: ");
|
|
for(int i = 0 ; i < ssidsAvail; i++) {
|
|
String wifiName = WiFi.SSID(i);
|
|
Serial.println(wifiName);
|
|
body += "<option value='";
|
|
body += wifiName;
|
|
body += "'>";
|
|
body += wifiName;
|
|
body += "</option>\n";
|
|
}
|
|
body += "</select><br>\n";
|
|
body += "Password: <input type='password' name='WIFIpassword'><br>\n";
|
|
body += "IP: <input type='text' name='WIFIip'><br>\n";
|
|
body += "Subnet mask: <input type='text' name='WIFInetmask'><br>\n";
|
|
body += "Gateway: <input type='text' name='WIFIgateway'><br>\n";
|
|
body += "DNS: <input type='text' name='WIFIdns'><br>\n";
|
|
body += "<input type='submit' value='Save'>\n";
|
|
body += "</form>\n";
|
|
body += FPSTR(HTMLfooter);
|
|
|
|
webserver.send(200, "text/html", body);
|
|
}
|
|
|
|
void WEBroot() {
|
|
|
|
String body = FPSTR(HTMLheader);
|
|
body += "<h1>configured!</h1>";
|
|
body += FPSTR(HTMLfooter);
|
|
|
|
webserver.send(200, "text/html", body);
|
|
}
|
|
|
|
|
|
/*
|
|
*
|
|
* 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");
|
|
String WIFIdns_new = webserver.arg("WIFIdns");
|
|
|
|
// convert String we got from webserver.arg to EEPROM friendly char[]
|
|
WIFIssid_new.toCharArray(WIFIssid, 32);
|
|
WIFIpassword_new.toCharArray(WIFIpassword, 64);
|
|
|
|
// if WIFIip_new was not set, we assume DHCP should be used
|
|
if(WIFIip_new.length() > 0) {
|
|
WIFIip.fromString(WIFIip_new);
|
|
WIFInetmask.fromString(WIFInetmask_new);
|
|
WIFIgateway.fromString(WIFIgateway_new);
|
|
WIFIdns.fromString(WIFIdns_new);
|
|
//
|
|
WIFIuseDHCP = false;
|
|
} else {
|
|
WIFIuseDHCP = true;
|
|
}
|
|
|
|
configured = true;
|
|
|
|
EEPROM.put(0, WIFIssid);
|
|
EEPROM.put(32, WIFIpassword);
|
|
EEPROM.put(96, WIFIip);
|
|
EEPROM.put(112, WIFInetmask);
|
|
EEPROM.put(128, WIFIgateway);
|
|
EEPROM.put(144, WIFIdns);
|
|
EEPROM.put(510, WIFIuseDHCP);
|
|
EEPROM.put(511, configured);
|
|
EEPROM.commit();
|
|
|
|
|
|
Serial.println(":: POSTwifiConfig ::");
|
|
|
|
Serial.print("WIFIssid: ");
|
|
Serial.println(WIFIssid_new);
|
|
Serial.println(WIFIssid);
|
|
Serial.print("WIFIpassword: ");
|
|
Serial.println(WIFIpassword_new);
|
|
Serial.println(WIFIpassword);
|
|
Serial.print("WIFIip: ");
|
|
Serial.println(WIFIip_new);
|
|
Serial.print("WIFInetmask: ");
|
|
Serial.println(WIFInetmask_new);
|
|
Serial.print("WIFIgateway: ");
|
|
Serial.println(WIFIgateway_new);
|
|
Serial.print("WIFIdns: ");
|
|
Serial.println(WIFIdns_new);
|
|
Serial.print("WIFIuseDHCP: ");
|
|
Serial.println(WIFIuseDHCP);
|
|
Serial.print("configured: ");
|
|
Serial.println(configured);
|
|
|
|
webserver.send(200, "text/html", "wifiConfig saved, please restart");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
*
|
|
*
|
|
* PLAYGROUND / TRASH
|
|
*
|
|
*
|
|
*/
|
|
|
|
/*
|
|
|
|
unsigned long currentTime = millis();
|
|
|
|
int valSoilmoisture0 = getSoilmoisture(0);
|
|
int valSoilmoisture1 = getSoilmoisture(1);
|
|
|
|
float valTemperature0 = getTemperature(0);
|
|
float valTemperature1 = getTemperature(1);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
// OUTPUT
|
|
if(currentTime - outputPrevTime >= 1000) {
|
|
|
|
|
|
// set display cursor to top left
|
|
display.setCursor(0,0);
|
|
// display text
|
|
display.print("I2C: ");
|
|
display.print(valSoilmoisture1);
|
|
display.print(", ");
|
|
display.println(valTemperature1);
|
|
|
|
Serial.print("I2C: ");
|
|
Serial.print(valSoilmoisture1);
|
|
Serial.print(", ");
|
|
Serial.println(valTemperature1);
|
|
|
|
|
|
display.print("DHT11: ");
|
|
display.print(valTemperature0);
|
|
display.print(", ");
|
|
display.println(valHumidity);
|
|
|
|
Serial.print("DHT11: ");
|
|
Serial.print(valTemperature0);
|
|
Serial.print(", ");
|
|
Serial.println(valHumidity);
|
|
|
|
|
|
display.print("Water Status: ");
|
|
display.println(valWaterlevel);
|
|
|
|
Serial.print("Water Status: ");
|
|
Serial.println(valWaterlevel);
|
|
|
|
display.print("ASM: ");
|
|
display.print(valSoilmoisture0);
|
|
display.println(", ");
|
|
|
|
Serial.print("ASM: ");
|
|
Serial.println(valSoilmoisture0);
|
|
|
|
// print everything on the display
|
|
display.display();
|
|
|
|
Serial.println("Test");
|
|
|
|
outputPrevTime = currentTime;
|
|
*/
|
|
|
|
|
|
/* if(D6status == true) {
|
|
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");
|
|
}
|
|
*/
|
|
|
|
/*
|
|
for(int dutyCycle = 0; dutyCycle < 255; dutyCycle++){
|
|
// changing the LED brightness with PWM
|
|
analogWrite(PINled, dutyCycle);
|
|
delay(1);
|
|
}
|
|
|
|
// decrease the LED brightness
|
|
for(int dutyCycle = 255; dutyCycle > 0; dutyCycle--){
|
|
// changing the LED brightness with PWM
|
|
analogWrite(PINled, dutyCycle);
|
|
delay(1);
|
|
}
|
|
*/
|
|
|
|
|
|
|