RPICT and MQTT: Difference between revisions

From lechacal
Jump to navigation Jump to search
 
(18 intermediate revisions by the same user not shown)
Line 1: Line 1:




There are two ways to setup MQTT.<br>
There are three ways to setup MQTT.<br>
1/ Running a python script.<br>
1/ Running a python script.<br>
2/ Using Emonhub.<br>
2/ Using Emonhub.<br>
3/ Using rpict-mqtt


==Using Python Script==
=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 hosts both the RPICT board and the MQTT broker. A script runs to convert the serial string from the RPICT into channel feed.


===Setup Mosquito broker===
==Setup Mosquito broker==


Install Mosquitto using the commands below:
Install Mosquitto using the commands below:
  sudo apt install -y mosquitto mosquitto-clients
  sudo apt install -y mosquitto mosquitto-clients


Enable the service to start at boot time:
Start the service:
sudo systemctl start mosquitto.service
 
If you want the service to start at boot time also run this one:
  sudo systemctl enable mosquitto.service
  sudo systemctl enable mosquitto.service




===Make the script===
==The mqtt script==
 
First of all install the mqtt python module
sudo pip install paho-mqtt
 
<small>If not installed already install pip first with sudo apt-get install python3-pip</small>
 
A simple python script has been made for this. Download it with
 
wget lechacal.com/RPICT/example/rpict-mqtt.py.zip
 
Then
unzip rpict-mqtt.py.zip
 
Open the file with and edit the top five lines.


A simple python script can be edited. Create a file with the content of the script below:
nano rpict-mqtt.py
 
This is the content of the file below


  MQTT_SERV = "localhost"
  MQTT_SERV = "localhost"
Line 61: Line 81:
  ser.close()
  ser.close()


Modify the five parameters '''MQTT_SERV''', '''MQTT_PATH''', '''MQTT_USER''', '''MQTT_PASS''' and '''CHANNELS''' to your needs.
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
Save the file and launch it with the command
  python myfile.py
  python rpict-mqtt.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 on the terminal.
 
=Using Emonhub=
 
If the Emoncms image is used then MQTT is already setup. Simply use Emonhub.
 
Include the following config in the emonhub config.
 
<nowiki>[[MQTT]]</nowiki>
    Type = EmonHubMqttInterfacer
    [[[init_settings]]]
        mqtt_host = 127.0.0.1
        mqtt_port = 1883
        mqtt_user = 'emonpi'
        mqtt_passwd = 'emonpimqtt2016'
    [[[runtimesettings]]]
        pubchannels = ToRFM12,
        subchannels = ToEmonCMS,
        # emonhub/rx/10/values format
        # Use with emoncms Nodes module
        node_format_enable = 1
        node_format_basetopic = emonhub/
        # emon/emontx/power1 format - use with Emoncms MQTT input
        # http://github.com/emoncms/emoncms/blob/master/docs/RaspberryPi/MQTT.md
        nodevar_format_enable = 1
        nodevar_format_basetopic = emon/
 
Make sure the config also include the sensor config for the RPICT. For our RPICT7V1 example this is:
 
  <nowiki>[[11]]</nowiki>
    nodename = my_RPICT7V1
    hardware = RPICT7V1
    [[[rx]]]
      names = RP1, RP2, RP3, RP4, RP5, RP6, RP7, Irms1, Irms2, Irms3, Irms4, Irms5, Irms6, Irms7, Vrms
      datacode = 0
      scales = 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      units =W,W,W,W,W,W,W,mA,mA,mA,mA,mA,mA,mA,V
 
Test the final setup using the command below:
mosquitto_sub -h localhost -t emon/my_RPICT7V1/Vrms -u emonpi -P emonpimqtt2016
 
=Using rpict-mqtt=
 
There is package on github designed specifically to connect rpict units to MQTT.
Credit to gtricot on Github for maintaining this tool.
 
https://gtricot.github.io/rpict-mqtt/#/


==Using Emonhub==
Please note that we are not the maintainers of this project.

Latest revision as of 20:29, 14 August 2022


There are three ways to setup MQTT.
1/ Running a python script.
2/ Using Emonhub.
3/ Using rpict-mqtt

Using Python Script

In this setup the Raspberrypi hosts both the RPICT board and the MQTT broker. A script runs 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

Start the service:

sudo systemctl start mosquitto.service

If you want the service to start at boot time also run this one:

sudo systemctl enable mosquitto.service


The mqtt script

First of all install the mqtt python module

sudo pip install paho-mqtt

If not installed already install pip first with sudo apt-get install python3-pip

A simple python script has been made for this. Download it with

wget lechacal.com/RPICT/example/rpict-mqtt.py.zip

Then

unzip rpict-mqtt.py.zip

Open the file with and edit the top five lines.

nano rpict-mqtt.py

This is the content of the file 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 rpict-mqtt.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 on the terminal.

Using Emonhub

If the Emoncms image is used then MQTT is already setup. Simply use Emonhub.

Include the following config in the emonhub config.

[[MQTT]]
    Type = EmonHubMqttInterfacer
    [[[init_settings]]]
        mqtt_host = 127.0.0.1
        mqtt_port = 1883
        mqtt_user = 'emonpi'
        mqtt_passwd = 'emonpimqtt2016'

    [[[runtimesettings]]]
        pubchannels = ToRFM12,
        subchannels = ToEmonCMS,

        # emonhub/rx/10/values format
        # Use with emoncms Nodes module
        node_format_enable = 1
        node_format_basetopic = emonhub/

        # emon/emontx/power1 format - use with Emoncms MQTT input
        # http://github.com/emoncms/emoncms/blob/master/docs/RaspberryPi/MQTT.md
        nodevar_format_enable = 1
        nodevar_format_basetopic = emon/

Make sure the config also include the sensor config for the RPICT. For our RPICT7V1 example this is:

 [[11]]
   nodename = my_RPICT7V1
   hardware = RPICT7V1
   [[[rx]]]
      names = RP1, RP2, RP3, RP4, RP5, RP6, RP7, Irms1, Irms2, Irms3, Irms4, Irms5, Irms6, Irms7, Vrms
      datacode = 0
      scales = 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
      units =W,W,W,W,W,W,W,mA,mA,mA,mA,mA,mA,mA,V

Test the final setup using the command below:

mosquitto_sub -h localhost -t emon/my_RPICT7V1/Vrms -u emonpi -P emonpimqtt2016

Using rpict-mqtt

There is package on github designed specifically to connect rpict units to MQTT. Credit to gtricot on Github for maintaining this tool.

https://gtricot.github.io/rpict-mqtt/#/

Please note that we are not the maintainers of this project.