Sunday, November 23, 2014

Smartthings Shield with Due update / Smartthings Shield with Mega 2560

So some bad news I guess I didn't realize the Due board would not work with the Shield. I am new to Arduino so not realizing that Duemilanova is not the same as Due is an understandable mistake. I picked up a mega 2560.

I again needed something with many inputs. I read some of the forum post about it working for other people but no one actually shared their project.

I need to the project to read the input of many different pins and upload that data to the smartthings cloud.

Before I upload the project below. I removed the shield from the app.


I setup my circuit as shown in the figure below.






Here is the code I used:


//*****************************************************************************
/// @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


//define all the input pins
#define DIG_INPUT20      20
#define DIG_INPUT21      21
#define DIG_INPUT22      22
#define DIG_INPUT23      23
#define DIG_INPUT24      24
#define DIG_INPUT25      25
#define DIG_INPUT26      26
#define DIG_INPUT27      27
#define DIG_INPUT28      28
#define DIG_INPUT29      29
#define DIG_INPUT30      30
#define DIG_INPUT31      31
#define DIG_INPUT32      32
#define DIG_INPUT33      33
#define DIG_INPUT34      34
#define DIG_INPUT35      35
#define DIG_INPUT36      36
#define DIG_INPUT37      37
#define DIG_INPUT38      38
#define DIG_INPUT39      39
#define DIG_INPUT40      40
#define DIG_INPUT41      41
#define DIG_INPUT42      42
#define DIG_INPUT43      43
#define DIG_INPUT44      44
#define DIG_INPUT45      45
#define DIG_INPUT46      46
#define DIG_INPUT47      47
#define DIG_INPUT48      48
#define DIG_INPUT49      49
#define DIG_INPUT50      50
#define DIG_INPUT51      51
#define DIG_INPUT52      52
#define DIG_INPUT53      53

//*****************************************************************************
// 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,"Generic Shield", true);  // 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 

//Last state of pin
int pin20state;
int pin21state;
int pin22state;
int pin23state;
int pin24state;
int pin25state;
int pin26state;
int pin27state;
int pin28state;
int pin29state;
int pin30state;
int pin31state;
int pin32state;
int pin33state;
int pin34state;
int pin35state;
int pin36state;
int pin37state;
int pin38state;
int pin39state;
int pin40state;
int pin41state;
int pin42state;
int pin43state;
int pin44state;
int pin45state;
int pin46state;
int pin47state;
int pin48state;
int pin49state;
int pin50state;
int pin51state;
int pin52state;
int pin53state;

