-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_subscribe.py
48 lines (37 loc) · 1.15 KB
/
simple_subscribe.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
import machine,time
from machine import Pin,PWM
from umqtt.simple import MQTTClient
ledPin = 2
ledState = Pin(ledPin,Pin.OUT,value = 1)
state = 0
def sub_cb(ledTopic, msg):
global state
print((ledTopic, msg))
if msg == b"on":
print('Recieved:',msg)
ledState.value(0)
state = 1
elif msg == b"off":
print('Recieved:',msg)
ledState.value(1)
state = 0
elif msg == b"toggle":
# LED is inversed, so setting it to current state
# value will make it toggle
ledState.value(state)
state = 1 - state
else:
print('I didn\'t recieve shit!')
myEspClient = MQTTClient("15c64500-4965-11e8-b4ef-898f2f5b9050","mqtt.mydevices.com",1883,"2689f050-995a-11e7-b0e9-e9adcff3788e","c320eee89ad304367312c68a8a0b5d05ac0f385e")
myEspClient.set_callback(sub_cb)
myEspClient.connect()
#
# subscribing to a TOPIC at cayenne. Atleast 1 argument required
#
myEspClient.subscribe(b"v1/2689f050-995a-11e7-b0e9-e9adcff3788e/things/15c64500-4965-11e8-b4ef-898f2f5b9050/data/0")
time.sleep(500)
try:
myEspClient.wait_msg()
finally:
myEspClient.disconnect()
print('Connection Lost with the Broker')