Controlling MCP23017 with webiopi

3,442 views
Skip to first unread message

Rhys Parker

unread,
Oct 27, 2013, 4:42:06 PM10/27/13
to web...@googlegroups.com
Hi,

I have a GPIO expansion board using an MCP23017 IC. I usually use a python script and i2c to control this, but would like to be able to call functions or scripts vi the webiopi interface.

I have read some documentation on https://code.google.com/p/webiopi/wiki/MCP230xx and have added the DEVICES section to the config file, however im not sure where to add the other section. DO I create my own python script for this? Or do I add it another py script?

If someone could provide me with some documentation or an example script and how to call it, that would be great.

Thanks in advance.
Message has been deleted

Toshi Bass

unread,
Oct 28, 2013, 8:30:21 AM10/28/13
to web...@googlegroups.com
Hi Rhys,

I think you may have completed most of the following according to your post, but for clarity and for every one else following  :-

There are 4 basic steps you need to complete and get your mcp23017 or indeed any expander or sensor working with webiopi.

1st step is of coarse is install webiopi and ensure it works, then for an i2c or spi device mod the following :

sudo nano /etc/modules. add the lines      
i2c-bcm2708
i2c-dev    
CTRL-X, then Y, then Return to save the file and exit.

Then you need to edit the modules blacklist file :

sudo nano /etc/modprobe.d/raspi-blacklist.conf and put a # symbol at the beginning of the two lines so that they look like this :
#blacklist spi-bcm2708    < Note* if you don't plan to use the spi bus then no need to remove the # in this line (it means you do not loose gpio's 7.8.9.10&11).
#blacklist i2c-bcm2708   
CTRL-X, then Y, then Return to save the file and exit.

2nd step amend the following lines in the webiopi config file  

sudo nano etc/webiopi/config     arrow down until you find the following lines and remove the # 

myscript = /home/pi/webiopi/examples/scripts/macros/script.py  

doc-root = /home/pi/webiopi/examples/scripts/macros              

welcome-file = index.html

By doing this you are telling webiopi to load an index.html file and a script.py file from the doc route /home/pi/webiopi/examples/scripts/macros when it starts up.

CTRL-X, then Y, then Return to save the file and exit.

Open script.py file located in /home/pi/webiopi/examples/scripts/macros and delete the following lines twice once in the setup section and once in destroy section :-
    gpio0 = webiopi.deviceInstance("gpio0")
    gpio0.digitalWrite(0, 0)

Save the file then shutdown the pi so that the changes in the config file take effect.

Restart the pi then at the command prompt or in you ssh window start webiopi in debug mode with      sudo webiopi -c /etc/webiopi/config       you will see a scrolling window with stuff like :_

2013-10-xx 19:36:56 - HTTP - DEBUG - "GET /* HTTP/1.1" 200 -

Note* if there are any lines ending in anything other than 200 then you have problem read what the problem is and fix it.

Look for the line:    
2013-10-xx 09:31:59 - WebIOPi - INFO - Loading myscript from /home/pi/webiopi/examples/scripts/macros/script.py      <This line is confirming that the python script.py file in the (doc route) is being loaded 

Open your browser and point to http://xxx.xxx.x.xx:8000  instead of the gpio header window you should see the buttons made by the new index.html file the button for gpio 25 named LED1 should be changing color and if you connect a led (dont forget the resistor) to gpio 25 that will also be going on / off that's the loop in the script.py  file - connecting a led to gpio 24 and pressing the button LED0 on the web page will switch this gpio on / off.

The two files Index.html and script.py are the files you play with to change the operation of what webiopi does and they form the basis of every mod you see on this group so its important to get these working correctly.

3rd step connect the sensor or expander and test it. 

For the  MCP23017 :--

sudo i2cdetect -y 1   you  should see the following:-

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Go back into the webiopi config file:

sudo nano etc/webiopi/config     and arrow down until you get to the [DEVICES] section

add a line mcp0 = MCP23017 slave:0x20   it may already be there, if so remove the # 

Note* if you have a different expander or your adding a sensor say a BMP085 add the appropriate line like Bar0 = BMP085 or remove the # if the line is there already.

CTRL-X, then Y, then Return to save the file and exit.

shut down the pi and restart, when you start webiopi in debug mode with      sudo webiopi -c /etc/webiopi/config    look for the following line:

2013-10-16 09:31:59 - WebIOPi - INFO - GPIOPort - MCP23017(slave=0x20) mapped to REST API /devices/mcp0   (or a line similar confirming whatever expander or sensor you added.) 

To confirm the expander or sensor is working in webiopi, point your browser at   http://xxx.xxx.x.x:8000/app/devices-monitor  there you will see the Device you added, if not, double check your changes in the config file don't forget to shutdown and restart the pi to ensure the any changes take effect

4th step to program the MCP23017  Add the following lines into script.py located at /home/pi/webiopi/examples/scripts/macros/ .

#from webiopi import deviceInstance
mcp0 = deviceInstance("mcp0")     # retrieve device named "mcp0" initiated in config file

To set the function of each of the 16 gpios, in the script loading section use:

# Called by WebIOPi at script loading
def setup():
mcp0.setFunction(x,GPIO.OUT) # or (x.GPIO.IN)       x being 0 to 15
mcp0.setFunction(x,GPIO.OUT) # or (x.GPIO.IN)

To change the state of each of the 16 gpios use:
mcp0.digitalWrite(x, 1) # or 0 or HIGH or LOW       x being 0 to 15

To read the state if each of the 16 gpios use:
x = (mcp0.digitalRead int(x))        x being 0 to 15


