RELAY 8 wire.read

The Terminal Shield breaks out all the Arduino headers to handy screw terminals, making it really easy to connect external wires without using a soldering iron. Ideal for quick experiments or for robust connections. [Product page]
Post Reply
cescoferraro
Posts: 20
Joined: Wed Jul 16, 2014 12:51 am

RELAY 8 wire.read

Post by cescoferraro » Tue Feb 24, 2015 9:26 pm

I could not find the proper Relay 8 discussion board so I placed here. Feel free to move it.
Is it possible to retrive information from the shield I2C? I'm designing an automation web server and it would be cool to know which channels are actually opened. Any snippets? Appreciate it! :D

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: RELAY 8 wire.read

Post by andrew » Wed Feb 25, 2015 1:20 am

You can read the GPIO registers to check their status.
We quickly modified the demo sketch to show you which pins are high and low after setting them:

Code: Select all

/*
 * Example sketch to control the RELAY8 8-Channel Relay
 * Driver Shield. Open serial console at 38400bps, and
 * send value "0" to reset all relays, or a channel number
 * to activate that relay. Requires Arduino IDE v1.0.1
 * or newer.
 */

#include "Wire.h"

#define I2C_ADDR  0x20  // 0x20 is the address with all jumpers removed

void setup()
{
  Serial.begin( 38400 );
  Serial.println("RELAY8 demonstration starting up");

  Wire.begin(); // Wake up I2C bus

  // Set I/O bank A to outputs
  Wire.beginTransmission(I2C_ADDR);
  Wire.write(0x00); // IODIRA register
  Wire.write(0x00); // Set all of bank A to outputs
  Wire.endTransmission();
  
  Serial.println("Ready. Type 0 to turn off relays, 1 - 8 to activate a relay.");

}

void loop()
{
    

   int command = 0;
   if (Serial.available()) 
   {
    command = Serial.read();
    if( command == '0' )
    {
      sendValueToLatch(0);
      Serial.println("Resetting all relays");
    }
    if( command == '1' )
    {
      sendValueToLatch(1);
      Serial.println("Activating relay 1");
    }
    if( command == '2' )
    {
      sendValueToLatch(2);
      Serial.println("Activating relay 2");
    }
    if( command == '3' )
    {
      sendValueToLatch(4);
      Serial.println("Activating relay 3");
    }
    if( command == '4' )
    {
      sendValueToLatch(8);
      Serial.println("Activating relay 4");
    }
    if( command == '5' )
    {
      sendValueToLatch(16);
      Serial.println("Activating relay 5");
    }
    if( command == '6' )
    {
      sendValueToLatch(32);
      Serial.println("Activating relay 6");
    }
    if( command == '7' )
    {
      sendValueToLatch(64);
      Serial.println("Activating relay 7");
    }
    if( command == '8' )
    {
      sendValueToLatch(128);
      Serial.println("Activating relay 8");
    }
      readPorts();
  }
}

void readPorts()
{
  byte inputs = 0;
 Wire.beginTransmission(I2C_ADDR);
 Wire.write(0x13); // set MCP23017 memory pointer to GPIOB address
 Wire.endTransmission();
 Wire.requestFrom(0x20, 1); // request one byte of data from MCP20317
 inputs=Wire.read(); // store the incoming byte into "inputs"
 Serial.print("GPIOB status > ");
  Serial.println(inputs, BIN); // display the contents of the GPIOB register in binary

 Wire.beginTransmission(I2C_ADDR);
 Wire.write(0x12); // set MCP23017 memory pointer to GPIOA address
 Wire.endTransmission();
 Wire.requestFrom(0x20, 1); // request one byte of data from MCP20317
 inputs=Wire.read(); // store the incoming byte into "inputs"
  Serial.print("GPIOA status > ");
 Serial.println(inputs, BIN); // display the contents of the GPIOA register in binary
}

void sendValueToLatch(int latchValue)
{
  Wire.beginTransmission(I2C_ADDR);
  Wire.write(0x12);        // Select GPIOA
  Wire.write(latchValue);  // Send value to bank A
  Wire.endTransmission();
}


cescoferraro
Posts: 20
Joined: Wed Jul 16, 2014 12:51 am

Re: RELAY 8 wire.read

Post by cescoferraro » Wed Feb 25, 2015 1:51 am

johnb, you are the man! Gonna test it ASAP!

andrew
Freetronics Staff
Freetronics Staff
Posts: 978
Joined: Sun Jul 14, 2013 7:06 am
Location: Melbourne, Australia
Contact:

Re: RELAY 8 wire.read

Post by andrew » Wed Feb 25, 2015 3:04 am

cescoferraro wrote:johnb, you are the man! Gonna test it ASAP!
You're welcome. Thank Angus too if you see him around here :)

cescoferraro
Posts: 20
Joined: Wed Jul 16, 2014 12:51 am

Re: RELAY 8 wire.read

Post by cescoferraro » Sun Mar 08, 2015 10:02 pm

I will! Just got the opportunity to test it over the weekend!
It work like a charm, but now I am having a hard time parsing it with Python. the variable length is a pain.
I am aiming at something like a Json file like ch1:on, ch2:0ff and so on, so I can retrieve the info with javascript and swift later.
Will post my results as soon as I get them. lol

cescoferraro
Posts: 20
Joined: Wed Jul 16, 2014 12:51 am

Re: RELAY 8 wire.read

Post by cescoferraro » Tue Sep 01, 2015 3:17 am

In case you might need. It returns a python true/false dict with the channels state.

http://stackoverflow.com/questions/2893 ... ble-number

Code: Select all

#!/usr/bin/env python2.7

def chan_values(num):
    """parses int into an array of True/False values for the channels"""
    channels = []

    while num > 0:
        channels.append(num%10==1)
        num /= 10

    channels += [False]*(8-len(channels))

    return channels

print chan_values(111)
print chan_values(10001000)

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest