Introduction of M5StickC (Temperature / Humidity measurement and MQTT transmission, UIFlow Python)

image.png

Screen display (temperature, humidity, barometric pressure, power supply voltage, MQTT transmission interval) image.png All UIFlow graphical programs (Python code is automatically generated in real time)

Introduction

I measured the temperature, humidity, and atmospheric pressure with "M5StickC" and "M5StickC ENV Hat (with DHT12 / BMP280 / BMM150)" and tried MQTT transmission. The development environment is UIFlow V1.4.0-Beta. (UIFlow V1.3.2 does not seem to support ENV Hat.) The Python code is generated in real time from the graphical program and it feels good.

M5StickC is small and good.

48(W)×24(D)×14(H)mm 18.1 g You can create a user interface with three switches (one is a power button), LED, LCD color display, etc. It also has a built-in button battery for the real-time clock.

ENV Hat is affected by the heat generated by the M5StickC main unit, and the temperature rises.

It feels like it can't be used as it is. It is necessary to devise ways to suppress heat generation.

The Blockly screen of the UIFlow IDE is fun to program graphically.

--You can switch between the graphical screen and the Python code screen with "Blockly" and "</ strong> Python" at the top of the UIFlow IDE. --The graphical program is reflected in the Python code, but not the other way around. --The graphical screen menu is a convenient alternative to the hardware API manual. --When you want to write Python code in the graphical screen, use the "advanced block" and "execute" blocks.

UIFlow V1.4.0-Beta had the following problems.

--If you enter'%' in the string, it will be'%' on the python side. --The properties set when creating the LCD screen layout are sometimes not reflected.

It seems possible to do LCD screen layout and hardware access in a graphical environment, then abandon the graphical environment and develop with only Python code.

hardware

M5StickC body

image.png From https://docs.m5stack.com/#/ja/core/m5stickc

M5StickC Power Management

image.png

From https://lang-ship.com/reference/unofficial/M5StickC/Tips/AXP192/

M5StickC ENV Hat (with DHT12 / BMP280 / BMM150)

image.png

From https://www.switch-science.com/catalog/5755/

Program specifications

LCD screen display information

--Temperature, humidity, barometric pressure

LED lighting

MQTT transmission data (graph creation and data display on the PC side)

image.png

Graphical program (Blockly screen)

Initialization (MQTT, screen, timer) and main loop (LED heartbeat, sensor read loop, median calculation, LCD display)

image.png

Processing of buttons A and B

--Button A (top surface, M5 mark) LCD on / off, press and hold to send MQTT --Button B (side) MQTT transmission timer selection (1/3/30 minutes), long press to change background color (blue / red)

image.png

Screen saver timer 1

image.png

MQTT transmission timer 2

image.png

Python program (automatically generated program)

The program automatically generated from the graphical program is shown below. No code additions or modifications have been made since the auto-generation.

main.py


from m5stack import *
from m5ui import *
from uiflow import *
from m5mqtt import M5mqtt
import hat

setScreenColor(0x111111)

hat_env0 = hat.get(hat.ENV)

m5mqtt = M5mqtt('M5StickC', '192.168.11.2', 1883, '', '', 300)

label0 = M5TextBox(74, 5, "Text", lcd.FONT_DejaVu18,0xFFFFFF, rotate=90)
label1 = M5TextBox(50, 5, "Text", lcd.FONT_DejaVu18,0xFFFFFF, rotate=90)
label2 = M5TextBox(25, 5, "Text", lcd.FONT_DejaVu18,0xFFFFFF, rotate=90)
label4 = M5TextBox(50, 97, "Text", lcd.FONT_DejaVu18,0xFFFFFF, rotate=90)
label3 = M5TextBox(74, 97, "Text", lcd.FONT_DejaVu18,0xFFFFFF, rotate=90)

from numbers import Number

x = None
LcdOn = None
mqttIntervalMinIndex = None
blueOn = None
ID = None
LcdOffMin = None
mqttIntervalMin = None
mqttIntervalMinList = None
msg = None
tempList = None
humiList = None
presList = None
battList = None
temp = None
humi = None
pres = None
batt = None

