MCP23017 - read pin state [plugin#mcpgpio#pinstate#1]

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
johndoe39274
Normal user
Posts: 14
Joined: 28 Nov 2021, 21:05
Location: Belgium

MCP23017 - read pin state [plugin#mcpgpio#pinstate#1]

#1 Post by johndoe39274 » 28 Nov 2021, 21:24

Hey,

I'm running ESP Easy on a Wemos D1 mini Pro, connected through MQTT to OpenHAB, and with 2 MCP23017's hooked up through I²C. I can control the inputs and outputs of the MCP's just fine from OpenHAB, so I know my hardware is working.

I am trying to set up a local Rule to trigger on mcp#17 (=MCP 0x21 PA0) and toggle the state of output mcpgpio,1 (=MCP 0x20 PA0) . This is my code:

Code: Select all

on System#Boot do
 Monitor MCPGPIO,17  // monitor pin for state changes
endon

on mcp#17 do
	Publish,%sysname%/Debug/OnMcp17,"on mcp#17 triggered"		//debug message 1
 	Publish,%sysname%/Debug/OnMcp17,[plugin#mcpgpio#pinstate#1]	//debug message 2
 	if [plugin#mcpgpio#pinstate#1]=1	//LED is off
  		Publish,%sysname%/Debug/IfPinState,"[plugin#mcpgpio#pinstate#1] triggered 1"	//debug message 3
  		mcpgpio,1,0        // set LED on
 	endif
 	if [plugin#mcpgpio#pinstate#1]=0	//LED is on
		Publish,%sysname%/Debug/IfPinState,"[plugin#mcpgpio#pinstate#1] triggered 2"	//debug message 4
  		mcpgpio,1,1        // set LED off
	endif
endon
When I press the button mcp#17 I can see debug messages 1 and 2 coming in over MQTT. I do NOT see messages 3 nor 4...

The value of [plugin#mcpgpio#pinstate#1] in debug message 2 is "" , which resolves to some UTF or ASCII encoding for "SOH"... I don"t understand this result. Shouldn't [plugin#mcpgpio#pinstate#1] return a 0 or 1 depending on the pin state low or high?

If not, how can I test if my MCP output pin is high or low so I can toggle it?

Running build ESP_Easy_mega_20211105_normal_beta_ESP8266_16M_LittleFS Nov 5 2021

Thanks in advance!
- John

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MCP23017 - read pin state [plugin#mcpgpio#pinstate#1]

#2 Post by TD-er » 29 Nov 2021, 00:51

Not entirely sure if this makes the difference (have to look into the code, but I decided to go to bed like 15 mins ago....)

See: https://espeasy.readthedocs.io/en/lates ... task-names

[Plugin#MCP#Pinstate#N] to get the pin state of a MCP pin.

You're using not "MCP" but "mcpgpio"
Maybe you can try as is mentioned in the docs?
Also you may also monitor that pin as I'm not sure if the pinstate is known if it isn't monitored.

johndoe39274
Normal user
Posts: 14
Joined: 28 Nov 2021, 21:05
Location: Belgium

Re: MCP23017 - read pin state [plugin#mcpgpio#pinstate#1]

#3 Post by johndoe39274 » 01 Dec 2021, 09:54

Thanks for the reply and for the pointers! Going to bed is never a bad idea :-)

I'm afraid it doesn't work though. Either pin state monitoring doesn't work, or the Pinstate#1 doesn't return an expected value. Given the fact that on mcp#17 do does trigger correctly, I would assume pin monitoring is working.

I tried these variations of the monitor command but with no ultimate success:

Code: Select all

 Monitor,MCP,1  // monitor pin for state changes
 Monitor,MCPGPIO,1  // monitor pin for state changes
 Monitor MCP,1  // monitor pin for state changes
 Monitor,MCPGPIO,1  // monitor pin for state changes
Returns the strange SOL character instead of the expected value of 0 or 1:

Code: Select all

Publish,%sysname%/Debug/Mcp17value,mcp#17
This one triggers correctly:

Code: Select all

on mcp#17 do
Both return the strange SOL character instead of the expected value of 0 or 1:

Code: Select all

 Publish,%sysname%/Debug/OnMcp17,[Plugin#MCP#Pinstate#1]
 Publish,%sysname%/Debug/OnMcp17,[Plugin#MCPGPIO#Pinstate#1]
Both conditions do not pass (same for condition =0 instead of =1):

Code: Select all

 if [Plugin#MCP#Pinstate#1]=1	//LED is off
 if [Plugin#MCPGPIO#Pinstate#1]=1	//LED is off
The documentation seems to confuse between the use of MCP and MCPGPIO. I can't wrap my head around it and make it work.

Would anyone have a snippet of working code for testing pin status with MCP23017 IO please?

leel1967l
Normal user
Posts: 73
Joined: 30 Nov 2017, 18:29

Re: MCP23017 - read pin state [plugin#mcpgpio#pinstate#1]

#4 Post by leel1967l » 02 Dec 2021, 09:17

Hi @johndoe39274,
I am using the 20211105 version and I am using the following snippet:

Code: Select all

on System#Boot do
 mcpgpio,1,1
 monitor,mcp,1
endon

on mcp#1 do
 publish %sysname%/Relay/r1,=[PLUGIN#MCPGPIO#PINSTATE#1]
endon
The only difference I see is that I am using the normal 4M version and not the 16M, beta, LittleFS version.

Please try this:
1) Add the following in the system#boot section:

Code: Select all

Monitor,MCPGPIO,1
2) In the TOOLS page, enter the following command, check the result in the log and report here the results:

Code: Select all

logentry,[plugin#mcpgpio#pinstate#1]
logentry,[plugin#mcpgpio#pinstate#17]
3) Finally, is there a chance you could test the following version: ESP_Easy_mega_20211105_normal_ESP8266_4M1M.bin ?

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

Re: MCP23017 - read pin state [plugin#mcpgpio#pinstate#1]

#5 Post by Ath » 02 Dec 2021, 10:12

leel1967l wrote: 02 Dec 2021, 09:17 Please try this:
1) Add the following in the system#boot section:

Code: Select all

Monitor,MCPGPIO,1
Shouldn't that be like this: ?

Code: Select all

Monitor,MCP,1
leel1967l wrote: 02 Dec 2021, 09:17 3) Finally, is there a chance you could test the following version: ESP_Easy_mega_20211105_normal_ESP8266_4M1M.bin ?
The ESP8266 16M build uses the same Arduino release and compiler flags, so that shouldn't make a difference, other than the file system being LittleFS instead of SPIFFS, and that shouldn't be of any influence on the handling of plugins.
/Ton (PayPal.me)

leel1967l
Normal user
Posts: 73
Joined: 30 Nov 2017, 18:29

Re: MCP23017 - read pin state [plugin#mcpgpio#pinstate#1]

#6 Post by leel1967l » 02 Dec 2021, 15:48

The MONITOR command accepts both MCP or MCPGPIO, it doesn't matter.
I was suggesting to try a diferent bin version, because in my setup the monitor and the [plugin#mcpgpio#pinstate#1] commands work.

johndoe39274
Normal user
Posts: 14
Joined: 28 Nov 2021, 21:05
Location: Belgium

Re: MCP23017 - read pin state [plugin#mcpgpio#pinstate#1]

#7 Post by johndoe39274 » 02 Dec 2021, 16:57

Thanks for the replies! Based on your suggestions and code I did some more troubleshooting. The following code works, but I have no idea why:

Code: Select all

on System#Boot do
 monitor,mcpgpio,17  // monitor pin for state changes
// monitor,mcpgpio,1  // monitor pin for state changes (it works with or without this line)
endon

on mcp#17 do
 if [plugin#mcpgpio#pinstate#17]=0        //button pressed
   if [plugin#mcpgpio#pinstate#1]=1        //LED is off
    mcpgpio,1,0        // set LED on
   else        //LED is on
    mcpgpio,1,1        // set LED off
  endif
 endif
endon

leel1967l
Normal user
Posts: 73
Joined: 30 Nov 2017, 18:29

Re: MCP23017 - read pin state [plugin#mcpgpio#pinstate#1]

#8 Post by leel1967l » 03 Dec 2021, 10:45

Hi
If I am not wrong you cannot have nested if..else..endif.
Maybe @TD-er can clarify this.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: MCP23017 - read pin state [plugin#mcpgpio#pinstate#1]

#9 Post by TD-er » 03 Dec 2021, 10:50

You can have nested if's, only there is a limit to it.

Post Reply

Who is online

Users browsing this forum: No registered users and 34 guests