My Plans (OpenHab, Mac Sierra (Siri in whole house), Everything Run Native DC) Let the Fun Begin

I had already tried “False” and blank and multiple other things none worked it still did on/off rapidly.

Here is ini

[Actuator1]
Class: rpiGPIOActuator.rpiGPIOActuator
; The chosen connection must support a register(path, handler) method the actuators can call
; to receive specific incoming messages. See mqttConn.py for an example
; restConnection is not supported.
Connection: MQTT
Pin: 2
Topic: kit/barlight
; When true set the pin to ON for half a second then turn off, otherwise switch the pin from one state to the o$
Toggle: False
Poll: 0

[Logging]
File: /var/log/mqttReporter.log
MaxSize: 67108864
NumFiles: 10
Syslog: NO

[Connection1]
Class: mqttConn.mqttConnection
Name: MQTT
User = *****
Password = ***********
Host = ***.***.***.****
Port = 1883
Keepalive = 60
; Topic to listen on, when any message is received, the current state of all
; are published to their respective topics.
Topic = sensors/getUpdate
; The MQTT broker will publish the following message on the following topic
; when the client disconnects (cleanly or crashes)
LWT-Topic = status/sensor-reporters
LWT-Msg = mqttReporter is dead
; If TLS is yes the connection will be encrypted, the Certificate is expected to be in
; ./certs/ca.crt"
TLS = NO

[Connection2]
Class: restConn.restConnection
Name: REST
URL = http://localhost:8080/rest/items/

It might be broken. I’ll take a look at the code this evening. I only ever use the toggle so this functionality really hasn’t been tested, at last by me. Though I think I accepted a PR on this functionality so I need to look into it.

I tinkered for a few hours last night and could not get it yet. :frowning:

I looked at the code. As written it should, if the message sent to the script is ON it will set the pin to LOW, otherwise it will send the pin to HIGH

The relevant lines are the last two in rpiGPIOActuator.py

If I pub from another session on the broker SS picks it up on the client and executes the script. I can modify the delay etc but setting it to False does not skip to the else: in rpiGPIOActuator.py from what I can see. everything thing else checks out.

Here is the output from mqttreporter when I send a publish

2016-11-26 21:58:31,560 INFO - Toggling pin 2 HIGH to LOW
2016-11-26 21:58:35,565 INFO - Toggling pin 2 LOW to HIGH

Here is my ini

[Actuator1]
Class: rpiGPIOActuator.rpiGPIOActuator
; The chosen connection must support a register(path, handler) method the actuators can call
; to receive specific incoming messages. See mqttConn.py for an example
; restConnection is not supported.
Connection: MQTT
Pin: 2
Topic: kit/barlight
; When true set the pin to ON for half a second then turn off, otherwise switch the pin from one state to the oth$
Toggle: False
Poll: 0

[Actuator2]
Class: rpiGPIOActuator.rpiGPIOActuator
; The chosen connection must support a register(path, handler) method the actuators can call
; to receive specific incoming messages. See mqttConn.py for an example
; restConnection is not supported.
Connection: MQTT
Pin: 3
Topic: kit/counterlight
; When true set the pin to ON for half a second then turn off, otherwise switch the pin from one state to the oth$
Toggle: False
Poll: 0

[Logging]
File: /var/log/mqttReporter.log
MaxSize: 67108864
NumFiles: 10
Syslog: NO

[Connection1]
Class: mqttConn.mqttConnection
Name: MQTT
User = user
Password = password
Host = 192.168.0.60
Port = 1883
Keepalive = 60
; Topic to listen on, when any message is received, the current state of all
; are published to their respective topics.
Topic = sensors/getUpdate
; The MQTT broker will publish the following message on the following topic
; when the client disconnects (cleanly or crashes)
LWT-Topic = status/sensor-reporters
LWT-Msg = mqttReporter is dead
; If TLS is yes the connection will be encrypted, the Certificate is expected to be in
; ./certs/ca.crt"
TLS = NO

The MQTT topic log:

Client mosqsub/2081-raspberryp received PUBLISH (d0, q0, r0, m0, 'kit/barlight', ... (2 bytes))
On

And the rpiGPIOActuator.py

import sys
import time
import RPi.GPIO as GPIO

