Difference between revisions of "MQTT"

From Hackerspace ACKspace
Jump to: navigation, search
m (Xopr moved page Sonoff to Mqtt: make project more generic (Sonoff->MQTT))
m (Cleaning up featured items)
 
(8 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
|State=Active
 
|State=Active
 
|Members=xopr
 
|Members=xopr
|Description=Space empty? Power off!
+
|Description=Space empty? Power off!
 
|GitHub=mqtt_spaceAPI
 
|GitHub=mqtt_spaceAPI
 
}}
 
}}
 
== synopsis ==
 
== synopsis ==
There is a sonoff Pow wifi switch in the space, connected to a powerstrip near the soldering table (in a brown plastic case with a toggle switch on top of it).
+
Read sensors and do some space automation using the Message Queuing Telemetry Transport protocol.
 +
 
 +
Currently, you can see the solder table powerstrip connected in the [[power]] graph.
 +
 
 +
== current status ==
 +
The MQTT server can be found at '''192.168.1.42''', via the regular port (1883) as well as the TLS port (8883) and secure websockets at port 443 (subject to change). To connect securely, use the [https://github.com/ACKspace/mqtt_spaceAPI/blob/master/ca.crt certificate authority chain]
 +
 
 +
There is a sonoff Pow wifi switch in the space, (not yet) connected to a powerstrip near the soldering table (in a brown plastic case with a toggle switch on top of it).
  
 
The idea is to gradually add them to every power strip so we can:
 
The idea is to gradually add them to every power strip so we can:
 
# measure (over)current
 
# measure (over)current
# switch them off centrally if the space is closed (or notify the user that they are still on).
+
# switch them off centrally if the space is closed (or notify the user that they are still on, for example, 3D printers).
  
 
It currently runs a modified version of the famous [https://github.com/arendst/Sonoff-Tasmota/ Tasmota] firmware (the modification allows dangerous GPIO whist using the energy measurement).
 
It currently runs a modified version of the famous [https://github.com/arendst/Sonoff-Tasmota/ Tasmota] firmware (the modification allows dangerous GPIO whist using the energy measurement).
Line 18: Line 25:
 
Also ''don't connect both serial and mains''; things will break.
 
Also ''don't connect both serial and mains''; things will break.
  
You can toggle it via wifi on [http://h1r-2067.lan h1r-2067.lan] (subject to change), or by issuing a <code>mosquitto_pub -h &lt;mqtt_ip&gt; -t "cmnd/h1r/POWER" -m "TOGGLE"</code>
+
You can toggle it via wifi on [http://h1r-2067.lan h1r-2067.lan] (subject to change), or by issuing a <code>mosquitto_pub -L 'mqtt://username:password@192.168.1.42/ackspace/hackspace/h1r/cmnd/POWER' -m 'TOGGLE'</code>
 +
 
 +
Read it's status by subscribing to the sensor topics: <code>mosquitto_sub -L mqtt://ackspace:ackspace@192.168.1.42/+/+/+/tele/SENSOR</code>
 +
Install <code>mosquitto-clients</code> to start experimenting.
 +
If you run into problems or find something interesting/broken, let [[User:Xopr|xopr]] know.
 +
 
 +
 
 +
== topics ==
 +
The proposed topic hierarchy is as followed:
 +
<code>[base]/[room]/[device]/[prefix]/[type]</code>
 +
Where both ''[prefix]'' and ''[type]'' come from [https://tasmota.github.io/docs/MQTT/#mqtt-topic-definition Tasmota's topic definition] (<code>tele</code> <code>stat</code> <code>cmnd</code> and <code>SENSOR</code> <code>STATE</code> <code>RESULT</code> <code>POWER</code> <code><abbr title="Last will & testament">LWT</abbr></code> respectively).
 +
Try to use all-lowercase names (with an exception of the last bit, often in CAPS),
 +
 
 +
At least <code>ackspace</code>, <code>common</code>, <code>outside</code> and <code>services</code> are supported as base. Services are a bit special; choose a category for ''room'' (i.e. <code>voip</code>) and an identifiable name for ''device'' (i.e. <code>freeswitch</code>); see the extra examples list of topics further below.
 +
 
 +
In the future, we can extend this to support something like <code>offsite</code> and the names of participants.
 +
 
 +
Since ACKspace doesn't have rooms at different levels, the second hierarchy level consist of the room, like <code>slackspace</code>, <code>hackspace</code> and <code>stackspace</code>. <code>device</code> is the name of the device (similar as Tasmota's <code>%topic%</code>) like <code>spacestate</code>, <code>temperature</code>, <code>hackswitch</code> or <code>fluorescent1</code>.
 +
 
 +
At least the following topics will be implemented first:
 +
* <code>ackspace/hackspace/spacestate/stat/RESULT</code> (json power state)
 +
** <code>ackspace/hackspace/spacestate/stat/POWER1</code> (string power state)
 +
** <code>ackspace/hackspace/spacestate/tele/STATE</code> (json state at interval)
 +
* <code>ackspace/hackspace/temperature/tele/SENSOR</code> (json telemetry data)
 +
* <code>outside/courtyard/barbecue/tele/SENSOR</code> (json telemetry data)
 +
 
 +
Extra examples:
 +
* <code>services/voip/freeswitch/tele/STATE</code> ([[FreeSWITCH]] specific data, like [[Telephone_system:Number_lookup|caller id]])
 +
* <code>services/network/bandwidth/tele/SENSOR</code> (Current network bandwidth usage)
 +
* <code>offsite/xopr/whatever/topic/he/pleases</code> (details might be {{Discuss|negotiated}} for ease of use)
 +
 
 +
== server setup ==
 +
<div class="mw-collapsible mw-collapsed" id="mw-customcollapsible-auth_conf">
 +
<div class="mw-customtoggle-auth_conf mw-code">/etc/mosquitto/conf.d/auth.conf</div>
 +
<pre class="mw-collapsible-content">
 +
allow_anonymous false
 +
password_file /etc/mosquitto/passwd
 +
acl_file /etc/mosquitto/acls
 +
 
 +
listener 1883
 +
protocol mqtt
 +
 
 +
listener 8883
 +
protocol mqtt
 +
cafile /etc/mosquitto/ca_certificates/ca.crt
 +
certfile /etc/mosquitto/certs/server.crt
 +
keyfile /etc/mosquitto/certs/server.key
 +
 
 +
listener 443
 +
protocol websockets
 +
cafile /etc/mosquitto/ca_certificates/ca.crt
 +
certfile /etc/mosquitto/certs/server.crt
 +
keyfile /etc/mosquitto/certs/server.key
 +
</pre>
 +
</div>
 +
 
 +
 
 +
 
 +
<div class="mw-collapsible mw-collapsed" id="mw-customcollapsible-acls">
 +
<div class="mw-customtoggle-acls mw-code">/etc/mosquitto/acls (modified)</div>
 +
<pre class="mw-collapsible-content">
 +
user spacestate
 +
topic write ackspace/+/spacestate/#
 +
 
 +
user temperature
 +
topic write ackspace/+/temperature/#
 +
topic write +/+/barbecue/#
 +
 
 +
user hackspace
 +
topic write ackspace/hackspace/#
 +
topic deny ackspace/+/spacestate/#
 +
 
 +
user slackspace
 +
topic write ackspace/slackspace/#
 +
topic deny ackspace/+/spacestate/#
 +
 
 +
user stackspace
 +
topic write ackspace/stackspace/#
 +
topic deny ackspace/+/spacestate/#
 +
 
 +
user myservice
 +
topic write services/category/myservice/#
 +
 
 +
user ackspace
 +
topic read #
 +
</pre>
 +
</div>
 +
 
 +
Passwords are generated with: <code>sudo mosquitto_passwd /etc/mosquitto/passwd [user]</code>
  
 
== todo ==
 
== todo ==
* add MQTT [[server]] that the switches can connect to, together with the [[space state]]
+
* add MOAR sensors (upgrade the current temperature sensors)
* make MQTT connect to [[spaceAPI]] and [[Power]] page
 
* add MOAR sensors
 
 
* MOAR sonoffs
 
* MOAR sonoffs

Latest revision as of 12:27, 22 April 2023

Project: MQTT
Featured: No
State Active
Members xopr
GitHub mqtt spaceAPI
Description Space empty? Power off!
Picture
No project picture! Fill in form Picture or Upload a jpeg here

synopsis

Read sensors and do some space automation using the Message Queuing Telemetry Transport protocol.

Currently, you can see the solder table powerstrip connected in the power graph.

current status

The MQTT server can be found at 192.168.1.42, via the regular port (1883) as well as the TLS port (8883) and secure websockets at port 443 (subject to change). To connect securely, use the certificate authority chain

There is a sonoff Pow wifi switch in the space, (not yet) connected to a powerstrip near the soldering table (in a brown plastic case with a toggle switch on top of it).

The idea is to gradually add them to every power strip so we can:

  1. measure (over)current
  2. switch them off centrally if the space is closed (or notify the user that they are still on, for example, 3D printers).

It currently runs a modified version of the famous Tasmota firmware (the modification allows dangerous GPIO whist using the energy measurement).

NOTE: the switch is built in a box with an external toggle switch. This switch has potentially 240v on its contacts so don't open it and touch the contacts when it is plugged in! Also don't connect both serial and mains; things will break.

You can toggle it via wifi on h1r-2067.lan (subject to change), or by issuing a mosquitto_pub -L 'mqtt://username:password@192.168.1.42/ackspace/hackspace/h1r/cmnd/POWER' -m 'TOGGLE'

Read it's status by subscribing to the sensor topics: mosquitto_sub -L mqtt://ackspace:ackspace@192.168.1.42/+/+/+/tele/SENSOR Install mosquitto-clients to start experimenting. If you run into problems or find something interesting/broken, let xopr know.


topics

The proposed topic hierarchy is as followed: [base]/[room]/[device]/[prefix]/[type] Where both [prefix] and [type] come from Tasmota's topic definition (tele stat cmnd and SENSOR STATE RESULT POWER LWT respectively). Try to use all-lowercase names (with an exception of the last bit, often in CAPS),

At least ackspace, common, outside and services are supported as base. Services are a bit special; choose a category for room (i.e. voip) and an identifiable name for device (i.e. freeswitch); see the extra examples list of topics further below.

In the future, we can extend this to support something like offsite and the names of participants.

Since ACKspace doesn't have rooms at different levels, the second hierarchy level consist of the room, like slackspace, hackspace and stackspace. device is the name of the device (similar as Tasmota's %topic%) like spacestate, temperature, hackswitch or fluorescent1.

At least the following topics will be implemented first:

  • ackspace/hackspace/spacestate/stat/RESULT (json power state)
    • ackspace/hackspace/spacestate/stat/POWER1 (string power state)
    • ackspace/hackspace/spacestate/tele/STATE (json state at interval)
  • ackspace/hackspace/temperature/tele/SENSOR (json telemetry data)
  • outside/courtyard/barbecue/tele/SENSOR (json telemetry data)

Extra examples:

  • services/voip/freeswitch/tele/STATE (FreeSWITCH specific data, like caller id)
  • services/network/bandwidth/tele/SENSOR (Current network bandwidth usage)
  • offsite/xopr/whatever/topic/he/pleases (details might be negotiated for ease of use)

server setup

/etc/mosquitto/conf.d/auth.conf
allow_anonymous false
password_file /etc/mosquitto/passwd
acl_file /etc/mosquitto/acls

listener 1883
protocol mqtt

listener 8883
protocol mqtt
cafile /etc/mosquitto/ca_certificates/ca.crt
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key

listener 443
protocol websockets
cafile /etc/mosquitto/ca_certificates/ca.crt
certfile /etc/mosquitto/certs/server.crt
keyfile /etc/mosquitto/certs/server.key


/etc/mosquitto/acls (modified)
user spacestate
topic write ackspace/+/spacestate/#

user temperature
topic write ackspace/+/temperature/#
topic write +/+/barbecue/#

user hackspace
topic write ackspace/hackspace/#
topic deny ackspace/+/spacestate/#

user slackspace
topic write ackspace/slackspace/#
topic deny ackspace/+/spacestate/#

user stackspace
topic write ackspace/stackspace/#
topic deny ackspace/+/spacestate/#

user myservice
topic write services/category/myservice/#

user ackspace
topic read #

Passwords are generated with: sudo mosquitto_passwd /etc/mosquitto/passwd [user]

todo

  • add MOAR sensors (upgrade the current temperature sensors)
  • MOAR sonoffs