more G-Labs products

Author Topic: MCP23017 Inputs / PIFACE ??  (Read 5123 times)

April 07, 2014, 03:43:46 PM
Read 5123 times

baptist

  • **
  • Information
  • Jr. Member
  • Posts: 27
First of all great software  :)

I have managed to get the outputs working with he stock MCP23017 example. Unfortunately not being a C# / python programmer I'm struggling to monitor inputs.

Basically I want 4 outputs and 12 inputs using the MCP23017. Has anyone managed to get the inputs working?. I would much appreciate any advice.

Also has anyone managed to add a Piface to HG ?

April 07, 2014, 03:51:47 PM
Reply #1

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Hi baptist,

in the MCP23017 example all pins are used as output as you can see at line 42 in the code:

Code: [Select]
deviceConnection.SetDirection(mcppin, Mcp23017PinDirection.Output);

to change the direction of a set of pin there should be used Mcp23017PinDirection.Input but also it should be added some code to read the changing state of the input pin in order to update the Status.Level property of the associated module accordingly.

April 07, 2014, 04:53:56 PM
Reply #2

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Hi baptist,

I modified the MCP23017 example so that it can now handle input pins as well.
Disable the current MCP23017 program and import the new one attacched to this message. After importing you'll get a new "Example MCP23017" program next to the old one but witha different pid (>=1000).
In order to activate the new program, you have to:

1) click it in the list
2) clik 'edit program'
3) click 'edit source code'
4) eventually change the pinConfiguration list definitions that is located at the beginning of the code (just change the Direction field of the pins)
5) select Update from Actions menu

After that you'll get new modules of type Switch associated to the output pins and modules of type Sensor associated to the input pins.

Currently pins are configured as follow:

Code: [Select]
var pinConfiguration = new List<dynamic>(){
  new { Address =  "1", Pin = Mcp23017Pin.A0, Direction = Mcp23017PinDirection.Output, Status = false },
  new { Address =  "2", Pin = Mcp23017Pin.A1, Direction = Mcp23017PinDirection.Output, Status = false },
  new { Address =  "3", Pin = Mcp23017Pin.A2, Direction = Mcp23017PinDirection.Output, Status = false },
  new { Address =  "4", Pin = Mcp23017Pin.A3, Direction = Mcp23017PinDirection.Output, Status = false },
  new { Address =  "5", Pin = Mcp23017Pin.A4, Direction = Mcp23017PinDirection.Input, Status = false },
  new { Address =  "6", Pin = Mcp23017Pin.A5, Direction = Mcp23017PinDirection.Input, Status = false },
  new { Address =  "7", Pin = Mcp23017Pin.A6, Direction = Mcp23017PinDirection.Input, Status = false },
  new { Address =  "8", Pin = Mcp23017Pin.A7, Direction = Mcp23017PinDirection.Input, Status = false },
  //
  new { Address =  "9", Pin = Mcp23017Pin.B0, Direction = Mcp23017PinDirection.Input, Status = false },
  new { Address = "10", Pin = Mcp23017Pin.B1, Direction = Mcp23017PinDirection.Input, Status = false },
  new { Address = "11", Pin = Mcp23017Pin.B2, Direction = Mcp23017PinDirection.Input, Status = false },
  new { Address = "12", Pin = Mcp23017Pin.B3, Direction = Mcp23017PinDirection.Input, Status = false },
  new { Address = "13", Pin = Mcp23017Pin.B4, Direction = Mcp23017PinDirection.Input, Status = false },
  new { Address = "14", Pin = Mcp23017Pin.B5, Direction = Mcp23017PinDirection.Input, Status = false },
  new { Address = "15", Pin = Mcp23017Pin.B6, Direction = Mcp23017PinDirection.Input, Status = false },
  new { Address = "16", Pin = Mcp23017Pin.B7, Direction = Mcp23017PinDirection.Input, Status = false }
};

Also you might want to change the polling interval for input pins that is actually set to 500ms (line 110).

I don't have a MCP23017/Piface for testing, so let me know if this is working.

Cheers,
g.
« Last Edit: April 08, 2014, 12:08:26 AM by Gene »

April 07, 2014, 07:20:19 PM
Reply #3

baptist

  • **
  • Information
  • Jr. Member
  • Posts: 27
Thanks so much Gene for your reply, I will let you know how I get on..  :D

April 07, 2014, 11:39:16 PM
Reply #4

baptist

  • **
  • Information
  • Jr. Member
  • Posts: 27
Hi Gene.. sorry but still having issues with the updated code

