🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Newbie question - how to get started?

    Micropython
    4
    19
    29.0k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      BricoMS @marcvl
      last edited by BricoMS

      if you do import module "m5stack"

      import m5stack

      and do a:

      dir(m5stack)

      ['__class__', '__name__', '__file__', 'btn', 'btnA', 'btnB', 'btnC', 'decoder', 'display', 'espidf', 'gc', 'lvesp32', 'machine', 'rtch', 'touch', 'power', 'rtc', 'lv', 'get_png_info', 'open_png', 'binascii', 'Axp192', 'i2c_bus', 'i2c', 'lcd', 'disp_buf1', 'buf1_1', 'buf1_2', 'disp_drv', 'indev_drv', 'button', 'time_ex', 'timEx', 'timeSchedule', 'timerSch', 'BM8563', 'Speaker', 'speaker', 'Rgb_multi', 'rgb', 'node_id', 'psram_init', '_remote_init', 'remoteInit']

      you will see one of those methods is called: lcd
      if you go again and do a

      dir(m5stack.lcd)

      ['__class__', 'clear', 'print', 'BLACK', 'BLUE', 'BMP', 'BOTTOM', 'CENTER', 'COLOR_BITS16', 'COLOR_BITS24', 'CYAN', 'DARKCYAN', 'DARKGREEN', 'DARKGREY', 'FONT_7seg', 'FONT_Comic', 'FONT_Default', 'FONT_DefaultSmall', 'FONT_DejaVu18', 'FONT_DejaVu24', 'FONT_DejaVu40', 'FONT_DejaVu56', 'FONT_DejaVu72', 'FONT_Minya', 'FONT_Small', 'FONT_Tooney', 'FONT_Ubuntu', 'GREEN', 'GREENYELLOW', 'HSPI', 'ICON_App', 'ICON_AppIcon', 'ICON_AppMenu', 'ICON_Cloud', 'ICON_Eath', 'ICON_Key', 'ICON_ListOFF', 'ICON_ListON', 'ICON_Machine', 'ICON_Retry', 'ICON_SetMenu', 'ICON_Setup', 'ICON_USB', 'ICON_Url', 'ICON_Wifi', 'ICON_WifiBig', 'JPG', 'LANDSCAPE', 'LANDSCAPE_FLIP', 'LASTX', 'LASTY', 'LIGHTGREY', 'M5STACK', 'MAGENTA', 'MAROON', 'NAVY', 'OLIVE', 'ORANGE', 'PINK', 'PORTRAIT', 'PORTRAIT_FLIP', 'PURPLE', 'RED', 'RIGHT', 'SPRITE_16BIT', 'SPRITE_1BIT', 'SPRITE_8BIT', 'VSPI', 'WHITE', 'YELLOW', 'arc', 'attrib7seg', 'circle', 'clearwin', 'compileFont', 'drawCircle', 'drawLine', 'drawPixel', 'drawRect', 'drawRoundRect', 'drawSwitchBtn', 'drawTriangle', 'ellipse', 'fill', 'fillCircle', 'fillRect', 'fillRoundRect', 'fillScreen', 'fillTriangle', 'font', 'fontSize', 'getCursor', 'get_bg', 'get_fg', 'hsb2rgb', 'image', 'image_buff', 'line', 'lineByAngle', 'orient', 'pixel', 'polygon', 'println', 'qrcode', 'rect', 'resetwin', 'restorewin', 'roundrect', 'savewin', 'screensize', 'setColor', 'setCursor', 'setRotation', 'setTextColor', 'set_bg', 'set_fg', 'setwin', 'sprite_create', 'sprite_delete', 'sprite_deselect', 'sprite_drawGrayMap', 'sprite_select', 'sprite_show', 'text', 'textClear', 'textWidth', 'text_x', 'text_y', 'tft_deselect', 'tft_readcmd', 'tft_select', 'tft_setspeed', 'tft_writecmd', 'tft_writecmddata', 'triangle', 'winsize']

      it will show you the methods for that.
      one of them being "clear"
      so...
      if you do:

      m5stack.lcd.clear()

      you will clear screen.

      AN alternative for dir() is to type the name of the module then a period "." and TAB key
      like this:

      m5stack.TABkey

      I could have just told you to do:

      import m5stack
      m5stack.lcd.clear()

      But i did all that so you can find more interesting stuff by yourself. Remember to check out some basic python tutorials.

      About your error of core2 rebooting... yes that is the RTOS rebooting for a wrong memory access.

      1 Reply Last reply Reply Quote 0
      • ajb2k3A
        ajb2k3 @marcvl
        last edited by

        @marcvl said in 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?

        Hello and welcome to the forum.
        First switch to thonny as its a little bit easier to use than mu
        https://thonny.org
        Next visit micropython.org.
        Next you will need to do a lot of reading as you need to learn about Python, micropython and then M5Stacks implementation of Micropython (reading up on adafruits circuitpython may confuse you.)

        UIFlow, so easy an adult can learn it!
        If I don't know it, be patient!
        I've ether not learned it or am too drunk to remember it!
        Author of the WIP UIFlow Handbook!
        M5Black, Go, Stick, Core2, and so much more it cant be fit in here!

        1 Reply Last reply Reply Quote 1
        • M
          marcvl
          last edited by

          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.

          1 Reply Last reply Reply Quote 1
          • ajb2k3A
            ajb2k3
            last edited by

            There is also my own work in progress documents https://github.com/Ajb2k3/UIFlowHandbook

            UIFlow, so easy an adult can learn it!
            If I don't know it, be patient!
            I've ether not learned it or am too drunk to remember it!
            Author of the WIP UIFlow Handbook!
            M5Black, Go, Stick, Core2, and so much more it cant be fit in here!

            1 Reply Last reply Reply Quote 1
            • M
              marcvl
              last edited by

              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?

              1 Reply Last reply Reply Quote 1
              • B
                BricoMS
                last edited by BricoMS

                @marcvl
                Hello,
                Besides the "auto align" of the uiflow interface, you would need to make that by programming. By programming i am refering to either pure code or BLOCKS)

                For example: (And i do not have the right numbers or functions at hand)

                if your screen is 320x180
                then the center of the screen would be 160x90
                If your button (or any object) is 20w x 10h... how would you put it at center?

                myX= 160-10
                myY= 90-5
                button1 = M5Btn(text='Button', x=myX, y=myY, w=20, h=10)

                So you sustract the half of the size of your object from the half size of your screen.
                And you can do that with relation to any object or any kind of reticle.

                Some objects like buttons have properties like width height, x and y.
                so if you ask for button1.w or button1.h (you have to check the real properties of your object, it could be button1.width or button1.height, check the documentation.) This way you can re arrenge your ui programatically asking for an object size and position.

                like this:(its just an example i dont think it works)
                button1 = M5Btn(text='Button', x=0, y=0 w=20, h=10)
                button1.x = myScreen.width - (button1.w / 2)
                button1.y = myScreen.height - (button1.h / 2)

                And from this you get into more complex reticles. A good advice is to plan your interface ahead of time and think of this aligments from the start.
                You will get a hang of that with time and will discover more easy ways to do it or more efficent ones.

                I recommend you search for tutorials on programming UI interfaces and aligning them. Most of the time i do not use the UI composition screen, i just drop the elements and i align them programatically.

                Maybe.. there is a way to do it easy on UIFLOW, but if you really want to have control of your elements and interface, you need to know by code where your objects are in relation to one another.

                Hope i did not confuse you too much.

                1 Reply Last reply Reply Quote 0
                • M
                  marcvl
                  last edited by

                  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

                  ajb2k3A D 2 Replies Last reply Reply Quote 0
                  • ajb2k3A
                    ajb2k3 @marcvl
                    last edited by

                    @marcvl said in 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

                    Some of us have only just discovered them but I haven't had the time to look and experiment let alone document!

                    UIFlow, so easy an adult can learn it!
                    If I don't know it, be patient!
                    I've ether not learned it or am too drunk to remember it!
                    Author of the WIP UIFlow Handbook!
                    M5Black, Go, Stick, Core2, and so much more it cant be fit in here!

                    1 Reply Last reply Reply Quote 0
                    • D
                      Dario @marcvl
                      last edited by

                      @marcvl on the Core2 the widgets are implemented trough LVGL. For the alignments flags you can look at this page:
                      https://docs.lvgl.io/v7/en/html/widgets/obj.html

                      M 1 Reply Last reply Reply Quote 0
                      • M
                        marcvl @Dario
                        last edited by

                        @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 that set_align(ALIGN.CENTER) should center-align the text within the label. However, the M5 equivalent set_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 :)

                        1 Reply Last reply Reply Quote 0
                        • M
                          marcvl
                          last edited by marcvl

                          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?

                          1 Reply Last reply Reply Quote 0
                          • M
                            marcvl
                            last edited by

                            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 before mqtt.start(). In my case, I had inserted it after mqtt.start().

                            Also round(topic_data) doesn't work because there's a type mismatch. round(float(topic_data)) did work.

                            ajb2k3A 1 Reply Last reply Reply Quote 0
                            • ajb2k3A
                              ajb2k3 @marcvl
                              last edited by

                              @marcvl Really?

                              Wow thanks for discovering the m5mqtt.subscribe/mqtt.start issue.
                              BTW is this in micropython?

                              UIFlow, so easy an adult can learn it!
                              If I don't know it, be patient!
                              I've ether not learned it or am too drunk to remember it!
                              Author of the WIP UIFlow Handbook!
                              M5Black, Go, Stick, Core2, and so much more it cant be fit in here!

                              M 1 Reply Last reply Reply Quote 0
                              • M
                                marcvl @ajb2k3
                                last edited by

                                @ajb2k3 Yes, this is in micropython. I need to do a little bit more work but will post my code on Github soon.

                                1 Reply Last reply Reply Quote 1
                                • M
                                  marcvl
                                  last edited by

                                  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.

                                  1 Reply Last reply Reply Quote 2
                                  • First post
                                    Last post