-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (56 loc) · 1.48 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
"""
recieves measurements from arduino and sends it to pi,
also controls relay for lamp
"""
import socket
from time import sleep
from machine import Pin, UART
import uos
from ntptime import settime
import utime
PI_IP = '192.168.178.3'
NUM_PLANTS = 3
light_pin = Pin(5, Pin.OUT)
light_pin.off()
uos.dupterm(None, 1)
uart = UART(0, 9600)
uart.init(9600, bits=8, parity=None, stop=1, timeout=1800000)
count = 0
while True:
try:
if not count:
print('set time')
settime()
except:
print('no time available')
print('localtime ', utime.localtime()[3])
if (utime.localtime()[3] >= 18 or utime.localtime()[3] <= 1):
light_pin.off()
print('light on')
else:
light_pin.on()
print('light off')
if not count % 30:
for i in range(NUM_PLANTS):
try:
val = uart.readline()
val = val.decode()
print('val ', val)
except:
print('no data')
val = -1
if val != -1:
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
print('trying')
soc.connect((PI_IP, 1234))
print('connected')
soc.send(val)
print('send')
except:
print('connection failed')
soc.close()
count += 1
print(count)
count %= 1440
sleep(1)