From c6e782c1894fd1b16356b7e6db674e1ba669dfbd Mon Sep 17 00:00:00 2001 From: Eric Duminil Date: Fri, 25 Dec 2020 13:48:24 +0100 Subject: [PATCH] Show LoRaWAN info on web server --- lorawan.cpp | 23 ++++++++--------------- lorawan.h | 6 ++++++ mqtt.cpp | 4 ++-- web_server.cpp | 8 ++++++++ web_server.h | 3 +++ 5 files changed, 27 insertions(+), 17 deletions(-) diff --git a/lorawan.cpp b/lorawan.cpp index b2765df..898c5e3 100644 --- a/lorawan.cpp +++ b/lorawan.cpp @@ -3,7 +3,7 @@ namespace config { // Values should be defined in config.h - uint16_t lora_sending_interval = LORAWAN_SENDING_INTERVAL; // [s] + uint16_t lorawan_sending_interval = LORAWAN_SENDING_INTERVAL; // [s] static const u1_t PROGMEM APPEUI[8] = LORAWAN_APPLICATION_EUI; static const u1_t PROGMEM DEVEUI[8] = LORAWAN_DEVICE_EUI; @@ -21,7 +21,6 @@ const lmic_pinmap lmic_pins = { .nss = 18, .rxtx = LMIC_UNUSED_PIN, .rst = 14, . // co2ampel-test/devices/esp3a7c94/up {"app_id":"co2ampel-test","dev_id":"esp3a7c94","hardware_serial":"00xxxxxxxx","port":1,"counter":5,"payload_raw":"TJd7","payload_fields":{"co2":760,"rh":61.5,"temp":20.2},"metadata":{"time":"2020-12-23T23:00:51.44020438Z","frequency":867.5,"modulation":"LORA","data_rate":"SF7BW125","airtime":51456000,"coding_rate":"4/5","gateways":[{"gtw_id":"eui-xxxxxxxxxxxxxxxxxx","timestamp":1765406908,"time":"2020-12-23T23:00:51.402519Z","channel":5,"rssi":-64,"snr":7.5,"rf_chain":0,"latitude":22.7,"longitude":114.24,"altitude":450}]}} // More info : https://www.thethingsnetwork.org/docs/applications/mqtt/quick-start.html -//TODO: Add infos to webserver (last timestamp, sending interval, connected or not?) //TODO: Test with config.public.h //TODO: Test with EPS8266 @@ -39,6 +38,8 @@ void os_getDevKey(u1_t *buf) { namespace lorawan { bool waiting_for_confirmation = false; + bool connected = false; + String last_transmission = ""; void printHex2(unsigned v) { v &= 0xff; @@ -48,18 +49,16 @@ namespace lorawan { } void onEvent(ev_t ev) { - static bool trying_to_join = false; Serial.print("LoRa - "); Serial.print(ntp::getLocalTime()); Serial.print(" - "); switch (ev) { case EV_JOINING: - trying_to_join = true; Serial.println(F("EV_JOINING")); break; case EV_JOINED: - trying_to_join = false; waiting_for_confirmation = false; + connected = true; LedEffects::onBoardLEDOff(); Serial.println(F("EV_JOINED")); { @@ -100,17 +99,11 @@ namespace lorawan { Serial.println(F("EV_REJOIN_FAILED")); break; case EV_TXCOMPLETE: - Serial.println(F("EV_TXCOMPLETE (includes waiting for RX windows)")); - if (LMIC.txrxFlags & TXRX_ACK) - Serial.println(F("Received ack")); - if (LMIC.dataLen) { - Serial.print(F("Received ")); - Serial.print(LMIC.dataLen); - Serial.println(F(" bytes of payload")); - } + last_transmission = ntp::getLocalTime(); + Serial.println(F("EV_TXCOMPLETE")); break; case EV_TXSTART: - waiting_for_confirmation = trying_to_join; + waiting_for_confirmation = !connected; Serial.println(F("EV_TXSTART")); break; case EV_TXCANCELED: @@ -195,7 +188,7 @@ namespace lorawan { void preparePayloadIfTimehasCome() { static unsigned long last_sent_at = 0; unsigned long now = seconds(); - if (now - last_sent_at > config::lora_sending_interval) { + if (now - last_sent_at > config::lorawan_sending_interval) { last_sent_at = now; preparePayload(); } diff --git a/lorawan.h b/lorawan.h index 4d4d813..fdbcf45 100644 --- a/lorawan.h +++ b/lorawan.h @@ -16,8 +16,14 @@ typedef uint8_t u1_t; #include "util.h" +namespace config { + extern uint16_t lorawan_sending_interval; // [s] +} + namespace lorawan { extern bool waiting_for_confirmation; + extern bool connected; + extern String last_transmission; void initialize(); void process(); void preparePayloadIfTimehasCome(); diff --git a/mqtt.cpp b/mqtt.cpp index ff8808b..21eeac9 100644 --- a/mqtt.cpp +++ b/mqtt.cpp @@ -46,10 +46,10 @@ namespace mqtt { snprintf(payload, sizeof(payload), json_sensor_format, timestamp.c_str(), co2, temperature, humidity); // Topic is the same as clientID. e.g. 'CO2sensors/ESP3d03da' if (mqttClient.publish(publish_topic.c_str(), payload)) { - Serial.println("OK"); + Serial.println(F("OK")); last_successful_publish = ntp::getLocalTime(); } else { - Serial.println("Failed."); + Serial.println(F("Failed.")); } LedEffects::onBoardLEDOff(); } diff --git a/web_server.cpp b/web_server.cpp index 8b80251..d2458f5 100644 --- a/web_server.cpp +++ b/web_server.cpp @@ -87,6 +87,11 @@ namespace web_server { #ifdef MQTT "Last MQTT publish%s\n" "MQTT publish timestep%5d s\n" +#endif +#if defined(LORAWAN) && defined(ESP32) + "Connected to LoRaWAN?%s\n" + "Last LoRaWAN transmission%s\n" + "LoRaWAN publish timestep%5d s\n" #endif "Temperature offset%.1fK\n" //TODO: Read it from sensor? "Local address%s.local\n" @@ -196,6 +201,9 @@ namespace web_server { csv_writer::last_successful_write.c_str(), config::csv_interval, #ifdef MQTT mqtt::last_successful_publish.c_str(), config::sending_interval, +#endif +#if defined(LORAWAN) && defined(ESP32) + lorawan::connected ? "Yes" : "No", lorawan::last_transmission.c_str(), config::lorawan_sending_interval, #endif config::temperature_offset, SENSOR_ID.c_str(), SENSOR_ID.c_str(), WiFi.localIP().toString().c_str(), WiFi.localIP().toString().c_str(), get_free_heap_size(), available_fs_space, max_loop_duration, BOARD, hh, mm, diff --git a/web_server.h b/web_server.h index 124253d..1bf4d1c 100644 --- a/web_server.h +++ b/web_server.h @@ -13,6 +13,9 @@ #ifdef MQTT # include "mqtt.h" #endif +#ifdef LORAWAN +# include "lorawan.h" +#endif namespace web_server { void initialize(); -- GitLab