|
| 1 | +#debug symbol |
| 2 | +debug = 0 |
| 3 | + |
| 4 | +#importing the libraries |
1 | 5 | from mqtt import MQTTClient |
2 | | -import machine |
| 6 | +import machine |
3 | 7 | from network import WLAN, STA_IF |
4 | 8 | import time |
5 | 9 |
|
| 10 | +#configure your data here. |
6 | 11 | ssid="<your wifi ssid>" |
7 | 12 | password="<your wifi password>" |
8 | 13 | username="<your username>" |
9 | 14 | secrete_key="<active key>" |
10 | 15 | read_obj_name="<led block title>" |
11 | 16 | write_obj_name="<text block title>" |
12 | | -wlan=WLAN(STA_IF) |
13 | | -wlan.active(True) |
14 | 17 |
|
15 | | -def sub_cb(topic, msg): |
16 | | - print("Msg from server:%s"%msg) |
17 | | - if str(msg) =="b'OFF'": |
18 | | - led.value(1) |
19 | | - print("off") |
20 | | - file=open('data.txt','w') |
21 | | - file.write(str(msg)) |
22 | | - file.close() |
23 | | - elif str(msg) =="b'ON'": |
24 | | - led.value(0) |
25 | | - print("on") |
26 | | - file=open('data.txt','w') |
27 | | - file.write(str(msg)) |
28 | | - file.close() |
| 18 | +#function to establish the connection with WiFi router. |
| 19 | +def make_connection(): |
| 20 | + if not wlan.isconnected(): |
| 21 | + wlan.scan() |
| 22 | + wlan.connect(ssid,password) |
| 23 | + while not wlan.isconnected(): |
| 24 | + try: |
| 25 | + print("ssid {s} is not found".format(s=ssid)) |
| 26 | + except KeyboardInterrupt: |
| 27 | + break |
| 28 | + time.sleep(1) |
| 29 | + if debug: |
| 30 | + print('network config:', wlan.ifconfig()) |
| 31 | + print("Successfully connected to {}".format(ssid)) |
| 32 | + return wlan.isconnected() |
29 | 33 |
|
30 | | -while(not wlan.active()): |
31 | | - wlan.scan() |
32 | | - if not wlan.isconnect(): |
33 | | - wlan.connect(ssid,password) |
34 | | - wlan.ifconfig() |
| 34 | +#function to read the data from server. |
| 35 | +def sub_cb(topic, msg): |
| 36 | + msg=msg.decode("utf-8") |
| 37 | + if debug: |
| 38 | + print("Msg from server: {}".format(msg)) |
| 39 | + if msg is "OFF": |
| 40 | + led.value(1) |
| 41 | + print("off") |
| 42 | + elif msg =="ON": |
| 43 | + led.value(0) |
| 44 | + print("on") |
| 45 | + file=open('data.txt','w') |
| 46 | + file.write(str(msg)) |
| 47 | + file.close() |
35 | 48 |
|
36 | | -led=machine.Pin(2, machine.Pin.OUT) |
| 49 | +led=machine.Pin(16, machine.Pin.OUT) |
37 | 50 | button=machine.Pin(12, machine.Pin.IN) |
38 | 51 |
|
39 | 52 | file=open('data.txt') |
40 | 53 | data=file.read() |
41 | | -print(data) |
42 | | -if str(data)=="b'ON'": |
43 | | - led.value(0) |
44 | | - print("on") |
45 | | -elif str(data)=="b'OFF'": |
46 | | - led.value(1) |
47 | | - print("off") |
48 | 54 | file.close() |
| 55 | +if data is "ON": |
| 56 | + led.value(0) |
| 57 | + print("on") |
| 58 | +elif data is "OFF": |
| 59 | + led.value(1) |
| 60 | + print("off") |
49 | 61 |
|
50 | | - |
| 62 | +#turn on WiFi |
| 63 | +wlan = WLAN(STA_IF) |
| 64 | +wlan.active(True) |
| 65 | +make_connection() |
51 | 66 |
|
52 | | -while(wlan.active()): |
53 | | - client = MQTTClient("my device","io.adafruit.com",0,username,secrete_key) |
54 | | - client.set_callback(sub_cb) |
55 | | - client.connect() |
56 | | - client.subscribe(topic=username+"/feeds/"+read_obj_name) |
57 | | - i="No Obstracle Detected" |
58 | | - print("Sending %s"%str(i)) |
59 | | - client.publish(topic=username+"/feeds/"+write_obj_name, msg=str(i)) |
60 | | - while True: |
61 | | - try: |
62 | | - val=button.value() |
63 | | - if val == 0: |
64 | | - i="Obstracle Detected" |
65 | | - print("Sending %s"%str(i)) |
66 | | - client.publish(topic=username+"/feeds/"+write_obj_name, msg=str(i)) |
67 | | - time.sleep(1) |
68 | | - print(client.check_msg()) |
69 | | - print(str(val)) |
70 | | - except KeyboardInterrupt: |
71 | | - break |
| 67 | +#infinite loop |
| 68 | +while True: |
| 69 | + if wlan.isconnected(): |
| 70 | + client = MQTTClient("my device","io.adafruit.com",0,username,secrete_key) |
| 71 | + client.set_callback(sub_cb) |
| 72 | + client.connect() |
| 73 | + client.subscribe(topic=username+"/feeds/"+read_obj_name) |
| 74 | + i="No Obstracle Detected" |
| 75 | + print("Sending %s"%str(i)) |
| 76 | + client.publish(topic=username+"/feeds/"+write_obj_name, msg=str(i)) |
| 77 | + while wlan.isconnected(): |
| 78 | + try: |
| 79 | + val=button.value() |
| 80 | + if val == 0: |
| 81 | + i="Obstracle Detected" |
| 82 | + print("Sending %s"%str(i)) |
| 83 | + client.publish(topic=username+"/feeds/"+write_obj_name, msg=str(i)) |
| 84 | + time.sleep(0.5) |
| 85 | + resp=client.check_msg() |
| 86 | + if debug: |
| 87 | + print(resp) |
| 88 | + print(str(val)) |
| 89 | + except KeyboardInterrupt: |
| 90 | + break |
| 91 | + while not make_connection(): |
| 92 | + pass |
| 93 | + print("Leaving main") |
| 94 | + client.disconnect() |
| 95 | + break |
| 96 | + |
72 | 97 |
|
73 | | - print("Leaving main") |
74 | | - client.disconnect() |
75 | | - break |
76 | 98 |
|
0 commit comments