We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e1f9e10 commit 64eab7cCopy full SHA for 64eab7c
umqtt.simple/example_pub_button.py
@@ -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
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