ESP32 based BLE tracking for mqtt_room

Hello everyone, I have a problem. I migrated with hasbian (presence detection successfully worked on it) on hassio. this is where the problem arose; I see that esp successfully connects to MQTT and transmits information about the number of found devices (5pcs). But sensor.car_home remains in the status of not_home

The question is removed, I figured it out. it turns out I changed in the config file define channel)))

It shouldn’t be too difficult to modify the code, although it is designed around connecting to and maintaining a reliable connection to an MQTT server. If you’re looking to change it from MQTT to a web endpoint, you’d need to refactor the code to remove this dependency, which would be doable, but it would take you far from the original codebase.

An easier solution may be to spin up an MQTT server, and use something like node-red to listen and simply relay all the payloads to a web endpoint. You’d be putting together “middleware” in order to keep the changes required completely separate from this project.

Good afternoon!
tell me, is it possible to connect the doorbell to the controller? My controller is standing next to the front door, it would be great if I could get information about the button, just like tracking BLE)

Unfortunately, this project is not able to accommodate extra GPIO sensors. I’m considering writing a component for esphome that would perform the mqtt room functions, so that multipurpose operation would be trivial. However, I have no timeline planned for this work.

1 Like

Hello! Fellow Canadian here. I’m absolutely new to Home Assistant and ESP32. I’m migrating from Smartthings and this BLE tracking sounds perfect. Be prepared for a lot of questions since I really don’t have a clue. Bought some ESP32 boards and am downloading ATOM right now.

Ugh. I can’t even get Atom to work properly. I’ll try again tomorrow. I may need some hand holding.

From what I’ve seen, you may find it easier to use VS Code. I haven’t gone down that route, as I’ve been using Atom for years. You could also try their own IDE. Check out https://platformio.org/install for more.

OK. What I lack in skill, I make up for in blind perseverance. I have managed to flash an ESP32 board now using ATOM. Next step is seeing if it’s available on Home Assistant.

I’m making some progress. The ESP32 modules are connecting to WiFi scanning BLE. It claims to be sending info to my MQTT Broker, but I can’t seem to see that in Home Assistant.

Room Assistant is throwing an error:

*** WARNING *** The program ‘node’ uses the Apple Bonjour compatibility layer of Avahi.

Have you seen the messages on the mqtt topic? That will let you know that the esp32 is working, and that home assistant just needs to be configured.

You’ll need to add each of the devices you want to track as a sensor in the mqtt_room platform.

In the log section of my Mosquitto Broker it has multiple mentions of my ESP devices:

1590540776: New client connected from 192.168.123.96 as esp32_livingroom (p2, c1, k60, u’mqttuser’).

Does that mean that MQTT is set up and communicating? I don’t know where to look to see what MAC addresses are being passed through.

I have added two items to mqtt_room platform as sensors. They show up in my master list of devices, but have been not triggered on or off.

This might help:

Check the section about mqtt. You can use the developer tools in home assistant to subscribe to the correct topic, probably something like room_presence

IT WORKS!

I subscribed to all of the MQTT communications by using this in the command line:

mosquitto_sub -u hass -P mqttpassword -t “#” -v

I was hoping that my watch would be able to be used as a beacon. Apparently not. Oddly enough, my TACX bike trainer does work as a beacon, so at least I’ll be able to know where in the house that is located.

Hi,
I’m trying to get this to work but i’m not having much luck. I tried installing Platformio for atom but the installer just hangs. I read that they now recommend using the extension with vscode so i have set that up instead.

I think I’m setting things up as per the documents but the build fails. I setup the settings_local.h filling out the ssid, password, port, user, password and room sections. Building is giving me the following error

