-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import gc | ||
import webrepl | ||
|
||
def do_connect(): | ||
import network | ||
|
||
wlan = network.WLAN(network.STA_IF) | ||
wlan.active(True) | ||
|
||
if not wlan.isconnected(): | ||
print('connecting to network...') | ||
wlan.connect('mimimi', 'phplandia') | ||
|
||
while not wlan.isconnected(): | ||
pass | ||
|
||
print('network config:', wlan.ifconfig()) | ||
|
||
|
||
def do_ntp(): | ||
from ntptime import settime | ||
|
||
settime() | ||
|
||
webrepl.start() | ||
gc.collect() | ||
|
||
do_connect() | ||
do_ntp() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import time | ||
import dht | ||
import machine | ||
import urequests as requests | ||
|
||
DHT_PIN = 2 | ||
API_THINGSPEAK_HOST = 'https://api.thingspeak.com/update' | ||
THINGSPEAK_WRITE_KEY = '' # put your key here | ||
MESUREMENT_INTERVAL = 300 | ||
DELAY = 30 | ||
|
||
|
||
def do_temp(): | ||
try: | ||
d = dht.DHT11(machine.Pin(DHT_PIN)) | ||
d.measure() | ||
|
||
t = d.temperature() | ||
h = d.humidity() | ||
except OSError as err: | ||
t = 0 | ||
h = 0 | ||
|
||
print('temperature = %.2f' % t) | ||
print('humidity = %.2f' % h) | ||
|
||
global THINGSPEAK_WRITE_KEY | ||
|
||
if not THINGSPEAK_WRITE_KEY: | ||
print('not ThingSpeak key specified, skip sending data') | ||
return | ||
|
||
print('send data to ThingSpeak') | ||
|
||
data = '{"field1":"%.2f", "field2": "%.2f"}' % (t, h) | ||
|
||
headers = {'X-THINGSPEAKAPIKEY': THINGSPEAK_WRITE_KEY, | ||
'Content-type': 'application/json'} | ||
|
||
r = requests.post(API_THINGSPEAK_HOST, data=data, headers=headers) | ||
results = r.json() | ||
|
||
print(results) | ||
|
||
last_mesurement_time = 0 | ||
|
||
while True: | ||
current_time = time.time() | ||
|
||
if current_time - last_mesurement_time > MESUREMENT_INTERVAL: | ||
do_temp() | ||
last_mesurement_time = current_time | ||
|
||
time.sleep(DELAY) |