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