]> vault307.fbx.one Git - Blynk.git/blob - blynkLibv1/mommybtnUPDATE.py
Blynk library and devices
[Blynk.git] / blynkLibv1 / mommybtnUPDATE.py
1 import machine,network,time, BlynkLib, sys, gc, ujson
2 from neopixel import NeoPixel
3 from machine import Pin
4
5 wlan=network.WLAN(network.STA_IF)
6 wlan.active(True)
7
8 def readSecrets():
9 with open('secrets.json') as fp:
10 secrets=ujson.loads(fp.read())
11 return secrets
12
13 def wifiConnect():
14 secrets=readSecrets()
15 if wlan.isconnected()==False:
16 wlan.connect(secrets['home_wifi']['ssid'],secrets['home_wifi']['pass'])
17 elif wlan.isconnected()==True:
18 pass
19
20 wifiConnect()
21
22 # connect to Blynk
23 secrets=readSecrets()
24 BLYNK_AUTH=secrets['home_wifi']['blynkAuth']
25 blynk=BlynkLib.Blynk(BLYNK_AUTH)
26 vpin=1
27
28 # button connected to pin 28, ground, when btn pressed value=0
29 btn=machine.Pin(39,machine.Pin.IN,machine.Pin.PULL_UP)
30
31 # button connected to pin 18, ground, when btn pressed blynk.disconnect
32 #btnD=machine.Pin(18,machine.Pin.IN,machine.Pin.PULL_UP)
33
34 # LED GP25
35 LED_GPIO = const(27)
36 matrix_size_x = const(5)
37 matrix_size_y = const(5)
38 is_atom_matrix = True
39
40 np = NeoPixel(Pin(LED_GPIO), matrix_size_x * matrix_size_y)
41
42 # button handler
43 def pressed(btn):
44 global pressed
45 if not pressed:
46 pressed=True
47
48 # set up datastream to Blynk (btn.value=pin v1)
49 @blynk.on('read V1')
50 def read_virtual_pin_handler(vpin):
51 blynk.virtual_write(vpin,int(btn.value()))
52
53 # LED handler (turn on when connected)
54 @blynk.on("connected")
55 def blynk_connected():
56 np.fill((0,25,0))
57 np.write()
58
59 # LED handler (turn off when disconnected)
60 @blynk.on("disconnected")
61 def blynk_disconnected():
62 np.fill((25,0,0))
63 np.write()
64
65 # garbage collection
66 gc.threshold(26144)
67 gc.enable()
68
69 # main loop
70 while True:
71 try:
72 read_virtual_pin_handler(vpin)
73 #print(int(btn.value()))
74 if int(btn.value())==0:
75 blynk.log_event("Mommy_alert")
76 time.sleep(.25)
77
78 # if int(btnD.value())==0:
79 # wlan.disconnect()
80 # blynk.disconnect()
81
82 blynk.run()
83 except KeyboardInterrupt:
84 blynk_disconnected()
85 blynk.disconnect()
86 sys.exit()