Hi: Is there an update to this request? It seems to me that this would be very easy fix ... just adding the 'retain flag' parameter to mqtt messages. This is a very essential feature for mqtt auto-discovery with remote clients (such as Home Assistant). Without retain, Home Assistant loses all the configuration details about the M5Stack device after every HA reboot. The M5stack becomes 'unavailable' to HA and it's only after rebooting M5Stack that the device becomes available again.
Posts made by marcvl
-
RE: [MQTT-Core] Support for RETAIN
-
RE: Core2 vs. Core
I'm not sure if anyone from M5Stack is monitoring this forum, but would love their feedback.
I think that the main difference between Core and Core2, as far as memory is concerned, is that the Core 2 has 8 MB of PSRAM (in addition to the 520KB SRAM of the ESP32), where as the Core only has the 520KB SRAM.
That's a huge difference in available memory. Could this explain why my program runs out of memory on the Core (while running fine on the Core2)? How do I verify how much memory my program is using?
-
[MQTT-Core] Support for RETAIN
RETAIN is an essential feature of any MQTT implementation. Without RETAIN, device information can get lost when a service like Home Assistant who uses MQTT subscription to get device info, has an issue. For instance, when Core sends out an MQTT discovery message (essential for Home Assistant auto-discovery) the RETAIN flag ensures that the device is still available after Home Assistant reboots.
-
Core2 vs. Core
I'm trying to get my Core2 micropython script to run on the Core. I'm gettin following error in Thonny:
MemoryError: memory allocation failed, allocatin
Any tips on how to fix this issue? My code is here. I was hoping that my code would run on both devices without having to make changes. Is that not the case?
EDITED: corrected the Github link to point to the right repository.
-
RE: Special character handling - MQTT error
So my last comment provided the solution....
The correct way of doing this is :
1/ Specify the Unicode character in the payload with
chr(186)
where186
is the character number (in this case representing MASCULINE ORDINAL INDICATOR - Unicode: U+00BA, UTF-8: C2 BA.2/ Ensure that your
json.dumps
applies utf-8 encoding before sending it to MQTT as follows:
m5mqtt.publish(topic, json.dumps(payload).encode('utf-8'))
Problem solved.
-
RE: Special character handling - MQTT error
Ok, so I tried two alternatives:
KEY_UNIT_OF_MEASUREMENT: chr(176) + "C",
KEY_UNIT_OF_MEASUREMENT: chr(186) + "C",
The first one represents 0xb0 and the second one 0xba.
Both result in the same error message (
[24400] MQTTException -> [Errno 104] ECONNRESET
) and a missing}
at the end of the JSON.Maybe the json.dumps function, which I'm using, needs a special parameter to indicate unicode characters?
-
RE: Special character handling - MQTT error
How would I do that?
My current code is:
KEY_UNIT_OF_MEASUREMENT: "ºC",
Also, I believe the correct Unicode character is 0xba, not 0xb0.
I checked Mosquitto output to see exactly what Core2 is putting out. It's very strange. When I add the 'degree' symbol, the JSON is missing a closing parenthesis (which is why HA is complaining). I'm showing you below the Mosquitto output for the temperature sensor and the humidity sensor. Both are formatted in the same way. The temperature sensor has the degree symbol.
homeassistant/sensor/core2/core2-temp/config {"avty_t": "~status", "pl_avail": "on", "val_tpl": "{{value_json.temperature}}", "dev_cla": "temperature", "unit_of_meas": "ºC", "~": "core2/env2/", "dev": {"mdl": "Core 2", "mf": "M5Stack", "name": "Core 2 Thermostat", "ids": ["12234"]}, "name": "Core2 Temperature", "stat_t": "~state", "pl_not_avail": "off", "uniq_id": "12234" homeassistant/sensor/core2/core2-humid/config {"avty_t": "~status", "pl_avail": "on", "val_tpl": "{{value_json.humidity}}", "dev_cla": "humidity", "unit_of_meas": "%", "~": "core2/env2/", "dev": {"mdl": "Core 2", "mf": "M5Stack", "name": "Core 2 Thermostat", "ids": ["12234"]}, "name": "Core2 Humidity", "stat_t": "~state", "pl_not_avail": "off", "uniq_id": "122347"}
And here's the same Mosquitto output, when I remove the degree symbol.
homeassistant/sensor/core2/core2-temp/config {"avty_t": "~status", "pl_avail": "on", "val_tpl": "{{value_json.temperature}}", "dev_cla": "temperature", "unit_of_meas": "C", "~": "core2/env2/", "dev": {"mdl": "Core 2", "mf": "M5Stack", "name": "Core 2 Thermostat", "ids": ["12234"]}, "name": "Core2 Temperature", "stat_t": "~state", "pl_not_avail": "off", "uniq_id": "12234"} homeassistant/sensor/core2/core2-humid/config {"avty_t": "~status", "pl_avail": "on", "val_tpl": "{{value_json.humidity}}", "dev_cla": "humidity", "unit_of_meas": "%", "~": "core2/env2/", "dev": {"mdl": "Core 2", "mf": "M5Stack", "name": "Core 2 Thermostat", "ids": ["12234"]}, "name": "Core2 Humidity", "stat_t": "~state", "pl_not_avail": "off", "uniq_id": "122347"}
-
Special character handling - MQTT error
I'm getting following MQTT error when publishing to a topic with a payload that contains the 'degree' character (º
MASCULINE ORDINAL INDICATOR - Unicode: U+00BA, UTF-8: C2 BA):[24941] MQTTException -> [Errno 104] ECONNRESET reconnect start ... reconnect finish ...
The MQTT message goes to Home Assistant where it also generates an error:
Logger: homeassistant.components.mqtt.discovery Source: components/mqtt/discovery.py:109 Integration: MQTT (documentation, issues) First occurred: 6:04:58 PM (1 occurrences) Last logged: 6:04:58 PM* *Unable to parse JSON core2-temp: '{"avty_t": "~status", "pl_avail": "on", "val_tpl": "{{value_json.temperature}}", "dev_cla": "temperature", "unit_of_meas": "ºC", "~": "core2/env2/", "dev": {"mdl": "Core 2", "mf": "M5Stack", "name": "Core 2 Thermostat", "ids": ["12234"]}, "name": "Core2 Temperature", "stat_t": "~state", "pl_not_avail": "off", "uniq_id": "12234"'
Strangely, the JSON seems to be missing closing "}" which might explain the HA error. My code has the right JSON formatting. Just removing the 'degree' symbol will solve the issue. So, there seems to be something strange with how special characters are handled. Or maybe I'm doing something wrong?
-
RE: Randomness ...
Update on the randomness .... it's gone :-)
Basically, I think my UIFlow project became too complex for the system to handle. So, I rewrote everything in micropython. The code runs very well, no more randomness.
Github repository of my MQTT Thermostat running on Core2 is here.
It's pretty cool. The thermostat supports various modes: off/auto/manual/heat/cool/fan. You can specify minimum cycle duration as well as set swing mode parameters. It automatically gets discovered by Home Assistant. You can set thermostat mode and target temperature in Home Assistant and they will automatically get reflected on the Core2 display.
It currently relies on the ENVII module for temperature reading, and requires a WIFI connection and an active MQTT broker. I haven't had time to implement error handling, so if something goes wrong, it might be non-obvious to figure out why. More work to be done.
-
RE: Newbie question - how to get started?
Github repository of my MQTT Thermostat running on Core2 is here.
It's pretty cool. The thermostat supports various modes: off/auto/manual/heat/cool/fan. You can specify minimum cycle duration as well as set swing mode parameters. It automatically gets discovered by Home Assistant. You can set thermostat mode and target temperature in Home Assistant and they will automatically get reflected on the Core2 display.
It currently relies on the ENVII module for temperature reading, and requires a WIFI connection and an active MQTT broker. I haven't had time to implement error handling, so if something goes wrong, it might be non-obvious to figure out why. More work to be done.
I couldn't figure out how to enable touch for a specific screen area without using visible buttons with M5stack_UI, so instead I used an lvgl button with invisible borders.
-
RE: Newbie question - how to get started?
@ajb2k3 Yes, this is in micropython. I need to do a little bit more work but will post my code on Github soon.
-
RE: Newbie question - how to get started?
Ok, this time I'm going to solve my own problem :-)
The issue was that the
m5mqtt.subscribe
code was in the wrong place. You have to put it beforemqtt.start()
. In my case, I had inserted it aftermqtt.start()
.Also
round(topic_data)
doesn't work because there's a type mismatch.round(float(topic_data))
did work. -
RE: Newbie question - how to get started?
On to the next issue.... I've been trying to get
m5mqtt_subscribe
to work and staring at the code for a few hours .... Maybe some fresh eyes can help pinpoint the problem.MQTT is set up correctly. M5 is publishing data to home assistant (through Mosquitto) without issues. However, I can't get the reverse to work (ie. Home Assistant publishing data to the M5).
I have confirmed that HA is doing the right thing .... i.e. publishing data on an agreed topic. Example: here's what HA publishes to change the target temperature of the thermostat:
core2/thermostat/temperature/command 21.11111111111111
Here's my micropython code on the M5:
DEFAULT_TOPIC_THERMOSTAT_PREFIX = "core2/thermostat/" TOPIC_TEMPERATURE_COMMAND = "temperature/command" m5mqtt.subscribe(DEFAULT_TOPIC_THERMOSTAT_PREFIX + TOPIC_TEMPERATURE_COMMAND, rcv_target_temp) def rcv_target_temp(topic_data): global target_temp m5mqtt.publish(DEFAULT_TOPIC_DEBUG,'Received target_temp: %f' % topic_data) target_temp = round(topic_data)
The
rcv_target_temp
code never gets executed. No errors are being reported. What am I doing wrong? -
RE: Newbie question - how to get started?
@dario That's great. I figured out how to do it...
Here's what I discovered:
-
It seems like the M5
set_align
function is slightly different from the LVGL implementation. My understanding from the LVGL documentation is thatset_align(ALIGN.CENTER)
should center-align the text within the label. However, the M5 equivalentset_align(ALIGN_CENTER)
also centers the entire label horizontally, ignoring the previous label position definition. -
The M5 version of
set_align
however takes extra arguments to specify x and y offset. So you can say<lbl_name>.set_align(ALIGN_CENTER, 0, -10)
-
There is no auto-align feature as far as I can tell. LVGL has a function
set_auto_realign(True)
, but I couldn't find the equivalent with M5. So every time I change the text in the label I have to re-run the alignment function.
But, hey, it works. So I'm not complaining :)
-
-
RE: Newbie question - how to get started?
Thanks. This all makes sense. How would you do this with an item that doesn't have a fixed width or height, specifically a label. The w and h of the label depend on the text in it. I'm struggling with finding a way to center align the text, either programmatically or through some of M5stack's functions?
There are bunch of functions in mstack_ui that deal with alignment (ALIGN_CENTER, ...) but I can't find any documentation on how to use them and these functions don't even have the basic Docstrings attached to them:
>>> help (M5Slider.set_align)
object <function set_align at 0x3f8402b0> is of type function -
RE: Newbie question - how to get started?
Made great progress. Thermostat is up and running. Display is rendering fine. MQTT integration with HA works...
One question as I'm working on aligning all the labels, sliders, images and other visual elements .... I can't figure out how to "center align" things. For example, I have a text label with varying length. I'd like to anchor that label on the screen by specifying the x,y coordinates of the center of the label, as opposed to the top left corner. How do I do that?
-
RE: Newbie question - how to get started?
This is all super helpful. Thank you! I will start building up my UIFlow-generated micro python code step by step to see when I hit the error that causes the reboot.
-
RE: Newbie question - how to get started?
Thanks. I was hoping that I could just import the micropython code that was generated in my UIFlow project and run it.... That would have been too easy:-).
I was able to load modules as you described, but haven't found the right module to get the lcd.clear() to work. Will keep digging. Also, I imported my UIFlow code, but the 'run' command in MU reboots my Core2. This is what the REPL window shows:
raw REPL; CTRL-B to exit
OK
MPY: soft reboot_ __ _
_ ()/ | | _____ __
| | | | | || |/ _ \ \ /\ / /
| || | | | | () \ V V /
_,||| ||___/ _/_/assertion "ATB_GET_KIND(block) == AT_HEAD" failed: file "../../py/gc.c", line 590, function: gc_free
abort() was called at PC 0x401e53d3 on core 1ELF file SHA256: 0000000000000000
Backtrace: 0x400961cb:0x3ffcf710 0x400965bd:0x3ffcf730 0x401e53d3:0x3ffcf750 0x400d3729:0x3ffcf780 0x400d2df1:0x3ffcf7a0 0x40121a8f:0x3ffcf7c0 0x40135f61:0x3ffcf7e0 0x4012e25a:0x3ffcf800 0x4012e271:0x3ffcf820 0x4013017f:0x3ffcf840 0x40136ff9:0x3ffcf870 0x40173253:0x3ffcf8a0 0x400e47c5:0x3ffcf8c0 0x400e0539:0x3ffcf8e0 0x400e068a:0x3ffcf900 0x400e61d4:0x3ffcf920 0x400e620f:0x3ffcf950 0x400ebb65:0x3ffcf970 0x400e070e:0x3ffcfa50 0x400eef69:0x3ffcfa90 0x400e499e:0x3ffcfb30 0x400e0539:0x3ffcfb90 0x400e0562:0x3ffcfbb0 0x400eb985:0x3ffcfbd0 0x400eba02:0x3ffcfc60 0x400ebd79:0x3ffcfc90 0x400e070e:0x3ffcfd70 0x400eef69:0x3ffcfdb0 0x400e499e:0x3ffcfe50 0x400e0539:0x3ffcfe80 0x400e0562:0x3ffcfea0 0x40174e70:0x3ffcfec0 0x40175139:0x3ffcff50 0x400f8372:0x3ffcff80 0x40097315:0x3ffcffb0
Rebooting...
ets Jul 29 2019 12:21:46rst:0xc (SW_CPU_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:5280
ho 0 tail 12 room 4
load:0x40078000,len:13424
load:0x40080400,len:3568
entry 0x4008063c_ __ _
_ ()/ | | _____ __
| | | | | || |/ _ \ \ /\ / /
| || | | | | () \ V V /
_,||| ||___/ _/_/APIKEY: E3FAB316
MicroPython d97cf60fd-dirty on 2021-03-16; M5Stack with ESP32
Type "help()" for more information. -
Newbie question - how to get started?
I'm so confused on how to run micropython directly on the Core2. I followed the M5Stack video about downloading MU but I can't get it to work.
- Mu is connecting to my Core2 ok. (Core2 is flashed with the latest version of UIFlow and connected over USB)
- In REPL mode, I'm getting a prompt, but the example of the video ("lcd.clear()") fails with 'NameError: 'lcd' isn't defined'
- The Files icon is greyed out (even after trying the workaround suggested in the video)
- I tried to run a Micropython program (copied it over from a UIFlow project). When tapping 'run', the device does a soft reboot and goes straight back into the program it was running before
What I am missing?
-
RE: [Mac OS] Copy/Paste
I found a workaround. Use the web version of UIFlow instead. Copy/Paste works great.