MCP23017: Port control - not Pin control

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Zodiac69
Normal user
Posts: 85
Joined: 13 Jun 2016, 17:20

MCP23017: Port control - not Pin control

#1 Post by Zodiac69 » 29 Sep 2020, 18:29

Hi All
Is it possable to send one command to change all 16 I/O pins at once.
At the moment i send 16 commands MCPGPIO,1,1.....MCPGPIO16,1 via MQTT. I have 32ESP's and updating all the status is NUTS...
Any way of sending one command to change the output of all 16 I/O pins?

Z69

User avatar
Ath
Normal user
Posts: 3419
Joined: 10 Jun 2018, 12:06
Location: NL

Re: MCP23017: Port control - not Pin control

#2 Post by Ath » 29 Sep 2020, 20:37

Well, the plugin doesn't have a command to switch on all 16 gpio's at once, but you can add an event in the rules to turn 'm all on using 1 command:

Code: Select all

on mcpgpioall do
  mcpgpio,1,%eventvalue%
  mcpgpio,2,%eventvalue%
  mcpgpio,3,%eventvalue%
  // and some more...
  mcpgpio,16,%eventvalue%
endon
That allows to set all gpio's to either 0 or 1 using the command event,mcpgpioall=0 or event,mcpgpioall=1
/Ton (PayPal.me)

Zodiac69
Normal user
Posts: 85
Joined: 13 Jun 2016, 17:20

Re: MCP23017: Port control - not Pin control

#3 Post by Zodiac69 » 29 Sep 2020, 21:00

Hi Ath

Yes, i looked at that, but i have 31 combenations that the ports need to be in, not just On/Off.
Depecning on a input value of 0 to 30, there is a "MAP" value that the ports need to be in.
That is why i was looking at sending a 16bit "Port" value as one comand instead of 16 commands to set each port when the input value change.

User avatar
Ath
Normal user
Posts: 3419
Joined: 10 Jun 2018, 12:06
Location: NL

Re: MCP23017: Port control - not Pin control

#4 Post by Ath » 29 Sep 2020, 21:39

Zodiac69 wrote: 29 Sep 2020, 21:00 i have 31 combenations that the ports need to be in, not just On/Off.
Depecning on a input value of 0 to 30, there is a "MAP" value that the ports need to be in.
That is why i was looking at sending a 16bit "Port" value as one comand instead of 16 commands to set each port when the input value change.
Using rules, you should be able to map the 0-30 value to the required gpio states, could be though making that smart, but be aware you can pass up to 4 values to the event (%eventvalue1% to %eventvalue4%, %eventvalue% is an alias for %eventvalue1%)
/Ton (PayPal.me)

Zodiac69
Normal user
Posts: 85
Joined: 13 Jun 2016, 17:20

Re: MCP23017: Port control - not Pin control?

#5 Post by Zodiac69 » 29 Sep 2020, 22:44

Hi Ath

Rules.... not enough space as i have to set all 16 pin states for each of the 31 "MAP" values.

Was thinking if it is possable to change P009 to include aditional command to set the "Port" instead of each pin - posted this on the GitHub as well.
Send MCPPort instead of MCPGPIO.
MCPPort, 0xFFFF will set all 16 I/O pins to output high(1)
MCPPort, 0x0000 will set all 16 I/O pins to output low(0)
MCPPort, 0xAA55 will set the 16 I/O pins to output in this patern - 1010 1010 0101 0101
MCPPortA, 0xAA will only affect the "A" port
MCPPortB, 0xAA will only affect the "B" port

User avatar
Ath
Normal user
Posts: 3419
Joined: 10 Jun 2018, 12:06
Location: NL

Re: MCP23017: Port control - not Pin control

#6 Post by Ath » 30 Sep 2020, 07:52

I like the idea (also saw your suggestion on github). That github improvement still needs some time tough, it seems.
/Ton (PayPal.me)

Zodiac69
Normal user
Posts: 85
Joined: 13 Jun 2016, 17:20

Re: MCP23017: Port control - not Pin control

#7 Post by Zodiac69 » 30 Sep 2020, 22:02

Hi Ath

Any pointers on how to do the "Port" part?
i tried to see if i can understand how to do it in P009, but i am not that good, need a lot of schooling on how to do that.
Just so i can control the port with one command instead of 16 to set / reset each pin

User avatar
ThomasB
Normal user
Posts: 1064
Joined: 17 Jun 2018, 20:41
Location: USA

Re: MCP23017: Port control - not Pin control

#8 Post by ThomasB » 01 Oct 2020, 05:14

Rules.... not enough space as i have to set all 16 pin states for each of the 31 "MAP" values.
You can create a short general purpose event rule that avoids hard coding 31 MAP values. Here is an example:

Code: Select all

on MCPport16 do
 logentry,"The 16-bit port pattern is %eventvalue1%"
 MCPGPIO,16,{substring:0:1:%eventvalue1%}
 MCPGPIO,15,{substring:1:2:%eventvalue1%}
 MCPGPIO,14,{substring:2:3:%eventvalue1%}
 MCPGPIO,13,{substring:3:4:%eventvalue1%}
 MCPGPIO,12,{substring:4:5:%eventvalue1%}
 MCPGPIO,11,{substring:5:6:%eventvalue1%}
 MCPGPIO,10,{substring:6:7:%eventvalue1%}
 MCPGPIO,9,{substring:7:8:%eventvalue1%}
 MCPGPIO,8,{substring:8:9:%eventvalue1%}
 MCPGPIO,7,{substring:9:10:%eventvalue1%}
 MCPGPIO,6,{substring:10:11:%eventvalue1%}
 MCPGPIO,5,{substring:11:12:%eventvalue1%}
 MCPGPIO,4,{substring:12:13:%eventvalue1%}
 MCPGPIO,3,{substring:13:14:%eventvalue1%}
 MCPGPIO,2,{substring:14:15:%eventvalue1%}
 MCPGPIO,1,{substring:15:16:%eventvalue1%}
endon
Then you call this event and pass the 16 bit port value as a binary value. Like this:

Code: Select all

event,MCPport16=1010101010101011
tl;dr: The MCPport16 rule event sets all 16 bits on the MCP23017.

I don't use the MCP23017 so I cannot test this. So you might have to tweak the code to get it to work correctly. Use Serial Log if debugging is necessary.

- Thomas

Zodiac69
Normal user
Posts: 85
Joined: 13 Jun 2016, 17:20

Re: MCP23017: Port control - not Pin control

#9 Post by Zodiac69 » 28 Nov 2020, 07:30

Hi ThomasB

Thank you, i finally managed to implement this and it works.
Thank you again for assistance.

User avatar
ThomasB
Normal user
Posts: 1064
Joined: 17 Jun 2018, 20:41
Location: USA

Re: MCP23017: Port control - not Pin control

#10 Post by ThomasB » 28 Nov 2020, 18:51

You are welcome. It is good to know you resolved the problem.

- Thomas

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 32 guests