Skip to content

Commit 88152f4

Browse files
Add files via upload
1 parent 5577bf9 commit 88152f4

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

SUBSCRIBER_LED.zip

2.8 KB
Binary file not shown.

diagram.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"version": 1,
3+
"author": "Anonymous maker",
4+
"editor": "wokwi",
5+
"parts": [
6+
{ "type": "wokwi-esp32-devkit-v1", "id": "esp", "top": 0, "left": -77.99, "attrs": {} },
7+
{
8+
"type": "wokwi-led",
9+
"id": "led1",
10+
"top": -74.81,
11+
"left": 103.67,
12+
"attrs": { "color": "purple" }
13+
},
14+
{
15+
"type": "wokwi-resistor",
16+
"id": "r1",
17+
"top": 8.52,
18+
"left": 100.33,
19+
"rotate": 270,
20+
"attrs": { "value": "1000" }
21+
}
22+
],
23+
"connections": [
24+
[ "esp:TX0", "$serialMonitor:RX", "", [] ],
25+
[ "esp:RX0", "$serialMonitor:TX", "", [] ],
26+
[ "led1:A", "r1:2", "red", [ "v0" ] ],
27+
[ "r1:1", "esp:D4", "red", [ "h0", "v67.07" ] ],
28+
[ "led1:C", "esp:GND.1", "black", [ "v0" ] ]
29+
]
30+
}

main.py

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

wokwi-project.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Downloaded from https://wokwi.com/projects/329467649115292243
2+
3+
Simulate this project on https://wokwi.com

0 commit comments

Comments
 (0)