-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.py
83 lines (63 loc) · 1.74 KB
/
code.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
import socket
import time
import machine
import onewire, ds18x20
BASE_URL = 'http://159.203.128.53/input/'
PUBLIC_KEY = 'bLzgdDwgq4CgqLZmwdrYHGK68908'
PRIVATE_KEY = 'GmPOGBYOW6spAV328wynUBEgzeGz'
def do_connect():
import network
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect('InmanSquareOasis', 'portauprince')
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
def http_get(url):
_, _, host, path = url.split('/', 3)
addr = socket.getaddrinfo(host, 80)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
while True:
data = s.recv(100)
if data:
print(str(data, 'utf8'), end='')
else:
break
s.close()
def get_temps():
# the device is on GPIO12
dat = machine.Pin(12)
# create the onewire object
ds = ds18x20.DS18X20(onewire.OneWire(dat))
# scan for devices on the bus
roms = ds.scan()
temps=[]
for rom in roms:
#print(rom)
ds.convert_temp()
time.sleep(1)
temp=ds.read_temp(rom)
temps.append(temp)
print(temps)
return temps
def post_values():
do_connect()
temps = get_temps()
url=BASE_URL+PUBLIC_KEY+'?private_key='+PRIVATE_KEY+'&temp1='+str(temps[0])+'&temp2='+str(temps[1])+'&temp3='+str(temps[2])
http_get(url)
def blink():
led = machine.Pin(0, machine.Pin.OUT)
led.low()
time.sleep(1)
led.high()
time.sleep(1)
while True:
blink()
blink()
post_values()
blink()
time.sleep(20)