from machine import Pin,SoftI2C
from bme680 import *
import ntptime
import utime
import ujson
import urequests
UTC_OFFSET=9
p21 = Pin(21, Pin.IN, Pin.PULL_UP)
p23 = Pin(23, Pin.IN, Pin.PULL_UP)
led = Pin(4, Pin.OUT)
i2c = SoftI2C(scl=Pin(21), sda=Pin(23))
def do_connect():
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect('essid', 'password')
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
led.value(1)
def get_jst():
sleep_ms(1000)
ntptime.settime()
tm=utime.localtime(utime.mktime(utime.localtime())+UTC_OFFSET*3600)
jst = str(tm[0])+'/'+str(tm[1])+'/'+str(tm[2])+' '+str(tm[3])+':'+str(tm[4])+':'+str(tm[5])
return(jst)
do_connect()
print(get_jst())
while True:
try:
bme = BME680_I2C(i2c=i2c)
temp = str(round(bme.temperature, 2))
hum = str(round(bme.humidity, 2))
pres = str(round(bme.pressure, 2))
gas = str(round(bme.gas/1000, 2))
dict = {
"temp_value":temp,
"hum_value":hum,
"pres_value":pres,
"gas_value":gas
}
response = urequests.post(
'https://***url***/datarcv.php',
data=ujson.dumps(dict).encode("utf-8"),
headers={'Content-Type': 'application/json'}
)
print (response.text)
except OSError as e:
print('Failed to read sensor.')
sleep(10)