Merge pull request #3 from ESPresense/wip
Make maxDistance configurable, move around mqtt topics
This commit is contained in:
commit
bf2a792a55
|
@ -24,8 +24,8 @@ binary_sensor:
|
||||||
# One entry per sensor node to understand when the device is online/offline and see device metadata such as IP address and settings values
|
# One entry per sensor node to understand when the device is online/offline and see device metadata such as IP address and settings values
|
||||||
- platform: mqtt
|
- platform: mqtt
|
||||||
name: ESP32 A
|
name: ESP32 A
|
||||||
state_topic: "espresense/esp32_a/telemetry/availability"
|
state_topic: "espresense/rooms/esp32_a/status"
|
||||||
json_attributes_topic: "espresense/esp32_a/telemetry
|
json_attributes_topic: "espresense/rooms/esp32_a/telemetry"
|
||||||
payload_on: "online"
|
payload_on: "online"
|
||||||
payload_off: "offline"
|
payload_off: "offline"
|
||||||
device_class: connectivity
|
device_class: connectivity
|
||||||
|
|
|
@ -46,7 +46,7 @@ Stopping wifi reconnect timer
|
||||||
If your device won't connect to your wireless network, copy and paste any logs you see into your issue.
|
If your device won't connect to your wireless network, copy and paste any logs you see into your issue.
|
||||||
|
|
||||||
### ESP32 MQTT (yellow arrow)
|
### ESP32 MQTT (yellow arrow)
|
||||||
To report devices, the ESP32 must be connected to your MQTT server. It will attempt to connect once it has established a connection to your wireless network. Once connected, the device will publish a status message ("CONNECTED") to the `availabilityTopic` as defined in your settings. It also publishes your configuration information to the `telemetryTopic` which consists of:
|
To report devices, the ESP32 must be connected to your MQTT server. It will attempt to connect once it has established a connection to your wireless network. Once connected, the device will publish a status message ("CONNECTED") to the `statusTopic` as defined in your settings. It also publishes your configuration information to the `telemetryTopic` which consists of:
|
||||||
* **room**: the configured room name
|
* **room**: the configured room name
|
||||||
* **ip**: the assigned IP address of the device
|
* **ip**: the assigned IP address of the device
|
||||||
* **hostname**: the host name, used for WiFi as well as client ID for MQTT (must be unique on your network)
|
* **hostname**: the host name, used for WiFi as well as client ID for MQTT (must be unique on your network)
|
||||||
|
@ -58,7 +58,7 @@ To report devices, the ESP32 must be connected to your MQTT server. It will atte
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
If you do not see any information being published to either the `availabilityTopic` or `telemetryTopic` then you will want to connect to the serial monitor and check the logs. Verify that your MQTT user name and password is correct, and check the logs on your MQTT server itself. You should see it reporting a new client connected from the ESP32's IP address:
|
If you do not see any information being published to either the `statusTopic` or `telemetryTopic` then you will want to connect to the serial monitor and check the logs. Verify that your MQTT user name and password is correct, and check the logs on your MQTT server itself. You should see it reporting a new client connected from the ESP32's IP address:
|
||||||
```
|
```
|
||||||
New client connected from 192.168.1.104 as esp32_d (c1, k60, u'my_mqtt_username').
|
New client connected from 192.168.1.104 as esp32_d (c1, k60, u'my_mqtt_username').
|
||||||
```
|
```
|
||||||
|
|
|
@ -90,9 +90,10 @@ BleFingerprint::BleFingerprint(BLEAdvertisedDevice *advertisedDevice, float fcmi
|
||||||
int major = ENDIAN_CHANGE_U16(oBeacon.getMajor());
|
int major = ENDIAN_CHANGE_U16(oBeacon.getMajor());
|
||||||
int minor = ENDIAN_CHANGE_U16(oBeacon.getMinor());
|
int minor = ENDIAN_CHANGE_U16(oBeacon.getMinor());
|
||||||
|
|
||||||
id = "iBeacon:" + proximityUUID + "-" + String(major) + "-" + String(minor);
|
id = "iBeacon:" + proximityUUID;
|
||||||
Serial.printf(", ID: %s", id.c_str());
|
Serial.printf(", ID: %s", id.c_str());
|
||||||
calRssi = oBeacon.getSignalPower();
|
calRssi = oBeacon.getSignalPower();
|
||||||
|
if (calRssi > 0) calRssi = defaultTxPower;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -187,7 +188,7 @@ bool BleFingerprint::report(JsonDocument *doc, int maxDistance)
|
||||||
|
|
||||||
auto now = esp_timer_get_time();
|
auto now = esp_timer_get_time();
|
||||||
|
|
||||||
if (abs(output.value.position - lastReported) < 0.05f && abs(now - lastReportedMicros) < 5000000)
|
if (abs(output.value.position - lastReported) < 0.1f && abs(now - lastReportedMicros) < 15000000)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
lastReportedMicros = now;
|
lastReportedMicros = now;
|
||||||
|
@ -197,16 +198,16 @@ bool BleFingerprint::report(JsonDocument *doc, int maxDistance)
|
||||||
String mac = SMacf(address);
|
String mac = SMacf(address);
|
||||||
if (output.value.position < 0.5)
|
if (output.value.position < 0.5)
|
||||||
{
|
{
|
||||||
if (!enroll)
|
if (!close)
|
||||||
{
|
{
|
||||||
Serial.printf("%d Enter | MAC: %s, ID: %-50s %lu %5.1f %5.1f %5.1f\n", xPortGetCoreID(), mac.c_str(), id.c_str(), output.timestamp, output.value.position, output.value.speed * 1e6, output.value.acceleration * 1e12);
|
Display.close(mac.c_str(), id.c_str());
|
||||||
enroll = true;
|
close = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (enroll && output.value.position > 1.5)
|
else if (close && output.value.position > 1.5)
|
||||||
{
|
{
|
||||||
Serial.printf("%d Left | MAC: %s, ID: %-50s %lu %5.1f %5.1f %5.1f\n", xPortGetCoreID(), mac.c_str(), id.c_str(), output.timestamp, output.value.position, output.value.speed * 1e6, output.value.acceleration * 1e12);
|
Display.left(mac.c_str(), id.c_str());
|
||||||
enroll = false;
|
close = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id != nullptr)
|
if (id != nullptr)
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#define _BLEFINGERPRINT_
|
#define _BLEFINGERPRINT_
|
||||||
|
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
#include <GUI.h>
|
||||||
#include <NimBLEAdvertisedDevice.h>
|
#include <NimBLEAdvertisedDevice.h>
|
||||||
#include <NimBLEBeacon.h>
|
#include <NimBLEBeacon.h>
|
||||||
#include <NimBLEDevice.h>
|
#include <NimBLEDevice.h>
|
||||||
|
@ -30,7 +31,7 @@ public:
|
||||||
long getLastSeen() { return lastSeenMicros; };
|
long getLastSeen() { return lastSeenMicros; };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool hasValue = false, enroll = false, reported = false;
|
bool hasValue = false, close = false, reported = false;
|
||||||
NimBLEAddress address;
|
NimBLEAddress address;
|
||||||
String id, name, url;
|
String id, name, url;
|
||||||
int rssi = -100, calRssi = 0;
|
int rssi = -100, calRssi = 0;
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
#include "GUI.h"
|
||||||
|
|
||||||
|
GUI Display;
|
||||||
|
|
||||||
|
void GUI::connected(bool wifi = false, bool mqtt = false)
|
||||||
|
{
|
||||||
|
status("Wifi: %s Mqtt: %s", (wifi ? "no" : "yes"), (wifi ? "no" : "yes"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void GUI::close(const char *mac, const char *id)
|
||||||
|
{
|
||||||
|
Serial.printf("%d Close | MAC: %s, ID: %-50s\n", xPortGetCoreID(), mac, id);
|
||||||
|
status("C: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GUI::left(const char *mac, const char *id)
|
||||||
|
{
|
||||||
|
Serial.printf("%d Left | MAC: %s, ID: %-50s\n", xPortGetCoreID(), mac, id);
|
||||||
|
status("L: %s", id);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GUI::status(const char *format, ...)
|
||||||
|
{
|
||||||
|
#ifdef M5STICK
|
||||||
|
sprite.fillSprite(TFT_BLACK);
|
||||||
|
sprite.setTextDatum(MC_DATUM);
|
||||||
|
#ifdef PLUS
|
||||||
|
char *message;
|
||||||
|
va_list args;
|
||||||
|
va_start(args, format);
|
||||||
|
vasprintf(&message, format, args);
|
||||||
|
va_end(args);
|
||||||
|
sprite.drawString(message, sprite.width() / 2, sprite.height() / 2, 4);
|
||||||
|
#else
|
||||||
|
sprite.drawString(message, sprite.width() / 2, sprite.height() / 2, 1);
|
||||||
|
#endif
|
||||||
|
free(message);
|
||||||
|
dirty = true;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void GUI::update()
|
||||||
|
{
|
||||||
|
#ifdef M5STICK
|
||||||
|
if (!init)
|
||||||
|
{
|
||||||
|
M5.begin(true, true, false);
|
||||||
|
M5.Lcd.setRotation(3);
|
||||||
|
sprite.createSprite(M5.Lcd.width(), M5.Lcd.height());
|
||||||
|
sprite.setSwapBytes(true);
|
||||||
|
init = true;
|
||||||
|
}
|
||||||
|
if (dirty)
|
||||||
|
{
|
||||||
|
sprite.pushSprite(0, 0);
|
||||||
|
M5.Axp.ScreenBreath(12);
|
||||||
|
dirty = false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
#ifndef GUI_h
|
||||||
|
#define GUI_h
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#ifdef M5STICK
|
||||||
|
#ifdef PLUS
|
||||||
|
#include <M5StickCPlus.h>
|
||||||
|
#else
|
||||||
|
#include <M5StickC.h>
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
class GUI
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void close(const char *mac, const char *id);
|
||||||
|
void left(const char *mac, const char *id);
|
||||||
|
void status(const char *message, ...);
|
||||||
|
void connected(bool wifi, bool mqtt);
|
||||||
|
void update();
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool init;
|
||||||
|
bool dirty;
|
||||||
|
#ifdef M5STICK
|
||||||
|
TFT_eSprite sprite = TFT_eSprite(&M5.Lcd);
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
extern GUI Display;
|
|
@ -28,9 +28,11 @@ board = esp32dev
|
||||||
lib_deps = ${common_env_data.lib_deps_external}
|
lib_deps = ${common_env_data.lib_deps_external}
|
||||||
board_build.partitions = partitions_singleapp.csv
|
board_build.partitions = partitions_singleapp.csv
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
monitor_filters = esp32_exception_decoder
|
||||||
build_flags =
|
build_flags =
|
||||||
-D FIRMWARE='"esp32"'
|
-D FIRMWARE='"esp32"'
|
||||||
-Wall
|
-Wall
|
||||||
|
|
||||||
[env:esp32-noupdate]
|
[env:esp32-noupdate]
|
||||||
platform = espressif32
|
platform = espressif32
|
||||||
framework = arduino
|
framework = arduino
|
||||||
|
@ -42,6 +44,7 @@ build_flags =
|
||||||
-D FIRMWARE='"esp32-noupdate"'
|
-D FIRMWARE='"esp32-noupdate"'
|
||||||
-D NOUPDATE
|
-D NOUPDATE
|
||||||
-Wall
|
-Wall
|
||||||
|
|
||||||
[env:esp32-verbose]
|
[env:esp32-verbose]
|
||||||
platform = espressif32
|
platform = espressif32
|
||||||
framework = arduino
|
framework = arduino
|
||||||
|
@ -49,10 +52,12 @@ board = esp32dev
|
||||||
lib_deps = ${common_env_data.lib_deps_external}
|
lib_deps = ${common_env_data.lib_deps_external}
|
||||||
board_build.partitions = partitions_singleapp.csv
|
board_build.partitions = partitions_singleapp.csv
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
monitor_filters = esp32_exception_decoder
|
||||||
build_flags =
|
build_flags =
|
||||||
-D FIRMWARE='"esp32-verbose"'
|
-D FIRMWARE='"esp32-verbose"'
|
||||||
-D VERBOSE
|
-D VERBOSE
|
||||||
-Wall
|
-Wall
|
||||||
|
|
||||||
[env:m5stickc]
|
[env:m5stickc]
|
||||||
platform = espressif32
|
platform = espressif32
|
||||||
framework = arduino
|
framework = arduino
|
||||||
|
@ -62,10 +67,12 @@ lib_deps =
|
||||||
${common_env_data.lib_deps_external}
|
${common_env_data.lib_deps_external}
|
||||||
board_build.partitions = partitions_singleapp.csv
|
board_build.partitions = partitions_singleapp.csv
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
monitor_filters = esp32_exception_decoder
|
||||||
build_flags =
|
build_flags =
|
||||||
-D M5STICK
|
-D M5STICK
|
||||||
-D FIRMWARE='"m5stickc"'
|
-D FIRMWARE='"m5stickc"'
|
||||||
-Wall
|
-Wall
|
||||||
|
|
||||||
[env:m5stickc-plus]
|
[env:m5stickc-plus]
|
||||||
platform = espressif32
|
platform = espressif32
|
||||||
framework = arduino
|
framework = arduino
|
||||||
|
@ -75,11 +82,13 @@ lib_deps =
|
||||||
${common_env_data.lib_deps_external}
|
${common_env_data.lib_deps_external}
|
||||||
board_build.partitions = partitions_singleapp.csv
|
board_build.partitions = partitions_singleapp.csv
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
monitor_filters = esp32_exception_decoder
|
||||||
build_flags =
|
build_flags =
|
||||||
-D M5STICK
|
-D M5STICK
|
||||||
-D PLUS
|
-D PLUS
|
||||||
-D FIRMWARE='"m5stickc-plus"'
|
-D FIRMWARE='"m5stickc-plus"'
|
||||||
-Wall
|
-Wall
|
||||||
|
|
||||||
[env:m5atom-matrix]
|
[env:m5atom-matrix]
|
||||||
platform = espressif32
|
platform = espressif32
|
||||||
framework = arduino
|
framework = arduino
|
||||||
|
@ -88,6 +97,7 @@ lib_deps =
|
||||||
${common_env_data.lib_deps_external}
|
${common_env_data.lib_deps_external}
|
||||||
board_build.partitions = partitions_singleapp.csv
|
board_build.partitions = partitions_singleapp.csv
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
monitor_filters = esp32_exception_decoder
|
||||||
build_flags =
|
build_flags =
|
||||||
-D M5ATOM
|
-D M5ATOM
|
||||||
-D MATRIX
|
-D MATRIX
|
||||||
|
|
|
@ -11,9 +11,6 @@
|
||||||
//Replace with your Wifi password; example: #define password "12345678"
|
//Replace with your Wifi password; example: #define password "12345678"
|
||||||
#define DEFAULT_WIFI_PASSWORD "$WIFI_PASSWORD$"
|
#define DEFAULT_WIFI_PASSWORD "$WIFI_PASSWORD$"
|
||||||
|
|
||||||
//Replace with a human-friendly host name. Must not contain spaces or special characters and be unique on your network
|
|
||||||
#define DEFAULT_HOSTNAME "esp32_room_presence"
|
|
||||||
|
|
||||||
//Replace with your MQTT Broker address
|
//Replace with your MQTT Broker address
|
||||||
#define DEFAULT_MQTT_HOST "mqtt.z13.org"
|
#define DEFAULT_MQTT_HOST "mqtt.z13.org"
|
||||||
|
|
||||||
|
@ -26,8 +23,8 @@
|
||||||
//Replace with your MQTT Broker password
|
//Replace with your MQTT Broker password
|
||||||
#define DEFAULT_MQTT_PASSWORD ""
|
#define DEFAULT_MQTT_PASSWORD ""
|
||||||
|
|
||||||
//Replace with the room name where the node will be placed
|
// Maximum distance (in meters) to report. Devices that are calculated to be further than this distance in meters will not be reported
|
||||||
#define DEFAULT_ROOM "living-room"
|
#define DEFAULT_MAX_DISTANCE 16
|
||||||
|
|
||||||
#ifdef M5STICK
|
#ifdef M5STICK
|
||||||
|
|
||||||
|
@ -69,9 +66,6 @@
|
||||||
#define BLE_SCAN_INTERVAL 40 // Used to determine antenna sharing between Bluetooth and WiFi. Do not modify unless you are confident you know what you're doing
|
#define BLE_SCAN_INTERVAL 40 // Used to determine antenna sharing between Bluetooth and WiFi. Do not modify unless you are confident you know what you're doing
|
||||||
#define BLE_SCAN_WINDOW 30 // Used to determine antenna sharing between Bluetooth and WiFi. Do not modify unless you are confident you know what you're doing
|
#define BLE_SCAN_WINDOW 30 // Used to determine antenna sharing between Bluetooth and WiFi. Do not modify unless you are confident you know what you're doing
|
||||||
|
|
||||||
// Maximum distance (in meters) to report. Devices that are calculated to be further than this distance in meters will not be reported
|
|
||||||
#define MAX_DISTANCE 16
|
|
||||||
|
|
||||||
// Max number of mac addresses to keep track of
|
// Max number of mac addresses to keep track of
|
||||||
#define MAX_MAC_ADDRESSES 50
|
#define MAX_MAC_ADDRESSES 50
|
||||||
|
|
||||||
|
|
78
src/main.cpp
78
src/main.cpp
|
@ -34,23 +34,29 @@ BleFingerprint *getFingerprint(BLEAdvertisedDevice *advertisedDevice)
|
||||||
|
|
||||||
bool sendTelemetry(int totalSeen = -1, int totalReported = -1, int totalAdverts = -1)
|
bool sendTelemetry(int totalSeen = -1, int totalReported = -1, int totalAdverts = -1)
|
||||||
{
|
{
|
||||||
if (initial)
|
if (!online)
|
||||||
{
|
{
|
||||||
initial = false;
|
if (mqttClient.publish(statusTopic.c_str(), 0, 1, "online"))
|
||||||
if (mqttClient.publish(availabilityTopic.c_str(), 0, 1, "online") == true)
|
|
||||||
{
|
{
|
||||||
Serial.println("Connected to MQTT");
|
online = true;
|
||||||
|
Display.status("Connected to MQTT");
|
||||||
reconnectTries = 0;
|
reconnectTries = 0;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Serial.println(F("Error sending availability"));
|
log_e("Error sending status=online");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
auto now = esp_timer_get_time();
|
||||||
|
|
||||||
|
if (abs(now - lastTeleMicros) < 15000000)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
lastTeleMicros = now;
|
||||||
|
|
||||||
StaticJsonDocument<512> tele;
|
StaticJsonDocument<512> tele;
|
||||||
tele["ip"] = localIp;
|
tele["ip"] = localIp;
|
||||||
tele["hostname"] = WiFi.getHostname();
|
|
||||||
tele["uptime"] = getUptimeSeconds();
|
tele["uptime"] = getUptimeSeconds();
|
||||||
tele["firm"] = String(FIRMWARE);
|
tele["firm"] = String(FIRMWARE);
|
||||||
tele["rssi"] = WiFi.RSSI();
|
tele["rssi"] = WiFi.RSSI();
|
||||||
|
@ -65,8 +71,8 @@ bool sendTelemetry(int totalSeen = -1, int totalReported = -1, int totalAdverts
|
||||||
tele["reported"] = totalReported;
|
tele["reported"] = totalReported;
|
||||||
if (totalAdverts > 0)
|
if (totalAdverts > 0)
|
||||||
tele["adverts"] = totalAdverts;
|
tele["adverts"] = totalAdverts;
|
||||||
if (sendFailures > 0)
|
if (teleFails > 0)
|
||||||
tele["sendFails"] = sendFailures;
|
tele["teleFails"] = teleFails;
|
||||||
if (reconnectTries > 0)
|
if (reconnectTries > 0)
|
||||||
tele["reconnectTries"] = reconnectTries;
|
tele["reconnectTries"] = reconnectTries;
|
||||||
|
|
||||||
|
@ -79,17 +85,15 @@ bool sendTelemetry(int totalSeen = -1, int totalReported = -1, int totalAdverts
|
||||||
char teleMessageBuffer[512];
|
char teleMessageBuffer[512];
|
||||||
serializeJson(tele, teleMessageBuffer);
|
serializeJson(tele, teleMessageBuffer);
|
||||||
|
|
||||||
String teleTopic = CHANNEL + "/" + room + "/telemetry";
|
|
||||||
|
|
||||||
for (int i = 0; i < 10; i++)
|
for (int i = 0; i < 10; i++)
|
||||||
{
|
{
|
||||||
if (mqttClient.publish(teleTopic.c_str(), 0, 0, teleMessageBuffer) == true)
|
if (!publishTele || mqttClient.publish(teleTopic.c_str(), 0, 0, teleMessageBuffer))
|
||||||
return true;
|
return true;
|
||||||
delay(20);
|
delay(50);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendFailures++;
|
teleFails++;
|
||||||
log_e("Error sending telemetry");
|
Serial.printf("Error after 10 tries sending telemetry (%d times since boot)\n", teleFails);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,15 +123,20 @@ void connectToWifi()
|
||||||
mqttUser = WiFiSettings.string("mqtt_user", DEFAULT_MQTT_USER);
|
mqttUser = WiFiSettings.string("mqtt_user", DEFAULT_MQTT_USER);
|
||||||
mqttPass = WiFiSettings.string("mqtt_pass", DEFAULT_MQTT_PASSWORD);
|
mqttPass = WiFiSettings.string("mqtt_pass", DEFAULT_MQTT_PASSWORD);
|
||||||
room = WiFiSettings.string("room", ESPMAC);
|
room = WiFiSettings.string("room", ESPMAC);
|
||||||
|
WiFiSettings.heading("Preferences");
|
||||||
publishTele = WiFiSettings.checkbox("pub_tele", true, "Send to telemetry topic");
|
publishTele = WiFiSettings.checkbox("pub_tele", true, "Send to telemetry topic");
|
||||||
publishRooms = WiFiSettings.checkbox("pub_rooms", true, "Send to rooms topic");
|
publishRooms = WiFiSettings.checkbox("pub_rooms", true, "Send to rooms topic");
|
||||||
publishDevices = WiFiSettings.checkbox("pub_devices", true, "Send to devices topic");
|
publishDevices = WiFiSettings.checkbox("pub_devices", true, "Send to devices topic");
|
||||||
|
maxDistance = WiFiSettings.integer("max_dist", DEFAULT_MAX_DISTANCE, "Maximum distance to report (in meters)");
|
||||||
|
|
||||||
WiFiSettings.hostname = "espresense-" + room;
|
WiFiSettings.hostname = "espresense-" + room;
|
||||||
|
|
||||||
if (!WiFiSettings.connect(true, 60))
|
if (!WiFiSettings.connect(true, 60))
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
|
|
||||||
|
#ifdef VERSION
|
||||||
|
Serial.println("Version: " + String(VERSION));
|
||||||
|
#endif
|
||||||
Serial.print("IP address: ");
|
Serial.print("IP address: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
Serial.print("DNS address: ");
|
Serial.print("DNS address: ");
|
||||||
|
@ -136,26 +145,34 @@ void connectToWifi()
|
||||||
Serial.println(WiFi.getHostname());
|
Serial.println(WiFi.getHostname());
|
||||||
Serial.print("Room: ");
|
Serial.print("Room: ");
|
||||||
Serial.println(room);
|
Serial.println(room);
|
||||||
|
Serial.print("Telemetry: ");
|
||||||
|
Serial.println(publishRooms ? "enabled" : "disabled");
|
||||||
|
Serial.print("Rooms: ");
|
||||||
|
Serial.println(publishRooms ? "enabled" : "disabled");
|
||||||
|
Serial.print("Devices: ");
|
||||||
|
Serial.println(publishDevices ? "enabled" : "disabled");
|
||||||
|
Serial.printf("Max Distance: %d\n", maxDistance);
|
||||||
|
|
||||||
localIp = WiFi.localIP().toString();
|
localIp = WiFi.localIP().toString();
|
||||||
|
roomsTopic = CHANNEL + "/rooms/" + room;
|
||||||
|
statusTopic = roomsTopic + "/status";
|
||||||
|
teleTopic = roomsTopic + "/telemetry";
|
||||||
}
|
}
|
||||||
|
|
||||||
void onMqttConnect(bool sessionPresent)
|
void onMqttConnect(bool sessionPresent)
|
||||||
{
|
{
|
||||||
xTimerStop(reconnectTimer, 0);
|
xTimerStop(reconnectTimer, 0);
|
||||||
initial = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
|
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason)
|
||||||
{
|
{
|
||||||
Serial.printf("Disconnected from MQTT; reason %d\n", (int)reason);
|
log_e("Disconnected from MQTT; reason %d\n", reason);
|
||||||
xTimerStart(reconnectTimer, 0);
|
xTimerStart(reconnectTimer, 0);
|
||||||
|
online = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void reconnect(TimerHandle_t xTimer)
|
void reconnect(TimerHandle_t xTimer)
|
||||||
{
|
{
|
||||||
Serial.println("Reconnecting...");
|
|
||||||
|
|
||||||
if (updateInProgress) return;
|
if (updateInProgress) return;
|
||||||
if (WiFi.isConnected() && mqttClient.connected()) return;
|
if (WiFi.isConnected() && mqttClient.connected()) return;
|
||||||
|
|
||||||
|
@ -166,21 +183,24 @@ void reconnect(TimerHandle_t xTimer)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!WiFi.isConnected())
|
if (!WiFi.isConnected())
|
||||||
|
{
|
||||||
|
Serial.println("Reconnecting to WiFi...");
|
||||||
if (!WiFiSettings.connect(true, 60))
|
if (!WiFiSettings.connect(true, 60))
|
||||||
ESP.restart();
|
ESP.restart();
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("Reconnecting to MQTT...");
|
||||||
mqttClient.connect();
|
mqttClient.connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
void connectToMqtt()
|
void connectToMqtt()
|
||||||
{
|
{
|
||||||
availabilityTopic = CHANNEL + "/" + room + "/telemetry/availability";
|
|
||||||
reconnectTimer = xTimerCreate("reconnectionTimer", pdMS_TO_TICKS(3000), pdTRUE, (void *)0, reconnect);
|
reconnectTimer = xTimerCreate("reconnectionTimer", pdMS_TO_TICKS(3000), pdTRUE, (void *)0, reconnect);
|
||||||
Serial.printf("Connecting to MQTT %s %d\n", mqttHost.c_str(), mqttPort);
|
Serial.printf("Connecting to MQTT %s %d\n", mqttHost.c_str(), mqttPort);
|
||||||
mqttClient.onConnect(onMqttConnect);
|
mqttClient.onConnect(onMqttConnect);
|
||||||
mqttClient.onDisconnect(onMqttDisconnect);
|
mqttClient.onDisconnect(onMqttDisconnect);
|
||||||
mqttClient.setServer(mqttHost.c_str(), mqttPort);
|
mqttClient.setServer(mqttHost.c_str(), mqttPort);
|
||||||
mqttClient.setWill(availabilityTopic.c_str(), 0, 1, "offline");
|
mqttClient.setWill(statusTopic.c_str(), 0, 1, "offline");
|
||||||
mqttClient.setCredentials(mqttUser.c_str(), mqttPass.c_str());
|
mqttClient.setCredentials(mqttUser.c_str(), mqttPass.c_str());
|
||||||
mqttClient.connect();
|
mqttClient.connect();
|
||||||
}
|
}
|
||||||
|
@ -208,14 +228,13 @@ private:
|
||||||
bool reportDevice(BleFingerprint *f)
|
bool reportDevice(BleFingerprint *f)
|
||||||
{
|
{
|
||||||
StaticJsonDocument<512> doc;
|
StaticJsonDocument<512> doc;
|
||||||
if (!f->report(&doc, MAX_DISTANCE))
|
if (!f->report(&doc, maxDistance))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
char JSONmessageBuffer[512];
|
char JSONmessageBuffer[512];
|
||||||
serializeJson(doc, JSONmessageBuffer);
|
serializeJson(doc, JSONmessageBuffer);
|
||||||
|
|
||||||
String publishTopic = CHANNEL + "/" + room;
|
String devicesTopic = CHANNEL + "/devices/" + f->getId() + "/" + room;
|
||||||
String publishTopic2 = CHANNEL + "/devices/" + f->getId() + "/" + room;
|
|
||||||
|
|
||||||
bool p1 = false, p2 = false;
|
bool p1 = false, p2 = false;
|
||||||
for (int i = 0; i < 10; i++)
|
for (int i = 0; i < 10; i++)
|
||||||
|
@ -223,17 +242,17 @@ bool reportDevice(BleFingerprint *f)
|
||||||
if (!mqttClient.connected())
|
if (!mqttClient.connected())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (!p1 && mqttClient.publish((char *)publishTopic.c_str(), 0, 0, JSONmessageBuffer))
|
if (!p1 && (!publishRooms || mqttClient.publish((char *)roomsTopic.c_str(), 0, 0, JSONmessageBuffer)))
|
||||||
p1 = true;
|
p1 = true;
|
||||||
|
|
||||||
if (!p2 && mqttClient.publish((char *)publishTopic2.c_str(), 0, 0, JSONmessageBuffer))
|
if (!p2 && (!publishDevices || mqttClient.publish((char *)devicesTopic.c_str(), 0, 0, JSONmessageBuffer)))
|
||||||
p2 = true;
|
p2 = true;
|
||||||
|
|
||||||
if (p1 && p2)
|
if (p1 && p2)
|
||||||
return true;
|
return true;
|
||||||
delay(20);
|
delay(20);
|
||||||
}
|
}
|
||||||
sendFailures++;
|
teleFails++;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -295,12 +314,6 @@ void setup()
|
||||||
setClock();
|
setClock();
|
||||||
connectToMqtt();
|
connectToMqtt();
|
||||||
xTaskCreatePinnedToCore(scanForDevices, "BLE Scan", 4096, nullptr, 1, &scannerTask, 1);
|
xTaskCreatePinnedToCore(scanForDevices, "BLE Scan", 4096, nullptr, 1, &scannerTask, 1);
|
||||||
|
|
||||||
#ifdef M5STICK
|
|
||||||
M5.begin();
|
|
||||||
M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
configureOTA();
|
configureOTA();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -308,4 +321,5 @@ void loop()
|
||||||
{
|
{
|
||||||
ArduinoOTA.handle();
|
ArduinoOTA.handle();
|
||||||
firmwareUpdate();
|
firmwareUpdate();
|
||||||
|
Display.update();
|
||||||
}
|
}
|
||||||
|
|
18
src/main.h
18
src/main.h
|
@ -22,33 +22,29 @@
|
||||||
#include "BleFingerprint.h"
|
#include "BleFingerprint.h"
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
|
|
||||||
#ifdef M5STICK
|
|
||||||
#ifdef PLUS
|
|
||||||
#include <M5StickCPlus.h>
|
|
||||||
#else
|
|
||||||
#include <M5StickC.h>
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
AsyncMqttClient mqttClient;
|
AsyncMqttClient mqttClient;
|
||||||
TimerHandle_t reconnectTimer;
|
TimerHandle_t reconnectTimer;
|
||||||
TaskHandle_t scannerTask;
|
TaskHandle_t scannerTask;
|
||||||
|
|
||||||
bool updateInProgress = false;
|
bool updateInProgress = false;
|
||||||
String localIp;
|
String localIp;
|
||||||
|
int64_t lastTeleMicros;
|
||||||
int reconnectTries = 0;
|
int reconnectTries = 0;
|
||||||
int sendFailures = 0;
|
int teleFails = 0;
|
||||||
|
bool online; // Have we successfully sent status=online
|
||||||
|
|
||||||
String mqttHost;
|
String mqttHost;
|
||||||
int mqttPort;
|
int mqttPort;
|
||||||
String mqttUser;
|
String mqttUser;
|
||||||
String mqttPass;
|
String mqttPass;
|
||||||
String room;
|
String room;
|
||||||
String availabilityTopic;
|
String statusTopic;
|
||||||
|
String teleTopic;
|
||||||
|
String roomsTopic;
|
||||||
bool publishTele;
|
bool publishTele;
|
||||||
bool publishRooms;
|
bool publishRooms;
|
||||||
bool publishDevices;
|
bool publishDevices;
|
||||||
bool initial = true; // First contact with mqtt
|
int maxDistance;
|
||||||
|
|
||||||
static SemaphoreHandle_t fingerprintSemaphore;
|
static SemaphoreHandle_t fingerprintSemaphore;
|
||||||
static std::list<BleFingerprint *> fingerprints;
|
static std::list<BleFingerprint *> fingerprints;
|
||||||
|
|
Loading…
Reference in New Issue