Skip to content

Weezmo/mqtt-bluetooth-pi

Repository files navigation

To get data from a Bluetooth device and send it to an MQTT broker on your Raspberry Pi running Raspbian OS Lite, you will need to follow these general steps:

  1. Install the necessary Bluetooth software on your Raspberry Pi. You can do this by running the following command in the terminal:

    sudo apt-get install bluez
    ```
    
    
  2. Pair your Bluetooth device with your Raspberry Pi. You can do this either by using the Bluetooth GUI or by running the following command in the terminal:

    bluetoothctl
    

    This will open the Bluetooth control panel. You can then use the scan on command to scan for nearby devices and the pair <device-id> command to pair with your Bluetooth device.

  3. Install the necessary MQTT software on your Raspberry Pi. You can do this by running the following command in the terminal:

    sudo apt-get install mosquitto mosquitto-clients
    
  4. Write a Python script that reads data from the Bluetooth device and publishes it to the MQTT broker. You can use the bluepy library to communicate with the Bluetooth device and the paho-mqtt library to publish data to the MQTT broker. Here's an example Python script:

    import bluepy.btle as btle
    import paho.mqtt.publish as publish
    
    # Set the MAC address of your Bluetooth device
    MAC_ADDRESS = '00:11:22:33:44:55'
    
    # Set the topic to publish data to on the MQTT broker
    MQTT_TOPIC = 'bluetooth/data'
    
    # Set the hostname and port number of the MQTT broker
    MQTT_HOST = 'localhost'
    MQTT_PORT = 1883
    
    # Define a callback function to handle incoming Bluetooth data
    def handle_data(handle, value):
        # Convert the data to a string
        data = value.decode('utf-8')
    
        # Publish the data to the MQTT broker
        publish.single(MQTT_TOPIC, data, hostname=MQTT_HOST, port=MQTT_PORT)
    
    # Connect to the Bluetooth device and enable notifications for the data characteristic
    device = btle.Peripheral(MAC_ADDRESS)
    data_service = device.getServiceByUUID('0000fff0-0000-1000-8000-00805f9b34fb')
    data_characteristic = data_service.getCharacteristics('0000fff1-0000-1000-8000-00805f9b34fb')[0]
    data_characteristic_handle = data_characteristic.getHandle()
    device.writeCharacteristic(data_characteristic_handle+1, b'\x01\x00')
    
    # Set up a notification handler for incoming data
    device.withDelegate(btle.DefaultDelegate(handle_data))
    
    # Start the main loop to receive Bluetooth data and publish it to the MQTT broker
    while True:
        device.waitForNotifications()
    ```
    
    This script connects to the Bluetooth device with the specified MAC address, enables notifications for the data characteristic, and sets up a callback function to handle incoming data. The callback function converts the data to a string and publishes it to the MQTT broker. The script then enters a loop to wait for incoming Bluetooth data and publish it to the MQTT broker.
  5. Run the Python script in the terminal using the following command:

    python bluetooth-mqtt.py
    

    This will start the script and begin reading data from the Bluetooth device and publishing it to the MQTT broker. You can then subscribe to the MQTT topic bluetooth/data to receive the data from the Bluetooth device.

About

Simple Collection for setting up mqtt on rpi 3, for bluetooth connecting and collecting data

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published