Python

The MQTT Python client code and Mosquitto broker should be installed on the Raspberry Pi .



MQTT allows asynchronous messaging between the Node Red code and the Python code on the raspberry pi.

The broker is running on the same device as the MQTT Node Red and Python clients and so the broker_address = "127.0.0.1"

client.loop_start() starts a new thread to run in the background awaiting a msg payload sent by Node Red and assigns it to the global variable MQTT_notify.

The Wiced Sense sensor data is sent in two packages with the first byte of the notification package being an indicator as to which sensor values are following. The accelerometer, gyro sensor and magnetometer package are more frequent than the humidity, pressure and temperature. While following Python code reads all the sensor data, only the temperature, pressure and humidity values are utilised in Node Red.

The Python module bluepy facilitates communication with Bluetooth Low Energy devices such as the Wiced Sense. (see BluePy documentation )

In bluepy, notifications are processed by creating a “delegate” object (MyDelegate) and registering it with the Peripheral.

Line 35 MyDelegate, a DefaultDelegate instance is created.

Line 64 the Wiced Sense's MAC address is used to set up a Peripheral object.

Line 68 registers the Peripheral object with MyDelegate.

A method in the delegate is called whenever a notification is received from the peripheral, as shown below: