summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Baumann <derflob@derflob.de>2020-05-07 23:51:48 +0200
committerFlorian Baumann <derflob@derflob.de>2020-05-07 23:51:48 +0200
commitdb5a7739cf13d3f33ffc2655a37ae6d4bf109bfc (patch)
treede0dc03b76e2e75974bd3c802dcb6667061af233
parent1585ceb1e4d6d3d5ca328406006986f57b082b84 (diff)
downloadULPSoilMonitor-db5a7739cf13d3f33ffc2655a37ae6d4bf109bfc.tar.gz
ULPSoilMonitor-db5a7739cf13d3f33ffc2655a37ae6d4bf109bfc.tar.bz2
add IotWefConf to connect to wifi
-rw-r--r--ULPSoilMonitor.ino173
1 files changed, 168 insertions, 5 deletions
diff --git a/ULPSoilMonitor.ino b/ULPSoilMonitor.ino
index ded644c..8c9884d 100644
--- a/ULPSoilMonitor.ino
+++ b/ULPSoilMonitor.ino
@@ -13,6 +13,9 @@
#include "ulptool.h"
#include <math.h>
+#include <MQTT.h>
+#include <IotWebConf.h>
+
#define ADC_FACTOR (3.5f)
#define ADC_VCC_PIN (ADC2_CHANNEL_9)
@@ -39,6 +42,12 @@ struct soil_data {
bool valid = false;
} soil;
+const char default_name[] = "ULPSoilMonitor";
+const char inital_ap_password[] = "SuperSavePassword";
+
+#define STRING_LEN 128
+#define CONFIG_VERSION "ulpsm02"
+
/* This function is called once after power-on reset, to load ULP program into
RTC memory and configure the ADC.
*/
@@ -62,9 +71,37 @@ static void calculate_soil_data();
/* logs current soil_data */
static void print_soil_data();
+static void setup_webconf();
+
+void wifi_connected();
+void config_saved();
+
+void goto_sleep();
+
+DNSServer dns;
+WebServer server(80);
+HTTPUpdateServer http_updater;
+WiFiClient net;
+MQTTClient mqtt_client;
+
+char mqtt_server[STRING_LEN];
+char mqtt_user_name[STRING_LEN];
+
+IotWebConf webconf(default_name, &dns, &server, inital_ap_password, CONFIG_VERSION);
+IotWebConfParameter param_mqtt_server = IotWebConfParameter("MQTT server", "mqtt_server", mqtt_server, STRING_LEN);
+
+boolean do_connect_mqtt = false;
+boolean do_reset = false;
+
+unsigned long last_mqtt_connection_attempt = 0;
+
void setup() {
Serial.begin(115200);
+ measure_vcc();
+
+ setup_webconf();
+
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
if (cause == ESP_SLEEP_WAKEUP_ULP) {
@@ -72,7 +109,6 @@ void setup() {
/* Count temperature form -5 ℃ , so ulp_temperature minus 5 */
Serial.printf("max_diff:%d\n", (uint16_t)ulp_max_diff);
- measure_vcc();
calculate_soil_data();
print_soil_data();
@@ -83,14 +119,47 @@ void setup() {
}
- Serial.printf("Entering deep sleep\n\n");
- start_ulp_program();
- ESP_ERROR_CHECK(esp_sleep_enable_ulp_wakeup());
- esp_deep_sleep_start();
}
void loop() {
+ webconf.doLoop();
+ mqtt_client.loop();
+
+ if (do_connect_mqtt) {
+ if (connect_mqtt()) {
+ do_connect_mqtt = false;
+ }
+ } else if (webconf.getState() == IOTWEBCONF_STATE_ONLINE && !mqtt_client.connected()) {
+ connect_mqtt();
+ }
+
+ if (do_reset) {
+ webconf.delay(1000);
+ ESP.restart();
+ }
+ if (mqtt_client.connected()) {
+ Serial.println("Sending data via MQTT.");
+ String topic_prefix = String("/ULPSoilMonitor/") + webconf.getThingName() + String("/");
+ if (soil.valid) {
+ mqtt_client.publish(topic_prefix + "vcc", String(soil.vcc));
+ mqtt_client.publish(topic_prefix + "soil0", String(soil.soil0));
+ mqtt_client.publish(topic_prefix + "soil1", String(soil.soil1));
+ mqtt_client.publish(topic_prefix + "soil2", String(soil.soil2));
+ mqtt_client.publish(topic_prefix + "soil3", String(soil.soil3));
+ mqtt_client.publish(topic_prefix + "soil4", String(soil.soil4));
+ mqtt_client.publish(topic_prefix + "soil5", String(soil.soil5));
+ } else {
+ mqtt_client.publish(topic_prefix + "vcc", String(NAN));
+ mqtt_client.publish(topic_prefix + "soil0", String(NAN));
+ mqtt_client.publish(topic_prefix + "soil1", String(NAN));
+ mqtt_client.publish(topic_prefix + "soil2", String(NAN));
+ mqtt_client.publish(topic_prefix + "soil3", String(NAN));
+ mqtt_client.publish(topic_prefix + "soil4", String(NAN));
+ mqtt_client.publish(topic_prefix + "soil5", String(NAN));
+ }
+ goto_sleep();
+ }
}
static void init_ulp_program()
@@ -220,3 +289,97 @@ static void print_soil_data()
Serial.printf("Soil4:%d -> %f\n", (uint16_t)ulp_soil4, soil.soil4);
Serial.printf("Soil5:%d -> %f\n", (uint16_t)ulp_soil5, soil.soil5);
}
+
+static void setup_webconf()
+{
+ webconf.addParameter(&param_mqtt_server);
+ webconf.setConfigSavedCallback(&config_saved);
+ webconf.setWifiConnectionCallback(&wifi_connected);
+ webconf.setupUpdateServer(&http_updater);
+ webconf.setWifiConnectionTimeoutMs(250);
+ webconf.setApTimeoutMs(1);
+
+ boolean config_valid = webconf.init();
+ if (!config_valid) {
+ mqtt_server[0] = '\0';
+
+ Serial.println("Config invalid");
+ }
+
+ webconf.setApTimeoutMs(500);
+
+ server.on("/", handle_root);
+ server.on("/config", []{ webconf.handleConfig(); });
+ server.onNotFound([](){ webconf.handleNotFound(); });
+
+ mqtt_client.begin(mqtt_server, net);
+
+ net.setNoDelay(true);
+}
+
+void handle_root()
+{
+ if (webconf.handleCaptivePortal()) {
+ // -- Captive portal request were already served.
+ return;
+ }
+
+ String s = "<!DOCTYPE html><html lang=\"en\"><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\"/>";
+ s += "<title>ULPSoilMonitor</title></head><body>MQTT App demo";
+ s += "<ul>";
+ s += "<li>MQTT server: ";
+ s += mqtt_server;
+ s += "</ul>";
+ s += "Go to <a href='config'>configure page</a> to change values.";
+ s += "</body></html>\n";
+
+ server.send(200, "text/html", s);
+}
+
+void wifi_connected()
+{
+ do_connect_mqtt = true;
+}
+
+void config_saved()
+{
+ do_reset = true;
+}
+
+boolean connect_mqtt()
+{
+ unsigned long now = millis();
+
+ if (1000 > now - last_mqtt_connection_attempt) {
+ return false;
+ }
+
+ Serial.println("Connecting to MQTT server...");
+ if (!mqtt_client.connect(webconf.getThingName())) {
+ last_mqtt_connection_attempt = now;
+ return false;
+ }
+
+ Serial.println("Connected to MQTT server!");
+ return true;
+}
+
+
+void goto_sleep()
+{
+ if (mqtt_client.connected()) {
+ mqtt_client.loop();
+ net.flush();
+ mqtt_client.disconnect();
+ mqtt_client.loop();
+ }
+
+ net.flush();
+ net.stop();
+ delay(20);
+
+ Serial.printf("Entering deep sleep\n\n");
+ start_ulp_program();
+ ESP_ERROR_CHECK(esp_sleep_enable_ulp_wakeup());
+ esp_deep_sleep_start();
+}