I receive the following error :(

Errors:

1. Property or indexer `<>__AnonType0.Status' cannot be assigned to (it is read-only)
2. at (wrapper dynamic-method) object.CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object,object) <0x001b8>
3. at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2 (System.Runtime.CompilerServices.CallSite,object,object) <0x0040b>
4. at HomeGenie.Automation.Scripting.ScriptingInstance.RunCode (string) <0x00ed3>
5. at HomeGenie.Automation.Scripting.ScriptingInstance.Run (string) <0x0003f>
Program disabled, fix errors first.

Also do I need to amend the trigger code? Many thanks for your support

April 08, 2014, 12:07:34 AM
Reply #5

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Ok fixed the error.
You don't need to change the Trigger Code.

Have a look at the new version of the GPIO program:
http://www.homegenie.it/forum/index.php?topic=144.0

g.

April 08, 2014, 12:22:07 AM
Reply #6

baptist

  • **
  • Information
  • Jr. Member
  • Posts: 27
Many Thanks .. It works !! :)

April 08, 2014, 12:29:26 AM
Reply #7

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Cool! Thanks for reporting. =)
Please feel free to give any suggestion for improving it.

g.

April 16, 2014, 04:29:21 PM
Reply #8

baptist

  • **
  • Information
  • Jr. Member
  • Posts: 27
Hi Gene..
Is it possible for HG to have >1 MCP23017 connected?
I have tried adding a second MCP23017 which has I have changed an the I2C address to 0x21 (original has 0x20)
Imported a backup of 'MCP23017 GPIO Modules #71'
and amended line 31 -
var deviceConnection = new Mcp23017I2cConnection(driver.Connect(0x21));
However when I run this second program it conflicts with the first?

Do you know where I might be going wrong?

Thanks for all your support..

April 16, 2014, 04:50:35 PM
Reply #9

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
You should also change the Domain for the second MCP23017.
I suggest to change both programs.

In the first one, find and change all occurences of the string "Components.MCP23017" to

"Components.MCP23017.1"

in the second one change to:

"Components.MCP23017.2"

as a better solution would suggest to put this "domain" string into a variable at the top of the code to run:

Code: [Select]
var mcpDomain = "Components.MCP23017.1";

and then replace all occurences with the variable field. So it would be easy to change for future use.

Let me know if this works.
Curious about what you are doing with 32 I/Os :)

Cheers,
g.

April 17, 2014, 12:45:08 AM
Reply #10

baptist

  • **
  • Information
  • Jr. Member
  • Posts: 27
Thanks again Gene. That worked :)
I now have 32 inputs / outputs for my alarm system also hoping to control my central heating in the near future.

June 30, 2014, 10:37:16 PM
Reply #11

Boeky

  • **
  • Information
  • Jr. Member
  • Posts: 31
Hi,


I tried exactly the same but for me this isn't working at all.

Homegenie completely crashed the first time and the In- and Outputs of my first chip become unstable again from the moment I connect the secend 0x21 address.

I've been working with one MCP23017 and the other already wired up for more then two weeks without any problems and even with an uptime of more than one week the inputs remain stable of the first chip.
Now that I duplicated the code and changed everything to A and B the first chip works correctly like before until I launch the second code and try to connect the 0x21 address.
First all my inputs become unstabel triggering at random, then an error on the code of the first chip is given :

Code: [Select]
21:32:17.772 Status.Level 0 16 Components.MCP23017.B
21:32:17.328 Status.Level 1 16 Components.MCP23017.B [b]-> Second Chip and script are working[/b]
21:20:39.231 Program.Status Idle 1000 HomeAutomation.HomeGenie.Automation
21:20:39.221 Runtime.Error CR: BCM2835_I2C_REASON_ERROR_DATA. Missing 1 bytes 1000 HomeAutomation.HomeGenie.Automation
21:20:16.982 Program.Status Disabled 1000 HomeAutomation.HomeGenie.Automation [b]-> Script of first chip crashes, second script
[/b]21:20:04.308 Status.Level 0 5 Components.MCP23017.A
21:20:00.392 Status.Level 1 8 Components.MCP23017.A
21:19:59.862 Status.Level 0 8 Components.MCP23017.A
21:19:55.942 Status.Level 0 4 Components.MCP23017.A
21:19:54.721 Status.Level 0 3 Components.MCP23017.A
21:19:54.566 Status.Level 0 2 Components.MCP23017.A
21:19:54.438 Status.Level 0 1 Components.MCP23017.A
21:19:54.203 Status.Level 1 16 Components.MCP23017.A
21:19:54.37 Status.Level 1 15 Components.MCP23017.A
21:19:53.859 Status.Level 1 14 Components.MCP23017.A
21:19:53.724 Status.Level 1 13 Components.MCP23017.A
21:19:53.576 Status.Level 1 12 Components.MCP23017.A
21:19:53.445 Status.Level 1 11 Components.MCP23017.A
21:19:53.307 Status.Level 1 10 Components.MCP23017.A
21:19:53.166 Status.Level 1 9 Components.MCP23017.A
21:19:53.38 Status.Level 1 8 Components.MCP23017.A
21:19:52.901 Status.Level 1 7 Components.MCP23017.A
21:19:52.773 Status.Level 1 6 Components.MCP23017.A
21:19:52.635 Status.Level 1 5 Components.MCP23017.A
21:19:52.456 Status.Level 1 4 Components.MCP23017.A
21:19:52.328 Status.Level 1 3 Components.MCP23017.A
21:19:52.194 Status.Level 1 2 Components.MCP23017.A
21:19:52.50 Status.Level 1 1 Components.MCP23017.A [b] -> Inputs become unstable of chip A triggering at random
[/b]21:19:51.362 Program.Status Running 1009 HomeAutomation.HomeGenie.Automation [b]-> Second script running
[/b]21:19:49.426 Program.Status Enabled 1009 HomeAutomation.HomeGenie.Automation

Did I miss something?