Updated to ArduinoJson 6
This commit is contained in:
parent
2d9035e441
commit
c1b427c5b6
|
@ -28,10 +28,10 @@ extern "C" {
|
||||||
#include "BLEBeacon.h"
|
#include "BLEBeacon.h"
|
||||||
#include "BLEEddystoneTLM.h"
|
#include "BLEEddystoneTLM.h"
|
||||||
#include "BLEEddystoneURL.h"
|
#include "BLEEddystoneURL.h"
|
||||||
#include "Settings_kitchen.h"
|
#include "Settings_a.h"
|
||||||
|
|
||||||
BLEScan* pBLEScan;
|
BLEScan* pBLEScan;
|
||||||
int scanTime = 3; //In seconds
|
int scanTime = 10; //In seconds
|
||||||
int waitTime = scanInterval; //In seconds
|
int waitTime = scanInterval; //In seconds
|
||||||
bool updateInProgress = false;
|
bool updateInProgress = false;
|
||||||
|
|
||||||
|
@ -89,7 +89,9 @@ float calculateDistance(int rssi, int txPower) {
|
||||||
void connectToWifi() {
|
void connectToWifi() {
|
||||||
Serial.println("Connecting to WiFi...");
|
Serial.println("Connecting to WiFi...");
|
||||||
WiFi.begin(ssid, password);
|
WiFi.begin(ssid, password);
|
||||||
WiFi.setHostname(hostname);
|
bool resp = WiFi.setHostname(hostname);
|
||||||
|
Serial.print("set host name result\t");
|
||||||
|
Serial.println(resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void connectToMqtt() {
|
void connectToMqtt() {
|
||||||
|
@ -105,8 +107,10 @@ void WiFiEvent(WiFiEvent_t event) {
|
||||||
switch(event) {
|
switch(event) {
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
case SYSTEM_EVENT_STA_GOT_IP:
|
||||||
digitalWrite(LED_BUILTIN, !LED_ON);
|
digitalWrite(LED_BUILTIN, !LED_ON);
|
||||||
Serial.println("IP address: ");
|
Serial.print("IP address: \t");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
|
Serial.print("Hostname: \t");
|
||||||
|
Serial.println(WiFi.getHostname());
|
||||||
connectToMqtt();
|
connectToMqtt();
|
||||||
break;
|
break;
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||||
|
@ -134,7 +138,7 @@ void onMqttConnect(bool sessionPresent) {
|
||||||
Serial.print("Session present: ");
|
Serial.print("Session present: ");
|
||||||
Serial.println(sessionPresent);
|
Serial.println(sessionPresent);
|
||||||
|
|
||||||
if (mqttClient.publish(availabilityTopic, 0, 0, "CONNECTED") == true) {
|
if (mqttClient.publish(availabilityTopic, 0, 1, "CONNECTED") == true) {
|
||||||
Serial.print("Success sending message to topic:\t");
|
Serial.print("Success sending message to topic:\t");
|
||||||
Serial.println(availabilityTopic);
|
Serial.println(availabilityTopic);
|
||||||
} else {
|
} else {
|
||||||
|
@ -156,8 +160,7 @@ void reportDevice(BLEAdvertisedDevice advertisedDevice) {
|
||||||
|
|
||||||
// Serial.printf("\n\n");
|
// Serial.printf("\n\n");
|
||||||
|
|
||||||
StaticJsonBuffer<500> JSONbuffer;
|
StaticJsonDocument<500> doc;
|
||||||
JsonObject& JSONencoder = JSONbuffer.createObject();
|
|
||||||
|
|
||||||
String mac_address = advertisedDevice.getAddress().toString().c_str();
|
String mac_address = advertisedDevice.getAddress().toString().c_str();
|
||||||
mac_address.replace(":","");
|
mac_address.replace(":","");
|
||||||
|
@ -165,17 +168,17 @@ void reportDevice(BLEAdvertisedDevice advertisedDevice) {
|
||||||
int rssi = advertisedDevice.getRSSI();
|
int rssi = advertisedDevice.getRSSI();
|
||||||
float distance;
|
float distance;
|
||||||
|
|
||||||
JSONencoder["id"] = mac_address;
|
doc["id"] = mac_address;
|
||||||
JSONencoder["uuid"] = mac_address;
|
doc["uuid"] = mac_address;
|
||||||
JSONencoder["rssi"] = rssi;
|
doc["rssi"] = rssi;
|
||||||
|
|
||||||
if (advertisedDevice.haveName()){
|
if (advertisedDevice.haveName()){
|
||||||
String nameBLE = String(advertisedDevice.getName().c_str());
|
String nameBLE = String(advertisedDevice.getName().c_str());
|
||||||
// Serial.print("Name: ");
|
// Serial.print("Name: ");
|
||||||
// Serial.println(nameBLE);
|
// Serial.println(nameBLE);
|
||||||
JSONencoder["name"] = nameBLE;
|
doc["name"] = nameBLE;
|
||||||
// } else {
|
// } else {
|
||||||
// JSONencoder["name"] = "unknown";
|
// doc["name"] = "unknown";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serial.printf("\n\n");
|
// Serial.printf("\n\n");
|
||||||
|
@ -191,7 +194,7 @@ void reportDevice(BLEAdvertisedDevice advertisedDevice) {
|
||||||
oBeacon.setData(strServiceData);
|
oBeacon.setData(strServiceData);
|
||||||
// Serial.printf("Eddystone Frame Type (Eddystone-URL) ");
|
// Serial.printf("Eddystone Frame Type (Eddystone-URL) ");
|
||||||
// Serial.printf(oBeacon.getDecodedURL().c_str());
|
// Serial.printf(oBeacon.getDecodedURL().c_str());
|
||||||
JSONencoder["url"] = oBeacon.getDecodedURL().c_str();
|
doc["url"] = oBeacon.getDecodedURL().c_str();
|
||||||
|
|
||||||
} else if (cServiceData[0]==0x20) {
|
} else if (cServiceData[0]==0x20) {
|
||||||
BLEEddystoneTLM oBeacon = BLEEddystoneTLM();
|
BLEEddystoneTLM oBeacon = BLEEddystoneTLM();
|
||||||
|
@ -230,28 +233,28 @@ void reportDevice(BLEAdvertisedDevice advertisedDevice) {
|
||||||
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());
|
||||||
|
|
||||||
JSONencoder["major"] = major;
|
doc["major"] = major;
|
||||||
JSONencoder["minor"] = minor;
|
doc["minor"] = minor;
|
||||||
|
|
||||||
JSONencoder["uuid"] = proximityUUID;
|
doc["uuid"] = proximityUUID;
|
||||||
JSONencoder["id"] = proximityUUID + "-" + String(major) + "-0";
|
doc["id"] = proximityUUID + "-" + String(major) + "-0";
|
||||||
JSONencoder["txPower"] = oBeacon.getSignalPower();
|
doc["txPower"] = oBeacon.getSignalPower();
|
||||||
JSONencoder["distance"] = distance;
|
doc["distance"] = distance;
|
||||||
|
|
||||||
Serial.printf("iBeacon Frame\n");
|
// Serial.printf("iBeacon Frame\n");
|
||||||
Serial.printf("Major: %d Minor: %d UUID: %s Power: %d Rssi: %d Distance: %f\n",ENDIAN_CHANGE_U16(oBeacon.getMajor()),ENDIAN_CHANGE_U16(oBeacon.getMinor()),proximityUUID.c_str(),oBeacon.getSignalPower(), rssi, distance);
|
// Serial.printf("Major: %d Minor: %d UUID: %s Power: %d Rssi: %d Distance: %f\n",ENDIAN_CHANGE_U16(oBeacon.getMajor()),ENDIAN_CHANGE_U16(oBeacon.getMinor()),proximityUUID.c_str(),oBeacon.getSignalPower(), rssi, distance);
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if (advertisedDevice.haveTXPower()) {
|
if (advertisedDevice.haveTXPower()) {
|
||||||
distance = calculateDistance(rssi, advertisedDevice.getTXPower());
|
distance = calculateDistance(rssi, advertisedDevice.getTXPower());
|
||||||
JSONencoder["txPower"] = advertisedDevice.getTXPower();
|
doc["txPower"] = advertisedDevice.getTXPower();
|
||||||
} else {
|
} else {
|
||||||
distance = calculateDistance(rssi, -59);
|
distance = calculateDistance(rssi, -59);
|
||||||
}
|
}
|
||||||
|
|
||||||
JSONencoder["distance"] = distance;
|
doc["distance"] = distance;
|
||||||
|
|
||||||
// Serial.printf("strManufacturerData: %d \n",strManufacturerData.length());
|
// Serial.printf("strManufacturerData: %d \n",strManufacturerData.length());
|
||||||
// TODO: parse manufacturer data
|
// TODO: parse manufacturer data
|
||||||
|
@ -261,11 +264,11 @@ void reportDevice(BLEAdvertisedDevice advertisedDevice) {
|
||||||
|
|
||||||
if (advertisedDevice.haveTXPower()) {
|
if (advertisedDevice.haveTXPower()) {
|
||||||
distance = calculateDistance(rssi, advertisedDevice.getTXPower());
|
distance = calculateDistance(rssi, advertisedDevice.getTXPower());
|
||||||
JSONencoder["txPower"] = advertisedDevice.getTXPower();
|
doc["txPower"] = advertisedDevice.getTXPower();
|
||||||
JSONencoder["distance"] = distance;
|
doc["distance"] = distance;
|
||||||
} else {
|
} else {
|
||||||
distance = calculateDistance(rssi, -59);
|
distance = calculateDistance(rssi, -59);
|
||||||
JSONencoder["distance"] = distance;
|
doc["distance"] = distance;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serial.printf("no Beacon Advertised ServiceDataUUID: %d %s \n", advertisedDevice.getServiceDataUUID().bitSize(), advertisedDevice.getServiceDataUUID().toString().c_str());
|
// Serial.printf("no Beacon Advertised ServiceDataUUID: %d %s \n", advertisedDevice.getServiceDataUUID().bitSize(), advertisedDevice.getServiceDataUUID().toString().c_str());
|
||||||
|
@ -273,11 +276,11 @@ void reportDevice(BLEAdvertisedDevice advertisedDevice) {
|
||||||
}
|
}
|
||||||
|
|
||||||
char JSONmessageBuffer[512];
|
char JSONmessageBuffer[512];
|
||||||
JSONencoder.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
|
serializeJson(doc, JSONmessageBuffer);
|
||||||
|
|
||||||
String publishTopic = String(channel) + "/" + room;
|
String publishTopic = String(channel) + "/" + room;
|
||||||
|
|
||||||
if (mqttClient.connected() && JSONencoder["distance"] < maxDistance) {
|
if (mqttClient.connected() && doc["distance"] < maxDistance) {
|
||||||
if (mqttClient.publish((char *)publishTopic.c_str(), 0, 0, JSONmessageBuffer) == true) {
|
if (mqttClient.publish((char *)publishTopic.c_str(), 0, 0, JSONmessageBuffer) == true) {
|
||||||
|
|
||||||
// Serial.print("Success sending message to topic: "); Serial.println(publishTopic);
|
// Serial.print("Success sending message to topic: "); Serial.println(publishTopic);
|
||||||
|
@ -323,19 +326,20 @@ void scanForDevices(void * parameter) {
|
||||||
// xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
|
// xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
|
||||||
Serial.print("Scanning...\t");
|
Serial.print("Scanning...\t");
|
||||||
BLEScanResults foundDevices = pBLEScan->start(scanTime);
|
BLEScanResults foundDevices = pBLEScan->start(scanTime);
|
||||||
Serial.printf("Scan done! Devices found: %d\t",foundDevices.getCount());
|
Serial.printf("Scan done! Devices found: %d\n",foundDevices.getCount());
|
||||||
// mqttClient.connect();
|
// mqttClient.connect();
|
||||||
// while (!mqttClient.connected()) {
|
// while (!mqttClient.connected()) {
|
||||||
// Serial.print(".");
|
// Serial.print(".");
|
||||||
// vTaskDelay(10 / portTICK_PERIOD_MS);
|
// vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||||
// }
|
// }
|
||||||
for (uint32_t i = 0; i < foundDevices.getCount(); i++) {
|
|
||||||
if (mqttClient.connected()) {
|
if (mqttClient.connected()) {
|
||||||
|
for (uint32_t i = 0; i < foundDevices.getCount(); i++) {
|
||||||
reportDevice(foundDevices.getDevice(i));
|
reportDevice(foundDevices.getDevice(i));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Serial.println("Cannot report; mqtt disconnected");
|
||||||
}
|
}
|
||||||
last = millis();
|
last = millis();
|
||||||
Serial.println("Reports sent");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,6 @@
|
||||||
|
|
||||||
// Define bluetooth scan parameters
|
// Define bluetooth scan parameters
|
||||||
#define scanInterval 2 // Define the interval in seconds between scans
|
#define scanInterval 2 // Define the interval in seconds between scans
|
||||||
#define advertisementDuration 3 // Define the duration in seconds that the device should advertise as a beacon to other sensors
|
|
||||||
|
|
||||||
// Maximum distance (in meters) to report. Devices that are calculated to be further than this distance in meters will not be reported
|
// Maximum distance (in meters) to report. Devices that are calculated to be further than this distance in meters will not be reported
|
||||||
#define maxDistance 5
|
#define maxDistance 5
|
||||||
|
|
|
@ -15,6 +15,6 @@ src_dir = .
|
||||||
platform = espressif32
|
platform = espressif32
|
||||||
framework = arduino
|
framework = arduino
|
||||||
board = esp32dev
|
board = esp32dev
|
||||||
lib_deps = ArduinoJSON@5.13.4, ESP32 BLE Arduino, AsyncMqttClient@0.8.2, AsyncTCP
|
lib_deps = ArduinoJSON^6, ESP32 BLE Arduino, AsyncMqttClient@0.8.2, AsyncTCP
|
||||||
board_build.partitions = partitions_singleapp.csv
|
board_build.partitions = partitions_singleapp.csv
|
||||||
upload_port = COM13
|
upload_port = COM13
|
||||||
|
|
Loading…
Reference in New Issue