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