UiFLOWのmicroWebSrvでWebAPI作成

概要

 UiFLOWのmicroPythonにてM5StickC上にWebAPIを実装し、遠隔からサーボモータをコントールする。

動作の様子

コード

from m5stack import *
from m5ui import *
from uiflow import *
from MicroWebSrv.microWebSrv import MicroWebSrv
import wifiCfg
import ubinascii
wifiCfg.autoConnect(lcdShow = False)
ip = wifiCfg.wlan_sta.ifconfig()  
label0 = M5TextBox(10, 25, ip[0], lcd.FONT_Default, 0xFFFFFF, rotate=0)

import hat
hat_servo_0 = hat.get(hat.SERVO)

@MicroWebSrv.route('/set0deg')
def handlerFuncGet(httpClient, httpResponse) :
  print("In GET-TEST HTTP set0deg")
  hat_servo_0.write_angle(0)

@MicroWebSrv.route('/set90deg')
def handlerFuncGet(httpClient, httpResponse) :
  print("In GET-TEST HTTP set90deg")
  hat_servo_0.write_angle(90)

'''
@MicroWebSrv.route('/post-test', 'POST')
def handlerFuncPost(httpClient, httpResponse) :
  print("In POST-TEST HTTP")
  hat_servo_0.write_angle(90)
'''

mws = MicroWebSrv()      # TCP port 80 and files in /flash/www
mws.Start(threaded=True) # Starts server in a new thread

def buttonA_wasPressed():
  # global params
  hat_servo_0.write_angle(0)
  pass
btnA.wasPressed(buttonA_wasPressed)

def buttonB_wasPressed():
  # global params
  hat_servo_0.write_angle(90)
  pass
btnB.wasPressed(buttonB_wasPressed)

参考

GitHub - jczic/MicroWebSrv: A micro HTTP Web server that supports WebSockets, html/python language templating and routing handlers, for MicroPython (used on Pycom modules & ESP32)

詰まったところ

  • USB接続でM5C単体の5Vピンからサーボモータの電源を取ると、電流不足のためかリセットを繰り返してしまった。
  • M5Stack社製の白い18650Cのリチウムバッテリーから電源を取ると動作したため、一先ずこれにした。
  • 同じ理由のためかM5 Atomでも動作しなかった。今度tailバッテリーを付けてみて試したい。

所感

 遠隔電源ON/OFFできない機器はこれを使うのが良い・・・?