Hi,
I’ve got openhab2 running on a raspberry pi 3.
To create a pulsed signal on gpio I’m using mqtt to interact with the pins.
The first step is working, I can transmit a switch command via mqtt to change a gpio pin.
I can also transmit a number to my python script.
My actual problem is, that I don’t know how to convert the number 'global_pwm ’ using it anyway in the script.
At the moment my sleep command isn’t changing.
My code:
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
import time
from time import sleep
MQTT_HOST = "192.168.4.1"
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 45
MQTT_TOPIC = "raspi3/pin18PWM"
MQTT_TOPIC1 = "raspi3/pin6"
MQTT_TOPIC2 = "raspi3/pin23"
LED1 = 6
LED2 = 23
servoPIN = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)
GPIO.setup(LED1, GPIO.OUT)
GPIO.setup(LED2, GPIO.OUT)
global_pwm = 2
p = GPIO.PWM(servoPIN, 50) # GPIO 18 als PWM mit 50Hz, 20ms
p.start(50) #
try:
def on_connect(self,mosq, obj, rc):
mqttc.subscribe(MQTT_TOPIC, 0)
mqttc.subscribe(MQTT_TOPIC1, 0)
print("Connect on "+MQTT_HOST)
def on_message(mosq, obj, msg):
if (msg.topic == 'raspi3/pin18PWM'):
global_pwm = float(msg.payload)
print "Topic: " + str(msg.topic)
print "message: " + str(msg.payload)
def on_subscribe(mosq, obj, mid, granted_qos):
print("Subscribed to Topic: " +
MQTT_TOPIC + " with QoS: " + str(granted_qos))
# Initiate MQTT Client
mqttc = mqtt.Client()
# Assign event callbacks
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
# Continue monitoring the incoming messages for subscribed topic
#mqttc.loop_forever():
while True:
mqttc.loop(0.01)
GPIO.output(LED1,True)
sleep(global_pwm)
mqttc.loop(0.01)
GPIO.output(LED1,False)
sleep(global_pwm)
except KeyboardInterrupt:
# here you put any code you want to run before the program
# exits when you press CTRL+C
p.ChangeDutyCycle(2.5)
p.stop()
GPIO.cleanup()
Can anybody help me with this?
Thanks a lot
Chris