Modified firmware to just publish values as json

This commit is contained in:
Thomas Basler 2019-11-13 22:45:49 +01:00
parent aa00aab7b6
commit 7575dae14f
2 changed files with 105 additions and 170 deletions

View File

@ -12,16 +12,12 @@
platform = espressif32 platform = espressif32
board = esp32dev board = esp32dev
framework = arduino framework = arduino
upload_port = COM7 upload_port = COM11
board_build.partitions = no_ota.csv board_build.partitions = no_ota.csv
lib_deps = lib_deps =
https://github.com/me-no-dev/AsyncTCP IotWebConf
ESP Async WebServer
ESP-DASH
ArduinoJson ArduinoJson
Button2
BH1750 BH1750
Adafruit BME280 Library
Adafruit Unified Sensor Adafruit Unified Sensor
DHT sensor library DHT sensor library

View File

@ -1,20 +1,9 @@
#include "Adafruit_Sensor.h" #include <Adafruit_Sensor.h>
#include "DHT.h" #include <ArduinoJson.h>
#include "DHT_U.h"
#include "esp_wifi.h"
#include <Adafruit_BME280.h>
#include <Arduino.h>
#include <AsyncTCP.h>
#include <BH1750.h> #include <BH1750.h>
#include <Button2.h> #include <DHT.h>
#include <ESPAsyncWebServer.h> #include <DHT_U.h>
#include <ESPDash.h> #include <IotWebConf.h>
#include <ESPmDNS.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <Wire.h>
#include <algorithm>
#include <iostream>
#define I2C_SDA 25 #define I2C_SDA 25
#define I2C_SCL 26 #define I2C_SCL 26
@ -26,129 +15,68 @@
#define POWER_CTRL 4 #define POWER_CTRL 4
#define USER_BUTTON 35 #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 BH1750 lightMeter(0x23); //0x23
Adafruit_BME280 bmp; //0x77
DHT_Unified dht(DHT12_PIN, DHT11); DHT_Unified dht(DHT12_PIN, DHT11);
AsyncWebServer server(80); void wifiConnected();
Button2 button(BOOT_PIN); void handleRoot();
Button2 useButton(USER_BUTTON); void handleValues();
WiFiMulti multi;
#define WIFI_SSID "mqtt.binary-kitchen.de" uint16_t soil = 0;
#define WIFI_PASSWD "test1234" uint32_t salt = 0;
float bat = 0;
float temperature = 0;
float humidity = 0;
float lux = 0;
bool bme_found = false; /**
* Handle web requests to "/" path.
void smartConfigStart(Button2& b) */
void handleRoot()
{ {
Serial.println("smartConfigStart..."); // -- Let IotWebConf test and handle captive portal requests.
WiFi.disconnect(); if (iotWebConf.handleCaptivePortal()) {
WiFi.beginSmartConfig(); // -- Captive portal request were already served.
while (!WiFi.smartConfigDone()) { return;
Serial.print(".");
delay(200);
} }
WiFi.stopSmartConfig(); String s = F("<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>");
Serial.println(); s += F("<title>IotWebConf 01 Minimal</title></head><body>Hello world!");
Serial.print("smartConfigStop Connected:"); s += F("Go to <a href='config'>configure page</a> to change settings.");
Serial.print(WiFi.SSID()); s += F("</body></html>\n");
Serial.print("PSW: ");
Serial.println(WiFi.psk()); server.send(200, "text/html", s);
} }
void sleepHandler(Button2& b) void handleValues()
{ {
Serial.println("Enter Deepsleep ..."); StaticJsonDocument<200> doc;
esp_sleep_enable_ext1_wakeup(GPIO_SEL_35, ESP_EXT1_WAKEUP_ALL_LOW); doc[F("temperature")] = temperature;
delay(1000); doc[F("humidity")] = humidity;
esp_deep_sleep_start(); 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; Serial.println("WiFi was connected.");
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, &current_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"));
}
} }
uint32_t readSalt() uint32_t readSalt()
@ -185,49 +113,60 @@ float readBattery()
return battery_voltage; 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() void loop()
{ {
static uint64_t timestamp; static uint64_t timestamp;
button.loop(); iotWebConf.doLoop();
useButton.loop();
if (millis() - timestamp > 1000) { if (millis() - timestamp > 1000) {
timestamp = millis(); timestamp = millis();
if (WiFi.status() == WL_CONNECTED) {
if (serverBegin()) {
float lux = lightMeter.readLightLevel();
if (bme_found) { lux = lightMeter.readLightLevel();
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);
}
sensors_event_t event; sensors_event_t event;
dht.temperature().getEvent(&event); dht.temperature().getEvent(&event);
float t12 = event.temperature; temperature = event.temperature;
// Read temperature as Fahrenheit (isFahrenheit = true) // Read temperature as Fahrenheit (isFahrenheit = true)
dht.humidity().getEvent(&event); dht.humidity().getEvent(&event);
float h12 = event.relative_humidity; humidity = event.relative_humidity;
if (!isnan(t12) && !isnan(h12)) { soil = readSoil();
ESPDash.updateTemperatureCard("temp2", (int)t12); salt = readSalt();
ESPDash.updateHumidityCard("hum2", (int)h12); bat = readBattery();
}
ESPDash.updateNumberCard("lux", (int)lux);
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", Serial.printf("DHTT:%.2f H:%.2f Lux:%.2f Soil:%u Salt:%u Bat:%.2f\n",
t12, h12, lux, soil, salt, bat); temperature, humidity, lux, soil, salt, bat);
}
}
} }
} }