Page 1 of 1

RELAY 8 wire.read

Posted: Tue Feb 24, 2015 9:26 pm
by cescoferraro
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

Re: RELAY 8 wire.read

Posted: Wed Feb 25, 2015 1:20 am
by andrew
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();
}


Re: RELAY 8 wire.read

Posted: Wed Feb 25, 2015 1:51 am
by cescoferraro
johnb, you are the man! Gonna test it ASAP!

Re: RELAY 8 wire.read

Posted: Wed Feb 25, 2015 3:04 am
by andrew
cescoferraro wrote:johnb, you are the man! Gonna test it ASAP!
You're welcome. Thank Angus too if you see him around here :)

Re: RELAY 8 wire.read

Posted: Sun Mar 08, 2015 10:02 pm
by cescoferraro
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

Re: RELAY 8 wire.read

Posted: Tue Sep 01, 2015 3:17 am
by cescoferraro
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)