Difference between revisions of "MQTT"
From PhotoVoltaic Logger new generation
(Created page with "{{TOCright}}PVLng can listen to a MQTT broker running on any server. <blockquote>MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designe...") |
m (→Terminal 1 - Start subscriber) |
||
Line 38: | Line 38: | ||
Wait for 1 message, print verbose message and exit | Wait for 1 message, print verbose message and exit | ||
− | # mosquitto_sub -d -v -i test-subscriber -t test | + | # mosquitto_sub -d -v -i test-subscriber -t test -C 1 |
Client test-subscriber sending CONNECT | Client test-subscriber sending CONNECT | ||
Client test-subscriber received CONNACK | Client test-subscriber received CONNACK |
Latest revision as of 21:07, 17 May 2017
MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. -- MQTT
PVLng scripts can send to any MQTT broker.
Setup your own mosquitto broker
sudo apt-get update sudo apt-get install mosquitto
If you run the PVLng scripts also on this host or on remote equipment running the scripts only you need only the clients scripts
sudo apt-get update sudo apt-get install mosquitto-clients
Configure MQTT broker
Put the following into /etc/mosquitto/conf.d/local.conf
cat <<EOT | sudo tee /etc/mosquitto/conf.d/local.conf log_type error log_type warning log_type notice log_type information connection_messages true log_timestamp true EOT
And restart
sudo /etc/init.d/mosquitto restart
Test your broker best with 2 terminals
Terminal 1 - Start subscriber
Wait for 1 message, print verbose message and exit
# mosquitto_sub -d -v -i test-subscriber -t test -C 1 Client test-subscriber sending CONNECT Client test-subscriber received CONNACK Client test-subscriber sending SUBSCRIBE (Mid: 1, Topic: test, QoS: 0) Client test-subscriber received SUBACK Subscribed (mid: 1): 0
Terminal 2 - Publish a zero length test message
# mosquitto_pub -d -n -i test-publisher -h localhost -t test Client test-publisher sending CONNECT Client test-publisher received CONNACK Client test-publisher sending PUBLISH (d0, q0, r0, m1, 'test', ... (0 bytes)) Client test-publisher sending DISCONNECT
Terminal 1 - Received the message?
... Client test-subscriber received PUBLISH (d0, q0, r0, m0, 'test', ... (0 bytes)) test (null) Client test-subscriber sending DISCONNECT
Run PVLng MQTT subscriber
The script is tools/mqtt-subscribe.sh
Start it at reboot via cron
cat <<EOT | sudo tee /etc/cron.d/mqtt-subscribe # MQTT listener, wait for mosquitto broker @reboot root sleep 60 && /path/to/PVLng/tools/mqtt-subscribe.sh start >/dev/null # And check at least each hour 0 * * * * root /path/to/PVLng/tools/mqtt-subscribe.sh start >/dev/null EOT
Check for safety reasons each hour.
To store data into PVLng database send a message to topic
pvlng/<API key>/data/<Channel GUID>
Configure PVLng scripts
The scripts check for configured MQTT broker and use it, otherwise the default curl method is used as before.
You need only set the MQTT broker in PVLng.conf and uncomment the line.
############################################################################## ### Send to mosquitto broker running on $mosquittoServer ############################################################################## ### <Sever>[:<port>] #mosquittoServer=$PVLngDomain:1883
If you test your scripts and send data, you can watch the stream in a terminal with
mosquitto_sub -v -t pvlng/+/data/#
This will give you something like this (2 temperature sensors)
pvlng/df9adc61-e81b-1e12-8fc7-8a1f000a3025/data/72f9-9c42-36c5-c2af-3c01-cd95-67a1-98d1 {"data":"29.5","timestamp":"1495051260"} pvlng/df9adc61-e81b-1e12-8fc7-8a1f000a3025/data/bef9-6e8b-29a1-15a3-4100-58d2-69b8-3bdb {"data":"24.1875","timestamp":"1495051260"}