I am able to run the utility that reports seeing the relay board and 8-port I/O board on ports 32 and 33.
I am trying to use the rickkus i2c library but am not having any luck figuring out how to read those inputs. Not sure what belongs in the loop() to capture the values of the inputs.
Could you give additional direction on how to proceed to read the inputs and use those results in my Photon code? I get the idea this should be easy, but I need more direction please!
Here is my current code. Thanks!!
// This #include statement was automatically added by the Particle IDE.
#include <MCP23008-RK.h>
/* Includes ------------------------------------------------------------------*/
#include “NCD8Relay.h”
#include “spark_wiring_print.h”
MCP23008 gpio(Wire,0);
SYSTEM_MODE(AUTOMATIC);
NCD8Relay relayController;
int triggerRelay(String command);
/* This function is called once at start up ----------------------------------*/
void setup()
{
Serial.begin(115200);
relayController.setAddress(0,0,0);
Spark.function(“controlRelay”, triggerRelay);
gpio.begin();
gpio.pinMode(0, INPUT);
gpio.digitalWrite(0, HIGH);
gpio.pinMode(1, INPUT);
gpio.digitalWrite(1, HIGH);
gpio.pinMode(2, INPUT);
gpio.digitalWrite(2, HIGH);
gpio.pinMode(3, INPUT);
gpio.digitalWrite(3, HIGH);
gpio.pinMode(4, INPUT);
gpio.digitalWrite(4, HIGH);
gpio.pinMode(5, INPUT);
gpio.digitalWrite(5, HIGH);
gpio.pinMode(6, INPUT);
gpio.digitalWrite(6, HIGH);
gpio.pinMode(7, INPUT);
gpio.digitalWrite(7, HIGH);
}
/* This function loops forever --------------------------------------------*/
void loop()
{
// triggerRelay(“1off”);
// relayCommand(“1”,“on”);
// delay(2000);
// triggerRelay(“1off”);
// relayCommand(“1”,“off”);
// delay(2000);
}
int triggerRelay(String command){
if(command.equalsIgnoreCase(“turnonallrelays”)){
relayController.turnOnAllRelays();
return 1;
}
if(command.equalsIgnoreCase(“turnoffallrelays”)){
relayController.turnOffAllRelays();
return 1;
}
if(command.startsWith(“setBankStatus:”)){
int status = command.substring(14).toInt();
if(status < 0 || status > 255){
return 0;
}
Serial.print("Setting bank status to: ");
Serial.println(status);
relayController.setBankStatus(status);
Serial.println(“done”);
return 1;
}
//Relay Specific Command
int relayNumber = command.substring(0,1).toInt();
Serial.print(“relayNumber: “);
Serial.println(relayNumber);
String relayCommand = command.substring(1);
Serial.print(“relayCommand:”);
Serial.print(relayCommand);
Serial.println(”.”);
if(relayCommand.equalsIgnoreCase(“on”)){
Serial.println(“Turning on relay”);
relayController.turnOnRelay(relayNumber);
Serial.println(“returning”);
return 1;
}
if(relayCommand.equalsIgnoreCase(“off”)){
relayController.turnOffRelay(relayNumber);
return 1;
}
if(relayCommand.equalsIgnoreCase(“toggle”)){
relayController.toggleRelay(relayNumber);
return 1;
}
if(relayCommand.equalsIgnoreCase(“momentary”)){
relayController.turnOnRelay(relayNumber);
delay(300);
relayController.turnOffRelay(relayNumber);
return 1;
}
return 0;
}