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

    Help 4-Channel Relay 13.2 Modbus

    Modules
    2
    10
    898
    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.
    • R
      Robertof
      last edited by

      Hello,

      I have purchased the 4-Channel Relay 13.2 module and I need an example to activate and deactivate the relays using Modbus, both as a master and as a slave. Could you please provide me with a sample code or guide on how to achieve this?

      Thank you!

      felmueF 1 Reply Last reply Reply Quote 0
      • felmueF
        felmue @Robertof
        last edited by felmue

        Hello @Robertof

        which M5Core / Modbus hardware are you panning to use?

        Documentation about the 4-Channel Relay module can be found here.

        The required libraries (and examples) can be found here:

        • M5Module-4Relay
        • Modbus
        • RS485

        I suggest you start with controlling the four relays from the M5Core first (w/o Modbus). Then learn about Modbus master and slave. And finally combine Modbus with the relays.

        Thanks
        Felix

        GPIO translation table M5Stack / M5Core2
        Information about various M5Stack products.
        Code examples

        R 1 Reply Last reply Reply Quote 0
        • R
          Robertof @felmue
          last edited by

          @felmue Hi Felix, the problem is that I only know how to use Uiflow. I followed an example that was available to read multiple records, and it worked with qmodmaster. I am using an M5Core 2.7. The information for Uiflow Modbus V1 and V2 is under construction, and I am frustrated because I can’t solve it. I appreciate your comment, regards.

          felmueF 1 Reply Last reply Reply Quote 0
          • felmueF
            felmue @Robertof
            last edited by felmue

            Hello @Robertof

            from what I can tell Modbus Slave (Server) blocks have not yet been implemented in UIFlow2.

            That said, please find below a Modbus Slave (Server) example in UIFlow1:

            M5StackModbusSlaveExample_UIFlow1.13.6_20240808.png

            from m5stack import *
            from m5ui import *
            from uiflow import *
            from modbus.slave.rtu import ModbusSlave
            
            setScreenColor(0x222222)
            
            myVal = None
            
            label0 = M5TextBox(20, 31, "label0", lcd.FONT_Default, 0xFFFFFF, rotate=0)
            label1 = M5TextBox(19, 69, "label1", lcd.FONT_Default, 0xFFFFFF, rotate=0)
            
            label0.setText('Coil 0 : off')
            label1.setText('Coil 1 : off')
            modbus_s = ModbusSlave(2, tx=17, rx=16, baudrate=9600, data_bits=8, stop_bits=1, parity=None, slaveID=4)
            modbus_s.function_init(5, 0, 2)
            while True:
              myVal = modbus_s.receive_req_create_pdu()
              if (modbus_s.find_function) == (5):
                if modbus_s.find_address==0:
                  if myVal == 0:
                    label0.setText('Coil 0 : off')
                  else:
                    label0.setText('Coil 0 : on')
                elif modbus_s.find_address==1:
                  if myVal == 0:
                    label1.setText('Coil 1 : off')
                  else:
                    label1.setText('Coil 1 : on')
                else:
                  pass
                modbus_s.create_slave_response(myVal)
              wait_ms(2)
            

            Thanks
            Felix

            GPIO translation table M5Stack / M5Core2
            Information about various M5Stack products.
            Code examples

            R 2 Replies Last reply Reply Quote 0
            • R
              Robertof @felmue
              last edited by

              @felmue uiflow_block_1723117206494.png

              1 Reply Last reply Reply Quote 0
              • R
                Robertof @felmue
                last edited by

                @felmue
                This has worked perfectly thanks to your help.
                Your comments helped me understand the code and adapt it to my needs.
                If there's any way to mark this post as resolved, I would appreciate it if you could let me know how to do it.
                I am very grateful for your help.
                Thank you so, so, so much.
                Roberto.

                felmueF 1 Reply Last reply Reply Quote 0
                • felmueF
                  felmue @Robertof
                  last edited by

                  Hello @Robertof

                  You are welcome. I am glad to hear you got it working to your liking.

                  I don't know of an official way to mark a post as resolved. Sometimes people change the title and prepend it with [Solved], but I haven't seen that in a while. I'll leave that up to you.

                  Thanks
                  Felix

                  GPIO translation table M5Stack / M5Core2
                  Information about various M5Stack products.
                  Code examples

                  R 1 Reply Last reply Reply Quote 0
                  • R
                    Robertof @felmue
                    last edited by

                    @felmue
                    Hi Felix, I haven’t been able to change the title.
                    I saw your GitHub page, it’s clear that C++ is your language.
                    Thanks for providing me with the UIflow example.
                    Roberto.

                    felmueF 1 Reply Last reply Reply Quote 0
                    • felmueF
                      felmue @Robertof
                      last edited by

                      Hello @Robertof

                      if you edit your first post it should allow you to change the title. Have you tired that?

                      Thanks
                      Felix

                      GPIO translation table M5Stack / M5Core2
                      Information about various M5Stack products.
                      Code examples

                      R 1 Reply Last reply Reply Quote 0
                      • R
                        Robertof @felmue
                        last edited by Robertof

                        Hola @felmue
                        capture.jpg

                        Now I did it but it can't be done!

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