Skip to content

Commit 64eab7c

Browse files
committed
umqtt.simple: Add example to publish a message on button press.
1 parent e1f9e10 commit 64eab7c

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

umqtt.simple/example_pub_button.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import time
2+
import ubinascii
3+
import machine
4+
from umqtt.simple import MQTTClient
5+
from machine import Pin
6+
7+
8+
# Many ESP8266 boards have active-low "flash" button on GPIO0.
9+
button = Pin(0, Pin.IN)
10+
11+
# Default MQTT server to connect to
12+
SERVER = "192.168.1.35"
13+
CLIENT_ID = ubinascii.hexlify(machine.unique_id())
14+
TOPIC = b"led"
15+
16+
17+
def main(server=SERVER):
18+
c = MQTTClient(CLIENT_ID, server)
19+
c.connect()
20+
print("Connected to %s, waiting for button presses" % server)
21+
while True:
22+
while True:
23+
if button.value() == 0:
24+
break
25+
time.sleep_ms(20)
26+
print("Button pressed")
27+
c.publish(TOPIC, b"toggle")
28+
time.sleep_ms(200)
29+
30+
c.disconnect()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy