summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Baumann <derflob@derflob.de>2020-06-24 23:51:41 +0200
committerFlorian Baumann <derflob@derflob.de>2020-06-24 23:51:41 +0200
commit09fcd77567728ec60c4e04c1ac0e261120df1077 (patch)
treea0b74309333c2e80d27dbb0f0b197b4a14d27fa9
parent06c4507a717616feaf57e70d84763c2145556e4d (diff)
downloadULPSoilMonitor-09fcd77567728ec60c4e04c1ac0e261120df1077.tar.gz
ULPSoilMonitor-09fcd77567728ec60c4e04c1ac0e261120df1077.tar.bz2
add reboot and ulp run counter
-rw-r--r--ULPSoilMonitor.ino46
-rw-r--r--ulp_soil.h3
-rw-r--r--ulp_soil.s16
3 files changed, 64 insertions, 1 deletions
diff --git a/ULPSoilMonitor.ino b/ULPSoilMonitor.ino
index 7349381..aeb414c 100644
--- a/ULPSoilMonitor.ino
+++ b/ULPSoilMonitor.ino
@@ -78,12 +78,18 @@ static void print_soil_data();
/* publish sensor config */
static void publish_sensor_config();
+static void publish_system_config();
+
/* publish current soil_data via MQTT */
static void publish_soil_data();
+static void publish_system_data();
+
void setup() {
Serial.begin(115200);
+ ulp_reboots += 1;
+
WiFi.onEvent([](WiFiEvent_t event, WiFiEventInfo_t info) {
Serial.printf("WiFi connected & got IP\n");
mqtt.connect(HOSTNAME);
@@ -129,6 +135,9 @@ void loop() {
esp_err_t err = ulptool_load_binary(0, ulp_main_bin_start, (ulp_main_bin_end - ulp_main_bin_start) / sizeof(uint32_t));
ESP_ERROR_CHECK(err);
+ ulp_reboots = 0;
+ ulp_runs = 0;
+
/* Set ULP wake up period */
ulp_set_wakeup_period(0, ULP_PERIOD_MS * 1000);
@@ -175,12 +184,14 @@ void loop() {
if (state == ULPSM_PUBLISHING_CONFIG) {
Serial.printf("Publishing config...\n");
publish_sensor_config();
+ publish_system_config();
state = ULPSM_PUBLISHED_CONFIG;
}
if (state == ULPSM_PUBLISHING_DATA) {
Serial.printf("Publishing data...\n");
publish_soil_data();
+ publish_system_data();
state = ULPSM_PUBLISHED_DATA;
}
@@ -204,6 +215,8 @@ void loop() {
if (state == ULPSM_DONE) {
Serial.printf("Starting ULP & entering deep sleep\n\n");
+
+ ulp_runs = 0;
start_ulp_program();
ESP_ERROR_CHECK(esp_sleep_enable_ulp_wakeup());
@@ -416,6 +429,32 @@ static void publish_sensor_config()
publish_sensor_config_soil(index);
}
+static void publish_system_config_entry(String name, String uom)
+{
+ String topic = get_topic(name);
+ String id = get_unique_id(name);
+
+ StaticJsonDocument<512> config;
+ config["unique_id"] = id;
+ config["name"] = id;
+ config["state_topic"] = topic + "state";
+ config["unit_of_measurement"] = uom;
+
+ JsonObject dev = config.createNestedObject("device");
+ get_device(&dev);
+
+ size_t message_size = measureJson(config);
+ mqtt.beginPublish((topic + "config").c_str(), message_size, true);
+ serializeJson(config, mqtt);
+ mqtt.endPublish();
+}
+
+static void publish_system_config()
+{
+ publish_system_config_entry(String("reboots"), String("#"));
+ publish_system_config_entry(String("ulp_runs"), String("#"));
+}
+
static void publish_vcc()
{
String topic = get_topic("vcc");
@@ -448,3 +487,10 @@ static void publish_soil_data()
publish_soil(4, soil.soil4);
publish_soil(5, soil.soil5);
}
+
+static void publish_system_data()
+{
+ mqtt.publish((get_topic(String("reboots")) + "state").c_str(), String((uint16_t)ulp_reboots).c_str(), true);
+ mqtt.publish((get_topic(String("ulp_runs")) + "state").c_str(), String((uint16_t)ulp_runs).c_str(), true);
+
+}
diff --git a/ulp_soil.h b/ulp_soil.h
index 70e069e..0c5f032 100644
--- a/ulp_soil.h
+++ b/ulp_soil.h
@@ -8,3 +8,6 @@ extern uint32_t ulp_soil2;
extern uint32_t ulp_soil3;
extern uint32_t ulp_soil4;
extern uint32_t ulp_soil5;
+
+extern uint32_t ulp_reboots;
+extern uint32_t ulp_runs;
diff --git a/ulp_soil.s b/ulp_soil.s
index d054d3a..f6244e4 100644
--- a/ulp_soil.s
+++ b/ulp_soil.s
@@ -76,12 +76,26 @@ soil5:
soil5_shadow:
.long 0
-
+
+ .global reboots
+reboots:
+ .long 0
+
+ .global runs
+runs:
+ .long 0
+
/* Entry point into code. */
.text
.global entry
entry:
+ /* increment ulp_runs counter by one */
+ move r0, runs
+ ld r1, r0, 0
+ add r1, r1, 1
+ st r1, r0, 0
+
/* set GPIO25/RTC_GPIO06 low to enable moisture sensors */
enable_sensors:
WRITE_RTC_REG(RTC_IO_PAD_DAC1_REG, RTC_IO_PDAC1_HOLD_S, 1, 0)