SmartThings Community

[RELEASE] ST_Anything v2.9.2 - Arduino/ESP8266/ESP32 to ST via ThingShield, Ethernet, or WiFi

arduino
alarm
dth_misc
esp8266
esp32

#2167

But the one you posted had the “switch” capability, so wouldn’t that imply an “on/off” control from the ST app and not just a sensor?


(Dan) #2168

Yes. But it depends on what you’re trying to accomplish. If you want to display these devices on a Dashboard system like Action Tiles, you need to conform with the ST Capabilities definitions. So, the only device that I know of that has values of ‘on’ and ‘off’ is a Switch Device.


#2169

Ah, yes, I see what you mean. I guess it just depends on if he’s even using AcctionTiles. But now, with the DTH, if you did push the button, would it change the state of the sensor until the actual sensor changed again (or hit the 5 min refresh)? I’m thinking I might use the DTH for a couple of my sensors to get the display in ActionTiles more correct. Also, did you update that DTH for the newest ST_Anything “model”? I just did that with all my DTHs and they all worked the first time! I was very proud. :sunglasses:


(L B) #2170

I changed the handler to this:
Does this break live tiles? Works great, and updates as it should. It no longer allows a button press.

tiles(scale: 2) {
multiAttributeTile(name:“switch”, type: “lighting”, width: 3, height: 4, canChangeIcon: true){
tileAttribute (“device.switch”, key: “PRIMARY_CONTROL”) {
attributeState “off”, label: ‘${name}’, action: “switch.on”, icon: “st.switches.switch.off”, backgroundColor: “#ffffff”, nextState:“off”
attributeState “on”, label: ‘${name}’, action: “switch.off”, icon: “st.switches.switch.on”, backgroundColor: “#00A0DC”, nextState:“on”
attributeState “turningOn”, label:’${name}’, action:“switch.off”, icon:“st.switches.switch.on”, backgroundColor:"#00A0DC", nextState:“turningOff”
attributeState “turningOff”, label:’${name}’, action:“switch.on”, icon:“st.switches.switch.off”, backgroundColor:"#ffffff", nextState:“turningOn”


(Allan) #2171

I updated a couple of the dths and am waiting for @ogiewon to approve my pull requests as a test. If they work I’ll do more or he can. I know Dan is using a hubitat hub for most of his stuff so supporting the new app might not be a priority for him.


(Dan) #2172

Those changes should be fine.


(Dan) #2173

Thanks Allen. I’ll take a look after work. I appreciate your continued support of ST_Anything on SmartThings!


(Stefan Giudici) #2174

Not super ST Anything specific, but I do plan on using ST anything for my ultimate project of blinds control with smartthings, and you seem to be a general wizard, so here-goes. I think I can code the Arduino side for blinds (probably not some awesome efficient way) but I can def ugly-code myself to it working. My question:

Is there a more direct way to sense ST interactions besides just callback-sniffing for them? Presumably your awesome built-in sketches are receiving information without parsing callback info, but I don’t think I’m advanced enough to modify that stuff, so I’ll probably just take the servo sketch, sniff the angle settings, translate that to a timeline from top to bottom and do some math like if command < current location move down distance between current location and command location, set new location. I also don’t know how I’d modify the callback to set a variable based strictly on the angle given in the callback data besides manually playing out each scenario (if msg = “servo1 50” set var=50 / if msg= “servo1 51” set var=51).

Thanks all.


(Dan) #2175

When parsing a String, this reference comes in very handy…

https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/

Please do not create 100 if statements! :wink:

The simplest solution may be to simply take the EX_Servo.cpp file and modify it for your own use. It essentially receives the string from ST, parses it, then updates the GPIO pin, and then sends an update back to ST.

It will really depend on exactly how you want to implement the control.

If you want the ultimate control, take a look at the example sketch in this post. I created for a user who was trying to control automatic vents. It is NOT an ST_Anything sketch, but does use the same communications libraries to interact with the ST Hub. It is a simpler, all in one sketch.


#2176

I’m confused. My question was specifically about the DTH that @ogiewon posted. I don’t know what DTH you’re referring to @vseven. The ones that are included in the github are all working. Which ones are you referring to?


(Dan) #2177

Allan,

Thanks for posting this as my email provider was marking all email from GitHub as spam. That’s why I hadn’t noticed your pull requests which I have now accepted.

I’ve been holding off on any changes to support the New SmartThings App until Samsung actually publishes developer guidelines and documentation. I just don’t see any compelling reason to use the New App as the Classic App still works much better for most users.

Dan


(Allan) #2178

No, they are all working fine in the Classic app but they don’t work in the new app so I wanted to push a couple updated for testing.

Ha…yeah Yahoo has been doing the same to me lately. I get most ST_Anything ones but miss most of the ESP32 ones I’ve subscribed to. No idea why.

I get that and I try to use the clasic app also but I just got a new Samsung washer & dryer and they don’t work right under classic but do work under the new one (or the Samsung “Smart Home” app). Also I got a new Note 9 (and had a Gear S3) so I’m trying to embrace the changes and get everything under one app which it does just not great yet.

I’ll look at updating the other DTH’s as soon as I have time. I know they haven’t release official specs but since they are supporting another standard I would think the device types shouldn’t be changing much and are “safe” to use.


#2179

OH!! I see. Sorry for my confusion. Got a little worried there for a sec.


(Harlan Lau) #2180

Regarding the Arduino Web Editor, is there a way to “git” the ST_Anything libraries and sketches directly into the Web Editor? I searched the Arduino libraries and didn’t find ST_Anything. I couldn’t figure out how to add a pointer to git for the ST_Anything. This seemed like a good way to keep the ST_Anything files up to date. Would it be possible to add the ST_Anything to the Arduino libraries supported by the Arduino Library Manager?

Or do I git the ST_Anything libraries and sketches onto my local computer, zip them up and then import them in the Arduino Web Editor? This seems like a pain when you want to update ST_Anything libraries and sketches.

Thanks.


(Dan) #2181

I have never used the Arduino Web Editor. I only support using the full Arduino IDE. You can use Git/GitHub to clone to your local computer and keep things up to date.


(L B) #2182

Dan,

I pulled out the old thingsshield I bought awhile ago. I am looking to monitor my old contacts from my alarm panel. I am using it with a mega.
I started setting it up and never finished. I don’t remember why, but I had jumpers going from communication pins 0,1 to pins 14,15. Is this still correct or ever? I looked all over the internet and I only see attaching them together. How many inputs and out am I able to use? I have a lot of contacts, motion and smoke.

Thanks for help as always!


(Dan) #2183

Yes, those jumpers allow the use of the Arduino MEGA’s extra Hardware UARTs for better performance and reliability. For ST_Anything. it should be Pin 2 to 14 AND Pin 3 to 15. Make sure the small switch on the ThingShield is set to use Pins2/3.

Here’s the information from my ‘SmartThings Library’ ReadMe within my ST_Anything Github (this extra ReadMe is nested with the Arduino\Libraries\SmartThings folder.)

The Arduino MEGA 2560 must use HardwareSerial "Serial1, Serial2, or Serial3" for communications with the ThingShield. MEGA 2560 Hardware Serial:

* Use the new Hardware Serial constructor passing in a pointer to a Hardware Serial device (&amp;Serial1, &amp;Serial2, &amp;Serial3)
  * definition st::SmartThingsThingShield(HardwareSerial* serial, SmartThingsCallout_t *callout);
  * sample st::SmartThingsThingShield(&amp;Serial3, callout);
* Make sure the ThingShield's switch in the "D2/D3" position
* Be certain to not use Pins 2 &amp; 3 in your Arduino sketch for I/O since they are electrically connected to the ThingShield. Pin6 is also reserved by the ThingShield. Best to avoid using it.
* On the MEGA, Serial1 uses pins 18/19, Serial2 uses pins 16/17, and Serial3 uses pins 14/15
* You will need to wire the MEGA's HardwareSerial port's pins to pins 2 &amp; 3 on the ThingShield. For example, using Serial3, wire Pin 2 to Pin 14 AND Pin3 to Pin 15. 

You can use almost all of the GPIO pins on the Arduino MEGA. There are a few reservations, which are listed within the comments of the ThingShield example sketch, ST_Anything_Multiples_ThingShield.ino.

Specifically…

//******************************************************************************************
//Define which Arduino Pins will be used for each device
//  Notes: -Serial Communications Pins are defined in Constants.h (avoid pins 0,1,2,3
//          for inputs and output devices below as they may be used for communications)
//         -Always avoid Pin 6 as it is reserved by the SmartThings Shield
//
//******************************************************************************************
//"RESERVED" pins for SmartThings ThingShield - best to avoid
#define PIN_O_RESERVED           0  //reserved by ThingShield for Serial communications OR USB Serial Monitor
#define PIN_1_RESERVED           1  //reserved by ThingShield for Serial communications OR USB Serial Monitor
#define pinTX                    2  //reserved by ThingShield for Serial communications
#define pinRX                    3  //reserved by ThingShield for Serial communications
#define PIN_6_RESERVED           6  //reserved by ThingShield (possible future use?)


(Harlan Lau) #2184

Oh, that’s good to know. I had the impression the Arduino IDE and Web Editor were interchangeable.

Has anyone else successfully used the Web Editor with ST_Anything libraries and sketches?


(L B) #2185

Can you look at this sketch? I can’t for the life of me see the problem, but I get an error compiling.

//“RESERVED” pins for SmartThings ThingShield - best to avoid
#define PIN_O_RESERVED 0 //reserved by ThingShield for Serial communications OR USB Serial Monitor
#define PIN_1_RESERVED 1 //reserved by ThingShield for Serial communications OR USB Serial Monitor
#define pinTX 2 //reserved by ThingShield for Serial communications
#define pinRX 3 //reserved by ThingShield for Serial communications
#define PIN_6_RESERVED 6 //reserved by ThingShield (possible future use?)

//Digital Pins
#define PIN_MOTION_1 22
#define PIN_MOTION_2 23
#define PIN_CONTACT_1 24
#define PIN_CONTACT_2 25
#define PIN_CONTACT_3 26
#define PIN_CONTACT_4 27
#define PIN_CONTACT_5 28
#define PIN_CONTACT_6 29
#define PIN_CONTACT_7 30
#define PIN_CONTACT_8 31
#define PIN_CONTACT_9 32
#define PIN_CONTACT_10 33

//If using SoftwareSerial (e.g. Arduino UNO), must define pins for transmit and receive
//#define pinRX 3
//#define pinTX 2

//******************************************************************************************
//st::Everything::callOnMsgSend() optional callback routine. This is a sniffer to monitor
// data being sent to ST. This allows a user to act on data changes locally within the
// Arduino sktech.
//******************************************************************************************
void callback(const String &msg)
{
//Serial.print(F("ST_Anything Callback: Sniffed data = "));
//Serial.println(msg);

//TODO: Add local logic here to take action when a device’s value/state is changed

//Masquerade as the ThingShield to send data to the Arduino, as if from the ST Cloud (uncomment and edit following line)
//st::receiveSmartString(“Put your command here!”); //use same strings that the Device Handler would send
}

//******************************************************************************************
//Arduino Setup() routine
//******************************************************************************************
void setup()
{
//******************************************************************************************
//Declare each Device that is attached to the Arduino
// Notes: - For each device, there is typically a corresponding “tile” defined in your
// SmartThings Device Hanlder Groovy code, except when using new COMPOSITE Device Handler
// - For details on each device’s constructor arguments below, please refer to the
// corresponding header (.h) and program (.cpp) files.
// - The name assigned to each device (1st argument below) must match the Groovy
// Device Handler names. (Note: “temphumid” below is the exception to this rule
// as the DHT sensors produce both “temperature” and “humidity”. Data from that
// particular sensor is sent to the ST Hub in two separate updates, one for
// “temperature” and one for “humidity”)
// - The new Composite Device Handler is comprised of a Parent DH and various Child
// DH’s. The names used below MUST not be changed for the Automatic Creation of
// child devices to work properly. Simply increment the number by +1 for each duplicate
// device (e.g. contact1, contact2, contact3, etc…) You can rename the Child Devices
// to match your specific use case in the ST Phone Application.
//******************************************************************************************
//Polling Sensors

//Interrupt Sensors
static st::IS_Motion sensor1(F(“motion1”), PIN_MOTION_1, HIGH, false, 500);
static st::IS_Motion sensor2(F(“motion2”), PIN_MOTION_2, HIGH, false, 500);
static st::IS_Contact sensor3(F(“contact1”), PIN_CONTACT_1, LOW, true, 500);
static st::IS_Contact sensor4(F(“contact2”), PIN_CONTACT_2, LOW, true, 500);
static st::IS_Contact sensor5(F(“contact3”), PIN_CONTACT_3, LOW, true, 500);
static st::IS_Contact sensor6(F(“contact4”), PIN_CONTACT_4, LOW, true, 500);
static st::IS_Contact sensor7(F(“contact5”), PIN_CONTACT_5, LOW, true, 500);
static st::IS_Contact sensor8(F(“contact6”), PIN_CONTACT_6, LOW, true, 500);
static st::IS_Contact sensor9(F(“contact7”), PIN_CONTACT_7, LOW, true, 500);
static st::IS_Contact sensor10(F(“contact8”), PIN_CONTACT_8, LOW, true, 500);
static st::IS_Contact sensor11(F(“contact9”), PIN_CONTACT_9, LOW, true, 500);
static st::IS_Contact sensor12(F(“contact10”), PIN_CONTACT_10, LOW, true, 500);

//Executors

//*****************************************************************************
// Configure debug print output from each main class
// -Note: Set these to “false” if using Hardware Serial on pins 0 & 1
// to prevent communication conflicts with the ST Shield communications
//*****************************************************************************
st::Everything::debug=true;
st::Executor::debug=true;
st::Device::debug=true;
st::PollingSensor::debug=true;
st::InterruptSensor::debug=true;

//*****************************************************************************
//Initialize the “Everything” Class
//*****************************************************************************

//Initialize the optional local callback routine (safe to comment out if not desired)
st::Everything::callOnMsgSend = callback;

//Create the SmartThings Thingshield Communications Object based on Arduino Model

#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO) || defined(ARDUINO_AVR_MINI) //Arduino UNO, NANO, MINI
st::Everything::SmartThing = new st::SmartThingsThingShield(pinRX, pinTX, st::receiveSmartString); //Use Software Serial
#elif defined(ARDUINO_AVR_LEONARDO) //Arduino Leonardo
st::Everything::SmartThing = new st::SmartThingsThingShield(&Serial1, st::receiveSmartString); //Use Hardware Serial
#elif defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) //Arduino MEGA 1280 or 2560
st::Everything::SmartThing = new st::SmartThingsThingShield(&Serial3, st::receiveSmartString); //Use Hardware Serial
#else
//assume user is using an UNO for the unknown case
st::Everything::SmartThing = new st::SmartThingsThingShield(pinRX, pinTX, st::receiveSmartString); //Software Serial
#endif

//Run the Everything class’ init() routine which establishes communications with SmartThings
st::Everything::init();

//*****************************************************************************
//Add each sensor to the “Everything” Class
//*****************************************************************************
st::Everything::addSensor(&sensor1);
st::Everything::addSensor(&sensor2);
st::Everything::addSensor(&sensor3);
st::Everything::addSensor(&sensor4);
st::Everything::addSensor(&sensor5);
st::Everything::addSensor(&sensor6);
st::Everything::addSensor(&sensor7);
st::Everything::addSensor(&sensor8);
st::Everything::addSensor(&sensor9);
st::Everything::addSensor(&sensor10);
st::Everything::addSensor(&sensor11);
st::Everything::addSensor(&sensor12);
//*****************************************************************************
//Add each executor to the “Everything” Class
//*****************************************************************************

//*****************************************************************************
//Initialize each of the devices which were added to the Everything Class
//*****************************************************************************
st::Everything::initDevices();
}

//******************************************************************************************
//Arduino Loop() routine
//******************************************************************************************
void loop()
{
//*****************************************************************************
//Execute the Everything run method which takes care of “Everything”
//*****************************************************************************
st::Everything::run();

}


(Dan) #2186

Was there a reason you stripped out all of the #include statements? Those are pretty important! :wink:

Here is your sketch with the appropriate header files included for using the ThingShield as well as the ST_Anything libraries. It compiles fine for me. Make sure you’ve selected the Arduino MEGA 2560 in the Arduino IDE’s Boards Manager.

//******************************************************************************************
//  File: ST_Anything_Multiples_Thingshield.ino
//  Authors: Dan G Ogorchock & Daniel J Ogorchock (Father and Son)
//
//  Summary:  This Arduino Sketch, along with the ST_Anything library and the revised SmartThings 
//            library, demonstrates the ability of one Arduino + SmartThings Shield to 
//            implement a multi input/output custom device for integration into SmartThings.
//            The ST_Anything library takes care of all of the work to schedule device updates
//            as well as all communications with the SmartThings Shield.
//
//            ST_Anything_Multiples implements the following ST Capabilities in multiples of 2 as a demo of what is possible with a single Arduino
//              - 2 x Door Control devices (used typically for Garage Doors - input pin (contact sensor) and output pin (relay switch)
//              - 2 x Contact Sensor devices (used to monitor magnetic door sensors)
//              - 2 x Switch devices (used to turn on a digital output (e.g. LED, relay, etc...)
//              - 2 x Water Sensor devices (using an analog input pin to measure voltage from a water detector baord)
//              - 2 x Illuminance Measurement devices (using a photoresitor attached to ananlog input)
//              - 2 x Voltage Measurement devices (using a photoresitor attached to ananlog input)
//              - 2 x Smoke Detector devices (using simple digital input)
//              - 2 x Carbon Monoxide Detector devices (using simple digital input)
//              - 2 x Motion devices (used to detect motion)
//              - 2 x Temperature Measurement devices (Temperature from DHT22 device)
//              - 2 x Humidity Measurement devices (Humidity from DHT22 device)
//              - 2 x Relay Switch devices (used to turn on a digital output for a set number of cycles And On/Off times (e.g.relay, etc...))
//              - 2 x Button devices (sends "pushed" if held for less than 1 second, else sends "held"
//              - 2 x Alarm devices - 1 siren only, 1 siren and strobe (using simple digital outputs)
//              - 2 x Dimmer Switch devices - uses 2 digital outputs, one for on/off and one for pwm level
//              - 2 x MQ-2 Smoke Detector devices (using simple analog input compared to user defined limit)
//
//            During the development of this re-usable library, it became apparent that the 
//            Arduino UNO R3's very limited 2K of SRAM was very limiting in the number of 
//            devices that could be implemented simultaneously.  A tremendous amount of effort
//            has gone into reducing the SRAM usage, including siginificant improvements to
//            the SmartThings Arduino library.  The SmartThings library was also modified to
//            include support for using Hardware Serial port(s) on the UNO, MEGA, and Leonardo.
//            During testing, it was determined that the Hardware Serial ports provide much
//            better performance and reliability versus the SoftwareSerial library.  Also, the
//            MEGA 2560's 8K of SRAM is well worth the few extra dollars to save your sanity
//            versus always running out of SRAM on the UNO R3.  The MEGA 2560 also has 4 Hardware
//            serial ports (i.e. UARTS) which makes it very easy to use Hardware Serial instead 
//            of SoftwareSerial, while still being able to see debug data on the USB serial 
//            console port (pins 0 & 1).  
//
//            Note: We did not have a Leonardo for testing, but did fully test on UNO R3 and 
//            MEGA 2560 using both SoftwareSerial and Hardware Serial communications to the 
//            Thing Shield.  This sketch, due to the number of devices implemented, requires an 
//            Arduino MEGA 2560 wuth ThingShield wired to "Serial3"  
//    
//  Change History:
//
//    Date        Who            What
//    ----        ---            ----
//    2015-01-03  Dan & Daniel   Original Creation
//    2015-03-28  Dan Ogorchock  Removed RCSwitch #include now that the libraries are split up
//    2015-03-31  Daniel O.      Memory optimizations utilizing progmem
//    2015-08-23  Dan Ogorchock  Added optional alarm limit argument to water sensor
//    2017-02-12  Dan Ogorchock  Revised to use the new SMartThings v2.0 library
//    2017-04-22  Dan Ogorchock  Added Voltage, Carbon Monoxide, and Alarm with Strobe
//    2017-04-25  Dan Ogorchock  Revised for Parent/Child Composite Device Handler
//    2017-11-21  Dan Ogorchock  Fixed Arduino UNO compatability (missing pinRx and pinTx definitions)
//
//******************************************************************************************
//******************************************************************************************
// SmartThings Library for Arduino Shield
//******************************************************************************************
#include <SmartThingsThingShield.h>    //Library to provide API to the SmartThings ThingShield

//******************************************************************************************
// ST_Anything Library 
//******************************************************************************************
#include <Constants.h>       //Constants.h is designed to be modified by the end user to adjust behavior of the ST_Anything library
#include <Device.h>          //Generic Device Class, inherited by Sensor and Executor classes
#include <Sensor.h>          //Generic Sensor Class, typically provides data to ST Cloud (e.g. Temperature, Motion, etc...)
#include <Executor.h>        //Generic Executor Class, typically receives data from ST Cloud (e.g. Switch)
#include <InterruptSensor.h> //Generic Interrupt "Sensor" Class, waits for change of state on digital input 
#include <PollingSensor.h>   //Generic Polling "Sensor" Class, polls Arduino pins periodically
#include <Everything.h>      //Master Brain of ST_Anything library that ties everything together and performs ST Shield communications

#include <PS_Illuminance.h>  //Implements a Polling Sensor (PS) to measure light levels via a photo resistor on an analog input pin 
#include <PS_Voltage.h>      //Implements a Polling Sensor (PS) to measure voltage on an analog input pin 
#include <PS_TemperatureHumidity.h>  //Implements a Polling Sensor (PS) to measure Temperature and Humidity via DHT library
#include <PS_Water.h>        //Implements a Polling Sensor (PS) to measure presence of water (i.e. leak detector) on an analog input pin 
#include <PS_MQ2_Smoke.h>    //Implements an Polling Sensor (PS) to monitor the status of an analog input pin from a MQ2 sensor
#include <IS_Motion.h>       //Implements an Interrupt Sensor (IS) to detect motion via a PIR sensor on a digital input pin
#include <IS_Contact.h>      //Implements an Interrupt Sensor (IS) to monitor the status of a digital input pin
#include <IS_Smoke.h>        //Implements an Interrupt Sensor (IS) to monitor the status of a digital input pin
#include <IS_CarbonMonoxide.h> //Implements an Interrupt Sensor (IS) to monitor the status of a digital input pin
#include <IS_DoorControl.h>  //Implements an Interrupt Sensor (IS) and Executor to monitor the status of a digital input pin and control a digital output pin
#include <IS_Button.h>       //Implements an Interrupt Sensor (IS) to monitor the status of a digital input pin for button presses
#include <EX_Switch.h>       //Implements an Executor (EX) via a digital output to a relay
#include <EX_Alarm.h>        //Implements Executor (EX)as an Alarm capability with Siren and Strobe via digital outputs to relays
#include <S_TimedRelay.h>    //Implements a Sensor to control a digital output pin with timing/cycle repeat capabilities
#include <EX_Switch_Dim.h>   //Implements an Executor (EX) for a switch (on/off) and pwm output (level) uses 2 digital output pins

//******************************************************************************************
//Define which Arduino Pins will be used for each device
//  Notes: -Serial Communications Pins are defined in Constants.h (avoid pins 0,1,2,3
//          for inputs and output devices below as they may be used for communications)
//         -Always avoid Pin 6 as it is reserved by the SmartThings Shield
//
//******************************************************************************************
//"RESERVED" pins for SmartThings ThingShield - best to avoid
#define PIN_O_RESERVED           0  //reserved by ThingShield for Serial communications OR USB Serial Monitor
#define PIN_1_RESERVED           1  //reserved by ThingShield for Serial communications OR USB Serial Monitor
#define pinTX                    2  //reserved by ThingShield for Serial communications
#define pinRX                    3  //reserved by ThingShield for Serial communications
#define PIN_6_RESERVED           6  //reserved by ThingShield (possible future use?)

//Digital Pins
#define PIN_MOTION_1 22
#define PIN_MOTION_2 23
#define PIN_CONTACT_1 24
#define PIN_CONTACT_2 25
#define PIN_CONTACT_3 26
#define PIN_CONTACT_4 27
#define PIN_CONTACT_5 28
#define PIN_CONTACT_6 29
#define PIN_CONTACT_7 30
#define PIN_CONTACT_8 31
#define PIN_CONTACT_9 32
#define PIN_CONTACT_10 33

//If using SoftwareSerial (e.g. Arduino UNO), must define pins for transmit and receive
//#define pinRX 3
//#define pinTX 2

//******************************************************************************************
//st::Everything::callOnMsgSend() optional callback routine. This is a sniffer to monitor
// data being sent to ST. This allows a user to act on data changes locally within the
// Arduino sktech.
//******************************************************************************************
void callback(const String &msg)
{
//Serial.print(F("ST_Anything Callback: Sniffed data = "));
//Serial.println(msg);

//TODO: Add local logic here to take action when a device’s value/state is changed

//Masquerade as the ThingShield to send data to the Arduino, as if from the ST Cloud (uncomment and edit following line)
//st::receiveSmartString("Put your command here!"); //use same strings that the Device Handler would send
}

//******************************************************************************************
//Arduino Setup() routine
//******************************************************************************************
void setup()
{
//******************************************************************************************
//Declare each Device that is attached to the Arduino
// Notes: - For each device, there is typically a corresponding "tile" defined in your
// SmartThings Device Hanlder Groovy code, except when using new COMPOSITE Device Handler
// - For details on each device’s constructor arguments below, please refer to the
// corresponding header (.h) and program (.cpp) files.
// - The name assigned to each device (1st argument below) must match the Groovy
// Device Handler names. (Note: "temphumid" below is the exception to this rule
// as the DHT sensors produce both "temperature" and "humidity". Data from that
// particular sensor is sent to the ST Hub in two separate updates, one for
// "temperature" and one for "humidity")
// - The new Composite Device Handler is comprised of a Parent DH and various Child
// DH’s. The names used below MUST not be changed for the Automatic Creation of
// child devices to work properly. Simply increment the number by +1 for each duplicate
// device (e.g. contact1, contact2, contact3, etc…) You can rename the Child Devices
// to match your specific use case in the ST Phone Application.
//******************************************************************************************
//Polling Sensors

//Interrupt Sensors
static st::IS_Motion sensor1(F("motion1"), PIN_MOTION_1, HIGH, false, 500);
static st::IS_Motion sensor2(F("motion2"), PIN_MOTION_2, HIGH, false, 500);
static st::IS_Contact sensor3(F("contact1"), PIN_CONTACT_1, LOW, true, 500);
static st::IS_Contact sensor4(F("contact2"), PIN_CONTACT_2, LOW, true, 500);
static st::IS_Contact sensor5(F("contact3"), PIN_CONTACT_3, LOW, true, 500);
static st::IS_Contact sensor6(F("contact4"), PIN_CONTACT_4, LOW, true, 500);
static st::IS_Contact sensor7(F("contact5"), PIN_CONTACT_5, LOW, true, 500);
static st::IS_Contact sensor8(F("contact6"), PIN_CONTACT_6, LOW, true, 500);
static st::IS_Contact sensor9(F("contact7"), PIN_CONTACT_7, LOW, true, 500);
static st::IS_Contact sensor10(F("contact8"), PIN_CONTACT_8, LOW, true, 500);
static st::IS_Contact sensor11(F("contact9"), PIN_CONTACT_9, LOW, true, 500);
static st::IS_Contact sensor12(F("contact10"), PIN_CONTACT_10, LOW, true, 500);

//Executors

//*****************************************************************************
// Configure debug print output from each main class
// -Note: Set these to "false" if using Hardware Serial on pins 0 & 1
// to prevent communication conflicts with the ST Shield communications
//*****************************************************************************
st::Everything::debug=true;
st::Executor::debug=true;
st::Device::debug=true;
st::PollingSensor::debug=true;
st::InterruptSensor::debug=true;

//*****************************************************************************
//Initialize the "Everything" Class
//*****************************************************************************

//Initialize the optional local callback routine (safe to comment out if not desired)
st::Everything::callOnMsgSend = callback;

//Create the SmartThings Thingshield Communications Object based on Arduino Model
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO) || defined(ARDUINO_AVR_MINI) //Arduino UNO, NANO, MINI
st::Everything::SmartThing = new st::SmartThingsThingShield(pinRX, pinTX, st::receiveSmartString); //Use Software Serial
#elif defined(ARDUINO_AVR_LEONARDO) //Arduino Leonardo
st::Everything::SmartThing = new st::SmartThingsThingShield(&Serial1, st::receiveSmartString); //Use Hardware Serial
#elif defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) //Arduino MEGA 1280 or 2560
st::Everything::SmartThing = new st::SmartThingsThingShield(&Serial3, st::receiveSmartString); //Use Hardware Serial
#else
//assume user is using an UNO for the unknown case
st::Everything::SmartThing = new st::SmartThingsThingShield(pinRX, pinTX, st::receiveSmartString); //Software Serial
#endif

//Run the Everything class’ init() routine which establishes communications with SmartThings
st::Everything::init();

//*****************************************************************************
//Add each sensor to the "Everything" Class
//*****************************************************************************
st::Everything::addSensor(&sensor1);
st::Everything::addSensor(&sensor2);
st::Everything::addSensor(&sensor3);
st::Everything::addSensor(&sensor4);
st::Everything::addSensor(&sensor5);
st::Everything::addSensor(&sensor6);
st::Everything::addSensor(&sensor7);
st::Everything::addSensor(&sensor8);
st::Everything::addSensor(&sensor9);
st::Everything::addSensor(&sensor10);
st::Everything::addSensor(&sensor11);
st::Everything::addSensor(&sensor12);
//*****************************************************************************
//Add each executor to the "Everything" Class
//*****************************************************************************

//*****************************************************************************
//Initialize each of the devices which were added to the Everything Class
//*****************************************************************************
st::Everything::initDevices();
}

//******************************************************************************************
//Arduino Loop() routine
//******************************************************************************************
void loop()
{
//*****************************************************************************
//Execute the Everything run method which takes care of "Everything"
//*****************************************************************************
st::Everything::run();

}