M5StickC+UIFlow로 회의실의 사용 상황을 모니터링하자 ①-2(마그넷 스위치 완성판)

개요



전회(준비편) 계속해서, ThingSpeak에의 송신까지 구현했습니다.


ThingSpeak는 횟수 제한이 있으므로 상태가 바뀔 때만 보내도록 하고 있습니다.


코드






qiita.py
from m5stack import *
from m5ui import *
from uiflow import *
import wifiCfg
import machine
import time
import urequests

setScreenColor(0x111111)


isOpen = None
LastStat = None

wifiCfg.doConnect('●●●●●', '●●●●●●●●●●●●')
wifiCfg.reconnect()
DoorStatus = M5Title(title="DoorStat", x=3, fgcolor=0xFFFFFF, bgcolor=0x0000FF)
L_DS = M5TextBox(3, 26, "DS", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
Logs = M5TextBox(1, 77, "Logs", lcd.FONT_Default, 0xFFFFFF, rotate=0)




pin0 = machine.Pin(26, mode=machine.Pin.IN, pull=machine.Pin.PULL_UP)
isOpen = 0
LastStat = 0
L_DS.setText('Started!')
wait(1)
while True:
  if not (wifiCfg.wlan_sta.isconnected()):
    lcd.print('Wifi NG', 0, 120, 0xff0000)
  lcd.print('Wifi OK', 0, 120, 0x33ff33)
  Logs.setText('Checking...')
  wait(1)
  isOpen = pin0.value()
  Logs.setText('Checked')
  L_DS.setText(str(isOpen))
  if isOpen != LastStat:
    Logs.setText('Sending...')
    wait(1)
    try:
      req = urequests.request(method='GET', url=(str('https://api.thingspeak.com/update?api_key=●●●●●&field1=') + str(isOpen)), headers={})
      Logs.setText('Posted')
    except:
      Logs.setText('NG')
  LastStat = isOpen
  wait(10)
  wait_ms(2)

좋은 웹페이지 즐겨찾기