//*****************************************************************************
// 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 setPins()
{
pin20state = digitalRead(DIG_INPUT20);
 pin21state= digitalRead(DIG_INPUT21);
 pin22state= digitalRead(DIG_INPUT22);
 pin23state= digitalRead(DIG_INPUT23);
 pin24state= digitalRead(DIG_INPUT24);
 pin25state= digitalRead(DIG_INPUT25);
 pin26state= digitalRead(DIG_INPUT26);
 pin27state= digitalRead(DIG_INPUT27);
 pin28state= digitalRead(DIG_INPUT28);
 pin29state= digitalRead(DIG_INPUT29);
 pin30state= digitalRead(DIG_INPUT30);
 pin31state= digitalRead(DIG_INPUT31);
 pin32state= digitalRead(DIG_INPUT32);
 pin33state= digitalRead(DIG_INPUT33);
 pin34state= digitalRead(DIG_INPUT34);
 pin35state= digitalRead(DIG_INPUT35);
 pin36state= digitalRead(DIG_INPUT36);
 pin37state= digitalRead(DIG_INPUT37);
 pin38state= digitalRead(DIG_INPUT38);
 pin39state= digitalRead(DIG_INPUT39);
 pin40state= digitalRead(DIG_INPUT40);
 pin41state= digitalRead(DIG_INPUT41);
 pin42state= digitalRead(DIG_INPUT42);
 pin43state= digitalRead(DIG_INPUT43);
 pin44state= digitalRead(DIG_INPUT44);
 pin45state= digitalRead(DIG_INPUT45);
 pin46state= digitalRead(DIG_INPUT46);
 pin47state= digitalRead(DIG_INPUT47);
 pin48state= digitalRead(DIG_INPUT48);
 pin49state= digitalRead(DIG_INPUT49);
 pin50state= digitalRead(DIG_INPUT50);
 pin51state= digitalRead(DIG_INPUT51);
 pin52state= digitalRead(DIG_INPUT52);
 pin53state= digitalRead(DIG_INPUT53);

 Serial.print("Pin20 ");
 Serial.println(DIG_INPUT20);

 Serial.print("Pin21 ");
 Serial.println(DIG_INPUT21);

 Serial.print("Pin22 ");
 Serial.println(DIG_INPUT22);

 Serial.print("Pin23 ");
 Serial.println(DIG_INPUT23);

 Serial.print("Pin24 ");
 Serial.println(DIG_INPUT24);

 Serial.print("Pin25 ");
 Serial.println(DIG_INPUT25);

 Serial.print("Pin26 ");
 Serial.println(DIG_INPUT26);

 Serial.print("Pin27 ");
 Serial.println(DIG_INPUT27);

 Serial.print("Pin28 ");
 Serial.println(DIG_INPUT28);

 Serial.print("Pin29 ");
 Serial.println(DIG_INPUT29);

 Serial.print("Pin30 ");
 Serial.println(DIG_INPUT30);

 Serial.print("Pin31 ");
 Serial.println(DIG_INPUT31);

 Serial.print("Pin32 ");
 Serial.println(DIG_INPUT32);

 Serial.print("Pin33 ");
 Serial.println(DIG_INPUT33);

 Serial.print("Pin34 ");
 Serial.println(DIG_INPUT34);

 Serial.print("Pin35 ");
 Serial.println(DIG_INPUT35);

 Serial.print("Pin36 ");
 Serial.println(DIG_INPUT36);

 Serial.print("Pin37 ");
 Serial.println(DIG_INPUT37);

 Serial.print("Pin38 ");
 Serial.println(DIG_INPUT38);

 Serial.print("Pin39 ");
 Serial.println(DIG_INPUT39);

 Serial.print("Pin40 ");
 Serial.println(DIG_INPUT40);

 Serial.print("Pin41 ");
 Serial.println(DIG_INPUT41);

 Serial.print("Pin42 ");
 Serial.println(DIG_INPUT42);

 Serial.print("Pin43 ");
 Serial.println(DIG_INPUT43);

 Serial.print("Pin44 ");
 Serial.println(DIG_INPUT44);

 Serial.print("Pin45 ");
 Serial.println(DIG_INPUT45);

 Serial.print("Pin46 ");
 Serial.println(DIG_INPUT46);

Serial.print("Pin47 ");
 Serial.println(DIG_INPUT47);

Serial.print("Pin48 ");
 Serial.println(DIG_INPUT48);

Serial.print("Pin49 ");
 Serial.println(DIG_INPUT49);

Serial.print("Pin50 ");
 Serial.println(DIG_INPUT50);

Serial.print("Pin51 ");
 Serial.println(DIG_INPUT51);

Serial.print("Pin52 ");
 Serial.println(DIG_INPUT52);

Serial.print("Pin53 ");
 Serial.println(DIG_INPUT53);

}

