-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
113 lines (89 loc) · 2.24 KB
/
main.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import network
from machine import Pin
from mqtt import MQTTClient
import machine
import time
import ubinascii
ssid = "Hot Fuzz"
wifipw = "nanobot706"
mac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()
print ("mac is " + mac)
# 60:01:94:1f:1b:f4 Black
# 60:01:94:25:8f:a9 Red
pin = Pin(16, Pin.OUT)
button = Pin(2, Pin.IN)
pin.value(1)
time.sleep(1)
pin.value(0)
time.sleep(1)
pin.value(1)
time.sleep(1)
pin.value(0)
prevvalue = 1
lizfeed = "heybox-toliz"
donfeed = "heybox-todon"
pubfeed = ""
subfeed = ""
if(mac == "60:01:94:1f:1b:f4"):
#black button, don's
pubfeed = "heybox-toliz"
subfeed = "heybox-todon"
devid = "dons"
if(mac == "60:01:94:25:8f:a9"):
#red button, liz's
pubfeed = "heybox-todon"
subfeed = "heybox-toliz"
devid = "lizs"
def sub_cb(topic, msg):
print("got message")
print(topic)
print(msg)
if(msg == b"ON"):
pin.value(1)
if(msg == b"OFF"):
pin.value(0)
station = network.WLAN(network.STA_IF)
station.active(True)
station.connect(ssid,wifipw)
while not station.isconnected():
print("not connected")
print(wifipw)
print(ssid)
time.sleep(1)
machine.idle()
print("Connected to Wifi\n")
pin.value(1)
time.sleep(1)
pin.value(0)
time.sleep(1)
pin.value(1)
time.sleep(1)
pin.value(0)
client = MQTTClient("heybox_donASDFQWER134l"+devid, "io.adafruit.com",user="donundeen", password="b20aea6d2bdc41beba537705083e094b", port=1883)
client.set_callback(sub_cb)
client.connect()
client.subscribe(topic="donundeen/feeds/"+subfeed)
client.publish(topic="donundeen/feeds/" +pubfeed, msg="OFF")
pin.value(0)
i = 0
while True:
i = i + 1
if(i % 500 == 0):
client.ping()
i = 1
client.check_msg()
buttonval = button.value()
if(prevvalue != buttonval and buttonval == 0):
print("on")
client.publish(topic="donundeen/feeds/"+pubfeed, msg="ON")
if(prevvalue != buttonval and buttonval == 1):
client.publish(topic="donundeen/feeds/"+pubfeed, msg="OFF")
print("off")
prevvalue = buttonval
if not station.isconnected():
station.connect(ssid,wifipw)
while not station.isconnected():
print("not connected")
time.sleep(1)
machine.idle()
machine.idle()