C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino: In function 'void WiFiEvent(system_event_id_t)':
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_SCAN_DONE' not handled in switch [-Wswitch]
   switch(event) {
         ^
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_STA_CONNECTED' not handled in switch [-Wswitch]
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_STA_AUTHMODE_CHANGE' not handled in switch [-Wswitch]   
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_STA_LOST_IP' not handled in switch [-Wswitch]
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_STA_WPS_ER_SUCCESS' not handled in switch [-Wswitch]    
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_STA_WPS_ER_FAILED' not handled in switch [-Wswitch]     
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_STA_WPS_ER_TIMEOUT' not handled in switch [-Wswitch]    
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_STA_WPS_ER_PIN' not handled in switch [-Wswitch]        
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_STA_WPS_ER_PBC_OVERLAP' not handled in switch [-Wswitch]
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_AP_START' not handled in switch [-Wswitch]
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_AP_STOP' not handled in switch [-Wswitch]
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_AP_STACONNECTED' not handled in switch [-Wswitch]   
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_AP_STADISCONNECTED' not handled in switch [-Wswitch]
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_AP_STAIPASSIGNED' not handled in switch [-Wswitch]  
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_AP_PROBEREQRECVED' not handled in switch [-Wswitch] 
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_GOT_IP6' not handled in switch [-Wswitch]
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_ETH_START' not handled in switch [-Wswitch]
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_ETH_STOP' not handled in switch [-Wswitch]
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_ETH_CONNECTED' not handled in switch [-Wswitch]     
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_ETH_DISCONNECTED' not handled in switch [-Wswitch]
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_ETH_GOT_IP' not handled in switch [-Wswitch]
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:248:9: warning: enumeration value 'SYSTEM_EVENT_MAX' not handled in switch [-Wswitch]
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino: In lambda function:
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:532:50: warning: unknown conversion type character 0xa in format [-Wformat=]
       Serial.printf("Progress: %u% \n\r", percent);
                                                  ^
In file included from C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:36:0:
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino: In function 'void setup()':
src\Settings.h:14:18: error: '$MQTT_PORT$' was not declared in this scope
 #define mqttPort $MQTT_PORT$
                  ^
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:568:34: note: in expansion of macro 'mqttPort'
   mqttClient.setServer(mqttHost, mqttPort);
                                  ^
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino: In function 'bool handleMqttDisconnect()':
C:/Users/MYUSER/Documents/ESP32-mqtt-room-master/src/ESP32-mqtt-room.ino:214:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
*** [.pio\build\esp32\src\ESP32-mqtt-room.ino.cpp.o] Error 1
================================================================================================== [FAILED] Took 6.74 seconds ==================================================================================================
The terminal process "C:\Users\MYUSER\.platformio\penv\Scripts\platformio.exe 'run', '--environment', 'esp32'" terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

I have already tried deleting the .pio/libdeps folder and rebuilding to no effect. About the port error, i thought the idea was to specify those details in setting_local?

Any ideas what might be causing the issue. Thanks

Ill try again after getting some sleep :joy:

EDIT: After some sleep i realised some things
*By sample you probably mean settings.h
** Setting the port in settings.h did allow it to build without errors. I don’t know why but if it works it works
*** I’m new to electronics and Arduino so i don’t really know what I’m doing but I’m trying to learn. I realised that I’m trying to load this not onto a esp32 but a 8266 :joy: so i guess while i wait for some esp32s to come from china ill study some electrical theory and Arduino stuff

You will need to use an esp32, since the esp8266 does not have bluetooth. Welcome to the hobby! You’re doing the right things to learn - trying stuff, having trouble, and asking questions, so keep doing it and you’ll get better all the time.

You’ll need to define all your settings to connect to your mqtt server properly. Did you find this section of the docs? You’ll want to make sure you get everything configured before you try to compile and upload, or the build will fail.

Hi,
Thanks for your encouragement. I have some esp32s on order. Ill try rereading your guide once they arrive.
I also purchased an Uno sensor starter kit and downloaded some pdf tutorials. Is there any other learning materials you can suggest?

I actually use esphome quite a bit rather than writing much code from scratch these days. It’s a lot more maintainable and pretty easy to get started. Take a look at the add-on and the esphome website to get some ideas.

Also, if you’re just getting started in the hobby, you’ll probably want a soldering iron and some solder, a multimeter, some form of power supply (can even just be a battery pack) and some breadboards and jumper wires to test your circuits.

Finally, there are some good youtubers out there doing cool stuff with Home Assistant and ESPs. DrZzs, The Hook Up, Intermit.Tech and GreatScott! are just a few channels you might find helpful.

Hi,

I see a LOT of work has been put into this BLE tracking. Thank you very much for all this effort. !

I have read things about BLE > home assistant for a couple of days now, but I dont fully understand some bits. Maybe you can help me with this process.

Currently I have 2 ESP32’s running nicely in home assistant. They are programmed to report to MQTT server. Home Assistant sees the reported things there. Temperature and humidity per room in this case.
The ESPHome addon is installed, and they are connected and flashable OTA.

I have gotten me a third ESP, also programmable in ESPHome.
I have working that my phone emits a BLE beacon (Beacon Scope) and it is seen by the ESP32. Detection: True/False.

I found out that HA does not see this as device_tracker > home/not_home.
What I am thinking now, is that I only need to rewrite the esp32 code so it pushes the correct payload “location/yadayada…” to mqtt server.

In my search to that piece of cde, I read here and more other places, that I need PlatformIO for atom to make it work.

What I am confused about: In my situation, do I still need to go through with the PlatformIO route? Why? what is the difference, the benefit etc. This part is just not clear to me. I do not see the path before me clearly yet.
And I miss the esp32 code part which gives me the correct mqtt topics + payload.
Any advice would be welcome .

Kind regards.

I think you’re confusing two different options - this project does not leverage ESPHome, it is entirely separate. That means that for your ESP32, you’ll need to use platformIO (can be installed on Atom or VSCode), and upload this project to your board.

It would be ideal to fold this functionality into the ESPHome project, but unfortunately I do not have the time to spend making this change. In fact, I am no longer using this project personally, so I won’t be developing it any further.

Cheers,

Thank you!
I think I understand it now. So, if I am correct now;
ESPHome and PlatformIO are two different methods of programming / interacting with the ESP32 board itself.

I have working ESPHome setup, so I should continue with getting mqtt propperly setup via that route.

Is this correct? :slight_smile: