3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

M5StickC+UIFlowで会議室の使用状況をモニタリングしよう①-2(マグネットスイッチ 完成版)

Posted at

#概要
前回(準備編)の続きで、ThingSpeakへの送信まで実装しました。

P5.jpg
ThingSpeakは回数制限があるので、ステータスが変わった時だけ送るようにしています。
TS1.JPG

#コード
C1.JPG
C2.JPG

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)
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?