Commit c6e782c1 authored by Eric Duminil's avatar Eric Duminil
Browse files

Show LoRaWAN info on web server

parent bf5954da
......@@ -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();
}
......
......@@ -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();
......
......@@ -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();
}
......
......@@ -87,6 +87,11 @@ namespace web_server {
#ifdef MQTT
"<tr><td>Last MQTT publish</td><td>%s</td></tr>\n"
"<tr><td>MQTT publish timestep</td><td>%5d s</td></tr>\n"
#endif
#if defined(LORAWAN) && defined(ESP32)
"<tr><td>Connected to LoRaWAN?</td><td>%s</td></tr>\n"
"<tr><td>Last LoRaWAN transmission</td><td>%s</td></tr>\n"
"<tr><td>LoRaWAN publish timestep</td><td>%5d s</td></tr>\n"
#endif
"<tr><td>Temperature offset</td><td>%.1fK</td></tr>\n" //TODO: Read it from sensor?
"<tr><td>Local address</td><td><a href='http://%s.local/'>%s.local</a></td></tr>\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,
......
......@@ -13,6 +13,9 @@
#ifdef MQTT
# include "mqtt.h"
#endif
#ifdef LORAWAN
# include "lorawan.h"
#endif
namespace web_server {
void initialize();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment