-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (49 loc) · 1.6 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import network
import time
from umqtt.simple import MQTTClient
from machine import Pin
##################################################################################
# MQTT CONFIG
MQTT_BROKER = "broker.hivemq.com"
MQTT_USER = ""
MQTT_PASSWORD = ""
MQTT_CLIENT_ID = ""
MQTT_TOPIC = "sala306/led"
MQTT_PORT = 1883
################################################################################
# CONNECT WI-FI WOKWI-GUEST
def wifi_connect():
print("Connecting", end="")
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('Wokwi-GUEST', '')
while not sta_if.isconnected():
print(".", end="")
time.sleep(0.3)
print(" Wi-Fi connected!")
################################################################################
# LED MESSAGE CONFIG
def message_arrived(topic, msg):
if (msg == b'1'):
led.value(1)
if (msg == b'0'):
led.value(0)
################################################################################
# SUBSCRIBER
def subscribe():
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, port=MQTT_PORT, user=MQTT_USER, password=MQTT_PASSWORD, keepalive=0)
# Subscribed messages will be delivered to this callback
client.set_callback(message_arrived)
client.connect()
client.subscribe(MQTT_TOPIC)
print("Conected on %s, topic subscribed: %s " % (MQTT_BROKER, MQTT_TOPIC))
return client
################################################################################
# MAIN PROGRAM
led = Pin(4, Pin.OUT)
led.value(0)
wifi_connect()
client = subscribe()
while True:
client.wait_msg()
#time.sleep(0.5) # BLINK LED