//*****************************************************************************
void checkChange()
{
  
 int TempPin20state = digitalRead(DIG_INPUT20);
 int TempPin21state= digitalRead(DIG_INPUT21);
 int TempPin22state= digitalRead(DIG_INPUT22);
 int TempPin23state= digitalRead(DIG_INPUT23);
 int TempPin24state= digitalRead(DIG_INPUT24);
 int TempPin25state= digitalRead(DIG_INPUT25);
 int TempPin26state= digitalRead(DIG_INPUT26);
 int TempPin27state= digitalRead(DIG_INPUT27);
 int TempPin28state= digitalRead(DIG_INPUT28);
 int TempPin29state= digitalRead(DIG_INPUT29);
 int TempPin30state= digitalRead(DIG_INPUT30);
 int TempPin31state= digitalRead(DIG_INPUT31);
 int TempPin32state= digitalRead(DIG_INPUT32);
 int TempPin33state= digitalRead(DIG_INPUT33);
 int TempPin34state= digitalRead(DIG_INPUT34);
 int TempPin35state= digitalRead(DIG_INPUT35);
 int TempPin36state= digitalRead(DIG_INPUT36);
 int TempPin37state= digitalRead(DIG_INPUT37);
 int TempPin38state= digitalRead(DIG_INPUT38);
 int TempPin39state= digitalRead(DIG_INPUT39);
 int TempPin40state= digitalRead(DIG_INPUT40);
 int TempPin41state= digitalRead(DIG_INPUT41);
 int TempPin42state= digitalRead(DIG_INPUT42);
 int TempPin43state= digitalRead(DIG_INPUT43);
 int TempPin44state= digitalRead(DIG_INPUT44);
 int TempPin45state= digitalRead(DIG_INPUT45);
 int TempPin46state= digitalRead(DIG_INPUT46);
 int TempPin47state= digitalRead(DIG_INPUT47);
 int TempPin48state= digitalRead(DIG_INPUT48);
 int TempPin49state= digitalRead(DIG_INPUT49);
 int TempPin50state= digitalRead(DIG_INPUT50);
 int TempPin51state= digitalRead(DIG_INPUT51);
 int TempPin52state= digitalRead(DIG_INPUT52);
 int TempPin53state= digitalRead(DIG_INPUT53); 
  
 if (TempPin20state==pin20state)
 {
   
 } else {
 Serial.print("Pin20 Changed");
  smartthing.send("Pin20 Changed");   
 }
 if (TempPin21state==pin21state)  
 {   
 } else {      
 Serial.print("Pin21 Changed");
  smartthing.send("Pin21 Changed"); 
 }
 if (TempPin22state==pin22state)  
 {      
 } else {      
 Serial.print("Pin22 Changed");
  smartthing.send("Pin22 Changed"); 
 }
 if (TempPin23state==pin23state)  
 {      
 } else {      
 Serial.print("Pin23 Changed");
  smartthing.send("Pin23 Changed"); 
 }
 if (TempPin24state==pin24state)  
 {      
 } else {      
 Serial.print("Pin24 Changed");
  smartthing.send("Pin24 Changed"); 
 }
 if (TempPin25state==pin25state)  
 {      
 } else {      
 Serial.print("Pin25 Changed");
  smartthing.send("Pin25 Changed"); 
 }
 if (TempPin26state==pin26state)  
 {      
 } else {       
 Serial.print("Pin26 Changed");
  smartthing.send("Pin26 Changed"); 
}
 if (TempPin27state==pin27state)  
 {      
 } else {      
 Serial.print("Pin27 Changed");
  smartthing.send("Pin27 Changed"); 
 }
 if (TempPin28state==pin28state)  
 {      
 } else {      
 Serial.print("Pin28 Changed");
  smartthing.send("Pin28 Changed"); 
 }
 if (TempPin29state==pin29state)  
 {      
 } else {      
 Serial.print("Pin29 Changed");
  smartthing.send("Pin29 Changed"); 
 }
 if (TempPin30state==pin30state)  
 {      
 } else {      
 Serial.print("Pin30 Changed");
  smartthing.send("Pin30 Changed"); 
 }
 if (TempPin31state==pin31state)  
 {      
 } else {      
 Serial.print("Pin31 Changed");
  smartthing.send("Pin31 Changed"); 
 }
 if (TempPin32state==pin32state)  
 {      
 } else {      
 Serial.print("Pin32 Changed");
  smartthing.send("Pin32 Changed"); 
 }
 if (TempPin33state==pin33state)  
 {      
 } else {      
 Serial.print("Pin33 Changed");
  smartthing.send("Pin33 Changed"); 
 }
 if (TempPin34state==pin34state)  
 {      
 } else {      
 Serial.print("Pin34 Changed");
  smartthing.send("Pin34 Changed"); 
 }
 if (TempPin35state==pin35state)  
 {      
 } else {      
 Serial.print("Pin35 Changed");
  smartthing.send("Pin35 Changed"); 
 }
 if (TempPin36state==pin36state)  
 {      
 } else {      
 Serial.print("Pin36 Changed");
  smartthing.send("Pin36 Changed"); 
 }
 if (TempPin37state==pin37state)  
 {      
 } else {      
 Serial.print("Pin37 Changed");
  smartthing.send("Pin37 Changed"); 
 }
 if (TempPin38state==pin38state)  
 {      
 } else {      
 Serial.print("Pin38 Changed");
  smartthing.send("Pin38 Changed"); 
 }
 if (TempPin39state==pin39state)  
 {      
 } else {      
 Serial.print("Pin39 Changed");
  smartthing.send("Pin39 Changed"); 
 }
 if (TempPin40state==pin40state)  
 {      
 } else {      
 Serial.print("Pin40 Changed");
  smartthing.send("Pin40 Changed"); 
 }
 if (TempPin41state==pin41state)  
 {      
 } else {      
 Serial.print("Pin41 Changed");
  smartthing.send("Pin41 Changed"); 
 }
 if (TempPin42state==pin42state)  
 {      
 } else {      
 Serial.print("Pin42 Changed");
  smartthing.send("Pin42 Changed"); 
 }
 if (TempPin43state==pin43state)  
 {      
 } else {      
 Serial.print("Pin43 Changed");
  smartthing.send("Pin43 Changed"); 
 }
 if (TempPin44state==pin44state)  
 {      
 } else {      
 Serial.print("Pin44 Changed");
  smartthing.send("Pin44 Changed"); 
 }
 if (TempPin45state==pin45state)  
 {      
 } else {      
 Serial.print("Pin45 Changed");
  smartthing.send("Pin45 Changed"); 
 }
 if (TempPin46state==pin46state)  
 {      
 } else {      

 Serial.print("Pin46 Changed");
  smartthing.send("Pin46 Changed"); 
}
 if (TempPin47state==pin47state)  
 {      
 } else {      
 Serial.print("Pin47 Changed");
  smartthing.send("Pin47 Changed"); 
 }
 if (TempPin48state==pin48state)  
 {      
 } else {      
 Serial.print("Pin48 Changed");
  smartthing.send("Pin48 Changed"); 
 }
 if (TempPin49state==pin49state)  
 {      
 } else {      
 Serial.print("Pin49 Changed");
  smartthing.send("Pin49 Changed"); 
 }
 if (TempPin50state==pin50state)  
 {      
 } else {      
 Serial.print("Pin50 Changed");
  smartthing.send("Pin50 Changed"); 
 }
 if (TempPin51state==pin51state)  
 {      
 } else {
Serial.print("Pin51 Changed");
  smartthing.send("Pin51 Changed");    
 }
 if (TempPin52state==pin52state)  
 {      
 } else {
Serial.print("Pin52 Changed");
  smartthing.send("Pin52 Changed");    
 }
 if (TempPin53state==pin53state)  
 {      
 } else {
  Serial.print("Pin53 Changed");
  smartthing.send("Pin53 Changed");  
 }
}
//*****************************************************************************
void setNetworkStateLED()
{
  SmartThingsNetworkState_t tempState = smartthing.shieldGetLastNetworkState();
  Serial.println(tempState);
  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()
{
  smartthing.shieldSetLED(2, 0, 0);
  // 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
  
  //setup digital pins
smartthing.shieldSetLED(0, 1, 1);
pinMode(DIG_INPUT20, INPUT);
pinMode(DIG_INPUT21, INPUT);
pinMode(DIG_INPUT22, INPUT);
pinMode(DIG_INPUT23, INPUT);
pinMode(DIG_INPUT24, INPUT);
pinMode(DIG_INPUT25, INPUT);
pinMode(DIG_INPUT26, INPUT);
pinMode(DIG_INPUT27, INPUT);
pinMode(DIG_INPUT28, INPUT);
pinMode(DIG_INPUT29, INPUT);
pinMode(DIG_INPUT30, INPUT);
pinMode(DIG_INPUT31, INPUT);
pinMode(DIG_INPUT32, INPUT);
pinMode(DIG_INPUT33, INPUT);
pinMode(DIG_INPUT34, INPUT);
pinMode(DIG_INPUT35, INPUT);
pinMode(DIG_INPUT36, INPUT);
pinMode(DIG_INPUT37, INPUT);
pinMode(DIG_INPUT38, INPUT);
pinMode(DIG_INPUT39, INPUT);
pinMode(DIG_INPUT40, INPUT);
pinMode(DIG_INPUT41, INPUT);
pinMode(DIG_INPUT42, INPUT);
pinMode(DIG_INPUT43, INPUT);
pinMode(DIG_INPUT44, INPUT);
pinMode(DIG_INPUT45, INPUT);
pinMode(DIG_INPUT46, INPUT);
pinMode(DIG_INPUT47, INPUT);
pinMode(DIG_INPUT48, INPUT);
pinMode(DIG_INPUT49, INPUT);
pinMode(DIG_INPUT50, INPUT);
pinMode(DIG_INPUT51, INPUT);
pinMode(DIG_INPUT52, INPUT);
pinMode(DIG_INPUT53, INPUT);

setPins();

  if (isDebugEnabled)
  { // setup debug serial port
    Serial.begin(9600);         // setup serial with a baud rate of 9600
    smartthing.shieldLeaveNetwork();
    Serial.println("setup..");  // print out 'setup..' on start
    delay(400);
    smartthing.shieldFindNetwork();
  }
}

//*****************************************************************************
void loop()
{
  // run smartthing logic
  smartthing.run();
  setNetworkStateLED();
  checkChange();
  delay(100);
  smartthing.shieldGetNetworkState();
  smartthing.shieldGetNodeID();
}

//*****************************************************************************
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();
  }
}


I added the shield back through the app.



Whenever a pin changes it sends a message to the smartthings cloud. This is exactly what I wanted. This project is just monitoring the inputs.





I randomly tested some pins to see the response time.

It took (27:61 (pin 52), 8:83 (pin 34), 30:12 (pin 41)) secs to fire the event off in the smartthings cloud.

I know this is from the code execution and systematically checking each input and then doing a compare.


Here are some extra pictures of the setup.







 I need to clean up the code on the arduino and then start programming the applet in smartthings graph.

My idea is each sensor (input into the arduino):

  • on
  • off
In the cloud each sensor:

  • on
  • off
  • home (monitor when site is home)
  • away (monitor when site is away) 

 I will post the final arduino and smartthing code.

No comments:

Post a Comment