Skip to content

Commit 393a559

Browse files
WiFi connectivity is improved.
1 parent 7586824 commit 393a559

File tree

1 file changed

+76
-54
lines changed

1 file changed

+76
-54
lines changed

main.py

Lines changed: 76 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,98 @@
1+
#debug symbol
2+
debug = 0
3+
4+
#importing the libraries
15
from mqtt import MQTTClient
2-
import machine
6+
import machine
37
from network import WLAN, STA_IF
48
import time
59

10+
#configure your data here.
611
ssid="<your wifi ssid>"
712
password="<your wifi password>"
813
username="<your username>"
914
secrete_key="<active key>"
1015
read_obj_name="<led block title>"
1116
write_obj_name="<text block title>"
12-
wlan=WLAN(STA_IF)
13-
wlan.active(True)
1417

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()
2933

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()
3548

36-
led=machine.Pin(2, machine.Pin.OUT)
49+
led=machine.Pin(16, machine.Pin.OUT)
3750
button=machine.Pin(12, machine.Pin.IN)
3851

3952
file=open('data.txt')
4053
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")
4854
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")
4961

50-
62+
#turn on WiFi
63+
wlan = WLAN(STA_IF)
64+
wlan.active(True)
65+
make_connection()
5166

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+
7297

73-
print("Leaving main")
74-
client.disconnect()
75-
break
7698

0 commit comments

Comments
 (0)