-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy paththingspeak.py
More file actions
35 lines (26 loc) · 786 Bytes
/
thingspeak.py
File metadata and controls
35 lines (26 loc) · 786 Bytes
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
import usocket as _socket
import ussl as ssl
import dht
import machine
import time
API_KEY = ""
DHT_PIN = 2
HOST = "api.thingspeak.com"
d = dht.DHT11(machine.Pin(DHT_PIN))
def main(use_stream=True):
while True:
d.measure()
data = b"api_key="+ API_KEY + "&field1=" + str(d.temperature()) + "&field2=" + str(d.humidity())
s = _socket.socket()
ai = _socket.getaddrinfo(HOST, 443)
addr = ai[0][-1]
s.connect(addr)
s = ssl.wrap_socket(s)
s.write("POST /update HTTP/1.0\r\n")
s.write("Host: " + HOST + "\r\n")
s.write("Content-Length: " + str(len(data)) + "\r\n\r\n")
s.write(data)
print(s.read(128))
s.close()
time.sleep(60)
main()