class rpiGPIOActuator:
    """Represents an actuator connected to a GPIO pin"""

    def __init__(self, connection, logger, params):
        """Sets the output and changes its state when it receives a command"""

        self.logger = logger

        self.pin = int(params("Pin"))

        GPIO.setmode(GPIO.BCM) # uses BCM numbering, not Board numbering
        GPIO.setup(self.pin, GPIO.OUT)
        GPIO.output(self.pin, GPIO.HIGH)

        self.destination = params("Topic")
        self.connection = connection
        self.toggle = bool(params("Toggle"))

        self.logger.info('----------Configuring rpiGPIOActuator: pin {0} on destination {1} with toggle {2}'.form$
        self.connection.register(self.destination, self.on_message)

    def on_message(self, client, userdata, msg):
        """Process a message"""
        self.logger.info('Received command on {0}: {1} Toggle = {2} PIN = {3}'.format(self.destination, msg.paylo$
        if self.toggle == True:
            self.logger.info('Toggling pin %s HIGH to LOW' % (self.pin))
            GPIO.output(self.pin, GPIO.LOW)
            time.sleep(4)
            GPIO.output(self.pin, GPIO.HIGH)
            self.logger.info('Toggling pin %s LOW to HIGH' % (self.pin))
        else:
            out = GPIO.LOW if msg.payload == "ON" else GPIO.HIGH
            GPIO.output(self.pin, out)


For grins, what happens if you change

if self.toggle == True

to

if self.toggle == "True"

Yup That was it…

Thanks! I’ll add an issue and have it fixed in the mainline version soon.

UPDATED: After blowing away the play RPi I installed from scratch to get a clean install going. At first I did not install the client as it was only the broker so I thought it did not need the mosquito-client too but it appears it does. I did not use the paho install but did use the apt-get method below. It’s working now but not sure if I’m going to run into something down the road since I did not do the paho install.

apt-get install mosquito mosquito-clients (think the syntax is right)

instead of using

pip install paho-mosquitto (syntax might not be right again but you know).

Everything is working so far is there a reason to do the paho version of the client? I also thought the broker software did away with the server needing the client side installed and that was only for the clients. My assumption there seems to be incorrect.

I’m not 100% certain I follow exactly what you did. In general you don’t need the mosquitto-clients unless you want to run the mosquitto-sub and mosquitto-pub commands for testing. You shouldn’t have to install mosquitto-clients at all, though it has been long enough since I’ve done it that I could be wrong.

The openHAB MQTT binding comes with paho so you don’t need to install paho separately for openHAB.

sensorReporter does require the paho python library be installed manually as a separate step. That is one of the steps in the install.sh script which is more of a documentation of what you need to do to get sensorReporter to run and less a true installation script.

Sorry, reading it again I don’t even understand it. :slight_smile: That was a bad day so I took a few off to clear my head and start again.

Ok here is what I have now. MQTT broker is passing messages to the clients. I can turn on and off the relays passing ON/OFF via _pub to the respective topics.

I installed OH and can connect to it with a simple sitemap. I tail the OH events log and I see this when flipping the switch using the OH GUI but do not see it published to the topic.

2016-12-01 20:18:35 - mqttsw1 received command ON
2016-12-01 20:18:35 - mqttsw1 received command OFF

But the message isn’t making it to the MQTT topic. I’m afraid I may have missed a dependency along the way.

Here is my openhab.cfg MQTT Transport section

################################# MQTT Transport ######################################
#
# Define your MQTT broker connections here for use in the MQTT Binding or MQTT
# Persistence bundles. Replace <broker> with a id you choose.
#

# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
mqtt:<broker>.url=tcp://192.168.1.1:1883

# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a default one is generated.
#mqtt:<broker>.clientId=<clientId>

# Optional. User id to authenticate with the broker.
# mqtt:<broker>.user=<user>

# Optional. Password to authenticate with the broker.
#mqtt:<broker>.pwd=<password>

# Optional. Set the quality of service level for sending messages to this broker.
# Possible values are 0 (Deliver at most once),1 (Deliver at least once) or 2
# (Deliver exactly once). Defaults to 0.
#mqtt:<broker>.qos=<qos>

# Optional. True or false. Defines if the broker should retain the messages sent to
# it. Defaults to false.
#mqtt:<broker>.retain=<retain>

# Optional. True or false. Defines if messages are published asynchronously or
# synchronously. Defaults to true.
#mqtt:<broker>.async=<async>

# Optional. Defines the last will and testament that is sent when this client goes offline
# Format: topic:message:qos:retained <br/>
#mqtt:<broker>.lwt=<last will definition>

My items file

Switch mqttsw1 "Switch 1" (all) {mqtt=">[mosquitto:lights/kit/bar:command:ON:default],>[mosquitto:lights/kit/bar:command:OFF:default]"}
Switch mqttsw2 "Switch 2" (all) {mqtt=">[mosquitto:lights/kit/counter:command:OFF:default],>[mosquitto:lights/kit/counter:command:OFF:default]"}

My sitemap

sitemap demo label="Main Menu"
{

Frame label="MQTT" {
Switch item=mqttsw1 label="Switch 1"
Switch item=mqttsw2 label="Switch 2"
}
}

My sensorReporter.ini

Actuator1]
Class: rpiGPIOActuator.rpiGPIOActuator
; The chosen connection must support a register(path, handler) method the actua$
; to receive specific incoming messages. See mqttConn.py for an example
; restConnection is not supported.
Connection: MQTT
Pin: 2
Topic: lights/kit/bar
; When true set the pin to ON for half a second then turn off, otherwise switch$
Toggle: False
Poll: 0

[Actuator2]
Class: rpiGPIOActuator.rpiGPIOActuator
; The chosen connection must support a register(path, handler) method the actua$
; to receive specific incoming messages. See mqttConn.py for an example
; restConnection is not supported.
Connection: MQTT
Pin: 3
Topic: lights/kit/counter
; When true set the pin to ON for half a second then turn off, otherwise switch$
Toggle: False
Poll: 0

[Logging]
File: /var/log/mqttReporter.log
MaxSize: 67108864
NumFiles: 10
Syslog: NO

[Connection1]
Class: mqttConn.mqttConnection
Name: MQTT
User = user
Password = password
Host = 192.168.1.1
Port = 1883
Keepalive = 60
; Topic to listen on, when any message is received, the current state of all
; are published to their respective topics.
Topic = sensors/getUpdate
; The MQTT broker will publish the following message on the following topic
; when the client disconnects (cleanly or crashes)
LWT-Topic = status/sensor-reporters
LWT-Msg = mqttReporter is dead
; If TLS is yes the connection will be encrypted, the Certificate is expected t$
; ./certs/ca.crt"
TLS = NO

In your openhab.cfg you missed the

replace <broker> with a id you choose.

in the comments.

Change all instances of <broker> with mosquitto.

Given that the rest of the parameters are commented out I’m assuming that you have not configured Moqsuitto to require a username and password. However, you do have a user and password defined in sensorReporter. Since sensorReporter seems to be working, make sure mqtt:mosquitto:user and mqtt:mosquitto:pwd match those used in sensorReporter.

Ok… Still not turning on and I did replace all the “broker” with mosquitto, duh should have seen that. Not sure if I saw this before or not but in the sensorReporter window when I now turn on via OH I see this.

I also matched the openhab.cfg usr/pwd to match what I have in sensorReporter.ini. I did NOT however set it on the broker on pub.


    Loading sensorReporter.ini
Configuring logger: file = /var/log/mqttReporter.log size = 67108864 num = 10
/lights/kit/bar
/lights/kit/bar
/lights/kit/bar
/lights/kit/bar
/lights/kit/counter
/lights/kit/counter
/lights/kit/counter
/lights/kit/counter
***

Wait found it… Had to remove the // in the items line see below…

[mosquitto:lights/kit/bar:command:ON:default]
1 Like

Yup I’m starting to add all 16 relays now and they are all working.

However sensorReporter service did not start after following the install.sh instructions. hmm

Do you get any errors in the log? When you run systemctl status sensorReporter after a reboot do you see any errors?

I’ve intermittently had it not start when my Pi reboots too but I’m seeing all sorts of other weirdness on that Pi (probably a failing SD card) and attributed it to that. But if you are seeing the same problem then it might be a bigger issue.

Not sure if you have checked in to it. But there is a system called mysensors.org. Uses a gateway to talk to RF devices you build. The gateway just communicates with mqtt. I use openhab as well and love my mysensors.org add on. I have been building whatever various modules, and in the end openhab or whatever other controller you want just has to talk to MQTT.

1 Like

Sorry for delay…

pi@PJSWCTL:~ $ systemctl status sensorReporter
● sensorReporter.service - Reports status and sensor readings over MQTT and REST
   Loaded: loaded (/etc/systemd/system/sensorReporter.service; enabled)
   Active: active (exited) (Result: exit-code) since Fri 2016-12-09 21:05:18 CST; 4min 41s ago
  Process: 424 ExecStart=/opt/sensorReporter/sensorReporter.py /opt/sensorReporter/sensorReporter.ini (code=exited, status=1/FAILURE)
 Main PID: 424 (code=exited, status=1/FAILURE)
   CGroup: /system.slice/sensorReporter.service

#Envirophat Monitor

Ok, now that relays are working time to move onto something else. I have a couple of envirophats https://github.com/pimoroni/enviro-phat laying around and hooked one up to a Pi zero and got her working.

Now, I need to create a class for it and believe me I butchered my first attempt. LOL… Too embarrassed to even post it, yup it was that bad.

Please point me in the right direction, yeah from the beginning, and I’ll muddle my way around. :slight_smile:

I glanced at the site, I may be wrong but it seems more of a hosted solution, unless I’m missing something.

I do not want any communications externally, except over VPN on my phone only. :slight_smile: