RPICT and MQTT: Difference between revisions
No edit summary |
|||
Line 5: | Line 5: | ||
2/ Using Emonhub.<br> | 2/ Using Emonhub.<br> | ||
=Using Python Script= | |||
In this setup the Raspberrypi where the RPICT is will be the MQTT broker. A script will run to convert the serial string from the RPICT into channel feed. | In this setup the Raspberrypi where the RPICT is will be the MQTT broker. A script will run to convert the serial string from the RPICT into channel feed. | ||
==Setup Mosquito broker== | |||
Install Mosquitto using the commands below: | Install Mosquitto using the commands below: | ||
Line 18: | Line 18: | ||
==Make the script== | |||
First of all install the mqtt python module | First of all install the mqtt python module | ||
Line 71: | Line 71: | ||
When running the above script all channels will be published as soon as data are sent by the RPICT board. | When running the above script all channels will be published as soon as data are sent by the RPICT board. | ||
==Test it== | |||
Test the channels by subscribing to one channel as below: | Test the channels by subscribing to one channel as below: | ||
Line 78: | Line 78: | ||
The value of Vrms will be printed in the terminal. | The value of Vrms will be printed in the terminal. | ||
=Using Emonhub= |
Revision as of 20:42, 31 May 2019
There are two ways to setup MQTT.
1/ Running a python script.
2/ Using Emonhub.
Using Python Script
In this setup the Raspberrypi where the RPICT is will be the MQTT broker. A script will run to convert the serial string from the RPICT into channel feed.
Setup Mosquito broker
Install Mosquitto using the commands below:
sudo apt install -y mosquitto mosquitto-clients
Enable the service to start at boot time:
sudo systemctl enable mosquitto.service
Make the script
First of all install the mqtt python module
sudo pip install paho-mqtt
A simple python script can be edited. Create a file with the content of the script below:
MQTT_SERV = "localhost" MQTT_PATH = "RPICT7V1" MQTT_USER = "" MQTT_PASS = "" CHANNELS = ["NodeID", "RP1", "RP2", "RP3", "RP4", "RP5", "RP6", "RP7", "Irms1", "Irms2", "Irms3", "Irms4", "Irms5", "Irms6", "Irms7", "Vrms"] import paho.mqtt.client as mqtt import serial ser = serial.Serial('/dev/ttyAMA0', 38400) client = mqtt.Client("P1") client.username_pw_set(MQTT_USER, MQTT_PASS) client.connect(MQTT_SERV) try: while 1: # Read one line from the serial buffer line = ser.readline() # Remove the trailing carriage return line feed line = line[:-2] # Create an array of the data Z = line.split(' ') # Print it for debug print line # Publish to the MQTT broker for i in range(len(Z)): client.publish("%s/%s" % (MQTT_PATH, CHANNELS[i]), Z[i]) except KeyboardInterrupt: client.disconnect() ser.close()
Modify the five parameters MQTT_SERV, MQTT_PATH, MQTT_USER, MQTT_PASS and CHANNELS to your needs. We are using a default RPICT7V1 for our example here.
Save the file and launch it with the command
python myfile.py
When running the above script all channels will be published as soon as data are sent by the RPICT board.
Test it
Test the channels by subscribing to one channel as below:
mosquitto_sub -h localhost -t RPICT7V1/Vrms -u "" -P ""
The value of Vrms will be printed in the terminal.