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