Adafruit_MQTT_Library/examples/mqtt_arbitrary_data/README.md

109 lines
3.4 KiB
Markdown
Raw Permalink Normal View History

2016-02-01 19:33:33 +01:00
# Adafruit MQTT Library Arbitrary Data Publish Example
2016-01-31 23:19:38 +01:00
2016-02-01 03:29:48 +01:00
This example illustrates publishing an arbitrary data packet using the Adafruit MQTT library to an MQTT feed which can then be parsed by the included python subscriber client. Possible usage cases include adding metadata (collection time, sensor info etc) to a datapoint.
2016-02-01 01:57:43 +01:00
2016-02-01 03:35:01 +01:00
![alt-text](https://raw.githubusercontent.com/stuthedew/Adafruit_MQTT_Library/Arbitrary_data_publish/examples/mqtt_arbitrary_data/python_subscriber/mqtt_figure.png "Arbitrary data flow diagram")
2016-02-01 03:17:21 +01:00
2016-02-01 03:24:07 +01:00
My motivation for this was wanting to be able to include metadata to a post.
Specifically, I was playing around with a [Teviso RD3024 radiation sensor](http://www.teviso.com/en/products/radiation-sensor-rd3024.htm), and a salvaged Americium radiation source from a smoke detector, at varying distances from the sensor. I wanted a way to associate the collection time, and distance between the source and sensor with the actual radiation reading itself.
2016-02-01 02:37:12 +01:00
---
2016-02-01 02:39:10 +01:00
## Installing and configuring Mosquitto broker (minimal working setup):
2016-02-01 02:35:02 +01:00
2016-02-01 02:38:22 +01:00
####_Installing on Raspberry Pi/Linux:_
2016-02-01 02:15:46 +01:00
2016-02-01 02:02:57 +01:00
```bash
sudo apt-get install mosquitto
2016-02-01 02:15:46 +01:00
cd /etc/mosquitto/
2016-02-01 03:36:11 +01:00
#See "Configuring Mosquitto Broker below"
2016-02-01 02:02:57 +01:00
```
2016-02-01 02:38:22 +01:00
####_Installing On a Mac:_
2016-02-01 02:02:57 +01:00
```bash
brew install mosquitto
2016-02-01 02:15:46 +01:00
cd /usr/local/etc/mosquitto
2016-02-01 03:36:11 +01:00
#See "Configuring Mosquitto Broker below"
2016-02-01 02:15:46 +01:00
```
2016-02-01 02:38:39 +01:00
---
2016-02-01 02:36:36 +01:00
####Configuring Mosquitto broker
2016-02-01 02:15:46 +01:00
```bash
sudo nano mosquitto.conf
```
2016-02-01 02:22:40 +01:00
Now we have to enable a password file to correctly interface with the Adafruit MQTT library. Scroll about two thirds of the way down until you see:
2016-02-01 02:15:46 +01:00
```bash
# -----------------------------------------------------------------
# Default authentication and topic access control
# -----------------------------------------------------------------
```
You should see `#password_file` about a paragraph after that.
Change
```bash
#password_file
```
To
```bash
password_file pwfile
2016-02-01 02:02:57 +01:00
```
2016-02-01 02:15:46 +01:00
Now `ctrl-x` to save and exit.
2016-02-01 02:02:57 +01:00
2016-02-01 02:27:23 +01:00
You're almost done! We just have to create and populate the password file we just configured. The default user info is:
2016-02-01 02:29:35 +01:00
* **Arduino Subscriber:**
2016-02-01 02:24:29 +01:00
* Username: TestUser
* Password: TestUser
2016-02-01 02:22:40 +01:00
2016-02-01 02:29:35 +01:00
* **Python Subscriber:**
* Username: TestPy
* Password: TestPy
2016-02-01 02:27:23 +01:00
2016-02-01 02:22:40 +01:00
```bash
touch pwfile #create the password file
2016-02-01 02:27:23 +01:00
mosquitto_passwd pwfile TestUser #Enter and confirm password when prompted
mosquitto_passwd pwfile TestPy #Enter and confirm password when prompted
2016-02-01 02:22:40 +01:00
```
2016-02-01 02:46:14 +01:00
####Running Mosquitto broker
2016-02-01 02:41:20 +01:00
Now run Mosquitto broker to allow Arduino publisher and Python subscriber to communicate
```bash
mosquitto
```
2016-02-01 02:32:56 +01:00
---
2016-02-01 02:37:12 +01:00
2016-02-01 02:39:10 +01:00
## Using Example Python Subscriber:
2016-02-01 01:57:43 +01:00
2016-02-01 02:46:14 +01:00
####Installing Python subscriber
Install dependencies if you haven't already
2016-02-01 01:59:14 +01:00
```bash
2016-02-01 01:57:43 +01:00
cd ../Adafruit_MQTT_Library/examples/mqtt_arbitrary_buffer/python_subscriber
pip install -r requirements.txt
2016-02-01 01:59:14 +01:00
```
2016-02-01 01:57:43 +01:00
2016-02-01 02:46:14 +01:00
####Installing Python subscriber
2016-02-01 02:22:40 +01:00
Run python script with default values and watch your parsed data print out.
2016-02-01 01:59:14 +01:00
```bash
2016-02-01 02:46:14 +01:00
python subscriber.py #Add -h flag to see modifiable options
2016-02-01 01:59:14 +01:00
```
2016-02-01 01:57:43 +01:00
2016-02-01 02:46:14 +01:00
Assuming that the Mosquitto broker is running in the background and the Adafruit_MQTT client (Arduino) is publishing, you should see the example data print out every 10 seconds.
2016-02-01 01:59:14 +01:00
```bash
2016-02-01 02:46:14 +01:00
MQTT: Connection successful
Connection successful
Subscribed to /feeds/arb_packet
Received char Array: "Hello!", val1: -4533, val2: 73102, val3: 3354...
Received char Array: "Hello!", val1: -4533, val2: 83611, val3: 3354...
Received char Array: "Hello!", val1: -4533, val2: 94115, val3: 3354...
2016-02-01 01:59:14 +01:00
```