-
1Setting up the raspi
First i flashed my Sandisk 16GB SD card with a Debian Desktopless image.
I would only need Node-red and ssh to interface with the raspi anyway.
After connecting the raspberryPi to my local wifi i started a putty ssh sesion:
Updating the system and installing nodered
sudo apt-get update sudo apt-get upgrade
enabling the launch of nodered at startup with systemctl
bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered) sudo systemctl enable nodered.service
Now i need to install the Dashboard nodes im using to visualize the data (no grafana nor influxdb yet)
cd ~/.node-red npn install node-red-dashboard
Reboot to allow the changes to take place
sudo reboot now
After this we should be able to connect to nodered with any PC in the same local network
Nodered's default port is :1880My raspi's ip is 192.168.0.178 so i its nodered service is reachable by my browser with:
http://192.168.0.178:1880
-
2Sniffing and decoding the BLE characteristics (of the XIAOMI MI sensors)
My BLE swissknife debugger of choice is LightBlue, i had it installed on my phone a while back.
Once there we Scan for our Sensor:
We connect to our sensor and the app automatically discovers all the BLE characteristics& reads their descriptors for us:
The interesting characteristic is the one with the descriptor "Temperature and Humidity"
We hit subscribe which effectively does two things
- The app sends the commad to the BLE sensor to enable its notifications for that characteristic.
- The app remains connected and prints the received values as they come.
Now the data just arrives without us having to read every time
Now the fun part, guessing what 88 07 34 39 0B actually means.
Using My Low level Infering powers i noticed:- ALL values are littleendianed
- First two bytes 88 07 tell us the temperaturex100
0x0788== 1928 ---> 19,28ºC - Third byte tell us the humidity %
0x34== 52% - Fourth and fifth bytes tell us the Voltage level of the coin cell (this was a hard one to come up with)
0x0B39== 2873mV
-
3Trying out GATTTOOL
Now that i know how to access and decode the data lets try the Raspi GATTTOOL default BLE handler:
Native to Debian and a lso to a bunch of linux distros or so i've heard of .First i reset the bluetooth of the raspi, just in case:
sudo service bluetooth restart
This is how we scan for ble devices
sudo hcitool lescan
LE Scan ...
33:26:36:CF:E7:FB (unknown)
2D:CE:BB:73:00:AC (unknown)
00:80:E1:26:C2:49 MyCST
00:80:E1:26:C2:49 (unknown)
18:C1:44:DF:5E:37 (unknown)
44:49:9C:22:F8:F1 (unknown)
78:A7:EB:13:64:09 (unknown)
A4:C1:38:CB:65:E0 (unknown)
4A:5F:3A:35:5F:D0 (unknown)
4A:5F:3A:35:5F:D0 (unknown)
13:D3:50:F5:38:95 (unknown)
5E:BC:E1:D4:66:76 (unknown)
A4:C1:38:57:E7:CA (unknown)
A4:C1:38:57:E7:CA LYWSD03MMC
A4:C1:38:6A:3B:0C (unknown)
A4:C1:38:6A:3B:0C LYWSD03MMC
78:A7:EB:13:64:09 Mi Air2
A4:C1:38:28:CE:FF (unknown)
A4:C1:38:28:CE:FF LYWSD03MMC
A4:C1:38:99:73:93 (unknown)
A4:C1:38:B0:60:59 (unknown)
A4:C1:38:B0:60:59 LYWSD03MMC
A4:C1:38:51:D1:B1 (unknown)Now we start an interactive session (we manually prompt the commands to the gatttool service)
sudo gatttool -b A4:C1:38:57:E7:CA -I [A4:C1:38:57:E7:CA][LE]> connect Attempting to connect to A4:C1:38:57:E7:CA Connection successful
We are connected to the BLE sensor!, lets discover its characteristics.
[A4:C1:38:57:E7:CA][LE]> char-desc
It has maaaany:
handle: 0x0001, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0002, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0003, uuid: 00002a00-0000-1000-8000-00805f9b34fb
handle: 0x0004, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0005, uuid: 00002a01-0000-1000-8000-00805f9b34fb
handle: 0x0006, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0007, uuid: 00002a04-0000-1000-8000-00805f9b34fb
handle: 0x0008, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0009, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x000a, uuid: 00002a05-0000-1000-8000-00805f9b34fb
handle: 0x000b, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x000c, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x000d, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x000e, uuid: 00002a24-0000-1000-8000-00805f9b34fb
handle: 0x000f, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0010, uuid: 00002a25-0000-1000-8000-00805f9b34fb
handle: 0x0011, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0012, uuid: 00002a26-0000-1000-8000-00805f9b34fb
handle: 0x0013, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0014, uuid: 00002a27-0000-1000-8000-00805f9b34fb
handle: 0x0015, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0016, uuid: 00002a28-0000-1000-8000-00805f9b34fb
handle: 0x0017, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0018, uuid: 00002a29-0000-1000-8000-00805f9b34fb
handle: 0x0019, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x001a, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x001b, uuid: 00002a19-0000-1000-8000-00805f9b34fb
handle: 0x001c, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x001d, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x001e, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x001f, uuid: 00010203-0405-0607-0809-0a0b0c0d2b12
handle: 0x0020, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0021, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0022, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0023, uuid: ebe0ccb7-7a0a-4b0c-8a1a-6ff2997da3a6
handle: 0x0024, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0025, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0026, uuid: ebe0ccb9-7a0a-4b0c-8a1a-6ff2997da3a6
handle: 0x0027, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0028, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0029, uuid: ebe0ccba-7a0a-4b0c-8a1a-6ff2997da3a6
handle: 0x002a, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x002b, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x002c, uuid: ebe0ccbb-7a0a-4b0c-8a1a-6ff2997da3a6
handle: 0x002d, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x002e, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x002f, uuid: ebe0ccbc-7a0a-4b0c-8a1a-6ff2997da3a6
handle: 0x0030, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0031, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0032, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0033, uuid: ebe0ccbe-7a0a-4b0c-8a1a-6ff2997da3a6
handle: 0x0034, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0035, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0036, uuid: ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6 <---this one is "Temperature and Humidity"
handle: 0x0037, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0038, uuid: 00002902-0000-1000-8000-00805f9b34fb <--- this one is the characteristic configuration
handle: 0x0039, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x003a, uuid: ebe0ccc4-7a0a-4b0c-8a1a-6ff2997da3a6
handle: 0x003b, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x003c, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x003d, uuid: ebe0ccc8-7a0a-4b0c-8a1a-6ff2997da3a6
handle: 0x003e, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x003f, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0040, uuid: ebe0ccd1-7a0a-4b0c-8a1a-6ff2997da3a6
handle: 0x0041, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0042, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0043, uuid: ebe0ccd7-7a0a-4b0c-8a1a-6ff2997da3a6
handle: 0x0044, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0045, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0046, uuid: ebe0ccd8-7a0a-4b0c-8a1a-6ff2997da3a6
handle: 0x0047, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0048, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0049, uuid: ebe0ccd9-7a0a-4b0c-8a1a-6ff2997da3a6
handle: 0x004a, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x004b, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x004c, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x004d, uuid: ebe0cff1-7a0a-4b0c-8a1a-6ff2997da3a6
handle: 0x004e, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x004f, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0050, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0051, uuid: 8edffff1-3d1b-9c37-4623-ad7265f14076
handle: 0x0052, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0053, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0054, uuid: 8edffff3-3d1b-9c37-4623-ad7265f14076
handle: 0x0055, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0056, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0057, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0058, uuid: 8edffff4-3d1b-9c37-4623-ad7265f14076
handle: 0x0059, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x005a, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x005b, uuid: 8edfffef-3d1b-9c37-4623-ad7265f14076
handle: 0x005c, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x005d, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x005e, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x005f, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0060, uuid: 00000004-0000-1000-8000-00805f9b34fb
handle: 0x0061, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0062, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0063, uuid: 00000010-0000-1000-8000-00805f9b34fb
handle: 0x0064, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0065, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0066, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0067, uuid: 00000017-0000-1000-8000-00805f9b34fb
handle: 0x0068, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0069, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x006a, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x006b, uuid: 00000018-0000-1000-8000-00805f9b34fb
handle: 0x006c, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x006d, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x006e, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x006f, uuid: 00000019-0000-1000-8000-00805f9b34fb
handle: 0x0070, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0071, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0072, uuid: 00002800-0000-1000-8000-00805f9b34fb
handle: 0x0073, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0074, uuid: 00000101-0065-6c62-2e74-6f696d2e696d
handle: 0x0075, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x0076, uuid: 00002902-0000-1000-8000-00805f9b34fb
handle: 0x0077, uuid: 00002803-0000-1000-8000-00805f9b34fb
handle: 0x0078, uuid: 00000102-0065-6c62-2e74-6f696d2e696d
handle: 0x0079, uuid: 00002901-0000-1000-8000-00805f9b34fb
handle: 0x007a, uuid: 00002902-0000-1000-8000-00805f9b34fbAs seen in previous step i already knew which Characteristic UUID corresponds with "Temperature and Humidity"
(uuid: ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6)I also know that in order to Activate Notifications i need to write a 0100 in the Characteristic configuration/properties
(uuid: 00002902-0000-1000-8000-00805f9b34fb)Why 0100? .... remember LittleEndianess
its a bit confusing, some places call this characteristic value notify and others broadcast
#define GATT_PROP_BCAST 0x01 //!< Permits broadcasts of the Characteristic Value
Gatttool asigned the handle 0x0036 to "Temperature and Humidity"
Gattol asigned the handle 0x0038 to "Temperature and Humidity"'s configI order to activate the notifications, we write the value 0100 into the config characteristic
[A4:C1:38:57:E7:CA][LE]> char-write-req 0x0038 0100
the sensor starts to notify periodically "Temperature and Humidity" 's value
Notification handle = 0x0036 value: 78 07 32 0f 0b
Notification handle = 0x0036 value: 79 07 32 0f 0b
Notification handle = 0x0036 value: 7b 07 32 0f 0b
Notification handle = 0x0036 value: 7a 07 32 0f 0b
Notification handle = 0x0036 value: 7b 07 32 0f 0b
Notification handle = 0x0036 value: 7c 07 32 0f 0b
Notification handle = 0x0036 value: 78 07 32 0f 0b
Notification handle = 0x0036 value: 7b 07 32 0f 0b
Notification handle = 0x0036 value: 7b 07 32 0f 0b
Notification handle = 0x0036 value: 7b 07 32 0f 0b -
4NodeRed, connect and display a single sensor
EXEC node executes a bash script:
Connects to the sensor, writes to its configuration in order to activate Notifications, remains connected and listens to incoming notificationssudo gatttool -i hci0 -b A4:C1:38:B0:60:59 --char-write-req -a 0x0038 --value=0100 --listen &
Important: spawn mode should be selected for this node to remain listening to incoming gatttool notifications
Function node uses Java to read the received string, rearrange endianness, parse it to variables and construct the msg.payload the chart nodes like.
index=msg.payload.indexOf("value: ")+7 msg.payload=msg.payload.substring(index,index+14)//find "value: " inside the received string and msg.topic="Sensor 10" msg.temperatura=""+msg.payload.substring(3,5)+msg.payload.substring(0,2) msg.humedad=""+msg.payload.substring(6,8) msg.pilavolt=""+msg.payload.substring(12,14)+msg.payload.substring(9,11) msg.temperatura=msg.temperatura.replace(" ","") msg.humedad=msg.humedad.replace(" ","") msg.pilavolt=msg.pilavolt.replace(" ","") msg.temperatura=parseInt(msg.temperatura, 16)/100 msg.humedad=parseInt(msg.humedad, 16) msg.pilavolt=parseInt(msg.pilavolt, 16)/1000 if (msg.temperatura !=NaN) { //return {topic:"temperature", payload:msg.temperatura}; //return {topic:"humidity", payload:msg.humedad}; return {topic:"batteryvolt", payload:msg.pilavolt}; }
now if i connect to (your raspi ip might differ) http://192.168.0.178:1880/ui/ in my browser i see the received data from that sensor
-
5InfluxDB, a database to store and manage the received data
Installing InfluxDB in a raspi Zero (slow as ****)
sudo apt-get update sudo apt-get install influxdb
If influxdb was installed succesfully we should be able to see the port being used:
sudo netstat -lptn
installing nodered influxdb's node, nodered will be able to store incoming data with the ifluxOUT node:
cd ~/.node-red npm install node-red-contrib-influxdb
-
6Grafana, cool data visualizations (web browser based)
TODO
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.