# Called by WebIOPi at server shutdown
def destroy():
mcp0.setFunction(x,GPIO.IN
mcp0.setFunction(x,GPIO.IN


5th step not one of the basic steps

To control a MCP23017 output from a button on the web page (assuming you have all the above working)   I use the following  Note* there may be better ways to do this!

Script.py

To add the following in the macro section.   

# To toggle the state of a mcp0 output pin
@webiopi.macro
def setmcp0(number):
    print (number)
    value = not mcp0.digitalRead (int(number))
    mcp0.digitalWrite(int(number), value) 

# To send the current state back to index.html of a mcp0 output pin
@webiopi.macro
def getmcp0(arg0):
    p0 = int(mcp0.digitalRead(0))
    print (p0) 
    return ("%s" % (p0))


index.html
 (This assumes you don't already have any macro called macro0 if you do change all instances of macro0 in the following to macro6 or 10 or something.)

add in the button section:

                // create a button which call MCP0_0 
                button = webiopi().createButton("macro0", "MCP0_0 Off", callMacro_MCP0_0);
content.append(button); // append button to content div
webiopi().setClass("macro0", "OFF"); //set starting state

In the Function section:
Make the function to call the macro in the python script to toggle the state of  (mcp0  output 0)  

function callMacro_MCP0_0(){
webiopi().callMacro("setmcp0", 0); 
callMacro_mcp_State()}

Make the function that will check the state of the output and change the color of the button etc

function callMacro_mcp_State(){
var args = [0]
webiopi().callMacro("getmcp0", args, macro_mcp_State_Callback);}
function macro_mcp_State_Callback(macro, args, data){
p0 = data.split(" ")[0];
if (p0==1){
webiopi().setClass("macro0", "ON");
webiopi().setLabel("macro0", "mcp0 On");}
else{
webiopi().setClass("macro0", "OFF");
webiopi().setLabel("macro0", "mcp Off");}

setInterval ("callMacro_mcp_State()", 3000);{
        }

In the   <style  section add

 .OFF { display: block;
background-color: LightBlue;  
margin: 0px 0px 0px 4px; 
width: 129px; 
height: 40px;
border-radius:10px;
font-size: 8pt; 
font-weight: 600;  }

 .ON  { display: block;
background-color: Yellow;  
margin: 0px 0px 0px 4px; 
width: 129px; 
height: 40px;
border-radius:10px;
font-size: 8pt; 
font-weight: 600;  }

If you get stuck with an error post that you cannot fix and you need help, post a copy of the whole terminal screen showing the error, include the startup bit as well + a copy of your script.py and index.html.

If you read some of the posts on this group you will find variations to this theme adding more buttons and different types of sensors ... 

Please feed back confirming it all works or with any mistakes you found in the above to help others following this..... hope it helps...

Toshi Bass

Rhys Parker

unread,
Oct 28, 2013, 2:22:54 PM10/28/13
to web...@googlegroups.com
Wow. Thanks for taking the time to write such a detailed and easy to follow answer. I now have it work and thanks to you it only took 5 minutes. I did have to move the app folder from htdocs to my new docroot under macros to get the devices monitor working, but besides that the instructions were perfect. Thanks again. Rhys

Toshi Bass

unread,
Oct 28, 2013, 3:26:50 PM10/28/13
to web...@googlegroups.com
Hi Rhys

No problem at all, happy its all working for you, this question keeps coming up so I thought I would spend the time to detail the whole set up for any one with the same same question, glad to be of assistance.

Toshi

Tomasz Śladewski

unread,
Dec 16, 2013, 7:15:23 AM12/16/13
to web...@googlegroups.com
Hello,
I was wondering if web interface will notice state change if the chip's output changes from outside of webiopi? (just like normal gpio's do) ie output change will occur by some other python script.
I'm asking because section 5 is still a mystery to me and I don't have the chip to test it.

Toshi Bass

unread,
Dec 16, 2013, 8:23:05 AM12/16/13
to web...@googlegroups.com
Hi Tomasz

Short answer yes.

Longer answer, in the python script the following function checks the state of the pin 0 of mcp0   (mcp23017 or indeed any GPIO expander you have added and setup)    on a continual basis whilst the webpage is open 
its controlled by the line    setInterval ("callMacro_mcp_State()", 3000);{        } in index.html file.

So in script.py the following is responsible to send the current state of mcp0  pin0 back to index.html
 
@webiopi.macro
def getmcp0(arg0):
    p0 = int(mcp0.digitalRead(0))
    print (p0) 
    return ("%s" % (p0))

so if an external program changed the state of pin 0 of mcp0 it would still be reflected on your webpage even if you started Webiopi after the the state of pin0 was changed, of coarse you can set up the index.html file have 16 buttons and script file to check the state of all 16 pins of say a mcp23017,  detail how to do this are elsewhere on this group, let me know if you cannot find that, 

Note* Once Webiopi is started even when your web page is not open script.py is still running, so for instance say you had a switch connected to an input pin 8 on mcp0 (the mcp23017 port expander) and a led connected to pin 0 and you programmed script.py like  if pin8 == 1 toggle pin0 from 0 to 1 (Off to On) the led would still switch On, then when you open your web page the button for pin0 would say On -- similarly if that program to turn the led On was in a separate python script the result would be the same because def getmcp0(arg0): is just checking and reporting back to the index.html what the state of pin0 on mcp0 is (answer 0 or 1) (Off or On).  .

Hope that's clear

Toshi Bass


Tomasz Śladewski

unread,
Dec 16, 2013, 9:38:56 AM12/16/13
to web...@googlegroups.com
Yes, now it's clear.
Thank you!


--
You received this message because you are subscribed to a topic in the Google Groups "WebIOPi" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webiopi/vt4JMSOFkG4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webiopi+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Eric PTAK

unread,
Dec 16, 2013, 3:11:22 PM12/16/13
to web...@googlegroups.com
Few things :
WebIOPi automatically loads required I2C kernel modules. So you don't need to edit raspi-blacklist. But if you already played with I2C, a reboot is required to get the Pi in a "known state"

If you "just" wanna try your MCP, you don't need all custom script / HTML stuff.
Just edit the [DEVICES] section in /etc/webiopi/config according to your MCP slave address (basically set with wires on the chip).
Start webiopi service, then open the device monitor, you will directly see your MCP I/O.

To resume :
- edit /etc/webiopi/config, add your MCP in [DEVICES]
- reboot the Pi
- /etc/init.d/webiopi start



--
You received this message because you are subscribed to the Google Groups "WebIOPi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webiopi+u...@googlegroups.com.

pobest...@gmail.com

unread,
Dec 26, 2013, 3:45:10 PM12/26/13
to web...@googlegroups.com
Its is possible to change from "switch" to pulse button?
i wanna control wireless power outlets, but im only need pulse out from mcp23017. If i use switch, the remote jump to programm mode.

Eric PTAK

unread,
Dec 26, 2013, 4:02:44 PM12/26/13
to web...@googlegroups.com
nop. sequence is only supported for the native GPIO.



On Thu, Dec 26, 2013 at 9:45 PM, <pobest...@gmail.com> wrote:
Its is possible to change from "switch" to pulse button?
i wanna control wireless power outlets, but im only need pulse out from mcp23017. If i use switch, the remote jump to programm mode.

--

pobest...@gmail.com

unread,
Dec 26, 2013, 4:14:03 PM12/26/13
to web...@googlegroups.com
Thank you for quick answer.
This is problem, hm... i must change design of my idea. i hope that will be added in future

Dne četrtek, 26. december 2013 22:02:44 UTC+1 je oseba trouch napisala:

Eric PTAK

unread,
Dec 26, 2013, 4:32:12 PM12/26/13
to web...@googlegroups.com
Not sure...

MCP230xx driver (as well as all others SPI/I2C devices drivers) is fully written in Python.
The Native GPIO driver is written in C, to access the SoC register.
C allows more precise sleeping than Python.
I need to test and measure speed of different combinaison between C and Python before deciding.

You can actually output sequences by yourself in a macro using mcp.digitalWrite and time.sleep.

Tomasz Śladewski

unread,
Dec 27, 2013, 4:58:24 AM12/27/13
to web...@googlegroups.com
Just an update:
There is a "}" missing at the end of following function posted by Toshi:
function macro_mcp_State_Callback(macro, args, data)
Probably copy/paste mistake.
Btw thank you again Toshi, it works flawlessly!

Toshi Bass

unread,
Dec 27, 2013, 11:21:01 AM12/27/13
to web...@googlegroups.com
Hi Pobest 

Just a quick question are you trying to send a sequence of pulses to control l wireless power outlets via mcp23017 or do you need one pulse   like  (On pause Off) ?

Sorry about missing } it was a paste error..

Toshi


pobest...@gmail.com

unread,
Dec 28, 2013, 8:08:30 AM12/28/13
to web...@googlegroups.com
i need pulse , because i will hack the original remote controler.

i must simulate buttons pressed by finger.

Dne petek, 27. december 2013 17:21:01 UTC+1 je oseba Toshi Bass napisala:

pobest...@gmail.com

unread,
Dec 28, 2013, 8:48:07 AM12/28/13
to web...@googlegroups.com
Is possible to get example with 2 buttons? i have trouble to understand what to add/modify to work with more buttons/ports

Toshi Bass

unread,
Dec 28, 2013, 11:33:41 AM12/28/13
to web...@googlegroups.com
Hi Pobest

Easy way is to toggle your pins On delay Off  would be to do it in your script.py try following this https://groups.google.com/forum/?fromgroups#!topic/webiopi/6b99If6fyJk

Hope it helps

Toshi

pobest...@gmail.com

unread,
Dec 28, 2013, 5:00:33 PM12/28/13
to web...@googlegroups.com
i will try as soon i figure out how to create second button to control pin1 and pin2 on mcp23017.

Dne sobota, 28. december 2013 17:33:41 UTC+1 je oseba Toshi Bass napisala:

Michał Krawczyk

unread,
Jan 10, 2014, 12:49:49 PM1/10/14
to web...@googlegroups.com
Hi guys,

I'm new to RPi and webiopi so I do apologize for lack of knowledge.

I'm trying to create button to trigger pins on MCP23017. Now, I followed Toshi's instruction and everything works up to the last step (python script with macros doesn't give errors - finally). However, I'm stuck on html file, where to add what. Would it be possible for someone to send me his index.html file with single button?

Thanks in advance.

Regards,
Mike

Eric PTAK

unread,
Jan 10, 2014, 12:52:12 PM1/10/14
to web...@googlegroups.com
I will update the wiki this week end with an HTML and Javascript example on the devices tutorials.


--

Michał Krawczyk

unread,
Jan 10, 2014, 2:43:24 PM1/10/14
to web...@googlegroups.com
Brilliant. Can't wait.

Richard Graham

unread,
Jan 12, 2014, 7:13:05 AM1/12/14
to web...@googlegroups.com
Hello. I have been using the GPIO Expander functions to control an MCP23017 via a php script using the REST API. Setting channel function, reading and writing to individual channels have worked ok, but I have encountered a problem with portWrite - POST/devices/name/*/interger/value. 

An example of the statement I am using is /devices/gpio0/*/integer/132 but this returns an error "Error code: 403 Message: invalid literal for int() with base 10: '*'."

I have set the individual channel values to the corresponding binary values (using the /devices/name/channel/value/digit api call) and then executed a portRead (/devices/gpio0/*/integer) and this correctly returns the 'integer' value expected.

Could someone please help with whatever it is I am doing wrong.

Eric PTAK

unread,
Jan 12, 2014, 7:21:24 AM1/12/14
to web...@googlegroups.com
just checked, this is a bug...
added an issue for that

Eric PTAK

unread,
Jan 12, 2014, 7:23:49 AM1/12/14
to web...@googlegroups.com
and don't forget to check the updated device tutorial ;)

Richard Graham

unread,
Jan 12, 2014, 10:14:39 AM1/12/14
to web...@googlegroups.com
Thank you trouch for quick response - I'll implement a plan B!

the webiopi functionality is brilliant - many thanks for your hard work.

Eric PTAK

unread,
Jan 12, 2014, 11:37:43 AM1/12/14
to web...@googlegroups.com
You can use mcp.portRead from a macro ;)

Michał Krawczyk

unread,
Jan 13, 2014, 12:18:48 PM1/13/14
to web...@googlegroups.com
Sadly, I'm still struggling with my test system. What I did so far is python script (see attachment). What it does is it reads MCP23017 port with push button connected and once that's detected it would light up LED connected to another port on MCP23017 (and that works perfectly). Now, I added macros to the script and created html file (see below), but for some reason I don't see anything in the web browser. I would be very grateful if someone could have a look at index.html file (see attachment). I have a hunch I f*cked something up.

script.py
index.html

Toshi Bass

unread,
Jan 17, 2014, 3:49:30 AM1/17/14
to web...@googlegroups.com
Hi Michal

Cannot test this at the moment no pi but I think you have some misplaced } try following:

//function to call the macro in the python script to toggle the state of (mcp0 output 0)
function callMacro_MCP0_0(){
webiopi().callMacro("setmcp0", 0);
       }

      //function that will check the state of the output and change the color of the button etc
      function callMacro_mcp_State(){
var args = [0]
webiopi().callMacro("getmcp0", args, macro_mcp_State_Callback);
      }

      function macro_mcp_State_Callback(macro, args, data){
p0 = data.split(" ")[0];
if (p0==1){
webiopi().setClass("macro0", "ON");
webiopi().setLabel("macro0", "mcp0 On");}
else{
webiopi().setClass("macro0", "OFF");
webiopi().setLabel("macro0", "mcp Off");}
            }

setInterval ("callMacro_mcp_State()", 3000);{
            }
 
Toshi

Michał Krawczyk

unread,
Jan 17, 2014, 9:12:28 AM1/17/14
to web...@googlegroups.com
Thanks for that. I got that right already. There's one more problem I've got with this. For some reason HTTP client doesn't receive the status of the diode. Would you mind have a look at the attached files? There's something messed up with getmcp macro (or JS on client side), but I just can't figure this out... Thanks.
index.html
script.py

Toshi Bass

unread,
Jan 17, 2014, 10:46:19 AM1/17/14
to web...@googlegroups.com

Hi Michal

Cannot test this at the moment no pi, I am on holiday, think script.py looks ok but you still had some misplaced } in html,and you miss refresh line, you have to take more care... try following:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" /> <title>WebIOPi | Demo</title> <script type="text/javascript" src="/webiopi.js"></script> <script type="text/javascript"> webiopi().ready(function() { var content, button; content = $("#content"); // create a button which call MCP0_0 button = webiopi().createButton("macro0", "LED 9 OFF", callMacro_MCP0_0); content.append(button); // append button to content div webiopi().setClass("macro0", "OFF"); //set starting state webiopi().refreshGPIO(true); }); // function to call the macro in the python script to toggle the state of (mcp0 output 0) function callMacro_MCP0_0(){ webiopi().callMacro("setmcp0", 9); callMacro_mcp_State() } // function that will check the state of the output and change the color of the button etc function callMacro_mcp_State(){ var args = [0] webiopi().callMacro("getmcp0", args, macro_mcp_State_Callback); } function macro_mcp_State_Callback(macro, args, data){ p0 = data.split(" ")[0]; if (p0==1){ webiopi().setClass("macro0", "ON"); webiopi().setLabel("macro0", "LED 9 ON");} else{ webiopi().setClass("macro0", "OFF"); webiopi().setLabel("macro0", "LED 9 OFF");} } setInterval ("callMacro_mcp_State()", 3000);{ } </script> <style type="text/css"> button { display: block; margin: 5px 5px 5px 5px; width: 160px; height: 45px; font-size: 24pt; font-weight: bold; color: white; } .OFF { display: block; background-color: black; margin: 0px 0px 0px 4px; width: 129px; height: 40px; border-radius:10px; font-size: 8pt; font-weight: 600; } .ON { display: block; background-color: blue; margin: 0px 0px 0px 4px; width: 129px; height: 40px; border-radius:10px; font-size: 8pt; font-weight: 600; } </style> </head> <body> <table><tr><td id="content">test</td></tr></table> <div id="content" align="center"></div> </body> </html>
Toshi

marti...@ntlworld.com

unread,
Feb 21, 2014, 8:13:48 PM2/21/14
to web...@googlegroups.com
Hi there. I'm new to all this and I had pretty much done some of the above, with the exception of editing the config file to suit the i2C.

Can anyone confirm if the information contained within this post is the same for MCP23008??

Regards

Martin.



On Sunday, 27 October 2013 20:42:06 UTC, Rhys Parker wrote:
Hi,

I have a GPIO expansion board using an MCP23017 IC. I usually use a python script and i2c to control this, but would like to be able to call functions or scripts vi the webiopi interface.

I have read some documentation on https://code.google.com/p/webiopi/wiki/MCP230xx and have added the DEVICES section to the config file, however im not sure where to add the other section. DO I create my own python script for this? Or do I add it another py script?

If someone could provide me with some documentation or an example script and how to call it, that would be great.

Thanks in advance.

Toshi Bass

unread,
Feb 22, 2014, 9:25:15 AM2/22/14
to web...@googlegroups.com
Hi Martin

Yes it should work fine for MCP23008 just make sure your expander is working in device monitor first, and you called your expander mcp0

Or change the following line in script.py to suit the name you called your expander in the config file

mcp0 = deviceInstance("XXXX")          # retrieve device named "XXXX" in configuration file

After you have it working you may want to try this different method which does the same same thing but without calls to script.py https://groups.google.com/d/msg/webiopi/dOX2L6QUp5A/Yl2JKZh_hWcJ

Toshi

Michał Krawczyk

unread,
Feb 22, 2014, 7:07:48 PM2/22/14
to web...@googlegroups.com

And also expander ports will go from 0 to 7. Everything else will be the same.

--
You received this message because you are subscribed to a topic in the Google Groups "WebIOPi" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webiopi/vt4JMSOFkG4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webiopi+u...@googlegroups.com.

marti...@ntlworld.com

unread,
Feb 24, 2014, 2:08:52 PM2/24/14
to web...@googlegroups.com
Hi Toshi

I managed to get myself in a bit of a pickle with all this so completely wiped the memory card and started again.

I should point out that the Expander I am using is the Custard Pi 7. Which has 3 MCP23008 chips and a   PFC8591 (0x4F), The MCP23008 that I am attempting to control has a default address of 0x27( add 7). MCP 23008 0x25 (add5) is for connecting an LCD display to and MCP23008 0x26 (add6) are GPIO pins, MCP23008 with the address of 7 has 4 open collector outputs, 2 relays and 2 inputs. Which can be seen when entering the command line that displays what is connected to the bus. However, when running the debug command line I get allsorts of errors I don't understand.

Firstly, I am therefore assuming where is says MCP0 in the following tutorial, this should actually refer to MCP7. And likewise the Import should be IMPORT MCP23008 slave 0x27in the script file. If I were to use MCP23017 I would get 16 bits as opposed to 8.

Am I on the right lines with this or completely wrong?


Martin.

Eric PTAK

unread,
Feb 24, 2014, 2:21:01 PM2/24/14
to web...@googlegroups.com

Send config, code and log

--
You received this message because you are subscribed to the Google Groups "WebIOPi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webiopi+u...@googlegroups.com.

Toshi Bass

unread,
Feb 24, 2014, 2:47:07 PM2/24/14
to web...@googlegroups.com

Hi Martin

I do not know anything about custard pi however you should try the following in the config file   sudo nano /etc/webiopi/config  add the following lines in the devices section :

mcp5 = mcp23008 slave:0x25
mcp6 = mcp23008 slave:0x26
mcp7 = mcp23008 slave:0x27

then check if you can see those 3 expanders in the device monitor, if you can then in script.py change the following :

mcp0 = webiopi.deviceInstance("mcp7") ## retrieve device named "mcp7" in configuration file

then change all instances of LED_9 to say LED_7 in both script.py and index.html - as Michel said the expander pins will be 0 - 7 not 0 - 15 on a mcp23017

Then your button should change the state on pin 7 on the expander 0x27

While I was writing this Saw Trouch's post and agree if after trying this you still have problems post code and log and also config file.

Toshi


marti...@ntlworld.com

unread,
Feb 24, 2014, 3:09:01 PM2/24/14
to web...@googlegroups.com
I was so nearly there. It was the bus addressing I messed up. At the moment, all I want to do is make the new button created in the html file turn on mcp7 bit 4 which effectively is relay 1

marti...@ntlworld.com

unread,
Feb 24, 2014, 4:52:09 PM2/24/14
to web...@googlegroups.com
Ok... It's kind of working....

In the devices monitor I can now see all 3 Expanders. I have added these to the config file and edited the mcp0 as mcp7 to avoid any later confusion. I have tested the devices monitor app on the appropriate outputs.

My Inputs and Outputs are as follows.

Ch1  Input 1 (Normally HIGH is actually OFF)     0
Ch2  Input 2 (Normally HIGH is actually OFF)     1
Ch3  Open Collector Output 1                           2
Ch4  Open Collector Output 2                           3
Ch5  Open Collector Output 3                           4
Ch6  Open Collector Output 4                           5
Ch7  On Board Relay 1                                    6
Ch8  On Board Relay 2                                    7

Other than a couple of latching issues all channels operate in the correct place. I have installed remote desktop on the pi. If you could forward an email to where i can send the log in details I can allow you to log on directly.

I have attached my script.py and index.html file.  I am not sure where the webiopi config and log files are stored.

I hope the attached highlights my errors.

Once again thanks guys for your help.
index.html
script.py

Michał Krawczyk

unread,
Feb 24, 2014, 5:40:40 PM2/24/14
to web...@googlegroups.com

Run webiopi in debug mode from bash and copy what it produces. This would be the easiest way.

--
You received this message because you are subscribed to a topic in the Google Groups "WebIOPi" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webiopi/vt4JMSOFkG4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webiopi+u...@googlegroups.com.

Toshi Bass

unread,
Feb 24, 2014, 5:46:43 PM2/24/14
to web...@googlegroups.com
Hi Marti

I suggested you change line  mcp0 = webiopi.deviceInstance("mcp7") because then the whole thing would have probably worked however I understand why you changed it to mcp7 = deviceInstance("mcp7") because its more aesthetically correct, it just means you have to make sure you changed all instance of mcp0 in the index file

So going through your code (for 1 button)

Script.py looks fine except for :

@webiopi.macro
def getmcp7(arg0):
    p0 = int(mcp7.digitalRead(7))
    p1 = int(mcp7.digitalRead(8))
    print (p0)
    print (p1)
    return ("%s" % (p0))
    return ("%s" % (p1))

For 1 button pin 4 you need 

@webiopi.macro
def getmcp7(arg0):
    p0 = int(mcp7.digitalRead(4))
    print (p0)
    return ("%s" % (p0))

Index.html

Change the following in red bold

     		//create a button which calls MCP7_4
		button = webiopi().createButton("macro4, "MCP7_4 On", callMacro_MCP7_4);
		content.append(button); //append button to content div
		webiopi().setClass("macro4","On"); //set starting state




	function callMacro_MCP7_4(){
		webiopi().callMacro("setmcp7",4);
		callMacro_mcp_State()
	}
        // Call the macro getmcp7 in script.py and return in callback 0 or 1
	function callMacro_mcp_State(){
		var args = [0] // think like arg0, arg1, arg2 etc
		webiopi().callMacro("getmcp7", args, macro_mcp_State_Callback);
	}
        // check returned 0 or 1 from above function and assign On or Off color and text 
	function macro_mcp_State_Callback(macro, args, data){
		p0 = data.split(" ")[0];
		if (p0==1){
			webiopi().setClass("macro4", "ON");
			webiopi().setLabel("macro4", "mcp7_4 On");
		}	
		else{
			webiopi().setClass("macro4", "OFF");
			webiopi().setLabel("macro4", "mcp7_4 Off");
		}
	setInterval ("callMacro_mcp_State()", 3000);{
	}

In script.py 
If you want to check more than 1 pin you would add code like this 

@webiopi.macro
def getmcp7(arg0, arg1, arg2):
    p0 = int(mcp7.digitalRead(0))
    p1 = int(mcp7.digitalRead(1))
    p2 = int(mcp7.digitalRead(2))
print (p0, p1, p2) return ("%s %s %s" % (p0, p1, p2))

and check them in same way in Index.html

Hope it helps

Toshi 

Michał Krawczyk

unread,
Feb 24, 2014, 5:51:35 PM2/24/14
to web...@googlegroups.com

Toshi,
Just out of curiosity as I can't figure it out. What does '%s' % do in macro?

Eric PTAK

unread,
Feb 24, 2014, 6:00:37 PM2/24/14
to web...@googlegroups.com

String Formatting Operator:

One of Python's coolest features is the string format operator %. This operator is unique to strings and makes up for the pack of having functions from C's printf() family. Following is a simple example:

#!/usr/bin/python

print "My name is %s and weight is %d kg!" % ('Zara', 21) 

When the above code is executed, it produces the following result:

My name is Zara and weight is 21 kg!

Here is the list of complete set of symbols which can be used along with %:


more at http://www.tutorialspoint.com/python/python_strings.htm

--
You received this message because you are subscribed to the Google Groups "WebIOPi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webiopi+u...@googlegroups.com.

Toshi Bass

unread,
Feb 24, 2014, 6:01:13 PM2/24/14
to web...@googlegroups.com
Embeds the values of p0 p1 p2 etc into a string - then your sending that string to index.html which then reads each element of the string and puts the result back into p0 p1 p2 etc for more info try googling What does '%s' % do

Toshi 

Michał Krawczyk

unread,
Feb 24, 2014, 6:12:44 PM2/24/14
to web...@googlegroups.com

That's cool. Thanks boys. I'll test it when I get the chance.

On 24 Feb 2014 23:02, "Toshi Bass" <toshib...@gmail.com> wrote:
Embeds the values of p0 p1 p2 etc into a string - then your sending that string to index.html which then reads each element of the string and puts the result back into p0 p1 p2 etc for more info try googling What does '%s' % do

Toshi 

--

Michał Krawczyk

unread,
Feb 24, 2014, 6:22:49 PM2/24/14
to web...@googlegroups.com

I've got question about HTML side. What I'm testing at the moment is one physical button controlling one diode and one software button (controlling diode and indicating its status on or off). Everything works fine until I turn the diode on with physical button before refreshing HTML. In this case diode is lid on but software button does not reflect this on HTML. Is this normal behaviour of software button or should it get diode status straight away?

Martin Workman

unread,
Feb 24, 2014, 7:00:42 PM2/24/14
to web...@googlegroups.com

I too noticed that Michal..... What I intend to do at a later date is... after establishing my output change... electronically create a new input on my GPIO inputs to drive the change of colour to the button that created the condition.

 

I can replicate your issue at the moment using the devices monitor. So, I too would be interested in knowing how to resolve that issue.

 

Martin

No virus found in this message.
Checked by AVG - www.avg.com
Version: 2014.0.4259 / Virus Database: 3705/7121 - Release Date: 02/24/14

Toshi Bass

unread,
Feb 24, 2014, 7:10:20 PM2/24/14
to web...@googlegroups.com

Martin

You have the right idea You don't put voltage onto you led connected to an pin set to output you apply voltage to an input pin and then in software check the input when it turns from ) to 1 then switch the output from 0 to 1 but I suggest you research how to connect a physical switch to a input because you should limit the current flow and tie the pin high or low or you will have a floating input when no voltage is present resulting in sometimes 1 sometimes 0

Toshi 

Martin Workman

unread,
Feb 24, 2014, 7:19:18 PM2/24/14
to web...@googlegroups.com

Whilst I am switching a relay, I could also switch one of the open collector outputs and feed that back in via a 100K resistor to pull the GPIO pin or add an external relay  triggered by the on-board relay then trigger one of the GPIO via a 100K resistor to 0V.

 

Either is possible. Now that I have an understanding of the MCP23008 I am beginning to like this add-on board. I’ve yet to correct my syntax and try things out. But hey.. it’s all part of the learning process. I’m in my late 40’s now and my concentration span isn’t what it was. When I was in college back in 1987 I only got as far as the TTL 74 series logic gates. I’m quite excited about what is actually achievable with change out of £50 worth of bits and some priceless input from you guys!

 

Martin

 

From: web...@googlegroups.com [mailto:web...@googlegroups.com] On Behalf Of Toshi Bass
Sent: 25 February 2014 00:10
To: web...@googlegroups.com
Subject: Re: Controlling MCP23017 with webiopi

 

Martin

You have the right idea You don't put voltage onto you led connected to an pin set to output you apply voltage to an input pin and then in software check the input when it turns from ) to 1 then switch the output from 0 to 1 but I suggest you research how to connect a physical switch to a input because you should limit the current flow and tie the pin high or low or you will have a floating input when no voltage is present resulting in sometimes 1 sometimes 0

 

Toshi 

--

You received this message because you are subscribed to a topic in the Google Groups "WebIOPi" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webiopi/vt4JMSOFkG4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webiopi+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Martin Workman

unread,
Feb 25, 2014, 12:16:31 PM2/25/14
to web...@googlegroups.com

Hi Toshi

 

All corrected other than using 4. I’m using 7 which is relay 1.

 

After rebooting the pi and opening the webpage http://192.168.1.51:8008 (8008 Because that’s where i wanted it so it wouldn’t collide with other port forwards I have in place) I get the log in, I haven’t changed from default. After entering the details and clicking on log in I’m getting just a blank page. I have viewed the source of the page and all seems to be fine.

 

By using my DDNS... The address is http://readingfire.zapto.org:8008 I get the same result.

 

Have you any ideas?

 

 

From: web...@googlegroups.com [mailto:web...@googlegroups.com] On Behalf Of Toshi Bass
Sent: 24 February 2014 22:47
To: web...@googlegroups.com
Subject: Re: Controlling MCP23017 with webiopi

 

Hi Marti

--

You received this message because you are subscribed to a topic in the Google Groups "WebIOPi" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webiopi/vt4JMSOFkG4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webiopi+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Toshi Bass

unread,
Feb 25, 2014, 1:47:41 PM2/25/14
to web...@googlegroups.com
Marti

To help trace fault in html using chrome, go top right icon like 3 bars - Tools - Javascript console and of course you are running webiopi in debug mode so you can see en errors it throws up    sudo webiopi -d -c /etc/webiopi/config

Toshi

Martin Workman

unread,
Feb 25, 2014, 3:47:23 PM2/25/14
to web...@googlegroups.com

I have one error...

 

 

Uncaught Syntax Error.

 

In debug within the terminal window I have a few lines that error. Error 98 Address already in use.

 

 

From: web...@googlegroups.com [mailto:web...@googlegroups.com] On Behalf Of Toshi Bass
Sent: 25 February 2014 18:48
To: web...@googlegroups.com
Subject: Re: Controlling MCP23017 with webiopi

 

Marti

 

To help trace fault in html using chrome, go top right icon like 3 bars - Tools - Javascript console and of course you are running webiopi in debug mode so you can see en errors it throws up    sudo webiopi -d -c /etc/webiopi/config

 

Toshi

--

You received this message because you are subscribed to a topic in the Google Groups "WebIOPi" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webiopi/vt4JMSOFkG4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webiopi+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No virus found in this message.
Checked by AVG - www.avg.com

Version: 2014.0.4259 / Virus Database: 3705/7124 - Release Date: 02/25/14

Message has been deleted

Toshi Bass

unread,
Feb 25, 2014, 5:10:13 PM2/25/14
to web...@googlegroups.com
Marti

Usualy Error 98 Address already in use. means you already have an instance of webiopi running so you cannot start another one, try stopping webiopi then starting it again or worst case reboot the pi, I say usualy because I have no idea what effect using 8080 will have,

Re syntax error there should be more info than that like line number were the error is, if your stuck post the entire screen.

Toshi

Eric PTAK

unread,
Feb 25, 2014, 5:36:45 PM2/25/14
to web...@googlegroups.com
You can have a webiopi instance running on port 8000 or anything else.
to know what, use netstat and ps :

pi@pidev ~ $ sudo netstat -antp | grep 8000

tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      10477/python3   

pi@pidev ~ $ ps aux | grep 10477

root     10477  0.1  2.7  42420 12232 ?        Sl   Feb24   1:51 /usr/bin/python3 -m webiopi -d -l /var/log/webiopi -c /etc/webiopi/config


it may happen you just have to stop the background service :

sudo /etc/init.d/webiopi stop

sometimes, it crashed and do not respond, so you need to kill it.

$ sudo kill -9 10477

where 10477 refers to the process ID (PID) found with netstat/ps commands.



Toshi

--
You received this message because you are subscribed to the Google Groups "WebIOPi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webiopi+u...@googlegroups.com.

Martin Workman

unread,
Feb 26, 2014, 10:26:01 AM2/26/14
to web...@googlegroups.com

I have stopped the service starting upon boot. I have also changed the port back to 8000. Netstat does not produce anything either on Port 8000 or 8008.

 

pi@raspberrypi ~ $ sudo webiopi –d  -c /etc/webiopi/config

 

File”/usr/local/lib/python3.2/dist-packages/WebIOpi-0.6.0-py3.2.linux-armv6l.egg/webiopi/__main__.py”, line 73, in <module>

            main(sys.argv)

 

File”/usr/local/lib/python3.2/dist-packages/WebIOpi-0.6.0-py3.2.linux-armv6l.egg/webiopi/__main__.py”, line 67, in main

            Server = Server(port=port, configfile=configfile)

 

File”/usr/local/lib/python3.2/dist-packages/WebIOpi-0.6.0-py3.2.linux-armv6l.egg/webiopi/serv er.py,  line 146, in__init__

            Self.http_server = http.HttpServer(self.host, http_port, self.restHandler, context, docroot, index, auth)

 

File”/usr/local/lib/python3.2/dist-packages/WebIOpi-0.6.0-py3.2.linux-armv6l.egg/webiopi/protocols/http.py”, line 38, in __init__

            BaseHTTPServer.HttpServer.__init__(self, (“”, port), HttpHandler)

 

File”/usr/local/lib/python3.2/socketserver.py” line 419 in __init__

            self.server_bind()

 

File”/usr/local/lib/python3.2/http/server.py”, line 132 in server_bind

            Socketserver.TCPServer.server(self)

 

File”/usr/local/lib/python3.2/socketserver.py” line 430, in server_bind

            self.socket.bind(self.server_address)

 

socket.error: [Errno 98] Address already in use

 

 

 

 

 

 

 

 

 

 

From: web...@googlegroups.com [mailto:web...@googlegroups.com] On Behalf Of Eric PTAK
Sent: 25 February 2014 22:37
To: web...@googlegroups.com
Subject: Re: Controlling MCP23017 with webiopi

 

You can have a webiopi instance running on port 8000 or anything else.

--
You received this message because you are subscribed to a topic in the Google Groups "WebIOPi" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webiopi/vt4JMSOFkG4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webiopi+u...@googlegroups.com.

Martin Workman

unread,
Feb 26, 2014, 10:33:16 AM2/26/14
to web...@googlegroups.com

Also from the javascript console I get:

 

Uncaught SyntaxError: Unexpected identifier

Toshi Bass

unread,
Feb 26, 2014, 12:39:33 PM2/26/14
to web...@googlegroups.com

Martin

In the javascript console follow the line to the right hand side you should see line number were error is, also you can click Sources that should show you the line were problem is.

Uncaught SyntaxError: Unexpected identifier

 if you cannot spot it Post your index.html file 

 Re your "address already in use" maybe Trouch can spot something from your post I don't know.


Toshi 

Martin Workman

unread,
Feb 26, 2014, 1:26:44 PM2/26/14
to web...@googlegroups.com

On the right hand side it says 192.168.1.51/:22

 

Is this referring to line 22 within index.html?

 

Martin

 

 

From: web...@googlegroups.com [mailto:web...@googlegroups.com] On Behalf Of Toshi Bass
Sent: 26 February 2014 17:40
To: web...@googlegroups.com
Subject: Re: Controlling MCP23017 with webiopi

 


Martin

--

You received this message because you are subscribed to a topic in the Google Groups "WebIOPi" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webiopi/vt4JMSOFkG4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webiopi+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Toshi Bass

unread,
Feb 26, 2014, 1:51:37 PM2/26/14
to web...@googlegroups.com
Martin

I am not sure about that, I dont recall ever seeing an ip address however did you press Sources ?

I am haveing some serious connection dropouts at the moment here is my console, you see sources in the top line select that and it will take you (funnily enough) to the source of the problem (with a bit of luck

you can also see were my index.html failed (my index.html file is in directory wip1: and the line number were it failed was 338  and also 325 

Toshi
Capture0.JPG

Martin Workman

unread,
Feb 26, 2014, 2:33:00 PM2/26/14
to web...@googlegroups.com

Weird.

 

I did exactly the same as you. And the previous was all it produced.

 

Just incase I have messed up my html file after editing... I’ll attach it again.

 

MCP7 ( 23008 ) Pin 7 The relay I am trying to switch

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<meta name="viewport" content = "height = device-height, width = 420, user-scalable = no" />

<title>WebIOPi | Demo</title>

<script type="text/javascript" src="/webiopi.js"></script>

<script type="text/javascript">

webiopi().ready(function() {

var content, button;

content = $("#content");

// create a "SWITCH" labeled button for GPIO 21

button = webiopi().createGPIOButton(21, "SWITCH");

content.append(button); // append button to content div

// create a "LED" labeled button for GPIO 25

button = webiopi().createGPIOButton(25, "LED1");

content.append(button); // append button to content div

//create a button which calls MCP7_7

button = webiopi().createButton("macro7, "MCP7_7 On", callMacro_MCP7_7);

content.append(button); //append button to content div

webiopi().setClass("macro7","On"); //set starting state

// create a button that outputs a single pulse

button = webiopi().createPulseButton("pulse", "Pulse", 25);

content.append(button); // append button to content div

// create a button which output a bit sequence on GPIO 25 with a 100ms period

button = webiopi().createSequenceButton("sos", "S.O.S 1", 25, 100, "01010100110011001100101010");

content.append(button); // append button to content div

// the previous button will always output the same sequence

// you can also create a simple button with your own function

button = webiopi().createButton("sos2", "S.O.S 2", outputSequence);

content.append(button); // append button to content div

// create a button which call PrintTime

button = webiopi().createMacroButton("macro", "Print Time", "PrintTime");

content.append(button); // append button to content div

// create a button which call HelloWorld with "User,Name" argument

button = webiopi().createMacroButton("macro", "Hello ?", "HelloWorld", ["User", "Name"]);

content.append(button); // append button to content div

// the previous button will always call HelloWorld with "User,Name" argument

// you can also create a simple button with your own function

button = webiopi().createButton("macro2", "Hello !", callMacro);

content.append(button); // append button to content div

// you can also create a button which calls a different function for mouse down and up events

button = webiopi().createButton("hold", "Hold", mousedown, mouseup);

content.append(button);

// Only for Chrome and Safari, create a slider that pulse out a -45 to +45° angle on GPIO 23

button = webiopi().createAngleSlider(23);

content.append(button);

// Only for Chrome and Safari, create a slider that pulse out a 0-100% duty cycle ratio on GPIO 24

button = webiopi().createRatioSlider(24);

content.append(button);

webiopi().refreshGPIO(true);

});

function mousedown() {

webiopi().setValue(25, 1);

}

function mouseup() {

webiopi().setValue(25, 0);

}

function outputSequence() {

var sequence = "01010100110011001100101010" // S.O.S. morse code or whatever you want

// output sequence on gpio 25 with a 100ms period

webiopi().outputSequence(25, 100, sequence, sequenceCallback);

}

function sequenceCallback(gpio, data) {

alert("sequence on " + gpio + " finished with " + data);

}

function callMacro() {

var args = ["User","Name"] // or whatever you want

// call HelloWorld(args)

webiopi().callMacro("HelloWorld", args, macroCallback);

}

function macroCallback(macro, args, data) {

alert(data);

}

function callMacro_MCP7_7(){

webiopi().callMacro("setmcp7",7);

callMacro_mcp_State()

}

function callMacro_mcp_State(){

var args = [0]

webiopi().callMacro("getmcp7", args, macro_mcp_State_Callback);

}

function macro_mcp_State_Callback(macro, args, data){

p0 = data.split(" ")[0];

if (p0==1){

webiopi().setClass("macro7", "ON");

webiopi().setLabel("macro7", "mcp7_7 On");

}

else{

webiopi().setClass("macro7", "OFF");

webiopi().setLabel("macro7", "mcp7_7 Off");

}

setInterval ("callMacro_mcp_State()", 3000);{

}

</script>

<style type="text/css">

button {

display: block;

margin: 5px 5px 5px 5px;

width: 160px;

height: 45px;

font-size: 24pt;

font-weight: bold;

color: black;

}

input[type="range"] {

display: block;

width: 160px;

height: 45px;

}

.LOW {

background-color: White;

}

.HIGH {

background-color: Red;

}

.OFF { display: block;

background-color: Red;

margin: 0px 0px 0px 4px;

width: 129px;

height: 40px;

border-radius: 10px;

font-size: 8pt;

font-weight: 600; }

.ON { display: block;

background-color: Green;

margin: 0px 0px 0px 4px;

width: 129px;

height: 40px;

border-radius: 10px;

font-size: 8pt;

font-weight: 600; }

</style>

</head>

<body>

<div id="content" align="center"></div>

</body>

</html>

 

 

From: web...@googlegroups.com [mailto:web...@googlegroups.com] On Behalf Of Toshi Bass
Sent: 26 February 2014 18:52
To: web...@googlegroups.com
Subject: Re: Controlling MCP23017 with webiopi

 

Martin

--

Toshi Bass

unread,
Feb 26, 2014, 3:19:48 PM2/26/14
to web...@googlegroups.com

Martin,


I think this is my fault your missing a    } 

function macro_mcp_State_Callback(macro, args, data){

    p0 = data.split(" ")[0];

        if (p0==1){

        webiopi().setClass("macro7", "ON");

        webiopi().setLabel("macro7", "mcp7_7 On");

      

        }

    else{

        webiopi().setClass("macro7", "OFF");

        webiopi().setLabel("macro7", "mcp7_7 Off");

    }

}      // <<<<here

setInterval ("callMacro_mcp_State()", 3000);{

}


Think that's all

Toshi

Martin Workman

unread,
Feb 26, 2014, 6:53:00 PM2/26/14
to web...@googlegroups.com

Many many thanks Toshi...

 

You were right regarding the { I had also added a where I did not need one.

I now have a Switch and an LED box on the page.

 

However.... I now need to define “macro7”

 

I’m going to assume that this is done in script.py file with the following syntax...

 

@webiopi.macro

def setmcp7(number):

                print (number)

                value = not mcp7.digitalread(int(number))

                mcp7.digitalWrite (int(number), value)

 

Whereby the highlighted parts are the macro number? And the value is either “on” or “off”

 

Am I correct?

 

Martin

 

 

 

From: web...@googlegroups.com [mailto:web...@googlegroups.com] On Behalf Of Toshi Bass
Sent: 26 February 2014 20:20
To: web...@googlegroups.com
Subject: Re: Controlling MCP23017 with webiopi

 

Martin,

--

You received this message because you are subscribed to a topic in the Google Groups "WebIOPi" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webiopi/vt4JMSOFkG4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webiopi+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No virus found in this message.
Checked by AVG - www.avg.com

Version: 2014.0.4259 / Virus Database: 3705/7127 - Release Date: 02/26/14

Toshi Bass

unread,
Feb 27, 2014, 1:04:28 AM2/27/14
to web...@googlegroups.com
Hi Marin

If I am reading your post correctly I think your mistaken you dont need to change anything at all in script.py . pressing your button should be energising your relay now pressing it again should be de-energising it.

index.html

// This function sends "number" 7 to def setmcp7 in script.py
function callMacro_MCP7_4(){
webiopi().callMacro("setmcp7",7);
callMacro_mcp_State()
}
 
so if you had several buttons you would have several of these functions all sending like 6 or 5 etc to def setmcp7 in script,py so what ever number you send will be in the variable called (number) so in your case its 7


script.py

@webiopi.macro
def setmcp7(number):
#var (number) will contain the number you sent from the function in your case 7
print (number)
# print (number) will print 7 on the debug screen
value = not mcp7.digitalread(int(number))
# this line reads the state of pin 7 say 1 and puts it in the var value
    # having bool "not" in that line means var value will be set to 0 not 1
     mcp7.digitalWrite (int(number), value)
# this line writes the var value (0) to var number (7) to mcp7

The next time you press the button for 7 the value read will be 0 so the bool "not" makes var value = 1 and that gets writern to pin 7 and so On Off On Off with each press

If you had sent number say 2 from a function in index.html file then def setmcp7 would have done all that to pin 2 insted of pin 7 with the same code just by changing the contents of variables "number" and "value"

Hope I make it clear

Toshi

 

Martin Workman

unread,
Feb 27, 2014, 3:37:48 PM2/27/14
to web...@googlegroups.com
Toshi,

Thank you for your continued patience. Once we have this sorted I only need to replicate it once.

The problem seems to be in the html file. With line 22 which reads:

button = webiopi().createButton(macro7, "MCP7_7 On", callMacro_MCP7_7);

The error in the browser javascript console says Macro not defined.........
The rest is as you have it

Martin



-----Original Message-----
From: web...@googlegroups.com [mailto:web...@googlegroups.com] On Behalf Of Toshi Bass
Sent: 27 February 2014 06:04
To: web...@googlegroups.com
Subject: Re: Controlling MCP23017 with webiopi

--
You received this message because you are subscribed to a topic in the Google Groups "WebIOPi" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webiopi/vt4JMSOFkG4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webiopi+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


-----

Martin Workman

unread,
Feb 27, 2014, 3:45:48 PM2/27/14
to web...@googlegroups.com
Hold that.... :) :) :)

I have found the issue, macro7 should have read "macro7"

The button works and switches the relay on. However it does not switch it off again and i think i know why. I'm going to play a little more.




-----Original Message-----
From: web...@googlegroups.com [mailto:web...@googlegroups.com] On Behalf Of Martin Workman
Sent: 27 February 2014 20:38
To: web...@googlegroups.com
Subject: RE: Controlling MCP23017 with webiopi

Version: 2014.0.4259 / Virus Database: 3705/7130 - Release Date: 02/27/14

Giuseppe di Fede

unread,
Aug 13, 2014, 5:59:38 PM8/13/14
to web...@googlegroups.com
Hi guys,

Can you tell me if you were able to use the PCA9555 like I2C expander port?
is the driver shared with MCO23017 or another one?
I am not able to use this device.

Thanks a lot in advance,
Peppe.

On Sunday, 27 October 2013 21:42:06 UTC+1, Rhys Parker wrote:
Hi,

I have a GPIO expansion board using an MCP23017 IC. I usually use a python script and i2c to control this, but would like to be able to call functions or scripts vi the webiopi interface.

I have read some documentation on https://code.google.com/p/webiopi/wiki/MCP230xx and have added the DEVICES section to the config file, however im not sure where to add the other section. DO I create my own python script for this? Or do I add it another py script?

If someone could provide me with some documentation or an example script and how to call it, that would be great.

Thanks in advance.

Andreas Riegg

unread,
Aug 14, 2014, 4:06:03 AM8/14/14
to web...@googlegroups.com
Giuseppe,

sorry, there is currently no driver available for PCA9555. As the MCP chips are from another manufacturer it is very unlikely that you can use the driver for MCP23017 for it.

There is a driver for PCF8574 which is from the same manufacturer and also has digital I/O ports, but that is a 8 bit one and it has quasi-bidirectional ports whereas the PCA9555 has not. Due to the quasi-bidirectional nature an extension of the PCF8475 to 16 bit will not be sufficient as the code to handle the IO direction register will miss.

As a bottom line, you will need a dedicated driver for PCA9555 (and optional its cousins PCA9554, PCA9556, PCA9557, PCA9558) that works for these NXP chips and handles the IO control register correct. You may be able to derive/copy that from MCP23xxx code, but it can't be subclasses of the MCP drivers, they should start as direct subclasses of I2C, GPIOPort within an own driver file.

Andreas

Giuseppe di Fede

unread,
Aug 14, 2014, 5:20:52 AM8/14/14
to web...@googlegroups.com
Hi Andreas and thank a lot for your answer.
So it seems I have to change device in order to se it with GPIO, right?

Andreas Riegg

unread,
Aug 14, 2014, 6:10:33 AM8/14/14
to web...@googlegroups.com
Yes, using a MCP23017 chip instead (DIL version looks to be available around 1 - 2 € on market) is the most simple and most stable solution.

Developing an new driver is not rocket science with my driver guide, but it will cost around 2-5 days effort (incl. intensive testing) even for experienced folks.

Andreas

Giuseppe di Fede

unread,
Aug 14, 2014, 9:28:41 AM8/14/14
to web...@googlegroups.com

Hi Andreas,
Please can you share your driver guide with me?

Thanks in avance,
Giuseppe

--
You received this message because you are subscribed to a topic in the Google Groups "WebIOPi" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webiopi/vt4JMSOFkG4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webiopi+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andreas Riegg

unread,
Aug 14, 2014, 10:12:20 AM8/14/14
to web...@googlegroups.com
You find it here:

http://www.t-h-i-n-x.net/3.html

It's currently v0.6 (meaning its 60% complete), I'm on the way to provide v0.8 in the next 1 - 3 weeks depending how much spare time I can invest ...

Andreas

Giuseppe di Fede

unread,
Aug 14, 2014, 3:18:55 PM8/14/14
to web...@googlegroups.com

Oh great.
I'll download it soon and I hope to have good results.

Have a nice evening.

Giuseppe

--

Giuseppe di Fede

unread,
Aug 18, 2014, 2:11:26 PM8/18/14
to web...@googlegroups.com
Hi Andreas,

thanks for sharing your guide with me.
Now, probably it's because I am not able to understand well how to write e new driver.
But, do you think it's not possible to use the MCP23017 driver dor PCA9555 too?
Well, I thought it was quite easy because they have the sane function, so...


On Thursday, 14 August 2014 21:18:55 UTC+2, Giuseppe di Fede wrote:

Oh great.
I'll download it soon and I hope to have good results.

Have a nice evening.

Giuseppe

On Aug 14, 2014 4:12 PM, "Andreas Riegg" <andrea...@googlemail.com> wrote:
You find it here:

http://www.t-h-i-n-x.net/3.html

It's currently v0.6 (meaning its 60% complete), I'm on the way to provide v0.8 in the next 1 - 3 weeks depending how much spare time I can invest ...

Andreas

--
You received this message because you are subscribed to a topic in the Google Groups "WebIOPi" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/webiopi/vt4JMSOFkG4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to webiopi+unsubscribe@googlegroups.com.

Andreas Riegg

unread,
Aug 19, 2014, 6:41:27 AM8/19/14
to web...@googlegroups.com
Hi,

sorry to say, but I can confirm that the MCP23017 driver will NOT work with PCA9555. You are right, from an outside look they are both just 16 bit GPIO chips.

However, WebIOPi chip drivers (as well as any other chip drivers) have to map the external functionality to the internal chip command and register structure and protocol. In the case of the chips above, the internal registers are structured in different ways and for that reason this will never work.

It looks like the PCA9555 has much more similarities with the PCA9698 (40 bit GPIO) for which I have written a driver some weeks ago with the help of a kind donation from someone here. This is no surprise as they are from the same vendor category. See issue 103 for details. Best way will be to use PCA9698 driver and modify it to work for PCA9555. But you need good Python skills to get that working anyway.

Andreas

Giuseppe di Fede

unread,
Aug 19, 2014, 4:06:33 PM8/19/14
to web...@googlegroups.com
Hi Andreas,
I agree with you and thanks one more time for your complete answer.
My highlight was to say if it's easy to change the register address in the driver written for the MCP23017.
They use I2C, so if the I2C protocol exist I believed it was easy to re-write it changing the register address in order to get a working driver for the PCA9555.

I use PCA955 at work and it works like a charm. My problem in this moment is that I have no idea about the driver implementation :-(

Andreas Riegg

unread,
Aug 20, 2014, 5:10:03 AM8/20/14
to web...@googlegroups.com
Guiseppe,

just in order to avaid any confusion, this thread is on MCP23017, so I will post any further replies to your PCA9555 inquiry on your new separate thread about this chip.

Andreas
Reply all
Reply to author
Forward
0 new messages