Updated docs

This commit is contained in:
jptrsn 2019-06-28 10:42:16 -04:00
parent cadc05f81e
commit 141c7d488b
11 changed files with 130 additions and 102 deletions

View File

@ -33,7 +33,7 @@ extern "C" {
#include "BLEBeacon.h"
#include "BLEEddystoneTLM.h"
#include "BLEEddystoneURL.h"
#include "Settings_d.h"
#include "Settings_local.h"
BLEScan* pBLEScan;
int scanTime = singleScanTime; //In seconds
@ -143,8 +143,8 @@ void connectToMqtt() {
void WiFiEvent(WiFiEvent_t event) {
Serial.printf("[WiFi-event] event: %x", event);
// Serial.println(event);
switch(event) {
switch(event) {
case SYSTEM_EVENT_STA_GOT_IP:
digitalWrite(LED_BUILTIN, !LED_ON);
Serial.print("IP address: \t");
@ -177,8 +177,6 @@ void WiFiEvent(WiFiEvent_t event) {
void onMqttConnect(bool sessionPresent) {
Serial.println("Connected to MQTT.");
Serial.print("Session present: ");
Serial.println(sessionPresent);
if (mqttClient.publish(availabilityTopic, 0, 1, "CONNECTED") == true) {
Serial.print("Success sending message to topic:\t");
@ -208,6 +206,8 @@ bool reportDevice(BLEAdvertisedDevice advertisedDevice) {
String mac_address = advertisedDevice.getAddress().toString().c_str();
mac_address.replace(":","");
mac_address.toLowerCase();
// Serial.print("mac:\t");
// Serial.println(mac_address);
int rssi = advertisedDevice.getRSSI();
float distance;
@ -224,8 +224,8 @@ bool reportDevice(BLEAdvertisedDevice advertisedDevice) {
// doc["name"] = "unknown";
}
// Serial.printf("\n\n");
// Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
// Serial.printf("\n\r");
// Serial.printf("Advertised Device: %s \n\r", advertisedDevice.toString().c_str());
std::string strServiceData = advertisedDevice.getServiceData();
uint8_t cServiceData[100];
strServiceData.copy((char *)cServiceData, strServiceData.length(), 0);
@ -245,9 +245,9 @@ bool reportDevice(BLEAdvertisedDevice advertisedDevice) {
// Serial.printf("Eddystone Frame Type (Unencrypted Eddystone-TLM) \n");
// Serial.printf(oBeacon.toString().c_str());
} else {
Serial.println("service data");
// Serial.println("service data");
for (int i=0;i<strServiceData.length();i++) {
Serial.printf("[%X]",cServiceData[i]);
// Serial.printf("[%X]",cServiceData[i]);
}
}
// Serial.printf("\n");
@ -296,7 +296,7 @@ bool reportDevice(BLEAdvertisedDevice advertisedDevice) {
doc["distance"] = distance;
// Serial.printf("strManufacturerData: %d \n",strManufacturerData.length());
// Serial.printf("strManufacturerData: %d \n\r",strManufacturerData.length());
// TODO: parse manufacturer data
}
@ -311,7 +311,7 @@ bool reportDevice(BLEAdvertisedDevice advertisedDevice) {
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\r", advertisedDevice.getServiceDataUUID().bitSize(), advertisedDevice.getServiceDataUUID().toString().c_str());
}
}
@ -435,7 +435,7 @@ void setup() {
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.setServer(mqttHost, mqttPort);
mqttClient.setWill(availabilityTopic, 0, 0, "DISCONNECTED");
mqttClient.setWill(availabilityTopic, 0, 1, "DISCONNECTED");
mqttClient.setKeepAlive(60);
connectToWifi();

View File

@ -1,62 +1,35 @@
See the full documentation at [https://jptrsn.github.io/ESP32-mqtt-room/](https://jptrsn.github.io/ESP32-mqtt-room/)
# ESP32-mqtt-room
An ESP32 based presence detection node for use with the [Home Assistant](https://www.home-assistant.io/) [`mqtt_room` component](https://www.home-assistant.io/components/sensor.mqtt_room/) for localized device presence detection.
This depends heavily on the hard work done by [pcbreflux](https://github.com/pcbreflux) and [nkolban](https://github.com/nkolban) both on GitHub and on their YouTube Channels. Specifically, it is a modified version of pcbreflux's [ESP32_BLE_beaconscan](https://github.com/pcbreflux/espressif/tree/master/esp32/arduino/sketchbook/ESP32_BLE_beaconscan).
## Credits
This project would not have been possible without the help of a variety of [folks on GitHub and YouTube.](https://jptrsn.github.io/ESP32-mqtt-room/index.md#credits)
## Getting Started
You can get started by cloning this repository to your local machine using git. Alternatively, you can [download the zip](https://github.com/jptrsn/ESP32-mqtt-room/archive/master.zip). To get the code onto your device, you can load it via the Arduino IDE or, thanks to some great work by [kylegordon](https://github.com/kylegordon), you can now build and upload using PlatformIO.
Create a copy of the `Settings.h` file, and rename it to `Settings_local.h`. This will allow you to set your own configuration without the risk of any commits to the repository of sensitive information.
Open the folder in Atom, using the `open project` option in the PlatformIO Home screen. Modify the settings in the `Settings_local.h` file to match your environment. Set the correct port in the `platformio.ini` file (or remove the line to use auto-detection), and upload to the board. Open the serial monitor after successful upload to check for success.
### OTA Support
It is possible to update the device using "Over the Air" (OTA) updates from the command line interface of PlatformIO. You will need to know the IP address of the device itself (check your router). From the command line, enter the command `platformio run -t upload --upload-port {{Device IP Address}}`
A full list of instructions is available in the [documentation](https://jptrsn.github.io/ESP32-mqtt-room/index.md#getting-started).
## Home Assistant Configuration
Once the device is running, it is important to configure Home Assistant to use the information from the MQTT topic to determine what devices to track. You can read the full documentation [on the Home Assistant website](https://www.home-assistant.io/components/sensor.mqtt_room/). It is critical that you configure your device IDs to include the Major version, but ignore Minor version (i.e. set it to 0). This is to match the configuration used on the ESP32.
### Sample Configuration.yaml Entry
Here is an example of how an entry into your `configuration.yaml` file should look:
```
sensor:
# One entry for each BLE beacon you want to track
- platform: mqtt_room
device_id: "fda50693a4e24fb1afcfc6eb07647825-5-0" # Note that major version must match, but any minor version will be ignored
name: 'iBeacon Room Presence'
state_topic: 'room_presence'
timeout: 60
away_timeout: 120
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
- platform: mqtt
name: ESP32 Room Presence
state_topic: "presence_nodes/esp32"
json_attributes_topic: "presence_nodes/esp32/tele"
payload_on: "CONNECTED"
payload_off: "DISCONNECTED"
device_class: connectivity
```
See the [documentation for configuring Home Assistant.](https://jptrsn.github.io/ESP32-mqtt-room/home_assistant.md)
## Trackable Devices
### iBeacon Hardware
Generic beacon hardware should be compatible, provided it meets a beacon standard (such as iBeacon or Eddystone). There are plenty of models based on the Nordic Semiconductor NRF51822 chip available from sellers such as [eBay](https://www.ebay.com/sch/i.html?_nkw=nrf51822+ibeacon) or [Ali Express](https://www.aliexpress.com/wholesale?SearchText=nrf51822+ibeacon). Some other devices that advertise as BLE (such as the Mi Flora plant sensors or the Mi Band fitness tracker) can also be tracked, as long as you can scan the device and see its hardware ID.
* [Beacon Hardware](https://jptrsn.github.io/ESP32-mqtt-room/beacons.md)
* [Android devices](https://jptrsn.github.io/ESP32-mqtt-room/android.md)
* [iOS devices](https://jptrsn.github.io/ESP32-mqtt-room/index.md#tracking-iphone)
* [Other devices](https://jptrsn.github.io/ESP32-mqtt-room/beacons.md#non-beacon-hardware)
### Tracking Android Phone
In order to track your Android phone as a BLE Beacon, you will need to emulate Beacon adertisement packets from your phone. This is the opposite to what most beacon apps do (i.e. scanning for beacons instead of emulating one). I have had success using the [Beacon Scope](https://play.google.com/store/apps/details?id=com.davidgyoungtech.beaconscanner) app. Configure a transmitter, and specify the device ID in your Home Assistant configuration files.
## Development Tasks
### Tracking iPhone
Unfortunately, Apple does not allow devices to advertise iBeacon data in the background. As I do not own an iPhone, I will not attempt to implement scanning and reporting by device name, but I welcome pull requests.
### To do
- [ ] Implement Mi Flora data parsing and reporting
- [ ] Save config to SPIFFS
- [ ] Update configuration via MQTT
#### Future Development Tasks
### Completed
- [x] Implement basic BLE packet discovery
- [x] Implement iBeacon data packet parsing
- [x] Setup instructions in README.md
- [x] Address watchdog issue
- [ ] Implement Mi Flora data parsing and reporting
- [x] Maximum distance limit (rounded to two decimal places)
- [x] Scan interval Settings
- [x] Implement Over-The-Air (OTA) updates

27
docs/android.md Normal file
View File

@ -0,0 +1,27 @@
# Tracking an Android phone
<img src="./images/beacon_scope_android.jpg" style="float:right;margin:24px;" width=200>
It is possible to track your Android phone by installing a simple beacon broadcasting app. I have used [Beacon Simulator](https://play.google.com/store/apps/details?id=net.alea.beaconsimulator) and [Beacon Scope](https://play.google.com/store/apps/details?id=com.davidgyoungtech.beaconscanner).
### Setup
You can use any iBeacon UUID in the app. Whatever you choose, ensure that it matches exactly with your entry in the Home Assistant configuration, including both the `major` and `minor` version numbers. An example phone configuration could be:
```
device ID: 2ec29f7da28e45eeadf491713bbe7948
major version number: 1
minor version number: 0
```
and the corresponding Home Assistant configuration would be:
```yaml
- platform: mqtt_room
device_id: "2ec29f7da28e45eeadf491713bbe7948-1-0"
name: 'Phone bt_room'
state_topic: 'room_presence'
timeout: 10
away_timeout: 15
```
### Why an App?
Both Android and iPhone use a strategy of [Bluetooth MAC address randomization](https://source.android.com/devices/tech/connect/wifi-mac-randomization) to prevent tracking. While this may not be a perfect solution, it means that the publicly-available bluetooth MAC address of your phone won't be consistent to devices with which it has not bee paired. As we're using Bluetooth Low Energy to scan, we don't want to pair the device. Thus, the need for an app to allow us to track.
### Known Issues
With the release of advanced battery management, Android now has a habit of killing background processes. This means that you may not be able to track your phone consistently. I have yet to find a solution for this problem - if you know of one, please open an issue.

13
docs/beacons.md Normal file
View File

@ -0,0 +1,13 @@
# Tracking Hardware
## Beacons
Generic beacon hardware should be compatible, provided it meets a beacon standard (such as iBeacon or Eddystone). There are plenty of generic beacons available from sellers such as:
* [Amazon](https://www.amazon.ca/gp/product/B07S3DQM6N/ref=as_li_tl?ie=UTF8&camp=15121&creative=330641&creativeASIN=B07S3DQM6N&linkCode=as2&tag=ptrsnja-20&linkId=2f55c80199c88db853bd583182384f50) - generic iBeacon
* [eBay](https://www.ebay.com/sch/i.html?_nkw=nrf51822+ibeacon)
* [Ali Express](https://www.aliexpress.com/wholesale?SearchText=nrf51822+ibeacon).
## Non-beacon Hardware
Some other devices that advertise as BLE (such as the Mi Flora plant sensors or the Mi Band fitness tracker) can also be tracked, as long as you can scan the device and see its hardware ID. Here is a list of devices known to be "trackable":
* MiFlora plant sensor
* MiBand 2 Fitness tracker
* [Puck-JS](https://www.espruino.com/Puck.js), if programmed to [broadcast beacon packets](https://gist.github.com/jptrsn/d6cb9b9cdbcd41f3500708f8b694cad2 "An example project to broadcast iBeacon packets")

30
docs/home_assistant.md Normal file
View File

@ -0,0 +1,30 @@
# Home Assistant Configuration
Once the ESP32 is running, it is important to configure Home Assistant to use the information from the MQTT topic to determine what devices to track. You can read the full documentation [on the Home Assistant website](https://www.home-assistant.io/components/sensor.mqtt_room/). It is critical that you configure your device IDs to include the Major and Minor versions, in the format ``{{beacon uuid}}-{{major}}-{{minor}}``
### configuration.yaml
Here is an example of how an entry into your `configuration.yaml` file should look:
```yaml
sensor:
# One entry for each BLE beacon you want to track
- platform: mqtt_room
device_id: "fda50693a4e24fb1afcfc6eb07647825-5-0" # Note that major version must match, but any minor version will be ignored
name: 'iBeacon Room Presence'
state_topic: 'room_presence'
timeout: 60
away_timeout: 120
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
- platform: mqtt
name: ESP32 A
state_topic: "presence_nodes/esp32_a"
json_attributes_topic: "presence_nodes/esp32_a/tele"
payload_on: "CONNECTED"
payload_off: "DISCONNECTED"
device_class: connectivity
```
Binary sensor with telemetry in the lovelace UI:
![Binary sensor with telemetry](./images/binary_sensor_with_telemetry.jpg)

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

BIN
docs/images/cover.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 113 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 856 KiB

View File

@ -1,63 +1,48 @@
<img src='./images/cover.jpg'>
# ESP32-mqtt-room
An ESP32 based presence detection node for use with the [Home Assistant](https://www.home-assistant.io/) [`mqtt_room` component](https://www.home-assistant.io/components/sensor.mqtt_room/) for localized device presence detection.
This depends heavily on the hard work done by [pcbreflux](https://github.com/pcbreflux) and [nkolban](https://github.com/nkolban) both on GitHub and on their YouTube Channels. Specifically, it is a modified version of pcbreflux's [ESP32_BLE_beaconscan](https://github.com/pcbreflux/espressif/tree/master/esp32/arduino/sketchbook/ESP32_BLE_beaconscan).
## Credits
This depends heavily on the hard work done by a number of people.
* pcbreflux [(GitHub)](https://github.com/pcbreflux) [(YouTube)](https://www.youtube.com/channel/UCvsMfEoIu_ZdBIgQVcY_AZA) - this code takes heavy inspiration from [ESP32_BLE_beaconscan](https://github.com/pcbreflux/espressif/tree/master/esp32/arduino/sketchbook/ESP32_BLE_beaconscan)
* Neil Kolban [(GitHub)](https://github.com/nkolban) [(YouTube)](https://www.youtube.com/channel/UChKn_BlaVrMrhEquPNI6HuQ) - provided the Bluetooth low enery libraries used
* Marvin Roger [(GitHub)](https://github.com/marvinroger/async-mqtt-client) - provided the async MQTT library
* Benoit Blanchon [(GitHub)](https://github.com/bblanchon/ArduinoJson) - provided the Arduino JSON library
* Me No Dev [(GitHub)](https://github.com/me-no-dev/AsyncTCP) - provided the async TCP library
* Kyle Gordon [(GitHub)](https://github.com/kylegordon) - PlatformIO support
### Requirements
You're going to need the following items:
* An [ESP32 development board](https://www.amazon.ca/gp/product/B07F1GWJ1N/ref=as_li_tl?ie=UTF8&camp=15121&creative=330641&creativeASIN=B07F1GWJ1N&linkCode=as2&tag=ptrsnja-20&linkId=7ccc8364c8a8961ac9053e03efa91268)
* A running instance of [Home Assistant](https://www.homeassistant.io)
* The [Atom](https://ide.atom.io/) IDE with the [PlatformIO](https://docs.platformio.org/en/latest/ide/atom.html) package installed.
* A [Bluetooth Low Energy beacon](https://www.amazon.ca/gp/product/B07S3DQM6N/ref=as_li_tl?ie=UTF8&camp=15121&creative=330641&creativeASIN=B07S3DQM6N&linkCode=as2&tag=ptrsnja-20&linkId=2f55c80199c88db853bd583182384f50) (either iBeacon or EddyStone), or
* An Android phone with a beacon app installed.
## Getting Started
You can get started by cloning this repository to your local machine using git. Alternatively, you can [download the zip](https://github.com/jptrsn/ESP32-mqtt-room/archive/master.zip). To get the code onto your device, you can load it via the Arduino IDE or, thanks to some great work by [kylegordon](https://github.com/kylegordon), you can now build and upload using PlatformIO.
Create a copy of the `Settings.h` file, and rename it to `Settings_local.h`. This will allow you to set your own configuration without the risk of any commits to the repository of sensitive information.
### Flashing the Hardware (ESP32)
You can get started by cloning this repository to your local machine using git. Alternatively, you can [download the zip](https://github.com/jptrsn/ESP32-mqtt-room/archive/master.zip). To get the code onto your device, thanks to some great work by [kylegordon](https://github.com/kylegordon), you can now build and upload using PlatformIO.
Open the folder in Atom, using the `open project` option in the PlatformIO Home screen. Modify the settings in the `Settings_local.h` file to match your environment. Set the correct port in the `platformio.ini` file (or remove the line to use auto-detection), and upload to the board. Open the serial monitor after successful upload to check for success.
### Define your settings
Create a copy of the `Settings.h` file, and rename it to `Settings_local.h` or something similar. Any variation of the name `Settings` followed by an underscore will be ignored by git. This will allow you to set your own configuration without the risk of any commits to the repository of sensitive information. It can also be helpful to keep a settings file for each device you will run, as they will differ.
### OTA Support
It is possible to update the device using "Over the Air" (OTA) updates from the command line interface of PlatformIO. You will need to know the IP address of the device itself (check your router). From the command line, enter the command `platformio run -t upload --upload-port {{Device IP Address}}`
### Flashing the device via USB
Open the project folder in Atom, using the `open project` option in the PlatformIO Home screen. Modify the settings in the `Settings_local.h` file to match your environment. Set the correct port in the `platformio.ini` file (or remove the line to use auto-detection), and upload to the board. Open the serial monitor after successful upload to check for success.
### Flashing/updating via OTA
It is possible to update the device using "Over the Air" (OTA) updates from the command line interface of PlatformIO. You will need to know the IP address of the device itself (check your router). From the command line, enter the command `platformio run -t upload --upload-port {{Device IP Address}}`. During the update process, you will see the on-board LED blinking slowly. Once the update has completed, you should see the device reconnect and update its telemetry.
## Home Assistant Configuration
Once the device is running, it is important to configure Home Assistant to use the information from the MQTT topic to determine what devices to track. You can read the full documentation [on the Home Assistant website](https://www.home-assistant.io/components/sensor.mqtt_room/). It is critical that you configure your device IDs to include the Major version, but ignore Minor version (i.e. set it to 0). This is to match the configuration used on the ESP32.
### Sample Configuration.yaml Entry
Here is an example of how an entry into your `configuration.yaml` file should look:
```
sensor:
# One entry for each BLE beacon you want to track
- platform: mqtt_room
device_id: "fda50693a4e24fb1afcfc6eb07647825-5-0" # Note that major version must match, but any minor version will be ignored
name: 'iBeacon Room Presence'
state_topic: 'room_presence'
timeout: 60
away_timeout: 120
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
- platform: mqtt
name: ESP32 Room Presence
state_topic: "presence_nodes/esp32"
json_attributes_topic: "presence_nodes/esp32/tele"
payload_on: "CONNECTED"
payload_off: "DISCONNECTED"
device_class: connectivity
```
See the section on [configuring Home Assistant](./home_assistant.md).
## Trackable Devices
### iBeacon Hardware
Generic beacon hardware should be compatible, provided it meets a beacon standard (such as iBeacon or Eddystone). There are plenty of models based on the Nordic Semiconductor NRF51822 chip available from sellers such as [eBay](https://www.ebay.com/sch/i.html?_nkw=nrf51822+ibeacon) or [Ali Express](https://www.aliexpress.com/wholesale?SearchText=nrf51822+ibeacon). Some other devices that advertise as BLE (such as the Mi Flora plant sensors or the Mi Band fitness tracker) can also be tracked, as long as you can scan the device and see its hardware ID.
### Beacon Hardware
See the section on [tracking beacon hardware](./beacons.md)
### Tracking Android Phone
In order to track your Android phone as a BLE Beacon, you will need to emulate Beacon adertisement packets from your phone. This is the opposite to what most beacon apps do (i.e. scanning for beacons instead of emulating one). I have had success using the [Beacon Scope](https://play.google.com/store/apps/details?id=com.davidgyoungtech.beaconscanner) app. Configure a transmitter, and specify the device ID in your Home Assistant configuration files.
[Read more here](./android.md) about getting your Android phone configured and tracked.
### Tracking iPhone
Unfortunately, Apple does not allow devices to advertise iBeacon data in the background. As I do not own an iPhone, I will not attempt to implement scanning and reporting by device name, but I welcome pull requests.
#### Future Development Tasks
- [x] Implement basic BLE packet discovery
- [x] Implement iBeacon data packet parsing
- [x] Setup instructions in README.md
- [x] Address watchdog issue
- [ ] Implement Mi Flora data parsing and reporting
- [x] Maximum distance limit (rounded to two decimal places)
- [x] Scan interval Settings
- [x] Implement Over-The-Air (OTA) updates
- [x] Build and upload via [PlatformIO](platformio.org)

View File

@ -15,6 +15,6 @@ src_dir = .
platform = espressif32
framework = arduino
board = esp32dev
lib_deps = ArduinoJSON@^6, ESP32 BLE Arduino@^1.0.1, AsyncMqttClient@^0.8.2, AsyncTCP@^1.0.3
lib_deps = ArduinoJSON@^6, ESP32 BLE Arduino@^1.0.1, AsyncMqttClient@^0.8.2, AsyncTCP@1.0.3, ArduinoOTA@1.0
board_build.partitions = partitions_singleapp.csv
upload_port = COM13