From 7575dae14f14371289ad67af16c75dfe77f27ace Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Wed, 13 Nov 2019 22:45:49 +0100 Subject: [PATCH] Modified firmware to just publish values as json --- platformio.ini | 16 ++- src/main.cpp | 259 +++++++++++++++++++------------------------------ 2 files changed, 105 insertions(+), 170 deletions(-) diff --git a/platformio.ini b/platformio.ini index 7ccd064..dd63bf5 100644 --- a/platformio.ini +++ b/platformio.ini @@ -12,16 +12,12 @@ platform = espressif32 board = esp32dev framework = arduino -upload_port = COM7 +upload_port = COM11 board_build.partitions = no_ota.csv lib_deps = - https://github.com/me-no-dev/AsyncTCP - ESP Async WebServer - ESP-DASH - ArduinoJson - Button2 - BH1750 - Adafruit BME280 Library - Adafruit Unified Sensor - DHT sensor library \ No newline at end of file + IotWebConf + ArduinoJson + BH1750 + Adafruit Unified Sensor + DHT sensor library \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 5cb643a..7e5915f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,20 +1,9 @@ -#include "Adafruit_Sensor.h" -#include "DHT.h" -#include "DHT_U.h" -#include "esp_wifi.h" -#include -#include -#include +#include +#include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include #define I2C_SDA 25 #define I2C_SCL 26 @@ -26,129 +15,68 @@ #define POWER_CTRL 4 #define USER_BUTTON 35 +// -- Initial name of the Thing. Used e.g. as SSID of the own Access Point. +const char thingName[] = "hiGrow"; + +// -- Initial password to connect to the Thing, when it creates an own Access Point. +const char wifiInitialApPassword[] = "test1234"; + +DNSServer dnsServer; +WebServer server(80); +IotWebConf iotWebConf(thingName, &dnsServer, &server, wifiInitialApPassword); + BH1750 lightMeter(0x23); //0x23 -Adafruit_BME280 bmp; //0x77 DHT_Unified dht(DHT12_PIN, DHT11); -AsyncWebServer server(80); -Button2 button(BOOT_PIN); -Button2 useButton(USER_BUTTON); -WiFiMulti multi; +void wifiConnected(); +void handleRoot(); +void handleValues(); -#define WIFI_SSID "mqtt.binary-kitchen.de" -#define WIFI_PASSWD "test1234" +uint16_t soil = 0; +uint32_t salt = 0; +float bat = 0; +float temperature = 0; +float humidity = 0; +float lux = 0; -bool bme_found = false; - -void smartConfigStart(Button2& b) +/** + * Handle web requests to "/" path. + */ +void handleRoot() { - Serial.println("smartConfigStart..."); - WiFi.disconnect(); - WiFi.beginSmartConfig(); - while (!WiFi.smartConfigDone()) { - Serial.print("."); - delay(200); + // -- Let IotWebConf test and handle captive portal requests. + if (iotWebConf.handleCaptivePortal()) { + // -- Captive portal request were already served. + return; } - WiFi.stopSmartConfig(); - Serial.println(); - Serial.print("smartConfigStop Connected:"); - Serial.print(WiFi.SSID()); - Serial.print("PSW: "); - Serial.println(WiFi.psk()); + String s = F(""); + s += F("IotWebConf 01 MinimalHello world!"); + s += F("Go to configure page to change settings."); + s += F("\n"); + + server.send(200, "text/html", s); } -void sleepHandler(Button2& b) +void handleValues() { - Serial.println("Enter Deepsleep ..."); - esp_sleep_enable_ext1_wakeup(GPIO_SEL_35, ESP_EXT1_WAKEUP_ALL_LOW); - delay(1000); - esp_deep_sleep_start(); + StaticJsonDocument<200> doc; + doc[F("temperature")] = temperature; + doc[F("humidity")] = humidity; + doc[F("soil")] = soil; + doc[F("salt")] = salt; + doc[F("battery")] = bat; + doc[F("lux")] = lux; + + String output; + serializeJson(doc, output); + + server.send(200, "application/json", output); } -bool serverBegin() +void wifiConnected() { - static bool isBegin = false; - if (isBegin) { - return true; - } - - ESPDash.init(server); - - isBegin = true; - if (MDNS.begin("soil")) { - Serial.println("MDNS responder started"); - } - // Add Respective Cards - if (bme_found) { - ESPDash.addTemperatureCard("temp", "BME Temperature/C", 0, 0); - ESPDash.addNumberCard("press", "BME Pressure/hPa", 0); - ESPDash.addNumberCard("alt", "BME Altitude/m", 0); - } - - ESPDash.addTemperatureCard("temp2", "DHT Temperature/C", 0, 0); - ESPDash.addHumidityCard("hum2", "DHT Humidity/%", 0); - ESPDash.addNumberCard("lux", "BH1750/lx", 0); - ESPDash.addHumidityCard("soil", "Soil", 0); - ESPDash.addNumberCard("salt", "Salt", 0); - ESPDash.addNumberCard("batt", "Battery/mV", 0); - server.begin(); - MDNS.addService("http", "tcp", 80); - return true; -} - -void setup() -{ - Serial.begin(115200); - WiFi.mode(WIFI_STA); - - wifi_config_t current_conf; - esp_wifi_get_config(WIFI_IF_STA, ¤t_conf); - int ssidlen = strlen((char*)(current_conf.sta.ssid)); - int passlen = strlen((char*)(current_conf.sta.password)); - - if (false && (ssidlen == 0 || passlen == 0)) { - multi.addAP(WIFI_SSID, WIFI_PASSWD); - Serial.println("Connect to defalut ssid, you can long press BOOT button enter smart config mode"); - while (multi.run() != WL_CONNECTED) { - Serial.print('.'); - } - } else { - WiFi.begin(WIFI_SSID, WIFI_PASSWD); - } - - if (WiFi.waitForConnectResult() != WL_CONNECTED) { - Serial.printf("WiFi connect fail!,please restart retry,or long press BOOT button enter smart config mode\n"); - } - if (WiFi.status() == WL_CONNECTED) { - Serial.print("IP Address: "); - Serial.println(WiFi.localIP()); - } - - button.setLongClickHandler(smartConfigStart); - useButton.setLongClickHandler(sleepHandler); - - Wire.begin(I2C_SDA, I2C_SCL); - - dht.begin(); - - //! Sensor power control pin , use deteced must set high - pinMode(POWER_CTRL, OUTPUT); - digitalWrite(POWER_CTRL, 1); - delay(1000); - - if (!bmp.begin()) { - Serial.println(F("Could not find a valid BMP280 sensor, check wiring!")); - bme_found = false; - } else { - bme_found = true; - } - - if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) { - Serial.println(F("BH1750 Advanced begin")); - } else { - Serial.println(F("Error initialising BH1750")); - } + Serial.println("WiFi was connected."); } uint32_t readSalt() @@ -185,49 +113,60 @@ float readBattery() return battery_voltage; } +void setup() +{ + Serial.begin(115200); + Serial.println(); + Serial.println("Starting up..."); + + iotWebConf.setWifiConnectionCallback(&wifiConnected); + iotWebConf.init(); + server.on("/", handleRoot); + server.on("/config", [] { iotWebConf.handleConfig(); }); + server.on("/values", handleValues); + server.onNotFound([]() { iotWebConf.handleNotFound(); }); + + Wire.begin(I2C_SDA, I2C_SCL); + dht.begin(); + + //! Sensor power control pin , use deteced must set high + pinMode(POWER_CTRL, OUTPUT); + digitalWrite(POWER_CTRL, 1); + delay(1000); + + Wire.begin(); + + if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) { + Serial.println(F("BH1750 Advanced begin")); + } else { + Serial.println(F("Error initialising BH1750")); + } + + Serial.println("Ready."); +} + void loop() { static uint64_t timestamp; - button.loop(); - useButton.loop(); + iotWebConf.doLoop(); + if (millis() - timestamp > 1000) { timestamp = millis(); - if (WiFi.status() == WL_CONNECTED) { - if (serverBegin()) { - float lux = lightMeter.readLightLevel(); - if (bme_found) { - float bme_temp = bmp.readTemperature(); - float bme_pressure = (bmp.readPressure() / 100.0F); - float bme_altitude = bmp.readAltitude(1013.25); - ESPDash.updateTemperatureCard("temp", (int)bme_temp); - ESPDash.updateNumberCard("press", (int)bme_pressure); - ESPDash.updateNumberCard("alt", (int)bme_altitude); - } + lux = lightMeter.readLightLevel(); - sensors_event_t event; - dht.temperature().getEvent(&event); - float t12 = event.temperature; - // Read temperature as Fahrenheit (isFahrenheit = true) - dht.humidity().getEvent(&event); - float h12 = event.relative_humidity; + sensors_event_t event; + dht.temperature().getEvent(&event); + temperature = event.temperature; + // Read temperature as Fahrenheit (isFahrenheit = true) + dht.humidity().getEvent(&event); + humidity = event.relative_humidity; - if (!isnan(t12) && !isnan(h12)) { - ESPDash.updateTemperatureCard("temp2", (int)t12); - ESPDash.updateHumidityCard("hum2", (int)h12); - } - ESPDash.updateNumberCard("lux", (int)lux); + soil = readSoil(); + salt = readSalt(); + bat = readBattery(); - uint16_t soil = readSoil(); - uint32_t salt = readSalt(); - float bat = readBattery(); - ESPDash.updateHumidityCard("soil", (int)soil); - ESPDash.updateNumberCard("salt", (int)salt); - ESPDash.updateNumberCard("batt", (int)bat); - - Serial.printf("DHTT:%.2f H:%.2f Lux:%.2f Soil:%u Salt:%u Bat:%.2f\n", - t12, h12, lux, soil, salt, bat); - } - } + Serial.printf("DHTT:%.2f H:%.2f Lux:%.2f Soil:%u Salt:%u Bat:%.2f\n", + temperature, humidity, lux, soil, salt, bat); } } \ No newline at end of file