def math_median(myList):
  localList = sorted([e for e in myList if isinstance(e, Number)])
  if not localList: return
  if len(localList) % 2 == 0:
    return (localList[len(localList) // 2 - 1] + localList[len(localList) // 2]) / 2.0
  else:
    return localList[(len(localList) - 1) // 2]

def setLcdTimer():
  global x, LcdOn, mqttIntervalMinIndex, blueOn, ID, LcdOffMin, mqttIntervalMin, mqttIntervalMinList, msg, tempList, humiList, presList, battList, temp, humi, pres, batt
  LcdOffMin = 3
  timerSch.stop('timer1')
  timerSch.run('timer1', (LcdOffMin * (1000 * 60)), 0x00)

def setMqttTimer(x):
  global LcdOn, mqttIntervalMinIndex, blueOn, ID, LcdOffMin, mqttIntervalMin, mqttIntervalMinList, msg, tempList, humiList, presList, battList, temp, humi, pres, batt
  timerSch.stop('timer2')
  if x:
    timerSch.run('timer2', (mqttIntervalMin * (1000 * 60)), 0x00)


def buttonA_wasReleased():
  global LcdOn, mqttIntervalMinIndex, blueOn, ID, LcdOffMin, mqttIntervalMin, x, mqttIntervalMinList, msg, tempList, humiList, presList, battList, temp, humi, pres, batt
  LcdOn = not LcdOn
  if LcdOn:
    axp.setLDO2Vol(3)
  else:
    axp.setLDO2Vol(0)
  setLcdTimer()
  pass
btnA.wasReleased(buttonA_wasReleased)

def buttonA_pressFor():
  global LcdOn, mqttIntervalMinIndex, blueOn, ID, LcdOffMin, mqttIntervalMin, x, mqttIntervalMinList, msg, tempList, humiList, presList, battList, temp, humi, pres, batt
  setMqttTimer(False)
  ttimer2()
  wait(3)
  setMqttTimer(True)
  setLcdTimer()
  pass
btnA.pressFor(0.8, buttonA_pressFor)

def buttonB_wasReleased():
  global LcdOn, mqttIntervalMinIndex, blueOn, ID, LcdOffMin, mqttIntervalMin, x, mqttIntervalMinList, msg, tempList, humiList, presList, battList, temp, humi, pres, batt
  mqttIntervalMinIndex = 1 + mqttIntervalMinIndex
  if mqttIntervalMinIndex >= 4:
    mqttIntervalMinIndex = mqttIntervalMinIndex - 3
  mqttIntervalMin = mqttIntervalMinList[int(mqttIntervalMinIndex - 1)]
  label4.setText(str((str(mqttIntervalMin) + 'min')))
  setMqttTimer(True)
  setLcdTimer()
  pass
btnB.wasReleased(buttonB_wasReleased)

def buttonB_pressFor():
  global LcdOn, mqttIntervalMinIndex, blueOn, ID, LcdOffMin, mqttIntervalMin, x, mqttIntervalMinList, msg, tempList, humiList, presList, battList, temp, humi, pres, batt
  blueOn = not blueOn
  if blueOn:
    setScreenColor(0x3366ff)
  else:
    setScreenColor(0xff0000)
  axp.setLDO2Vol(3)
  label0.setText('')
  label1.setText('')
  label2.setText('')
  label3.setText('')
  label4.setText('')
  setLcdTimer()
  pass
btnB.pressFor(0.8, buttonB_pressFor)

@timerSch.event('timer1')
def ttimer1():
  global LcdOn, mqttIntervalMinIndex, blueOn, ID, LcdOffMin, mqttIntervalMin, x, mqttIntervalMinList, msg, tempList, humiList, presList, battList, temp, humi, pres, batt
  axp.setLDO2Vol(0)
  LcdOn = False
  pass

@timerSch.event('timer2')
def ttimer2():
  global LcdOn, mqttIntervalMinIndex, blueOn, ID, LcdOffMin, mqttIntervalMin, x, mqttIntervalMinList, msg, tempList, humiList, presList, battList, temp, humi, pres, batt
  M5Led.on()
  msg = temp  + ' ,' + humi + ',' + pres + ',' + batt
  m5mqtt.publish(str('M5StickC'),str(((ID + ((',' + msg))))))
  wait(0.001)
  M5Led.off()
  pass


ID = 'I5001'
m5mqtt.start()
LcdOn = True
axp.setLDO2Vol(3)
blueOn = True
setScreenColor(0x3366ff)
setLcdTimer()
mqttIntervalMinIndex = 3
mqttIntervalMinList = [1, 3, 30]
mqttIntervalMin = mqttIntervalMinList[int(mqttIntervalMinIndex - 1)]
setMqttTimer(True)
while True:
  M5Led.on()
  wait(0.0007)
  M5Led.off()
  tempList = []
  humiList = []
  presList = []
  battList = []
  for count in range(5):
    tempList.append(int(((hat_env0.temperature) * 100)))
    humiList.append(int(((hat_env0.humidity) * 100)))
    presList.append(int(((hat_env0.pressure) * 100)))
    battList.append(int(((axp.getBatVolt()) * 100)))
    wait(0.2)
  temp = (("%.2f"%((math_median(tempList) / 100))) + 'C')
  humi = (("%.2f"%((math_median(humiList) / 100))) + 'pct')
  pres = (("%.2f"%((math_median(presList) / 100))) + 'hPa')
  batt = (("%.2f"%((math_median(battList) / 100))) + 'V')
  label0.setText(str(temp))
  label1.setText(str(humi))
  label2.setText(str(pres))
  label3.setText(str(batt))
  label4.setText(str((str(mqttIntervalMin) + 'min')))
  wait_ms(2)

Related information

M5StickC Quick Start-UIFlow https://docs.m5stack.com/#/ja/quick_start/m5stickc/m5stickc_quick_start_with_uiflow

M5StickC (M5Stack Docs) https://docs.m5stack.com/#/ja/core/m5stickc https://docs.m5stack.com/#/en/uiflow/hardware

m5stack/M5StickC https://github.com/m5stack/M5StickC

Try using the attic laboratory M5Stack http://itoi.jp/M5Stack.html

Survey of M5StickC unofficial Japanese reference AXP192 https://lang-ship.com/reference/unofficial/M5StickC/Tips/AXP192/

I examined the AXP192 additional function of M5StickC 0.0.7 https://lang-ship.com/blog/?p=696#_LightSleep

M5Stick C I tried disassembling for the time being https://twitter.com/Ghz2000/status/1125058416014176263

BeetleC arrived http://gijin77.blog.jp/archives/20843205.html

Move the 3D model of M5Stack on your computer according to the movement of M5Stack http://pages.switch-science.com/letsiot/rotateM5Stack/

[This] I went to the M5Stack headquarters, which the world's makers are paying attention to![I can't win] https://wirelesswire.jp/2019/08/71960/

Adafruit M5Stick-C Pico Mini IoT Development Board https://www.adafruit.com/product/4290

Recommended Posts

Introduction of M5StickC (Temperature / Humidity measurement and MQTT transmission, UIFlow Python)
Easy introduction of python3 series and OpenCV3
Introduction of Python
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
Simple FPS measurement of python
Source installation and installation of Python
Introduction and tips of mlflow.Tracking
[Introduction to Python] I compared the naming conventions of C # and Python.
[Introduction to Udemy Python3 + Application] 69. Import of absolute path and relative path
[Introduction to Udemy Python3 + Application] 12. Indexing and slicing of character strings
[Introduction to Data Scientists] Basics of Python ♬ Conditional branching and loops
[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.
The story of Python and the story of NaN
Installation of SciPy and matplotlib (Python)
[Introduction to Python3 Day 1] Programming and Python
Introduction and implementation of activation function
Hadoop introduction and MapReduce with Python
This and that of python properties
Introduction of python drawing package pygal
Coexistence of Python2 and 3 with CircleCI (1.0)
Record of Python introduction for newcomers
Summary of Python indexes and slices
Reputation of Python books and reference books
Production of temperature control system with Raspberry Pi and ESP32 (2) Production of transmission device