Skip to content

Instantly share code, notes, and snippets.

@halka
Created August 23, 2021 18:56
Show Gist options
  • Save halka/0dc6138bf1c2311836c8a52916d4b660 to your computer and use it in GitHub Desktop.
Save halka/0dc6138bf1c2311836c8a52916d4b660 to your computer and use it in GitHub Desktop.
from m5stack import *
from m5ui import *
from uiflow import *
import wifiCfg
import urequests
import time
import unit
import ujson
setScreenColor(0x000000)
env2_1 = unit.get(unit.ENV2, unit.PORTA)
tvoc_1 = unit.get(unit.TVOC, unit.PORTA)
status = None
url = 'https://gw.machinist.iij.jp/endpoint'
headers={'Content-Type':'application/json',
'Authorization':'Bearer <your token>'
}
SSID = '<SSID>'
password = '<password of SSID>'
wifiCfg.doConnect(SSID, password)
label0 = M5TextBox(0, 0, 'Init', lcd.FONT_Default, 0xFFFFFF, rotate=0)
data = {'agent':'MySpace',
'metrics':[
{'name':'temperature','namespace':'Environment Sensor','data_point':{'value':env2_1.temperature}},
{'name':'pressure','namespace':'Environment Sensor','data_point':{'value': float(('%.2f'%float(env2_1.pressure / 33.86)))}},
{'name':'humidity','namespace':'Environment Sensor','data_point':{'value':env2_1.humidity}},
{'name':'TVOC','namespace':'Environment Sensor','data_point':{'value':tvoc_1.TVOC}},
{'name':'CO2','namespace':'Environment Sensor','data_point':{'value':tvoc_1.eCO2}},
{'name':'H2','namespace':'Environment Sensor','data_point':{'value':tvoc_1.H2}},
{'name':'Ethanol','namespace':'Environment Sensor','data_point':{'value':tvoc_1.Ethanol}}
]
}
while status is 'Succeeded' or status is None:
try:
wifiCfg.reconnect()
label0.setText('sending...')
res = urequests.post(
url,
json = ujson.dumps(data),
headers = headers
)
except:
pass
res = res.json()
status = res['message']
label0.setText(str(status))
lcd.qrcode('https://app.machinist.iij.jp/dashboard', x=0, y=20, width=220, version=6)
time.sleep(300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment