diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index 2286d37..d3fc026 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -21,11 +21,96 @@ * */ +// external Libraries #include #include #include #include #include "DHT.h" +#include +#include +#include +#include + +/* + * + * 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( + + +)EOF"; + +const char HTMLfooter[] PROGMEM = R"EOF( + + +)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 @@ -40,20 +125,7 @@ unsigned long outputPrevTime = 0; */ 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 * @@ -61,14 +133,15 @@ uint8_t PINanalog = A0; #define WIRE Wire -/* DHT Stuff +/* + * DHT Stuff * */ #define DHTTYPE DHT11 DHT dht(PINdht, DHTTYPE); -/* Display Stuff - * +/* + * Display Stuff */ Adafruit_SSD1306 display = Adafruit_SSD1306(128, 32, &WIRE); // 'CanGrow_Logo', 128x32px @@ -115,10 +188,16 @@ const unsigned char* bmpallArray[1] = { /* + * + * * Functions * + * */ +/* + * Chirp functions + */ void writeI2CRegister8bit(int addr, int value) { Wire.beginTransmission(addr); Wire.write(value); @@ -136,6 +215,12 @@ unsigned int readI2CRegister16bit(int addr, int reg) { return t; } +/* + * + * Sensor functions + * + */ + int getWaterlevel() { /* @@ -160,6 +245,7 @@ int getWaterlevel() { // 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); @@ -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 * @@ -262,6 +369,7 @@ void setup() { pinMode(PINsoilmoisture, OUTPUT); pinMode(PINled, OUTPUT); pinMode(PINpump, OUTPUT); + pinMode(PIN_WIPE, INPUT); // set all OUTPUT to low digitalWrite(PINfan, LOW); @@ -269,24 +377,21 @@ void setup() { digitalWrite(PINsoilmoisture, LOW); digitalWrite(PINled, 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 Wire.begin(); - // initialise Serial output - Serial.begin(115200); - //Serial.println("Test123"); - - - // reset chirp - writeI2CRegister8bit(0x20, 6); //reset // initialise I2C display display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3C for 128x32 - - // initialise DHT11 - dht.begin(); - display.clearDisplay(); display.display(); @@ -297,20 +402,114 @@ void setup() { // display Logo display.drawBitmap(0, 0, bmpCanGrow_Logo, 128, 32, WHITE); display.display(); - delay(2500); - // clear display - display.clearDisplay(); - display.display(); + + // reset chirp + writeI2CRegister8bit(0x20, 6); //TODO: Do only, when configured + // 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 * + * */ void loop() { + //Serial.println("yolo"); + webserver.handleClient(); + } +/* + * + * Web pages + * + */ + +void webRoot() { + + String body = FPSTR(HTMLheader); + body += "

configured!

"; + body += FPSTR(HTMLfooter); + + webserver.send(200, "text/html", body); +} + +void webRootAP() { + + String body = FPSTR(HTMLheader); + body += "

unconfigured!

"; + body += FPSTR(HTMLfooter); + + webserver.send(200, "text/html", body); +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +/* + * + * + * PLAYGROUND / TRASH + * + * + */ + + /* + unsigned long currentTime = millis(); int valSoilmoisture0 = getSoilmoisture(0); @@ -344,9 +543,7 @@ void loop() { } - /* - * Output - */ +// OUTPUT if(currentTime - outputPrevTime >= 1000) { @@ -394,7 +591,7 @@ void loop() { Serial.println("Test"); outputPrevTime = currentTime; - } +*/ /* if(D6status == true) { @@ -428,4 +625,4 @@ void loop() { */ -} +