Thank you so much Rich, I have installed mosquitto and set the username and password and generated the 3 key/ca files. That worked well and Mosquitto server starts up great.
I have my openhab item setup to match and that looks great too:
Switch node2_sw1 “sw2” (node2,all) {mqtt=“>[mosquitto:devices/phaze3131/get:command:ON:1],>[mosquitto:devices/phaze3131/get:command:OFF:0]”}
I change my python code to match all the correct username/passwords/clientid.
Changed this:
client.connect(“10.10.10.103:1883”)
So that points to the Mosquitto server Rpi (which also has openhab running)
But when I run my code I am now getting these errors, I’m hoping you could maybe shed some light as to why they are coming up and if the python code is even being executed. I’m aware the gpio errors don’t mean much but it’s the errors at the end that concern me.
pi@raspberrypi ~ $ sudo python mqttlight.py
mqttlight.py:23: RuntimeWarning: No channels have been set up yet - nothing to clean up! Try cleaning up at the end of your program instead!
GPIO.cleanup() # clean up resources
mqttlight.py:24: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(ledPin, GPIO.OUT) # led pin setup
Traceback (most recent call last):
File “mqttlight.py”, line 78, in
client.connect(“10.10.10.103:1883”) # MQTT server address
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py”, line 612, in connect
return self.reconnect()
File “/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py”, line 734, in reconnect
sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
File “/usr/lib/python2.7/socket.py”, line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
I’m going to post my python code just in case:
import paho.mqtt.client as paho
import RPi.GPIO as GPIO
import json, time
& device credentials
device_id = ‘phaze3131>’ & * set your device id (will be the MQTT client username)
device_secret = ‘********’ & * set your device secret (will be the MQTT client password)
random_client_id = ‘openhab’ & * set a random client_id (max 23 char)
& -------------- &
& Board settings &
& -------------- &
buttonPin = 7
ledPin = 12
GPIO.setmode(GPIO.BOARD) & use P1 header pin numbering convention
GPIO.cleanup() & clean up resources
GPIO.setup(ledPin, GPIO.OUT) & led pin setup
GPIO.setup(buttonPin, GPIO.IN) & button pin setup
& --------------- &
& Callback events &
& --------------- &
& connection event
def on_connect(client, data, flags, rc):
print('Connected, rc: ’ + str(rc))
& subscription event
def on_subscribe(client, userdata, mid, gqos):
print('Subscribed: ’ + str(mid))
& received message event
def on_message(client, obj, msg):
& get the JSON message
json_data = msg.payload
& check the status property value
print(json_data)
value = json.loads(json_data)[‘properties’][0][‘value’]
if value == ‘on’:
led_status = GPIO.HIGH
GPIO.output(ledPin, GPIO.HIGH)
else:
led_status = GPIO.LOW
GPIO.output(ledPin, GPIO.LOW)
& confirm changes to Leylan
client.publish(out_topic, json_data)
& ------------- &
& MQTT settings &
& ------------- &
& create the MQTT client
client = paho.Client(client_id=random_client_id, protocol=paho.MQTTv31) & * set a random string (max 23 chars)
& assign event callbacks
client.on_message = on_message
client.on_connect = on_connect
client.on_subscribe = on_subscribe
& device topics
in_topic = ‘devices/’ + device_id + ‘/get’ & receiving messages
out_topic = ‘devices/’ + device_id + ‘/set’ & publishing messages
& client connection
client.username_pw_set(device_id, device_secret) & MQTT server credentials
client.connect(“10.10.10.103:1883”) & MQTT server address
client.subscribe(in_topic, 0) & MQTT subscribtion (with QoS level 0)
& ------------ &
& Button logic &
& ------------ &
prev_status = GPIO.LOW
led_status = GPIO.LOW
updated_at = 0 & the last time the output pin was toggled
debounce = 0.2 & the debounce time, increase if the output flickers
& Continue the network loop, exit when an error occurs
rc = 0
while rc == 0:
rc = client.loop()
button = GPIO.input(buttonPin)
if button != prev_status and time.time() - updated_at > debounce:
prev_status = button
updated_at = time.time()
if button:
led_status = not led_status
button_payload = 'off'
if led_status == GPIO.HIGH:
button_payload = 'on'
& effectively update the light status
GPIO.output(ledPin, led_status)
payload = { 'properties': [{ 'id': '518be5a700045e1521000001', 'value': button_payload }] }
client.publish(out_topic, json.dumps(payload))
print('rc: ’ + str(rc))
Thanks so much for all your help Rich, this is really starting to make sense to me.
Travis