I have the following boards:
Arduino Uno Rev3
Seeed Studio Relay Shield https://www.seeedstudio.com/Relay-Shield-v3.0-p-2440.html#
SmartThings Shield https://support.smartthings.com/hc/en-us/articles/213652126-SmartThings-Shield-for-Arduino
I am trying to adapt the code from SmartThings to switch a relay on for 1 second when the On command is sent and switch a second relay on for a one second duration when the Off command is sent via SmartThings.
I have tried adding the basic code to the prewritten SmartThings Sketch however I am struggling to get it to work.
I want to assign relay1 to pin 7 and relay2 to pin 6 with a one second activation of the relay before it returns to its none energized state. effectively a momentary make switch.
Any assistance with this would be much appreciated.
the Sketch is below:
//*****************************************************************************
/// @file
/// @brief
/// Arduino SmartThings Shield LED Example with Network Status
/// @note
/// ______________
/// | |
/// | SW[] |
/// |[]RST |
/// | AREF |--
/// | GND |--
/// | 13 |--X LED
/// | 12 |--
/// | 11 |--
/// --| 3.3V 10 |--
/// --| 5V 9 |--
/// --| GND 8 |--
/// --| GND |
/// --| Vin 7 |--
/// | 6 |--
/// --| A0 5 |--
/// --| A1 ( ) 4 |--
/// --| A2 3 |--X THING_RX
/// --| A3 ____ 2 |--X THING_TX
/// --| A4 | | 1 |--
/// --| A5 | | 0 |--
/// |____| |____|
/// |____|
///
//*****************************************************************************
#include <SoftwareSerial.h> //TODO need to set due to some weird wire language linker, should we absorb this whole library into smartthings
#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_LED 13
#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 stateLED; // state to track last set value of LED
int stateNetwork; // state of the network
//*****************************************************************************
// 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 on()
{
stateLED = 1; // save state as 1 (on)
digitalWrite(PIN_LED, HIGH); // turn LED on
smartthing.shieldSetLED(0, 0, 2);
smartthing.send("on"); // send message to cloud
}
//*****************************************************************************
void off()
{
stateLED = 0; // set state to 0 (off)
digitalWrite(PIN_LED, LOW); // turn LED off
smartthing.shieldSetLED(0, 0, 0);
smartthing.send("off"); // 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;
stateLED = 0; // matches state of hardware pin set below
stateNetwork = STATE_JOINED; // set to joined to keep state off if off
// setup hardware pins
pinMode(PIN_LED, OUTPUT); // define PIN_LED as an output
digitalWrite(PIN_LED, LOW); // set value to LOW (off) to match stateLED=0
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("on"))
{
on();
}
else if (message.equals("off"))
{
off();
}
}
[code
[/code]