diff --git a/ESP32-mqtt-room.ino b/ESP32-mqtt-room.ino index bf2502d..4c30c02 100644 --- a/ESP32-mqtt-room.ino +++ b/ESP32-mqtt-room.ino @@ -28,7 +28,7 @@ extern "C" { #include "BLEBeacon.h" #include "BLEEddystoneTLM.h" #include "BLEEddystoneURL.h" -#include "Settings_bedroom.h" +#include "Settings_kitchen.h" BLEScan* pBLEScan; int scanTime = 3; //In seconds @@ -73,7 +73,9 @@ float calculateDistance(int rssi, int txPower) { if (!txPower) { // somewhat reasonable default value txPower = -59; - } + } else if (txPower > 0) { + txPower = txPower * -1; + } const float ratio = rssi * 1.0 / txPower; if (ratio < 1.0) { @@ -161,6 +163,7 @@ void reportDevice(BLEAdvertisedDevice advertisedDevice) { mac_address.replace(":",""); mac_address.toLowerCase(); int rssi = advertisedDevice.getRSSI(); + float distance; JSONencoder["id"] = mac_address; JSONencoder["uuid"] = mac_address; @@ -215,10 +218,7 @@ void reportDevice(BLEAdvertisedDevice advertisedDevice) { String proximityUUID = getProximityUUIDString(oBeacon); - // Serial.printf("iBeacon Frame\n"); - // Serial.printf("Major: %d Minor: %d UUID: %s Power: %d\n",ENDIAN_CHANGE_U16(oBeacon.getMajor()),ENDIAN_CHANGE_U16(oBeacon.getMinor()),proximityUUID.c_str(),oBeacon.getSignalPower()); - - float distance = calculateDistance(rssi, oBeacon.getSignalPower()); + distance = calculateDistance(rssi, oBeacon.getSignalPower()); // Serial.print("RSSI: "); // Serial.print(rssi); @@ -238,9 +238,11 @@ void reportDevice(BLEAdvertisedDevice advertisedDevice) { JSONencoder["txPower"] = oBeacon.getSignalPower(); JSONencoder["distance"] = distance; - } else { + 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); - float distance; + + } else { if (advertisedDevice.haveTXPower()) { distance = calculateDistance(rssi, advertisedDevice.getTXPower()); @@ -258,11 +260,11 @@ void reportDevice(BLEAdvertisedDevice advertisedDevice) { } else { if (advertisedDevice.haveTXPower()) { - float distance = calculateDistance(rssi, advertisedDevice.getTXPower()); + distance = calculateDistance(rssi, advertisedDevice.getTXPower()); JSONencoder["txPower"] = advertisedDevice.getTXPower(); JSONencoder["distance"] = distance; } else { - float distance = calculateDistance(rssi, -59); + distance = calculateDistance(rssi, -59); JSONencoder["distance"] = distance; } @@ -286,9 +288,9 @@ void reportDevice(BLEAdvertisedDevice advertisedDevice) { Serial.print("Message: "); Serial.println(JSONmessageBuffer); } - } else if (mqttClient.connected() && JSONencoder["distance"] >= maxDistance) { + } else if (mqttClient.connected() && distance >= maxDistance) { - Serial.printf("%d exceeded distance threshold\n", JSONencoder["distance"]); + // Serial.printf("%f exceeded distance threshold\n", distance); } else { diff --git a/Settings.h b/Settings.h index b006816..2be23da 100644 --- a/Settings.h +++ b/Settings.h @@ -35,4 +35,8 @@ #define availabilityTopic "presence_nodes/" room // Define bluetooth scan parameters -#define scanInterval 15 // 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 +#define maxDistance 5 diff --git a/lib/BLEEddystoneTLM/BLEEddystoneTLM.cpp b/lib/BLEEddystoneTLM/BLEEddystoneTLM.cpp deleted file mode 100644 index c944a29..0000000 --- a/lib/BLEEddystoneTLM/BLEEddystoneTLM.cpp +++ /dev/null @@ -1,135 +0,0 @@ -/* - * BLEEddystoneTLM.cpp - * - * Created on: Mar 12, 2018 - * Author: pcbreflux - * See original repository at https://github.com/pcbreflux/espressif/tree/master/esp32/arduino/sketchbook/ESP32_BLE_beaconscan - */ -#include "Arduino.h" -#include "sdkconfig.h" -#if defined(CONFIG_BT_ENABLED) -#include -#include -#include "BLEEddystoneTLM.h" - -static const char LOG_TAG[] = "BLEEddystoneTLM"; -#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8)) -#define ENDIAN_CHANGE_U32(x) ((((x)&0xFF000000)>>24) + (((x)&0x00FF0000)>>8)) + ((((x)&0xFF00)<<8) + (((x)&0xFF)<<24)) - -BLEEddystoneTLM::BLEEddystoneTLM() { - beconUUID = 0xFEAA; - m_eddystoneData.frameType = EDDYSTONE_TLM_FRAME_TYPE; - m_eddystoneData.version = 0; - m_eddystoneData.volt = 3300; // 3300mV = 3.3V - m_eddystoneData.temp = (uint16_t)((float)23.00); - m_eddystoneData.advCount = 0; - m_eddystoneData.tmil = 0; -} // BLEEddystoneTLM - -std::string BLEEddystoneTLM::getData() { - return std::string((char*)&m_eddystoneData, sizeof(m_eddystoneData)); -} // getData - -BLEUUID BLEEddystoneTLM::getUUID() { - return BLEUUID(beconUUID); -} // getUUID - -uint8_t BLEEddystoneTLM::getVersion() { - return m_eddystoneData.version; -} // getVersion - -uint16_t BLEEddystoneTLM::getVolt() { - return m_eddystoneData.volt; -} // getVolt - -float BLEEddystoneTLM::getTemp() { - return (float)m_eddystoneData.temp; -} // getTemp - -uint32_t BLEEddystoneTLM::getCount() { - return m_eddystoneData.advCount; -} // getCount - -uint32_t BLEEddystoneTLM::getTime() { - return m_eddystoneData.tmil; -} // getTime - -std::string BLEEddystoneTLM::toString() { - std::string out = ""; - String buff; - uint32_t rawsec; - - out += "Version "; - buff = String(m_eddystoneData.version, DEC); - out += buff.c_str(); - out += "\n"; - - out += "Battery Voltage "; - buff = String(ENDIAN_CHANGE_U16(m_eddystoneData.volt), DEC); - out += buff.c_str(); - out += " mV\n"; - - out += "Temperature "; - buff = String((float)m_eddystoneData.temp, 1); - out += buff.c_str(); - out += " °C\n"; - - out += "Adv. Count "; - buff = String(ENDIAN_CHANGE_U32(m_eddystoneData.advCount), DEC); - out += buff.c_str(); - out += "\n"; - - out += "Time "; - rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil); - buff = "0000"+String(rawsec/864000, DEC); - out += buff.substring(buff.length()-4,buff.length()).c_str(); - out += "."; - buff = "00"+String((rawsec/36000)%24, DEC); - out += buff.substring(buff.length()-2,buff.length()).c_str(); - out += ":"; - buff = "00"+String((rawsec/600)%60, DEC); - out += buff.substring(buff.length()-2,buff.length()).c_str(); - out += ":"; - buff = "00"+String((rawsec/10)%60, DEC); - out += buff.substring(buff.length()-2,buff.length()).c_str(); - out += "\n"; - - return out; -} // toString - -/** - * Set the raw data for the beacon record. - */ -void BLEEddystoneTLM::setData(std::string data) { - if (data.length() != sizeof(m_eddystoneData)) { - ESP_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and expected %d", data.length(), sizeof(m_eddystoneData)); - return; - } - memcpy(&m_eddystoneData, data.data(), data.length()); -} // setData - -void BLEEddystoneTLM::setUUID(BLEUUID l_uuid) { - beconUUID = l_uuid.getNative()->uuid.uuid16; -} // setUUID - -void BLEEddystoneTLM::setVersion(uint8_t version) { - m_eddystoneData.version = version; -} // setVersion - -void BLEEddystoneTLM::setVolt(uint16_t volt) { - m_eddystoneData.volt = volt; -} // setVolt - -void BLEEddystoneTLM::setTemp(float temp) { - m_eddystoneData.temp = (uint16_t)temp; -} // setTemp - -void BLEEddystoneTLM::setCount(uint32_t advCount) { - m_eddystoneData.advCount = advCount; -} // setCount - -void BLEEddystoneTLM::setTime(uint32_t tmil) { - m_eddystoneData.tmil = tmil; -} // setTime - -#endif diff --git a/lib/BLEEddystoneTLM/BLEEddystoneTLM.h b/lib/BLEEddystoneTLM/BLEEddystoneTLM.h deleted file mode 100644 index 94327a3..0000000 --- a/lib/BLEEddystoneTLM/BLEEddystoneTLM.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * BLEEddystoneTLM.cpp - * - * Created on: Mar 12, 2018 - * Author: pcbreflux - * See original repository at https://github.com/pcbreflux/espressif/tree/master/esp32/arduino/sketchbook/ESP32_BLE_beaconscan - */ - -#ifndef _BLEEddystoneTLM_H_ -#define _BLEEddystoneTLM_H_ -#include "BLEUUID.h" - -#define EDDYSTONE_TLM_FRAME_TYPE 0x20 - -/** - * @brief Representation of a beacon. - * See: - * * https://github.com/google/eddystone - */ -class BLEEddystoneTLM { -private: - uint16_t beconUUID; - struct { - uint8_t frameType; - int8_t version; - uint16_t volt; - uint16_t temp; - uint32_t advCount; - uint32_t tmil; - } __attribute__((packed))m_eddystoneData; -public: - BLEEddystoneTLM(); - std::string getData(); - BLEUUID getUUID(); - uint8_t getVersion(); - uint16_t getVolt(); - float getTemp(); - uint32_t getCount(); - uint32_t getTime(); - std::string toString(); - void setData(std::string data); - void setUUID(BLEUUID l_uuid); - void setVersion(uint8_t version); - void setVolt(uint16_t volt); - void setTemp(float temp); - void setCount(uint32_t advCount); - void setTime(uint32_t tmil); - -}; // BLEEddystoneTLM - -#endif /* _BLEEddystoneTLM_H_ */ diff --git a/lib/BLEEddystoneURL/BLEEddystoneURL.cpp b/lib/BLEEddystoneURL/BLEEddystoneURL.cpp deleted file mode 100644 index 19f02fa..0000000 --- a/lib/BLEEddystoneURL/BLEEddystoneURL.cpp +++ /dev/null @@ -1,150 +0,0 @@ -/* - * BLEEddystoneURL.cpp - * - * Created on: Mar 12, 2018 - * Author: pcbreflux - * See original repository at https://github.com/pcbreflux/espressif/tree/master/esp32/arduino/sketchbook/ESP32_BLE_beaconscan - */ -#include "sdkconfig.h" -#if defined(CONFIG_BT_ENABLED) -#include -#include -#include "BLEEddystoneURL.h" - -static const char LOG_TAG[] = "BLEEddystoneURL"; - -BLEEddystoneURL::BLEEddystoneURL() { - beconUUID = 0xFEAA; - lengthURL = 0; - m_eddystoneData.frameType = EDDYSTONE_URL_FRAME_TYPE; - m_eddystoneData.advertisedTxPower = 0; - memset(m_eddystoneData.url, 0, sizeof(m_eddystoneData.url)); -} // BLEEddystoneURL - -std::string BLEEddystoneURL::getData() { - return std::string((char*)&m_eddystoneData, sizeof(m_eddystoneData)); -} // getData - -BLEUUID BLEEddystoneURL::getUUID() { - return BLEUUID(beconUUID); -} // getUUID - -int8_t BLEEddystoneURL::getPower() { - return m_eddystoneData.advertisedTxPower; -} // getPower - -std::string BLEEddystoneURL::getURL() { - return std::string((char*)&m_eddystoneData.url, sizeof(m_eddystoneData.url)); -} // getURL - -std::string BLEEddystoneURL::getDecodedURL() { - std::string decodedURL = ""; - - switch (m_eddystoneData.url[0]) { - case 0x00: - decodedURL += "http://www."; - break; - case 0x01: - decodedURL += "https://www."; - break; - case 0x02: - decodedURL += "http://"; - break; - case 0x03: - decodedURL += "https://"; - break; - default: - decodedURL += m_eddystoneData.url[0]; - } - - for (int i=1;i33&&m_eddystoneData.url[i]<127) { - decodedURL += m_eddystoneData.url[i]; - } else { - switch (m_eddystoneData.url[i]) { - case 0x00: - decodedURL += ".com/"; - break; - case 0x01: - decodedURL += ".org/"; - break; - case 0x02: - decodedURL += ".edu/"; - break; - case 0x03: - decodedURL += ".net/"; - break; - case 0x04: - decodedURL += ".info/"; - break; - case 0x05: - decodedURL += ".biz/"; - break; - case 0x06: - decodedURL += ".gov/"; - break; - case 0x07: - decodedURL += ".com"; - break; - case 0x08: - decodedURL += ".org"; - break; - case 0x09: - decodedURL += ".edu"; - break; - case 0x0A: - decodedURL += ".net"; - break; - case 0x0B: - decodedURL += ".info"; - break; - case 0x0C: - decodedURL += ".biz"; - break; - case 0x0D: - decodedURL += ".gov"; - break; - } - } - } - - - return decodedURL; -} // getDecodedURL - - - -/** - * Set the raw data for the beacon record. - */ -void BLEEddystoneURL::setData(std::string data) { - if (data.length() > sizeof(m_eddystoneData)) { - ESP_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and max expected %d", data.length(), sizeof(m_eddystoneData)); - return; - } - memset(&m_eddystoneData, 0, sizeof(m_eddystoneData)); - memcpy(&m_eddystoneData, data.data(), data.length()); - lengthURL=data.length()-(sizeof(m_eddystoneData)-sizeof(m_eddystoneData.url)); - -} // setData - -void BLEEddystoneURL::setUUID(BLEUUID l_uuid) { - beconUUID = l_uuid.getNative()->uuid.uuid16; -} // setUUID - -void BLEEddystoneURL::setPower(int8_t advertisedTxPower) { - m_eddystoneData.advertisedTxPower = advertisedTxPower; -} // setPower - -void BLEEddystoneURL::setURL(std::string url) { - if (url.length() > sizeof(m_eddystoneData.url)) { - ESP_LOGE(LOG_TAG, "Unable to set the url ... length passed in was %d and max expected %d", url.length(), sizeof(m_eddystoneData.url)); - return; - } - memset(m_eddystoneData.url, 0, sizeof(m_eddystoneData.url)); - memcpy(m_eddystoneData.url, url.data(), url.length()); - lengthURL=url.length(); -} // setURL - - -#endif diff --git a/lib/BLEEddystoneURL/BLEEddystoneURL.h b/lib/BLEEddystoneURL/BLEEddystoneURL.h deleted file mode 100644 index 3ce4632..0000000 --- a/lib/BLEEddystoneURL/BLEEddystoneURL.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * BLEEddystoneURL.cpp - * - * Created on: Mar 12, 2018 - * Author: pcbreflux - * See original repository at https://github.com/pcbreflux/espressif/tree/master/esp32/arduino/sketchbook/ESP32_BLE_beaconscan - */ - -#ifndef _BLEEddystoneURL_H_ -#define _BLEEddystoneURL_H_ -#include "BLEUUID.h" - -#define EDDYSTONE_URL_FRAME_TYPE 0x10 - -/** - * @brief Representation of a beacon. - * See: - * * https://github.com/google/eddystone - */ -class BLEEddystoneURL { -private: - uint16_t beconUUID; - uint8_t lengthURL; - struct { - uint8_t frameType; - int8_t advertisedTxPower; - uint8_t url[16]; - } __attribute__((packed))m_eddystoneData; -public: - BLEEddystoneURL(); - std::string getData(); - BLEUUID getUUID(); - int8_t getPower(); - std::string getURL(); - std::string getDecodedURL(); - void setData(std::string data); - void setUUID(BLEUUID l_uuid); - void setPower(int8_t advertisedTxPower); - void setURL(std::string url); - -}; // BLEEddystoneURL - -#endif /* _BLEEddystoneURL_H_ */ diff --git a/lib/readme.txt b/lib/readme.txt deleted file mode 100644 index cfa16df..0000000 --- a/lib/readme.txt +++ /dev/null @@ -1,41 +0,0 @@ - -This directory is intended for project specific (private) libraries. -PlatformIO will compile them to static libraries and link them to executable files. - -The source code of each library should be placed in separate directories, like -"lib/private_lib/[here are source files]". - -For example, see the structure of the following two libraries `Foo` and `Bar`: - -|--lib -| | -| |--Bar -| | |--docs -| | |--examples -| | |--src -| | |- Bar.c -| | |- Bar.h -| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html -| | -| |--Foo -| | |- Foo.c -| | |- Foo.h -| | -| |- readme.txt --> THIS FILE -| -|- platformio.ini -|--src - |- main.c - -Then in `src/main.c` you should use: - -#include -#include - -// rest H/C/CPP code - -PlatformIO will find your libraries automatically, configure preprocessor's -include paths and build them. - -More information about PlatformIO Library Dependency Finder -- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini index 9949f39..2535451 100644 --- a/platformio.ini +++ b/platformio.ini @@ -17,4 +17,4 @@ framework = arduino board = esp32dev lib_deps = ArduinoJSON@5.13.2, ESP32 BLE Arduino, AsyncMqttClient, AsyncTCP board_build.partitions = partitions_singleapp.csv -upload_port = COM13 +upload_port = COM9