primo test funzionante
This commit is contained in:
53
pyButtons.py
53
pyButtons.py
@@ -1,36 +1,53 @@
|
|||||||
from gpiozero import Button
|
from gpiozero import Button
|
||||||
from paho.mqtt import client as mqtt
|
from paho.mqtt import client as mqtt
|
||||||
from signal import pause
|
import time
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
#numero pin GPIO secondo lo schema BCM
|
#numero pin GPIO secondo lo schema BCM
|
||||||
SU =1
|
SU = "GPIO5"
|
||||||
GIU =2
|
GIU = "GPIO6"
|
||||||
SX =3
|
SX = "GPIO13"
|
||||||
DX =4
|
DX = "GPIO19"
|
||||||
OK =5
|
OK = "GPIO12"
|
||||||
CAN =6
|
CAN = "GPIO16"
|
||||||
|
|
||||||
|
# code mqtt
|
||||||
|
MQTT_SEND = "buttons/send"
|
||||||
|
MQTT_RECEIVE = "buttons/receive"
|
||||||
|
|
||||||
#variabili globali
|
#variabili globali
|
||||||
isRunning = True
|
isRunning = True
|
||||||
|
client = None
|
||||||
|
|
||||||
|
def on_message(client, userdata, msg):
|
||||||
|
print(f"{msg.topic} -> {str(msg.payload)}")
|
||||||
|
if "STOP" in str(msg.payload):
|
||||||
|
isRunning = False
|
||||||
|
pass
|
||||||
|
|
||||||
def send_SU():
|
def send_SU():
|
||||||
|
client.publish(MQTT_SEND,payload="SU",qos=1)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def send_GIU():
|
def send_GIU():
|
||||||
|
client.publish(MQTT_SEND,payload="GIU",qos=1)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def send_SX():
|
def send_SX():
|
||||||
|
client.publish(MQTT_SEND,payload="SX",qos=1)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def send_DX():
|
def send_DX():
|
||||||
|
client.publish(MQTT_SEND,payload="DX",qos=1)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def send_OK():
|
def send_OK():
|
||||||
|
client.publish(MQTT_SEND,payload="OK",qos=1)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def send_CAN():
|
def send_CAN():
|
||||||
pass
|
client.publish(MQTT_SEND,payload="CAN",qos=1)
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@@ -43,15 +60,27 @@ def main():
|
|||||||
|
|
||||||
while isRunning:
|
while isRunning:
|
||||||
try:
|
try:
|
||||||
client = mqtt.Client()
|
if client is None:
|
||||||
client.connect('localhost',1883)
|
client = mqtt.Client()
|
||||||
|
client.connect('localhost',1883)
|
||||||
|
client.subscribe(MQTT_RECEIVE)
|
||||||
|
client.on_message = on_message
|
||||||
|
btn_su.when_pressed = send_SU
|
||||||
|
btn_giu.when_pressed = send_GIU
|
||||||
|
btn_sx.when_pressed = send_SX
|
||||||
|
btn_dx.when_pressed = send_DX
|
||||||
|
btn_ok.when_pressed = send_OK
|
||||||
|
btn_can.when_pressed = send_CAN
|
||||||
|
while isRunning:
|
||||||
|
client.loop(timeout=1)
|
||||||
|
time.sleep(1)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Exception: {e}")
|
print(f"Exception: {e}")
|
||||||
|
client.disconnect()
|
||||||
|
client=None
|
||||||
pass
|
pass
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
if __name__=="__main__":
|
if __name__=="__main__":
|
||||||
sys.exit(main())
|
sys.exit(main())
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user