Here’s the sketch I’m using:
//*****************************************************************************
/// @file
/// @brief
/// RV Switches
/// @note
///
//*****************************************************************************
#include <SoftwareSerial.h>
#include <SmartThings.h>
//*****************************************************************************
// Pin Definitions | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
// V V V V V V V V V V V V V V V V V V V V V V V V V V V V V
//*****************************************************************************
#define PIN_SWITCH1 22
#define PIN_SWITCH2 24
#define PIN_SWITCH3 26
#define PIN_SWITCH4 28
#define PIN_SWITCH5 30
#define PIN_SWITCH6 32
#define PIN_SWITCH7 34
#define PIN_SWITCH8 36
#define PIN_THING_RX 3
#define PIN_THING_TX 2
//*****************************************************************************
// Global Variables | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
// V V V V V V V V V V V V V V V V V V V V V V V V V V V V V
//*****************************************************************************
SmartThingsCallout_t messageCallout; // call out function forward decalaration
SmartThings smartthing(PIN_THING_RX, PIN_THING_TX, messageCallout); // constructor
bool isDebugEnabled; // enable or disable debug in this example
int stateSwitch1; // state to track last set value of switch 1
int stateSwitch2; // state to track last set value of switch 2
int stateSwitch3; // state to track last set value of switch 3
int stateSwitch4; // state to track last set value of switch 4
int stateSwitch5; // state to track last set value of switch 5
int stateSwitch6; // state to track last set value of switch 6
int stateSwitch7; // state to track last set value of switch 7
int stateSwitch8; // state to track last set value of switch 8
int stateNetwork;
//*****************************************************************************
// Local Functions | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
// V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V
//*****************************************************************************
void on1()
{
stateSwitch1 = 1; // save state as 1 (on)
digitalWrite(PIN_SWITCH1, HIGH); // turn relay on
smartthing.send("on1"); // send message to cloud
}
void off1()
{
stateSwitch1 = 0; // set state to 0 (off)
digitalWrite(PIN_SWITCH1, LOW); // turn relay off
smartthing.send("off1"); // send message to cloud
}
void on2()
{
stateSwitch2 = 1; // save state as 1 (on)
digitalWrite(PIN_SWITCH2, HIGH); // turn relay on
smartthing.send("on2"); // send message to cloud
}
void off2()
{
stateSwitch2 = 0; // set state to 0 (off)
digitalWrite(PIN_SWITCH2, LOW); // turn relay off
smartthing.send("off2"); // send message to cloud
}
void on3()
{
stateSwitch3 = 1; // save state as 1 (on)
digitalWrite(PIN_SWITCH3, HIGH); // turn relay on
smartthing.send("on3"); // send message to cloud
}
void off3()
{
stateSwitch3 = 0; // set state to 0 (off)
digitalWrite(PIN_SWITCH3, LOW); // turn relay off
smartthing.send("off3"); // send message to cloud
}
void on4()
{
stateSwitch4 = 1; // save state as 1 (on)
digitalWrite(PIN_SWITCH4, HIGH); // turn relay on
smartthing.send("on4"); // send message to cloud
}
void off4()
{
stateSwitch4 = 0; // set state to 0 (off)
digitalWrite(PIN_SWITCH4, LOW); // turn relay off
smartthing.send("off4"); // send message to cloud
}
void on5()
{
stateSwitch5 = 1; // save state as 1 (on)
digitalWrite(PIN_SWITCH5, HIGH); // turn relay on
smartthing.send("on5"); // send message to cloud
}
void off5()
{
stateSwitch5 = 0; // set state to 0 (off)
digitalWrite(PIN_SWITCH5, LOW); // turn relay off
smartthing.send("off5"); // send message to cloud
}
void on6()
{
stateSwitch6 = 1; // save state as 1 (on)
digitalWrite(PIN_SWITCH6, HIGH); // turn relay on
smartthing.send("on6"); // send message to cloud
}
void off6()
{
stateSwitch6 = 0; // set state to 0 (off)
digitalWrite(PIN_SWITCH6, LOW); // turn relay off
smartthing.send("off6"); // send message to cloud
}
void on7()
{
stateSwitch7 = 1; // save state as 1 (on)
digitalWrite(PIN_SWITCH7, HIGH); // turn relay on
smartthing.send("on7"); // send message to cloud
}
void off7()
{
stateSwitch7 = 0; // set state to 0 (off)
digitalWrite(PIN_SWITCH7, LOW); // turn relay off
smartthing.send("off7"); // send message to cloud
}
void on8()
{
stateSwitch8 = 1; // save state as 1 (on)
digitalWrite(PIN_SWITCH8, HIGH); // turn relay on
smartthing.send("on8"); // send message to cloud
}
void off8()
{
stateSwitch8 = 0; // set state to 0 (off)
digitalWrite(PIN_SWITCH8, LOW); // turn relay off
smartthing.send("off8"); // send message to cloud
}
//*****************************************************************************
void setNetworkStateLED()
{
SmartThingsNetworkState_t tempState = smartthing.shieldGetLastNetworkState();
if (tempState != stateNetwork)
{
switch (tempState)
{
case STATE_NO_NETWORK:
if (isDebugEnabled) Serial.println("NO_NETWORK");
smartthing.shieldSetLED(2, 0, 0); // red
break;
case STATE_JOINING:
if (isDebugEnabled) Serial.println("JOINING");
smartthing.shieldSetLED(2, 0, 0); // red
break;
case STATE_JOINED:
if (isDebugEnabled) Serial.println("JOINED");
smartthing.shieldSetLED(0, 0, 0); // off
break;
case STATE_JOINED_NOPARENT:
if (isDebugEnabled) Serial.println("JOINED_NOPARENT");
smartthing.shieldSetLED(2, 0, 2); // purple
break;
case STATE_LEAVING:
if (isDebugEnabled) Serial.println("LEAVING");
smartthing.shieldSetLED(2, 0, 0); // red
break;
default:
case STATE_UNKNOWN:
if (isDebugEnabled) Serial.println("UNKNOWN");
smartthing.shieldSetLED(0, 2, 0); // green
break;
}
stateNetwork = tempState;
}
}
//*****************************************************************************
// API Functions | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
// V V V V V V V V V V V V V V V V V V V V V V V V V V V V V V
//*****************************************************************************
void setup()
{
// setup default state of global variables
isDebugEnabled = true;
stateSwitch1 = 0; // matches state of hardware pin set below
stateSwitch2 = 0; // matches state of hardware pin set below
stateSwitch3 = 0; // matches state of hardware pin set below
stateSwitch4 = 0; // matches state of hardware pin set below
stateSwitch5 = 0; // matches state of hardware pin set below
stateSwitch6 = 0; // matches state of hardware pin set below
stateSwitch7 = 0; // matches state of hardware pin set below
stateSwitch8 = 0; // matches state of hardware pin set below
// setup hardware pins
pinMode(PIN_SWITCH1, OUTPUT); // define pin as an output
digitalWrite(PIN_SWITCH1, LOW); // set value to LOW (off) to match state=0
pinMode(PIN_SWITCH2, OUTPUT); // define pin as an output
digitalWrite(PIN_SWITCH2, LOW); // set value to LOW (off) to match state=0
pinMode(PIN_SWITCH3, OUTPUT); // define pin as an output
digitalWrite(PIN_SWITCH3, LOW); // set value to LOW (off) to match state=0
pinMode(PIN_SWITCH4, OUTPUT); // define pin as an output
digitalWrite(PIN_SWITCH4, LOW); // set value to LOW (off) to match state=0
pinMode(PIN_SWITCH5, OUTPUT); // define pin as an output
digitalWrite(PIN_SWITCH5, LOW); // set value to LOW (off) to match state=0
pinMode(PIN_SWITCH6, OUTPUT); // define pin as an output
digitalWrite(PIN_SWITCH6, LOW); // set value to LOW (off) to match state=0
pinMode(PIN_SWITCH7, OUTPUT); // define pin as an output
digitalWrite(PIN_SWITCH7, LOW); // set value to LOW (off) to match state=0
pinMode(PIN_SWITCH8, OUTPUT); // define pin as an output
digitalWrite(PIN_SWITCH8, LOW); // set value to LOW (off) to match state=0
stateNetwork = STATE_JOINED; // set to joined to keep state off if off
if (isDebugEnabled)
{ // setup debug serial port
Serial.begin(9600); // setup serial with a baud rate of 9600
Serial.println("setup.."); // print out 'setup..' on start
}
}
//*****************************************************************************
void loop()
{
// run smartthing logic
smartthing.run();
setNetworkStateLED();
}
//*****************************************************************************
void messageCallout(String message)
{
// if debug is enabled print out the received message
if (isDebugEnabled)
{
Serial.print("Received message: '");
Serial.print(message);
Serial.println("' ");
}
// if message contents equals to 'on' then call on() function
// else if message contents equals to 'off' then call off() function
if (message.equals("On1"))
{
on1();
}
if (message.equals("Off1"))
{
off1();
}
if (message.equals("On2"))
{
on2();
}
if (message.equals("Off2"))
{
off2();
}
if (message.equals("On3"))
{
on3();
}
if (message.equals("Off3"))
{
off3();
}
if (message.equals("On4"))
{
on4();
}
if (message.equals("Off4"))
{
off4();
}
if (message.equals("On5"))
{
on5();
}
if (message.equals("Off5"))
{
off5();
}
if (message.equals("On6"))
{
on6();
}
if (message.equals("Off6"))
{
off6();
}
if (message.equals("On7"))
{
on7();
}
if (message.equals("Off7"))
{
off7();
}
if (message.equals("On8"))
{
on8();
}
if (message.equals("Off8"))
{
off8();
}
}