#ESP32_BME280_MicroPython

##################################################################

# boot.py (WiFiへ接続)

def connect():
    import network
 
    ssid = "--ssid--"
    password =  "++password++"
 
    station = network.WLAN(network.STA_IF)
 
    if station.isconnected() == True:
        print("接続済みです")
        return
 
    station.active(True)
    station.connect(ssid, password)
 
    while station.isconnected() == False:
        pass
 
    print("接続しました")
    print(station.ifconfig())
connect()

##################################################################

# main.py ambientへ送信

from machine import Pin,I2C
import utime
import ssd1306
import bme280
import ambient


led=Pin(2,Pin.OUT)        #create LED object from pin2,Set Pin2 to output
i2c = I2C(scl=Pin(22), sda=Pin(21), freq=100000)
lcd=ssd1306.SSD1306_I2C(128,64,i2c)
bme = bme280.BME280(i2c=i2c)
am = ambient.Ambient(100, '000000000000')  #チャンネルとライトキー
for i in range(5):
  led.value(1) 
  utime.sleep(0.3)  
  led.value(0)   
  utime.sleep(0.3)
  
while True:
    led.value(1)            #Set led turn on
    print(bme.values)
    data = bme.read_compensated_data()
    r = am.send({'d1': data[0] / 100.0, 'd2': data[2] / 1024.0, 'd3': data[1] / 25600.0})
    print(r.status_code)
    r.close()
    lcd.fill(0) #clear
    lcd.text('', 0, 0)
    d0=str(data[0] / 100.0)
    lcd.text(d0[:5]+' C', 0, 16)
    d2=str(data[2] / 1024.0)
    lcd.text(d2[:5]+' %', 0, 32)
    d1=str(data[1]/25600.0)
    lcd.text(d1[:7]+' hPa', 0, 48)
    lcd.show() 
    led.value(0)            #Set led turn off
    utime.sleep(60)

##################################################################