IP Configuration
This commit is contained in:
162
lib/SmartThings/README.md
Normal file
162
lib/SmartThings/README.md
Normal file
@@ -0,0 +1,162 @@
|
||||
History:
|
||||
- v2.0 2017-02-11 Initial Release
|
||||
- v2.1 2017-02-20 Added support for Arduino + ESP-01 hardware combination
|
||||
- v2.2 2017-04-26 Added additional optional argument to each constructor which can be queried to determine maximum data transfer rate for each type of communication. This field is not used directly by the SmartThings library. It is used by ST_Anything's Everything Class.
|
||||
- v2.3 2017-05-25 Added support for the W5500 Ethernet Shield. This required the library to be split into multiple library folders to avoid duplicate class defintions at compile/link time.
|
||||
- v2.4 2017-08-16 Added support for the ESP32
|
||||
- v2.5 2017-01-06 Added Arduino OTA support for the ESP8266 boards
|
||||
|
||||
SmartThings v2.x
|
||||
================
|
||||
This is a new version of the old SmartThings Arduino Library created by SmartThings for use with their Zigbee-based ThingShield. Recently, SmartThings has decided to no longer produce nor support the ThingShield. In an attempt to provide the Maker Community with alternatives, I decided to re-architect the C++ Class hierarchy of the SmartThings library to include continued support for the old ThingShield, as well as adding new support for the Arduino + Ethernet W5100 shield, Arduino + ESP-01 WiFi board, and the standalone NodeMCU ESP8266 and ESP-01 boards. This allows users five options for connecting their DIY projects to SmartThings.
|
||||
|
||||
This library currently implements the following C++ Classes and associated constructors:
|
||||
-st::SmartThings (base class, not to be used in your sketch!)
|
||||
-st::SmartThingsThingShield (use this class "#include <SmartThingsThingShield.h" if connecting an Arduino UNO/MEGA using a ThingShield)
|
||||
-st::SmartThingsEthernet (base class, not to be used in your sketch!)
|
||||
-st::SmartThingsEthernetW5100 (use this class "#include <SmartThingsEthernetW5100.h" if connecting an Arduino UNO/MEGA using a W5100 Ethernet shield)
|
||||
-st::SmartThingsEthernetW5500 (use this class "#include <SmartThingsEthernetW5500.h" if connecting an Arduino UNO/MEGA using a W5500 Ethernet shield)
|
||||
-st::SmartThingsWiFiEsp (use this class "#include <SmartThingsWiFiEsp.h>" if using an Arduino attached to a ESP-01 (AT Firmware) for WiFi)
|
||||
-st::SmartThingsESP8266WiFi (use this class "#include <SmartThingsESP8266.h" if using a standalone NodeMCU ESP8266 or ESP-01)
|
||||
-st::SmartThingsESP32WiFi (use this class "#include <SmartThingsESP32.h" if using a standalone ESP32)
|
||||
|
||||
All five of the usable classes implement the following methods:
|
||||
- send(String) used to send an ASCII string to the SmartThings Device Handler
|
||||
- run() used in the sketch's loop() routine to execute background processing for the library to check for incoming data from the SmartThings Device Handler
|
||||
- init() used in the sketch's setup() routine to establish the initial connection to the ST Hub
|
||||
|
||||
## Overview
|
||||
SmartThings library consists of:
|
||||
- Arduino SmartThings*.h and .cpp files to implement the class hierarchy show above.
|
||||
- In the `examples` folder, sample sketches for using the Arduino/ThingShield, Arduino/EthernetW5100, Arduino/ESP-01, and NodeMCU ESP8266.
|
||||
- SmartThings_On_Off_LED_ThingShield
|
||||
- SmartThings_On_Off_LED_EthernetW5100
|
||||
- SmartThings_On_Off_LED_EthernetW5500
|
||||
- SmartThings_On_Off_LED_WiFiEsp
|
||||
- SmartThings_On_Off_LED_ESP8266WiFi (no Arduino!)
|
||||
- SmartThings_On_Off_LED_ESP01WiFi (no Arduino!)
|
||||
- In the `extras` folder, two SmartThings Groovy Device Handlers to be used with the example sketches above
|
||||
-On_Off_LED_ThingShield.device.groovy for use with a ThingShield
|
||||
-On_Off_LED_Ethernet.device.groovy for use with
|
||||
- Arduino/W5100 combo
|
||||
- Arduino/W5500 combo
|
||||
- Arduino/ESP-01 combo (not reliable currently)
|
||||
- NodeMCU ESP8266 board (standalone)
|
||||
- ESP-01 module (standalone)
|
||||
|
||||
|
||||
##SmartThings Library Installation Instructions
|
||||
- Download the SmartThings library and copy it to your your Arduino "libraries" folder
|
||||
- On Windows, it's located in `C:\My Documents\Arduino\libraries`
|
||||
- On Mac, it's located in `~/Documents/Arduino/libraries`
|
||||
- Restart the Arduino IDE so it will scan the new library
|
||||
- Assumption: If you're using an ESP8266 or ESP32 based board, you should already have added support for it to the IDE (Google it if needed.)
|
||||
|
||||
##Pre-Requisites for using Ethernet based connectivity (Arduino/W5100, Arduino/ESP-01, NodeMCU ESP8266, ESP-01)
|
||||
- Your SmartThings HUB must have a Static TCP/IP Address assigned via your router's DHCP Server. Since this procedure varies by router model, Google it!
|
||||
- You'll need to identify a static TCP/IP address for your Arduino/W5100, Arduino/ESP-01, NodeMCU ESP8266, or ESP-01, as you'll need this later when setting up the sketch. Choose an unused IP address outside of the range your router's DHCP server uses.
|
||||
|
||||
##Arduino IDE Instructions
|
||||
- Open the Arduino IDE and select File->Examples->SmartThings
|
||||
- Select the example sketch that matches your hardware
|
||||
- Select File->Save As and select your "Sketches" folder, and click "Save"
|
||||
- If using Arduino/W5100, Arduino/ESP-01 or ESP8266, find the lines of the Sketch where it says "<---You must edit this line!"
|
||||
- The Arduino must be assigned a static TCP/IP address, Gateway, DNS, Subnet Mask, MAC Address (W5100 only), SSID + Password (WiFiEsp + ESP8266 only)
|
||||
- *** NOTE: If using the W5100 Shield, YOU MUST ASSIGN IT A UNIQUE MAC ADDRESS in the sketch! Please leave the first octet in the MAC Address '06' as certain MAC addresses are UNICAST while others are MULTICAST. Your MAC must be UNICAST and be a 'Locally Administered Address' Please see https://en.wikipedia.org/wiki/MAC_address#Address_details for more information ***
|
||||
- *** NOTE: If using the W5500 Shield, YOU MUST ASSIGN IT THE MAC ADDRESS that came with the shield in the sketch!
|
||||
- Note: If using an ESP-01 with an Arduino, the example assumes you're using an Arduino MEGA 2560. Attach the ESP-01 to Hardware Serial "Serial1" *** THE ARDUINO + ESP01 is currently not very stable!
|
||||
- Your IDE Serial Monitor Window should be set to 9600 baud
|
||||
- With the Serial Monitor windows open, load your sketch and watch the output
|
||||
- If using an Arduino/ESP-01, NodeMCU ESP8266 board, ESP-01, or ESP32 the MAC Address will be printed out in the serial monitor window. Write this down as you will need it to configure the Device using your ST App on your phone. (Note: MAC Address must later be entered with no delimeters in the form of "06AB23CD45EF" (without quotes!))
|
||||
|
||||
##SmartThings IDE Device Handler Installation Instructions
|
||||
- Create an account and/or log into the SmartThings Developers Web IDE.
|
||||
|
||||
Arduino/ThingShield
|
||||
- If using a ThingShield, join it to the SmartThings hub by using the ST App on your phone - Add new device
|
||||
- Install the ThingShield example Device Handler
|
||||
- Click on "My Device Handlers" from the navigation menu.
|
||||
- Click on "+ New Device Handler" button.
|
||||
- Select the "From Code" Tab near the top of the page
|
||||
- Paste the code from the `On_Off_LED_ThingShield.device.groovy` file from the `..Arduino\libraries\SmartThings\extras` folder
|
||||
- Click on "Create" near the bottom of the page.
|
||||
- Click on "Save" in the IDE.
|
||||
- Click on "Publish" -> "For Me" in the IDE.
|
||||
- Click on "My Devices" from navigation menu
|
||||
- Select your "Arduino ThingShield" device from the list
|
||||
- Click the Edit button at the bottom of the screen
|
||||
- Change the Type to "On/Off ThingShield"
|
||||
- Click the Update button at the bottom of the screen
|
||||
- On your phone, you should now see a device that has simple On/Off tile
|
||||
|
||||
Ethernet Arduino/W5100, Arduino/W5500, Arduino/ESP-01, NodeMCU ESP8266, ESP-01, or ESP32
|
||||
- Install the Ethernet example Device Handler
|
||||
- Click on "+ New Device Handler" button.
|
||||
- Select the "From Code" Tab near the top of the page
|
||||
- Paste the code from the `On_Off_LED_Ethernet.device.groovy` file from the `..Arduino\libraries\SmartThings\extras` folder
|
||||
- Click on "Create" near the bottom of the page.
|
||||
- Click on "Save" in the IDE.
|
||||
- Click on "Publish" -> "For Me" in the IDE.
|
||||
- Click on "My Devices" from navigation menu
|
||||
- Click on the "+ New Device" button
|
||||
- Enter a "Name" (whatever you like)
|
||||
- Enter a "Label" (whatever you like)
|
||||
- Change the Type to "On/Off Ethernet"
|
||||
- Change "Version" to "Published"
|
||||
- Select your "Location"
|
||||
- Select your "Hub"
|
||||
- Click "Create"
|
||||
- On your phone, you should now see a device that has simple "On/Off" tile and a "Configure" tile
|
||||
- One your phone, after selecting the new device, click on the settings icon (small gear in top right corner)
|
||||
- Enter your Arduino or ESP8266 device's TCP/IP Address, Port, and MAC Address (you should already know these from when you configured your sketch)
|
||||
- Note: MAC Address should be entered with no delimeters in the form of "06AB23CD45EF" (no quotes!)
|
||||
- Click "Done"
|
||||
- CLick the "Configure" tile to make sure the 'networkDeviceID' for your new ST Device is set to the MAC address you just entered
|
||||
|
||||
- Note: If you're trying to use a ESP-01 WiFi board with an Arduino MEGA, please make sure the ESP-01 is running the "AT Firmware" and figure out the baudrate ahead of time. Please use the examples in the WiFiEsp library to get the ESP-01 wired and working on "Serial1" before attempting to use this library. If you need further help on this, please Google "Arduino MEGA ESP-01" and I am sure you will find some good guides. Also, please note that the Arduino boards really are not rated to power the ESP-01 from the Arduino's 3.3v pin. I had to use an external 3.3v power supply for the ESP-01 to get everything working reliably. Just remember to tie the 3.3v powersupply GND to the Arduino GND. Do NOT connect the +3.3v power supply to the Arduino's 3.3v pin. This would be bad! You may also want to "level shift" the Arduino's 5v Tx pin down to 3.3v before connecting it to the Rx pin on the ESP-01. Some folks online say it is a must, others say the ESP-01 input is 5v tolerant. YMMV.
|
||||
|
||||
.
|
||||
.
|
||||
.
|
||||
|
||||
###WARNING - Geeky Material Ahead!!!
|
||||
|
||||
.
|
||||
.
|
||||
.
|
||||
|
||||
The following applies only to using the ThingShield!
|
||||
|
||||
If you want to use the new SmartThings v2.x libary with your existing ThingShield sketches (or to just learn more about how all this stuff works, keep reading...)
|
||||
|
||||
Remember to "#include <SmartThingsThingShield.h>" to use the new v2.x SmartThings library with the ThingShield.
|
||||
|
||||
The Arduino UNO uses the SoftwareSerial library constructor since the UNO has only one Hardware UART port ("Serial") which is used by the USB port for programming and debugging.
|
||||
Arduino UNO R3 SoftwareSerial:
|
||||
- Use the NEW SoftwareSerial constructor passing in pinRX=3 and pinTX=2
|
||||
- st::SmartThingsThingShield(uint8_t pinRX, uint8_t pinTX, SmartThingsCallout_t *callout) call.
|
||||
- Make sure the ThingShield's switch in the "D2/D3" position
|
||||
- Be certain to not use Pins 2 & 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. Also, pins 0 & 1 are used for USB communications, so do not use them if possible.
|
||||
|
||||
The new v2.x SmartThings ThingShield library automatically disables support for Software Serial if using a Leonardo or MEGA based Arduino board.
|
||||
|
||||
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 (&Serial1, &Serial2, &Serial3)
|
||||
- definition st::SmartThingsThingShield(HardwareSerial* serial, SmartThingsCallout_t *callout);
|
||||
- sample st::SmartThingsThingShield(&Serial3, callout);
|
||||
- Make sure the ThingShield's switch in the "D2/D3" position
|
||||
- Be certain to not use Pins 2 & 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 & 3 on the ThingShield. For example, using Serial3, wire Pin 2 to Pin 14 AND Pin3 to Pin 15.
|
||||
|
||||
The Arduino Leonardo must use HardwareSerial "Serial1" for communications with the ThingShield.
|
||||
Leonardo Hardware Serial:
|
||||
- Use the new Hardware Serial constructor passing in a pointer to a Hardware Serial device (&Serial1)
|
||||
- definition st::SmartThingsThingShield(HardwareSerial* serial, SmartThingsCallout_t *callout);
|
||||
- sample st::SmartThingsThingShield(&Serial1, callout);
|
||||
- Make sure the ThingShield's switch in the "D0/D1" position
|
||||
- Be certain to not use Pins 0 & 1 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.
|
||||
|
||||
|
||||
|
||||
34
lib/SmartThings/SmartThings.cpp
Normal file
34
lib/SmartThings/SmartThings.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
//*******************************************************************************
|
||||
// SmartThings Arduino Library Base Class
|
||||
//
|
||||
// License
|
||||
// (C) Copyright 2017 Dan Ogorchock
|
||||
//
|
||||
// History
|
||||
// 2017-02-04 Dan Ogorchock Created
|
||||
//*******************************************************************************
|
||||
#include <SmartThings.h>
|
||||
|
||||
namespace st
|
||||
{
|
||||
//*******************************************************************************
|
||||
// SmartThings Constructor
|
||||
//*******************************************************************************
|
||||
SmartThings::SmartThings(SmartThingsCallout_t *callout, String shieldType, bool enableDebug, int transmitInterval) :
|
||||
_calloutFunction(callout),
|
||||
_shieldType(shieldType),
|
||||
_isDebugEnabled(enableDebug),
|
||||
m_nTransmitInterval(transmitInterval)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//SmartThings::~SmartThings()
|
||||
//*****************************************************************************
|
||||
SmartThings::~SmartThings()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
67
lib/SmartThings/SmartThings.h
Normal file
67
lib/SmartThings/SmartThings.h
Normal file
@@ -0,0 +1,67 @@
|
||||
//*******************************************************************************
|
||||
// SmartThings Arduino Library Base Class
|
||||
//
|
||||
// License
|
||||
// (C) Copyright 2017 Dan Ogorchock
|
||||
//
|
||||
// History
|
||||
// 2017-02-04 Dan Ogorchock Created
|
||||
//*******************************************************************************
|
||||
#ifndef __SMARTTHINGS_H__
|
||||
#define __SMARTTHINGS_H__
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
//*******************************************************************************
|
||||
// Callout Function Definition for Messages Received from SmartThings
|
||||
//*******************************************************************************
|
||||
typedef void SmartThingsCallout_t(String message);
|
||||
|
||||
namespace st
|
||||
{
|
||||
class SmartThings
|
||||
{
|
||||
private:
|
||||
|
||||
protected:
|
||||
SmartThingsCallout_t *_calloutFunction;
|
||||
bool _isDebugEnabled;
|
||||
String _shieldType;
|
||||
int m_nTransmitInterval;
|
||||
|
||||
public:
|
||||
|
||||
//*******************************************************************************
|
||||
// SmartThings Constructor
|
||||
//*******************************************************************************
|
||||
SmartThings(SmartThingsCallout_t *callout, String shieldType = "Unknown", bool enableDebug = false, int transmitInterval = 100);
|
||||
|
||||
//*******************************************************************************
|
||||
// SmartThings Destructor
|
||||
//*******************************************************************************
|
||||
virtual ~SmartThings();
|
||||
|
||||
//*******************************************************************************
|
||||
/// Initialize SmartThings Library
|
||||
//*******************************************************************************
|
||||
virtual void init(void) = 0; //all derived classes must implement this pure virtual function
|
||||
|
||||
//*******************************************************************************
|
||||
/// Run SmartThings Library
|
||||
//*******************************************************************************
|
||||
virtual void run(void) = 0; //all derived classes must implement this pure virtual function
|
||||
|
||||
//*******************************************************************************
|
||||
/// Send Message to the Hub
|
||||
//*******************************************************************************
|
||||
virtual void send(String message) = 0; //all derived classes must implement this pure virtual function
|
||||
|
||||
//*******************************************************************************
|
||||
/// Send Message to the Hub
|
||||
//*******************************************************************************
|
||||
virtual int getTransmitInterval() const { return m_nTransmitInterval; }
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
67
lib/SmartThings/SmartThingsEthernet.cpp
Normal file
67
lib/SmartThings/SmartThingsEthernet.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
//*******************************************************************************
|
||||
// SmartThings Arduino Ethernet Library
|
||||
//
|
||||
// License
|
||||
// (C) Copyright 2017 Dan Ogorchock
|
||||
//
|
||||
// History
|
||||
// 2017-02-04 Dan Ogorchock Created
|
||||
//*******************************************************************************
|
||||
|
||||
#include "SmartThingsEthernet.h"
|
||||
|
||||
namespace st
|
||||
{
|
||||
//*******************************************************************************
|
||||
// SmartThingsEthernet Constructor - Static IP
|
||||
//*******************************************************************************
|
||||
SmartThingsEthernet::SmartThingsEthernet(IPAddress localIP, IPAddress localGateway, IPAddress localSubnetMask, IPAddress localDNSServer, uint16_t serverPort, IPAddress hubIP, uint16_t hubPort, SmartThingsCallout_t *callout, String shieldType, bool enableDebug, int transmitInterval, bool DHCP) :
|
||||
SmartThings(callout, shieldType, enableDebug, transmitInterval),
|
||||
st_localIP(localIP),
|
||||
st_localGateway(localGateway),
|
||||
st_localSubnetMask(localSubnetMask),
|
||||
st_localDNSServer(localDNSServer),
|
||||
st_hubIP(hubIP),
|
||||
st_serverPort(serverPort),
|
||||
st_hubPort(hubPort),
|
||||
st_DHCP(DHCP)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//*******************************************************************************
|
||||
// SmartThingsEthernet Constructor - Static IP (Abbreviated version for WiFiEsp library)
|
||||
//*******************************************************************************
|
||||
SmartThingsEthernet::SmartThingsEthernet(IPAddress localIP, uint16_t serverPort, IPAddress hubIP, uint16_t hubPort, SmartThingsCallout_t *callout, String shieldType, bool enableDebug, int transmitInterval, bool DHCP) :
|
||||
SmartThings(callout, shieldType, enableDebug, transmitInterval),
|
||||
st_localIP(localIP),
|
||||
st_hubIP(hubIP),
|
||||
st_serverPort(serverPort),
|
||||
st_hubPort(hubPort),
|
||||
st_DHCP(DHCP)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//*******************************************************************************
|
||||
// SmartThingsEthernet Constructor - DHCP
|
||||
//*******************************************************************************
|
||||
SmartThingsEthernet::SmartThingsEthernet(uint16_t serverPort, IPAddress hubIP, uint16_t hubPort, SmartThingsCallout_t *callout, String shieldType, bool enableDebug, int transmitInterval, bool DHCP) :
|
||||
SmartThings(callout, shieldType, enableDebug, transmitInterval),
|
||||
st_hubIP(hubIP),
|
||||
st_serverPort(serverPort),
|
||||
st_hubPort(hubPort),
|
||||
st_DHCP(DHCP)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//SmartThingsEthernet::~SmartThingsEthernet()
|
||||
//*****************************************************************************
|
||||
SmartThingsEthernet::~SmartThingsEthernet()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
106
lib/SmartThings/SmartThingsEthernet.h
Normal file
106
lib/SmartThings/SmartThingsEthernet.h
Normal file
@@ -0,0 +1,106 @@
|
||||
//*******************************************************************************
|
||||
// SmartThings Arduino Ethernet Library
|
||||
//
|
||||
// License
|
||||
// (C) Copyright 2017 Dan Ogorchock
|
||||
//
|
||||
// History
|
||||
// 2017-02-04 Dan Ogorchock Created
|
||||
// 2017-05-02 Dan Ogorchock Add support for W5500 Ethernet2 Shield
|
||||
// 2018-01-06 Dan Ogorchock Added RSSI Interval as user-definable interval
|
||||
// 2020-07-26 Dan Ogorchock Changed the final RSSI interval from 60 seconds to 900 seconds
|
||||
//*******************************************************************************
|
||||
|
||||
#ifndef __SMARTTHINGSETHERNET_H__
|
||||
#define __SMARTTHINGSETHERNET_H__
|
||||
|
||||
#include "SmartThings.h"
|
||||
|
||||
//Adjust the RSSI Transmit Interval below as you see fit (in milliseconds)
|
||||
// Note: When the board first boots, it transmits frequently, then slows over
|
||||
// time to the interval below.
|
||||
#define RSSI_TX_INTERVAL 900000
|
||||
|
||||
//*******************************************************************************
|
||||
// Using Ethernet Shield
|
||||
//*******************************************************************************
|
||||
#include <IPAddress.h>
|
||||
/*
|
||||
//#if defined ARDUINO_ARCH_AVR
|
||||
//#include <SPI.h>
|
||||
//#include <Ethernet.h>
|
||||
//#elif defined ARDUINO_ARCH_ESP8266
|
||||
//#include <ESP8266WiFi.h>
|
||||
//#endif
|
||||
*/
|
||||
namespace st
|
||||
{
|
||||
class SmartThingsEthernet: public SmartThings
|
||||
{
|
||||
private:
|
||||
|
||||
protected:
|
||||
IPAddress st_localIP;
|
||||
IPAddress st_localGateway;
|
||||
IPAddress st_localSubnetMask;
|
||||
IPAddress st_localDNSServer;
|
||||
IPAddress st_hubIP;
|
||||
uint16_t st_serverPort;
|
||||
uint16_t st_hubPort;
|
||||
bool st_DHCP;
|
||||
|
||||
public:
|
||||
|
||||
//*******************************************************************************
|
||||
/// @brief SmartThings Ethernet Constructor - STATIC
|
||||
/// @param[in] localIP - TCP/IP Address of the Arduino
|
||||
/// @param[in] localGateway - TCP/IP Gateway Address of local LAN (your Router's LAN Address)
|
||||
/// @param[in] localSubnetMask - Subnet Mask of the Arduino
|
||||
/// @param[in] localDNSServer - DNS Server
|
||||
/// @param[in] serverPort - TCP/IP Port of the Arduino
|
||||
/// @param[in] hubIP - TCP/IP Address of the ST Hub
|
||||
/// @param[in] hubPort - TCP/IP Port of the ST Hub
|
||||
/// @param[in] callout - Set the Callout Function that is called on Msg Reception
|
||||
/// @param[in] shieldType (optional) - Set the Reported SheildType to the Server
|
||||
/// @param[in] enableDebug (optional) - Enable internal Library debug
|
||||
//*******************************************************************************
|
||||
SmartThingsEthernet(IPAddress localIP, IPAddress localGateway, IPAddress localSubnetMask, IPAddress localDNSServer, uint16_t serverPort, IPAddress hubIP, uint16_t hubPort, SmartThingsCallout_t *callout, String shieldType = "EthernetShield", bool enableDebug = false, int transmitInterval = 100, bool DHCP = false);
|
||||
|
||||
//Abrreviated STATIC version for Arduino + ESP-01 WiFiEsp library (no Gateway, Subnet Mask, or DNS can be user provided)
|
||||
SmartThingsEthernet(IPAddress localIP, uint16_t serverPort, IPAddress hubIP, uint16_t hubPort, SmartThingsCallout_t *callout, String shieldType = "EthernetShield", bool enableDebug = false, int transmitInterval = 100, bool DHCP = false);
|
||||
|
||||
//*******************************************************************************
|
||||
/// @brief SmartThings Ethernet Constructor - DHCP
|
||||
/// @param[in] serverPort - TCP/IP Port of the Arduino
|
||||
/// @param[in] hubIP - TCP/IP Address of the ST Hub
|
||||
/// @param[in] hubPort - TCP/IP Port of the ST Hub
|
||||
/// @param[in] callout - Set the Callout Function that is called on Msg Reception
|
||||
/// @param[in] shieldType (optional) - Set the Reported SheildType to the Server
|
||||
/// @param[in] enableDebug (optional) - Enable internal Library debug
|
||||
//*******************************************************************************
|
||||
SmartThingsEthernet(uint16_t serverPort, IPAddress hubIP, uint16_t hubPort, SmartThingsCallout_t *callout, String shieldType = "EthernetShield", bool enableDebug = false, int transmitInterval = 100, bool DHCP = true);
|
||||
|
||||
//*******************************************************************************
|
||||
/// Destructor
|
||||
//*******************************************************************************
|
||||
~SmartThingsEthernet();
|
||||
|
||||
//*******************************************************************************
|
||||
/// Initialize SmartThings Library
|
||||
//*******************************************************************************
|
||||
virtual void init(void) = 0; //all derived classes must implement this pure virtual function
|
||||
|
||||
//*******************************************************************************
|
||||
/// Run SmartThings Library
|
||||
//*******************************************************************************
|
||||
virtual void run(void) = 0; //all derived classes must implement this pure virtual function
|
||||
|
||||
//*******************************************************************************
|
||||
/// Send Message to the Hub
|
||||
//*******************************************************************************
|
||||
virtual void send(String message) = 0; //all derived classes must implement this pure virtual function
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
#endif
|
||||
538
lib/SmartThings/SmartThingsThingShield.cpp
Normal file
538
lib/SmartThings/SmartThingsThingShield.cpp
Normal file
@@ -0,0 +1,538 @@
|
||||
//*******************************************************************************
|
||||
/// @file
|
||||
/// @brief
|
||||
/// SmartThingsThingShield Arduino Library
|
||||
/// @section License
|
||||
/// (C) Copyright 2013 Physical Graph
|
||||
///
|
||||
/// Updates by Daniel Ogorchock 12/30/2014 - Arduino Mega 2560 HW Serial support
|
||||
/// -Numerous performance and size optimizations (helpful on UNO with only 2K SRAM)
|
||||
/// -Arduino UNO should use the SoftwareSerial library Constructor since the UNO has
|
||||
/// only one Hardware UART port ("Serial") which is used by the USB port for
|
||||
/// programming and debugging typically. UNO can use the Hardware "Serial"
|
||||
/// if desired, but USB programming and debug will be troublesome.
|
||||
/// Leonardo and Mega can use SoftwareSerial BUT cannot use Pin3 for Rx - use
|
||||
/// Pin10 for Rx and add jumper from Pin10 to Pin3.
|
||||
/// -Arduino LEONARDO should use the Hardware Serial Constructor since it has 1 UART
|
||||
/// separate from the USB port. "Serial1" port uses pins 0(Rx) and 1(Tx).
|
||||
/// -Arduino MEGA should use the Hardware Serial Constructor since it has 4 UARTs.
|
||||
/// "Serial3" port uses pins 14(Tx) and 15(Rx). Wire Pin14 to Pin2 and Pin15 to Pin3.
|
||||
/// -Be certain to not use Pins 2 & 3 in your Arduino sketch for I/O since they are
|
||||
/// electrically connected to the ThingShield if set to D2/D3.
|
||||
/// -Note - Pin6 is reserved by the ThingShield as well. Best to avoid using it.
|
||||
/// -The SoftwareSerial library has the following known limitations:
|
||||
/// - If using multiple software serial ports, only one can receive data at a time.
|
||||
/// - Not all pins on the Mega and Mega 2560 support change interrupts, so only
|
||||
/// the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53,
|
||||
/// A8(62), A9(63), A10(64), A11(65), A12(66), A13(67), A14(68), A15(69).
|
||||
/// - Not all pins on the Leonardo and Micro support change interrupts, so only
|
||||
/// the following can be used for RX : 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
|
||||
/// -2016-06-04 Dan Ogorchock Added improved support for Arduino Leonardo
|
||||
/// -2017-02-04 Dan Ogorchock Modified to be a subclass of new SmartThings base class
|
||||
/// -2017-02-08 Dan Ogorchock Cleaned up. Now uses HardwareSerial* objects directly.
|
||||
//*******************************************************************************
|
||||
#include "SmartThingsThingShield.h"
|
||||
|
||||
namespace st
|
||||
{
|
||||
//*****************************************************************************
|
||||
void SmartThingsThingShield::debugPrintBuffer(String prefix, uint8_t * pBuf, uint_fast8_t nBuf)
|
||||
{
|
||||
if (_isDebugEnabled)
|
||||
{
|
||||
Serial.print(prefix);
|
||||
for (uint_fast8_t i = 0; i < nBuf; i++)
|
||||
{
|
||||
Serial.print(char(pBuf[i]));
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
bool SmartThingsThingShield::isRxLine(uint8_t * pLine)
|
||||
{
|
||||
// line starts with "T00000000:RX"
|
||||
return ((pLine[0] == 'T') && (pLine[9] == ':') && (pLine[10] == 'R') && (pLine[11] == 'X'));
|
||||
}
|
||||
|
||||
//*******************************************************************************
|
||||
bool SmartThingsThingShield::isAsciiHex(uint8_t ascii)
|
||||
{
|
||||
bool retVal = false;
|
||||
if (
|
||||
((ascii >= 'A') && (ascii <= 'F')) ||
|
||||
((ascii >= 'a') && (ascii <= 'f')) ||
|
||||
((ascii >= '0') && (ascii <= '9'))
|
||||
)
|
||||
{
|
||||
retVal = true;
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
//*******************************************************************************
|
||||
/// @note this function doesn't check for hex validity before converting
|
||||
//*******************************************************************************
|
||||
uint8_t SmartThingsThingShield::asciiToHexU8(uint8_t pAscii[2])
|
||||
{
|
||||
uint8_t hex;
|
||||
hex = ((pAscii[0] - (((pAscii[0] >> 6) & 0x1) * 0x37)) & 0xF);
|
||||
hex <<= 4;
|
||||
hex |= ((pAscii[1] - (((pAscii[1] >> 6) & 0x1) * 0x37)) & 0xF);
|
||||
return hex;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
uint_fast8_t SmartThingsThingShield::translatePayload(uint8_t *pBuf, uint_fast8_t nBuf)
|
||||
{
|
||||
uint_fast8_t payloadLength = 0; // return value
|
||||
uint_fast8_t payloadStart = 0;
|
||||
uint_fast8_t payloadEnd = 0;
|
||||
|
||||
uint_fast8_t i;
|
||||
|
||||
// find [ ] message from the back of the message
|
||||
for (i = nBuf - 1; i > 0; i--)
|
||||
{
|
||||
if (pBuf[i] == ']')
|
||||
{
|
||||
payloadEnd = i;
|
||||
}
|
||||
else if (pBuf[i] == '[')
|
||||
{
|
||||
payloadStart = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (_isDebugEnabled)
|
||||
{
|
||||
Serial.print(F("payload start: "));
|
||||
Serial.print(payloadStart);
|
||||
Serial.print(F(" end: "));
|
||||
Serial.print(payloadEnd);
|
||||
Serial.print(F(" : "));
|
||||
for (i = payloadStart + 1; i < payloadEnd; i++)
|
||||
{
|
||||
Serial.print(pBuf[i]);
|
||||
Serial.print(' ');
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
if ((payloadStart != 0) && (payloadEnd != 0) && (payloadEnd - payloadStart > 4) && (pBuf[payloadStart + 1] == '0') && (pBuf[payloadStart + 2] == 'A'))
|
||||
{ // if valid message then parse
|
||||
i = payloadStart + 4; // start+3 should be ' '
|
||||
while (i < payloadEnd)
|
||||
{
|
||||
if (pBuf[i] != ' ')
|
||||
{
|
||||
if (isAsciiHex(pBuf[i]) && isAsciiHex(pBuf[i + 1]))
|
||||
{
|
||||
pBuf[payloadLength++] = asciiToHexU8(&(pBuf[i++]));
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
pBuf[payloadLength] = 0x0; // null-terminate the string
|
||||
return payloadLength;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
void SmartThingsThingShield::_process(void)
|
||||
{
|
||||
uint32_t nowMilliseconds = millis();
|
||||
|
||||
if ((nowMilliseconds < _lastShieldMS) || ((nowMilliseconds - _lastShieldMS) > 5000))
|
||||
{
|
||||
_shieldGetNetworkInfo();
|
||||
_lastShieldMS = nowMilliseconds;
|
||||
}
|
||||
else if ((_networkState == STATE_JOINED) &&
|
||||
((nowMilliseconds < _lastPingMS) || ((nowMilliseconds - _lastPingMS) > 60000)))
|
||||
{ // ping every minutes or on rollover
|
||||
send("ping");
|
||||
_lastPingMS = nowMilliseconds;
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
void SmartThingsThingShield::handleLine(void)
|
||||
{
|
||||
if (_nBufRX > 0)
|
||||
{
|
||||
if (isRxLine(_pBufRX))
|
||||
{
|
||||
debugPrintBuffer("->| ", _pBufRX, _nBufRX);
|
||||
{
|
||||
uint_fast8_t messageBufLength = translatePayload(_pBufRX, _nBufRX);
|
||||
|
||||
if (messageBufLength > 0)
|
||||
{
|
||||
debugPrintBuffer("->| payload :: ", (uint8_t *)_pBufRX, messageBufLength);
|
||||
|
||||
_calloutFunction(String((char *)_pBufRX)); // call out to main application
|
||||
}
|
||||
else
|
||||
{
|
||||
debugPrintBuffer("->| no payload from :: ", _pBufRX, _nBufRX);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{ //XXX Totally slapped together since this is temp-- will go away with command set change
|
||||
uint_fast8_t i = 0;
|
||||
bool found = false;
|
||||
if (_nBufRX >= 32) //netinfo:0022A3000000B675,E30E,02
|
||||
{
|
||||
while (i < _nBufRX)
|
||||
{
|
||||
if (
|
||||
(_pBufRX[i] == 'n') &&
|
||||
(_pBufRX[i + 1] == 'e') &&
|
||||
(_pBufRX[i + 2] == 't') &&
|
||||
(_pBufRX[i + 3] == 'i') &&
|
||||
(_pBufRX[i + 4] == 'n') &&
|
||||
(_pBufRX[i + 5] == 'f') &&
|
||||
(_pBufRX[i + 6] == 'o') &&
|
||||
(_pBufRX[i + 7] == ':') &&
|
||||
(_pBufRX[i + 24] == ',') &&
|
||||
(_pBufRX[i + 29] == ',')
|
||||
)
|
||||
{
|
||||
// parse off EUI
|
||||
if (
|
||||
isAsciiHex(_pBufRX[i + 8]) &&
|
||||
isAsciiHex(_pBufRX[i + 9]) &&
|
||||
isAsciiHex(_pBufRX[i + 10]) &&
|
||||
isAsciiHex(_pBufRX[i + 11]) &&
|
||||
isAsciiHex(_pBufRX[i + 12]) &&
|
||||
isAsciiHex(_pBufRX[i + 13]) &&
|
||||
isAsciiHex(_pBufRX[i + 14]) &&
|
||||
isAsciiHex(_pBufRX[i + 15]) &&
|
||||
isAsciiHex(_pBufRX[i + 16]) &&
|
||||
isAsciiHex(_pBufRX[i + 17]) &&
|
||||
isAsciiHex(_pBufRX[i + 18]) &&
|
||||
isAsciiHex(_pBufRX[i + 19]) &&
|
||||
isAsciiHex(_pBufRX[i + 20]) &&
|
||||
isAsciiHex(_pBufRX[i + 21]) &&
|
||||
isAsciiHex(_pBufRX[i + 22]) &&
|
||||
isAsciiHex(_pBufRX[i + 23]) &&
|
||||
|
||||
isAsciiHex(_pBufRX[i + 25]) &&
|
||||
isAsciiHex(_pBufRX[i + 26]) &&
|
||||
isAsciiHex(_pBufRX[i + 27]) &&
|
||||
isAsciiHex(_pBufRX[i + 28]) &&
|
||||
|
||||
isAsciiHex(_pBufRX[i + 30]) &&
|
||||
isAsciiHex(_pBufRX[i + 31])
|
||||
)
|
||||
{
|
||||
uint8_t tempNetStat = asciiToHexU8(&(_pBufRX[i + 30]));
|
||||
if (tempNetStat <= STATE_LEAVING) // make sure it maps to the enum
|
||||
{
|
||||
_networkState = (SmartThingsNetworkState_t)tempNetStat;
|
||||
|
||||
_nodeID = asciiToHexU8(&(_pBufRX[i + 25]));
|
||||
_nodeID <<= 8;
|
||||
_nodeID |= asciiToHexU8(&(_pBufRX[i + 27]));
|
||||
|
||||
_eui64[7] = asciiToHexU8(&(_pBufRX[i + 8]));
|
||||
_eui64[6] = asciiToHexU8(&(_pBufRX[i + 10]));
|
||||
_eui64[5] = asciiToHexU8(&(_pBufRX[i + 12]));
|
||||
_eui64[4] = asciiToHexU8(&(_pBufRX[i + 14]));
|
||||
_eui64[3] = asciiToHexU8(&(_pBufRX[i + 16]));
|
||||
_eui64[2] = asciiToHexU8(&(_pBufRX[i + 18]));
|
||||
_eui64[1] = asciiToHexU8(&(_pBufRX[i + 20]));
|
||||
_eui64[0] = asciiToHexU8(&(_pBufRX[i + 22]));
|
||||
|
||||
debugPrintBuffer(" |~> ", &(_pBufRX[i]), 32);
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
if (found == false)
|
||||
debugPrintBuffer("->| IGNORING :: ", _pBufRX, _nBufRX);
|
||||
}
|
||||
_nBufRX = 0;
|
||||
}
|
||||
}
|
||||
//*****************************************************************************
|
||||
void SmartThingsThingShield::_shieldGetNetworkInfo(void)
|
||||
{
|
||||
_mySerial->print(F("custom netinfo\n"));
|
||||
|
||||
if (_isDebugEnabled)
|
||||
{
|
||||
Serial.print(F(" |<~ custom netinfo\n"));
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
// Thing API | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
|
||||
// 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 V V V
|
||||
//*****************************************************************************
|
||||
|
||||
// SoftwareSerial Constructor
|
||||
#ifndef DISABLE_SOFTWARESERIAL
|
||||
SmartThingsThingShield::SmartThingsThingShield(uint8_t pinRX, uint8_t pinTX, SmartThingsCallout_t *callout, String shieldType, bool enableDebug, int transmitInterval) :
|
||||
SmartThings(callout, shieldType, enableDebug, transmitInterval),
|
||||
//_SerialPort(SW_SERIAL),
|
||||
_lastPingMS(0xFFFFFF00),
|
||||
_lastShieldMS(0xFFFFFF00),
|
||||
_networkState(STATE_UNKNOWN),
|
||||
_nBufRX(0)
|
||||
{
|
||||
_mySerial = new SoftwareSerial(pinRX, pinTX);
|
||||
_mySerial->begin(2400);
|
||||
}
|
||||
#else
|
||||
//Hardware Serial Constructor
|
||||
SmartThingsThingShield::SmartThingsThingShield(HardwareSerial* hwSerialPort, SmartThingsCallout_t *callout, String shieldType, bool enableDebug, int transmitInterval) :
|
||||
SmartThings(callout, shieldType, enableDebug, transmitInterval),
|
||||
_mySerial(hwSerialPort),
|
||||
_lastPingMS(0xFFFFFF00),
|
||||
_lastShieldMS(0xFFFFFF00),
|
||||
_networkState(STATE_UNKNOWN),
|
||||
_nBufRX(0)
|
||||
{
|
||||
// _isDebugEnabled = hwSerialPort != Serial ? enableDebug : false; //Do not allow debug print statements if using Hardware Serial (pins 0,1) for ST Communications
|
||||
_mySerial->begin(2400);
|
||||
}
|
||||
#endif
|
||||
//*****************************************************************************
|
||||
//SmartThings::~SmartThings()
|
||||
SmartThingsThingShield::~SmartThingsThingShield()
|
||||
{
|
||||
}
|
||||
|
||||
//*******************************************************************************
|
||||
/// Initialize SmartThings Thingshield Library
|
||||
//*******************************************************************************
|
||||
void SmartThingsThingShield::init(void)
|
||||
{
|
||||
unsigned long tmpTime = millis();
|
||||
do
|
||||
{
|
||||
//updateNetworkState();
|
||||
run();
|
||||
delay(10);
|
||||
updateNetworkState();
|
||||
} while ((_networkState != STATE_JOINED) && ((millis() - tmpTime) < 5000));
|
||||
|
||||
if (_isDebugEnabled)
|
||||
{
|
||||
if (_networkState != STATE_JOINED)
|
||||
{
|
||||
Serial.println(F("SmartThingsThingShield: Intialization timed out waiting for shield to connect."));
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("SmartThingsThingShield: Intialization Successful. Shield connected."));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//*****************************************************************************
|
||||
void SmartThingsThingShield::run(void)
|
||||
{
|
||||
uint8_t readByte;
|
||||
|
||||
while ((_nBufRX < SMARTTHINGS_RX_BUFFER_SIZE) && _mySerial->available())
|
||||
{
|
||||
|
||||
readByte = _mySerial->read();
|
||||
|
||||
if ((readByte == 0x0D) || (readByte == 0x0A))
|
||||
{ // handle data from SmartThing Hub line-by-line, execute user's callback function for each line
|
||||
handleLine();
|
||||
}
|
||||
else
|
||||
{
|
||||
// keep track of everything that comes in until we reach a newline
|
||||
// TODO(cwvh): validate bufferlength 1988-10-19
|
||||
//if (_nBufRX > 200)
|
||||
// panic("too many characters!");
|
||||
_pBufRX[_nBufRX++] = readByte;
|
||||
}
|
||||
|
||||
}
|
||||
_process();
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
void SmartThingsThingShield::send(String message)
|
||||
{
|
||||
// e.g. thing.print("raw 0x0 {00 00 0A 0A 62 75 74 74 6f 6e 20 64 6f 77 6e }");
|
||||
_mySerial->print(F("raw 0x0 { 00 00 0A 0A "));
|
||||
|
||||
if (_isDebugEnabled)
|
||||
{
|
||||
Serial.print(F("<-| raw 0x0 { 00 00 0A 0A "));
|
||||
}
|
||||
|
||||
for (int i = 0; i < message.length(); i++)
|
||||
{
|
||||
_mySerial->print(message[i], HEX);
|
||||
_mySerial->print(' ');
|
||||
|
||||
if (_isDebugEnabled)
|
||||
{
|
||||
Serial.print(message[i], HEX);
|
||||
Serial.print(' ');
|
||||
}
|
||||
}
|
||||
|
||||
_mySerial->print(F("}\nsend 0x0 1 1\n"));
|
||||
|
||||
if (_isDebugEnabled)
|
||||
{
|
||||
Serial.print(F("}\nsend 0x0 1 1\n"));
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
void SmartThingsThingShield::shieldSetLED(uint8_t red, uint8_t green, uint8_t blue)
|
||||
{
|
||||
if (red > 9) red = 9;
|
||||
if (green > 9) green = 9;
|
||||
if (blue > 9) blue = 9;
|
||||
|
||||
_mySerial->print(F("custom rgb "));
|
||||
_mySerial->write((red + '0'));
|
||||
_mySerial->print(' ');
|
||||
_mySerial->write((green + '0'));
|
||||
_mySerial->print(' ');
|
||||
_mySerial->write((blue + '0'));
|
||||
_mySerial->print(F(" \n"));
|
||||
|
||||
if (_isDebugEnabled)
|
||||
{
|
||||
Serial.print(F(" |<~ custom rgb "));
|
||||
Serial.write(red + '0');
|
||||
Serial.print(' ');
|
||||
Serial.write(green + '0');
|
||||
Serial.print(' ');
|
||||
Serial.write(blue + '0');
|
||||
Serial.print(F(" \n"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//*****************************************************************************
|
||||
SmartThingsNetworkState_t SmartThingsThingShield::shieldGetLastNetworkState(void)
|
||||
{
|
||||
return _networkState;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
SmartThingsNetworkState_t SmartThingsThingShield::shieldGetNetworkState(void)
|
||||
{
|
||||
_shieldGetNetworkInfo();
|
||||
return _networkState;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
uint16_t SmartThingsThingShield::shieldGetNodeID(void)
|
||||
{
|
||||
_shieldGetNetworkInfo();
|
||||
return _nodeID;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
void SmartThingsThingShield::shieldGetEUI64(uint8_t eui[8])
|
||||
{
|
||||
_shieldGetNetworkInfo();
|
||||
{
|
||||
uint_fast8_t i = 7;
|
||||
do
|
||||
{
|
||||
eui[i] = _eui64[i];
|
||||
} while (i--);
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
void SmartThingsThingShield::shieldFindNetwork(void)
|
||||
{
|
||||
_mySerial->print(F("custom find\n"));
|
||||
|
||||
if (_isDebugEnabled)
|
||||
{
|
||||
Serial.print(F(" |<~ custom find\n"));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
void SmartThingsThingShield::shieldLeaveNetwork(void)
|
||||
{
|
||||
_mySerial->print(F("custom leave\n"));
|
||||
|
||||
|
||||
if (_isDebugEnabled)
|
||||
{
|
||||
Serial.print(F(" |<~ custom leave\n"));
|
||||
}
|
||||
}
|
||||
|
||||
//*******************************************************************************
|
||||
// Update the network State/LED
|
||||
//*******************************************************************************
|
||||
void SmartThingsThingShield::updateNetworkState() //get the current zigbee network status of the ST Shield
|
||||
{
|
||||
static SmartThingsNetworkState_t tempState = shieldGetLastNetworkState();
|
||||
|
||||
if (_isDebugEnabled)
|
||||
{
|
||||
Serial.println(F("Called updateNetworkState()"));
|
||||
}
|
||||
|
||||
if (tempState != _networkState)
|
||||
{
|
||||
if (_isDebugEnabled)
|
||||
{
|
||||
Serial.print(F("Called updateNetworkState() tmpState ="));
|
||||
Serial.println(tempState);
|
||||
}
|
||||
|
||||
switch (_networkState)
|
||||
{
|
||||
case STATE_NO_NETWORK:
|
||||
if (_isDebugEnabled) Serial.println(F("Everything: NO_NETWORK"));
|
||||
shieldSetLED(2, 0, 0); // red
|
||||
break;
|
||||
case STATE_JOINING:
|
||||
if (_isDebugEnabled) Serial.println(F("Everything: JOINING"));
|
||||
shieldSetLED(2, 0, 0); // red
|
||||
break;
|
||||
case STATE_JOINED:
|
||||
if (_isDebugEnabled) Serial.println(F("Everything: JOINED"));
|
||||
shieldSetLED(0, 0, 0); // off
|
||||
break;
|
||||
case STATE_JOINED_NOPARENT:
|
||||
if (_isDebugEnabled) Serial.println(F("Everything: JOINED_NOPARENT"));
|
||||
shieldSetLED(2, 0, 2); // purple
|
||||
break;
|
||||
case STATE_LEAVING:
|
||||
if (_isDebugEnabled) Serial.println(F("Everything: LEAVING"));
|
||||
shieldSetLED(2, 0, 0); // red
|
||||
break;
|
||||
default:
|
||||
case STATE_UNKNOWN:
|
||||
if (_isDebugEnabled) Serial.println(F("Everything: UNKNOWN"));
|
||||
shieldSetLED(0, 2, 0); // green
|
||||
break;
|
||||
}
|
||||
_networkState = tempState;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
212
lib/SmartThings/SmartThingsThingShield.h
Normal file
212
lib/SmartThings/SmartThingsThingShield.h
Normal file
@@ -0,0 +1,212 @@
|
||||
//*******************************************************************************
|
||||
/// @file
|
||||
/// @brief
|
||||
/// SmartThingsThingShield Arduino Library
|
||||
/// @section License
|
||||
/// (C) Copyright 2013 Physical Graph
|
||||
///
|
||||
/// Updates by Daniel Ogorchock 12/30/2014 - Arduino Mega 2560 HW Serial support
|
||||
/// -Numerous performance and size optimizations (helpful on UNO with only 2K SRAM)
|
||||
/// -Arduino UNO should use the SoftwareSerial library Constructor since the UNO has
|
||||
/// only one Hardware UART port ("Serial") which is used by the USB port for
|
||||
/// programming and debugging typically. UNO can use the Hardware "Serial"
|
||||
/// if desired, but USB programming and debug will be troublesome.
|
||||
/// Leonardo and Mega can use SoftwareSerial BUT cannot use Pin3 for Rx - use
|
||||
/// Pin10 for Rx and add jumper from Pin10 to Pin3.
|
||||
/// -Arduino LEONARDO should use the Hardware Serial Constructor since it has 1 UART
|
||||
/// separate from the USB port. "Serial1" port uses pins 0(Rx) and 1(Tx).
|
||||
/// -Arduino MEGA should use the Hardware Serial Constructor since it has 4 UARTs.
|
||||
/// "Serial3" port uses pins 14(Tx) and 15(Rx). Wire Pin14 to Pin2 and Pin15 to Pin3.
|
||||
/// -Be certain to not use Pins 2 & 3 in your Arduino sketch for I/O since they are
|
||||
/// electrically connected to the ThingShield if set to D2/D3.
|
||||
/// -Note - Pin6 is reserved by the ThingShield as well. Best to avoid using it.
|
||||
/// -The SoftwareSerial library has the following known limitations:
|
||||
/// - If using multiple software serial ports, only one can receive data at a time.
|
||||
/// - Not all pins on the Mega and Mega 2560 support change interrupts, so only
|
||||
/// the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53,
|
||||
/// A8(62), A9(63), A10(64), A11(65), A12(66), A13(67), A14(68), A15(69).
|
||||
/// - Not all pins on the Leonardo and Micro support change interrupts, so only
|
||||
/// the following can be used for RX : 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
|
||||
/// -2016-06-04 Dan Ogorchock Added improved support for Arduino Leonardo
|
||||
/// -2017-02-04 Dan Ogorchock Modified to be a subclass of new SmartThings base class
|
||||
/// -2017-02-08 Dan Ogorchock Cleaned up. Now uses HardwareSerial* objects directly.
|
||||
/// -2017-08-14 Dan Ogorchock Disabled SoftwareSerial support if compiling ESP32 board
|
||||
//*******************************************************************************
|
||||
#ifndef __SMARTTHINGS_THINGSHIELD_H__
|
||||
#define __SMARTTHINGS_THINGSHIELD_H__
|
||||
|
||||
#include "SmartThings.h"
|
||||
|
||||
//*******************************************************************************
|
||||
#define BOARD_TYPE_UNO 0
|
||||
#define BOARD_TYPE_LEONARDO 1
|
||||
#define BOARD_TYPE_MEGA 2
|
||||
//*******************************************************************************
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
//*******************************************************************************
|
||||
// Set the correct board type automatically
|
||||
//*******************************************************************************
|
||||
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__)
|
||||
#define BOARD_TYPE BOARD_TYPE_UNO
|
||||
//#define DISABLE_SOFTWARESERIAL // uncomment to disable SoftwareSerial to save some program space if neccessary while using HW Serial
|
||||
#elif defined(__AVR_ATmega32U4__)
|
||||
#define BOARD_TYPE BOARD_TYPE_LEONARDO
|
||||
#define DISABLE_SOFTWARESERIAL //Assume HW Serial is being used. Saves some program space while using HW Serial
|
||||
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
||||
#define BOARD_TYPE BOARD_TYPE_MEGA
|
||||
#define DISABLE_SOFTWARESERIAL //Assume HW Serial is being used. Saves some program space while using HW Serial
|
||||
#elif defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_ESP32)
|
||||
#define DISABLE_SOFTWARESERIAL //The SAMD chip (e.g. MKR1000) and ESP32 do not support SoftwareSerial
|
||||
#else //assume user is using an UNO for the unknown case
|
||||
#define BOARD_TYPE BOARD_TYPE_UNO
|
||||
//#define DISABLE_SOFTWARESERIAL // uncomment to disable SoftwareSerial to save some program space if neccessary while using HW Serial
|
||||
#endif
|
||||
|
||||
#ifndef DISABLE_SOFTWARESERIAL
|
||||
#include <SoftwareSerial.h>
|
||||
#endif
|
||||
|
||||
//*******************************************************************************
|
||||
#define SMARTTHINGS_RX_BUFFER_SIZE 256 // if > 255: change _nBufRX to u16
|
||||
#define SMARTTHINGS_SHIELDTYPE_SIZE 32 // if > 255: change _shieldTypeLen to u16
|
||||
|
||||
namespace st
|
||||
{
|
||||
//*******************************************************************************
|
||||
/// @brief ZigBee Network State Definition
|
||||
//*******************************************************************************
|
||||
typedef enum
|
||||
{
|
||||
STATE_NO_NETWORK,
|
||||
STATE_JOINING,
|
||||
STATE_JOINED,
|
||||
STATE_JOINED_NOPARENT,
|
||||
STATE_LEAVING,
|
||||
STATE_UNKNOWN
|
||||
} SmartThingsNetworkState_t;
|
||||
|
||||
//*******************************************************************************
|
||||
|
||||
class SmartThingsThingShield: public SmartThings
|
||||
{
|
||||
private:
|
||||
#ifndef DISABLE_SOFTWARESERIAL
|
||||
SoftwareSerial* _mySerial;
|
||||
#else
|
||||
HardwareSerial* _mySerial;
|
||||
#endif
|
||||
//SmartThingsSerialType_t _SerialPort;
|
||||
|
||||
uint32_t _lastPingMS;
|
||||
uint32_t _lastShieldMS;
|
||||
SmartThingsNetworkState_t _networkState;
|
||||
uint8_t _eui64[8];
|
||||
uint16_t _nodeID;
|
||||
|
||||
uint8_t _pBufRX[SMARTTHINGS_RX_BUFFER_SIZE];
|
||||
uint_fast8_t _nBufRX;
|
||||
|
||||
void _shieldGetNetworkInfo(void);
|
||||
void _process(void);
|
||||
|
||||
void debugPrintBuffer(String prefix, uint8_t * pBuf, uint_fast8_t nBuf);
|
||||
bool isRxLine(uint8_t * pLine);
|
||||
bool isAsciiHex(uint8_t ascii);
|
||||
uint8_t asciiToHexU8(uint8_t pAscii[2]);
|
||||
uint_fast8_t translatePayload(uint8_t *pBuf, uint_fast8_t nBuf);
|
||||
void handleLine(void);
|
||||
|
||||
public:
|
||||
//*******************************************************************************
|
||||
/// @brief SmartThings SoftwareSerial Constructor
|
||||
/// @param[in] pinRX - Receive Pin for the SoftwareSerial Port to the Arduino
|
||||
/// @param[in] pinTX - Transmit Pin for the SoftwareSerial Port to the Arduino
|
||||
/// @param[in] callout - Set the Callout Function that is called on Msg Reception
|
||||
/// @param[in] shieldType (optional) - Set the Reported SheildType to the Server
|
||||
/// @param[in] enableDebug (optional) - Enable internal Library debug
|
||||
//*******************************************************************************
|
||||
#ifndef DISABLE_SOFTWARESERIAL
|
||||
SmartThingsThingShield(uint8_t pinRX, uint8_t pinTX, SmartThingsCallout_t *callout, String shieldType = "ThingShield", bool enableDebug = false, int transmitInterval = 1000);
|
||||
#else
|
||||
//*******************************************************************************
|
||||
/// @brief SmartThings HardwareSerial Constructor
|
||||
/// @param[in] hwSerialPort - HardwareSerial Port of the Arduino (i.e. &Serial, &Serial1, &Serial2, &Serial3)
|
||||
/// @param[in] callout - Set the Callout Function that is called on Msg Reception
|
||||
/// @param[in] shieldType (optional) - Set the Reported SheildType to the Server
|
||||
/// @param[in] enableDebug (optional) - Enable internal Library debug
|
||||
//*******************************************************************************
|
||||
SmartThingsThingShield(HardwareSerial* hwSerialPort, SmartThingsCallout_t *callout, String shieldType = "ThingShield", bool enableDebug = false, int transmitInterval = 1000);
|
||||
#endif
|
||||
//*******************************************************************************
|
||||
/// @brief Destructor
|
||||
//*******************************************************************************
|
||||
virtual ~SmartThingsThingShield();
|
||||
|
||||
//*******************************************************************************
|
||||
/// @brief Initialize SmartThings Library
|
||||
//*******************************************************************************
|
||||
virtual void init(void);
|
||||
|
||||
//*******************************************************************************
|
||||
/// @brief Run SmartThings Library
|
||||
//*******************************************************************************
|
||||
virtual void run(void);
|
||||
|
||||
//*******************************************************************************
|
||||
/// @brief Send Message out over ZigBee to the Hub
|
||||
/// @param[in] message to send
|
||||
//*******************************************************************************
|
||||
virtual void send(String message);
|
||||
|
||||
//*******************************************************************************
|
||||
/// @brief Set SmartThings Shield MultiColor LED
|
||||
/// @param[in] red: intensity {0=off to 9=max}
|
||||
/// @param[in] green: intensity {0=off to 9=max}
|
||||
/// @param[in] blue: intensity {0=off to 9=max}
|
||||
//*******************************************************************************
|
||||
void shieldSetLED(uint8_t red, uint8_t green, uint8_t blue);
|
||||
|
||||
//*******************************************************************************
|
||||
/// @brief Get Last Read Shield State
|
||||
/// @return Last Read Network State
|
||||
//*******************************************************************************
|
||||
SmartThingsNetworkState_t shieldGetLastNetworkState(void);
|
||||
|
||||
//*******************************************************************************
|
||||
/// @brief Get Last Read Shield State and Trigger Refresh of Network Info
|
||||
/// @return Last Read Network State
|
||||
//*******************************************************************************
|
||||
SmartThingsNetworkState_t shieldGetNetworkState(void);
|
||||
|
||||
//*******************************************************************************
|
||||
/// @brief Get Last Read NodeID and Trigger Refresh of Network Info
|
||||
/// @return Last Read NodeID
|
||||
//*******************************************************************************
|
||||
uint16_t shieldGetNodeID(void);
|
||||
|
||||
//*******************************************************************************
|
||||
/// @brief Get Last Read EUI64 and Trigger Refresh of Network Info
|
||||
/// @return Last Read EUI64
|
||||
//*******************************************************************************
|
||||
void shieldGetEUI64(uint8_t eui[8]);
|
||||
|
||||
//*******************************************************************************
|
||||
/// @brief Find and Join a Network
|
||||
//*******************************************************************************
|
||||
void shieldFindNetwork(void);
|
||||
|
||||
//*******************************************************************************
|
||||
/// @brief Leave the Current ZigBee Network
|
||||
//*******************************************************************************
|
||||
void shieldLeaveNetwork(void);
|
||||
|
||||
//*******************************************************************************
|
||||
// Update the network State/LED
|
||||
//*******************************************************************************
|
||||
void updateNetworkState(void); //get the current zigbee network status of the ST Shield
|
||||
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,123 @@
|
||||
//*****************************************************************************
|
||||
/// @file
|
||||
/// @brief
|
||||
/// Arduino SmartThings Ethernet ESP01 WiFi On/Off with LED Example
|
||||
///
|
||||
/// Revised by Dan Ogorchock on 2017-02-21 to work with new "SmartThings v2.0" Library
|
||||
///
|
||||
/// Notes: The ESP-01 communicates via WiFi to your home network router,
|
||||
/// then to the ST Hub, and eventually to the ST cloud servers.
|
||||
///
|
||||
/// The ESP-01 module has 2 GPIO pins, 0 and 2. There is no onboard
|
||||
/// LED, so you will need to wire an external LED to GPIO2 for this example.
|
||||
///
|
||||
//*****************************************************************************
|
||||
|
||||
#include <SmartThingsESP8266WiFi.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 2 //ESP-01 has 2 GPIO pins - GPIO0 (0) and GPIO2 (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
|
||||
|
||||
//******************************************************************************************
|
||||
//ESP8266 WiFi Information CHANGE THIS INFORMATION ACCORDINGLY FOR YOUR NETWORK!
|
||||
//******************************************************************************************
|
||||
String str_ssid = "yourSSIDhere"; // <---You must edit this line!
|
||||
String str_password = "yourWiFiPasswordhere"; // <---You must edit this line!
|
||||
IPAddress ip(192, 168, 1, 209); // Device IP Address // <---You must edit this line!
|
||||
IPAddress gateway(192, 168, 1, 1); //router gateway // <---You must edit this line!
|
||||
IPAddress subnet(255, 255, 255, 0); //LAN subnet mask // <---You must edit this line!
|
||||
IPAddress dnsserver(192, 168, 1, 1); //DNS server // <---You must edit this line!
|
||||
const unsigned int serverPort = 8090; // port to run the http server on
|
||||
|
||||
// Smartthings Hub Information
|
||||
IPAddress hubIp(192, 168, 1, 149); // smartthings hub ip // <---You must edit this line!
|
||||
const unsigned int hubPort = 39500; // smartthings hub port
|
||||
|
||||
|
||||
//Create a SmartThings Ethernet ESP8266WiFi object
|
||||
st::SmartThingsESP8266WiFi smartthing(str_ssid, str_password, ip, gateway, subnet, dnsserver, serverPort, hubIp, hubPort, messageCallout);
|
||||
|
||||
bool isDebugEnabled; // enable or disable debug in this example
|
||||
|
||||
//*****************************************************************************
|
||||
// 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()
|
||||
{
|
||||
digitalWrite(PIN_LED, HIGH); // turn LED on
|
||||
smartthing.send("on"); // send message to cloud
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
void off()
|
||||
{
|
||||
digitalWrite(PIN_LED, LOW); // turn LED off
|
||||
smartthing.send("off"); // send message to cloud
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
// 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;
|
||||
|
||||
if (isDebugEnabled)
|
||||
{ // setup debug serial port
|
||||
Serial.begin(9600); // setup serial with a baud rate of 9600
|
||||
Serial.println("");
|
||||
Serial.println("setup.."); // print out 'setup..' on start
|
||||
}
|
||||
|
||||
// setup hardware pins
|
||||
pinMode(PIN_LED, OUTPUT); // define PIN_LED as an output
|
||||
digitalWrite(PIN_LED, LOW); // set value to LOW (off)
|
||||
|
||||
//Run the SmartThings init() routine to make sure the ThingShield is connected to the ST Hub
|
||||
smartthing.init();
|
||||
|
||||
//synch up the ST cloud
|
||||
smartthing.send("off"); // send message to cloud
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
void loop()
|
||||
{
|
||||
// run smartthing logic
|
||||
smartthing.run();
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
//*****************************************************************************
|
||||
/// @file
|
||||
/// @brief
|
||||
/// Arduino SmartThings Ethernet ESP8266 WiFi On/Off with LED Example
|
||||
///
|
||||
/// Revised by Dan Ogorchock on 2017-02-11 to work with new "SmartThings v2.0" Library
|
||||
///
|
||||
/// Notes: The NodeMCU ESP communicates via WiFi to your home network router,
|
||||
/// then to the ST Hub, and eventually to the ST cloud servers.
|
||||
///
|
||||
///
|
||||
//*****************************************************************************
|
||||
|
||||
#include <SmartThingsESP8266WiFi.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
|
||||
//*****************************************************************************
|
||||
//******************************************************************************************
|
||||
//NodeMCU ESP8266 Pin Definitions (makes it much easier as these match the board markings)
|
||||
//******************************************************************************************
|
||||
#define LED_BUILTIN 16
|
||||
#define BUILTIN_LED 16
|
||||
|
||||
#define D0 16
|
||||
#define D1 5
|
||||
#define D2 4
|
||||
#define D3 0
|
||||
#define D4 2
|
||||
#define D5 14
|
||||
#define D6 12
|
||||
#define D7 13
|
||||
#define D8 15
|
||||
#define D9 3
|
||||
#define D10 1
|
||||
|
||||
|
||||
#define PIN_LED LED_BUILTIN //Onboard LED
|
||||
|
||||
//*****************************************************************************
|
||||
// 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
|
||||
|
||||
//******************************************************************************************
|
||||
//ESP8266 WiFi Information CHANGE THIS INFORMATION ACCORDINGLY FOR YOUR NETWORK!
|
||||
//******************************************************************************************
|
||||
String str_ssid = "yourSSIDhere"; // <---You must edit this line!
|
||||
String str_password = "yourWiFiPasswordhere"; // <---You must edit this line!
|
||||
IPAddress ip(192, 168, 1, 202); // Device IP Address // <---You must edit this line!
|
||||
IPAddress gateway(192, 168, 1, 1); //router gateway // <---You must edit this line!
|
||||
IPAddress subnet(255, 255, 255, 0); //LAN subnet mask // <---You must edit this line!
|
||||
IPAddress dnsserver(192, 168, 1, 1); //DNS server // <---You must edit this line!
|
||||
const unsigned int serverPort = 8090; // port to run the http server on
|
||||
|
||||
// Smartthings Hub Information
|
||||
IPAddress hubIp(192, 168, 1, 149); // smartthings hub ip // <---You must edit this line!
|
||||
const unsigned int hubPort = 39500; // smartthings hub port
|
||||
|
||||
|
||||
//Create a SmartThings Ethernet ESP8266WiFi object
|
||||
st::SmartThingsESP8266WiFi smartthing(str_ssid, str_password, ip, gateway, subnet, dnsserver, serverPort, hubIp, hubPort, messageCallout);
|
||||
|
||||
bool isDebugEnabled; // enable or disable debug in this example
|
||||
|
||||
|
||||
//*****************************************************************************
|
||||
// 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()
|
||||
{
|
||||
digitalWrite(PIN_LED, LOW); // turn LED on
|
||||
smartthing.send("on"); // send message to cloud
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
void off()
|
||||
{
|
||||
digitalWrite(PIN_LED, HIGH); // turn LED off
|
||||
smartthing.send("off"); // send message to cloud
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
// 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;
|
||||
|
||||
if (isDebugEnabled)
|
||||
{ // setup debug serial port
|
||||
Serial.begin(9600); // setup serial with a baud rate of 9600
|
||||
Serial.println("");
|
||||
Serial.println("setup.."); // print out 'setup..' on start
|
||||
}
|
||||
|
||||
// setup hardware pins
|
||||
pinMode(PIN_LED, OUTPUT); // define PIN_LED as an output
|
||||
digitalWrite(PIN_LED, HIGH); // set value to HIGH (off)
|
||||
|
||||
//Run the SmartThings init() routine to make sure the ThingShield is connected to the ST Hub
|
||||
smartthing.init();
|
||||
|
||||
//synch up the ST cloud
|
||||
smartthing.send("off"); // send message to cloud
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
void loop()
|
||||
{
|
||||
// run smartthing logic
|
||||
smartthing.run();
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
//*****************************************************************************
|
||||
/// @file
|
||||
/// @brief
|
||||
/// Arduino SmartThings Ethernet W5x00 On/Off with LED Example
|
||||
///
|
||||
/// Revised by Dan Ogorchock on 2017-02-10 to work with new "SmartThings v2.0" Library
|
||||
///
|
||||
/// Notes: Arduino communicates with both the W5x00 and SD card using the SPI bus (through the ICSP header).
|
||||
/// This is on digital pins 10, 11, 12, and 13 on the Uno and pins 50, 51, and 52 on the Mega.
|
||||
/// On both boards, pin 10 is used to select the W5x00 and pin 4 for the SD card.
|
||||
/// These pins cannot be used for general I/O. On the Mega, the hardware SS pin, 53,
|
||||
/// is not used to select either the W5x00 or the SD card, but it must be kept as an output
|
||||
/// or the SPI interface won't work.
|
||||
/// See https://www.arduino.cc/en/Main/ArduinoEthernetShieldV1 for details on the W5x00 Sield
|
||||
///
|
||||
///
|
||||
//*****************************************************************************
|
||||
|
||||
#include <SmartThingsEthernetW5x00.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
|
||||
//*****************************************************************************
|
||||
// "RESERVED" pins for W5x00 Ethernet Shield - best to avoid
|
||||
#define PIN_4_RESERVED 4 //reserved by W5x00 Shield on both UNO and MEGA
|
||||
#define PIN_1O_RESERVED 10 //reserved by W5x00 Shield on both UNO and MEGA
|
||||
#define PIN_11_RESERVED 11 //reserved by W5x00 Shield on UNO
|
||||
#define PIN_12_RESERVED 12 //reserved by W5x00 Shield on UNO
|
||||
#define PIN_13_RESERVED 13 //reserved by W5x00 Shield on UNO
|
||||
#define PIN_50_RESERVED 50 //reserved by W5x00 Shield on MEGA
|
||||
#define PIN_51_RESERVED 51 //reserved by W5x00 Shield on MEGA
|
||||
#define PIN_52_RESERVED 52 //reserved by W5x00 Shield on MEGA
|
||||
#define PIN_53_RESERVED 53 //reserved by W5x00 Shield on MEGA
|
||||
|
||||
#define PIN_LED 13 //Onboard LED
|
||||
|
||||
//*****************************************************************************
|
||||
// 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
|
||||
|
||||
//******************************************************************************************
|
||||
//W5x00 Ethernet Shield Information CHANGE THIS INFORMATION ACCORDINGLY FOR YOUR NETWORK!
|
||||
//******************************************************************************************
|
||||
byte mac[] = {0x06,0x02,0x03,0x04,0x05,0x06}; //MAC address, leave first octet 0x06, change others to be unique // <---You must edit this line!
|
||||
IPAddress ip(192, 168, 1, 204); //Arduino device IP Address // <---You must edit this line!
|
||||
IPAddress gateway(192, 168, 1, 1); //router gateway // <---You must edit this line!
|
||||
IPAddress subnet(255, 255, 255, 0); //LAN subnet mask // <---You must edit this line!
|
||||
IPAddress dnsserver(192, 168, 1, 1); //DNS server // <---You must edit this line!
|
||||
const unsigned int serverPort = 8090; // port to run the http server on
|
||||
|
||||
// Smartthings hub information
|
||||
IPAddress hubIp(192,168,1,149); // smartthings hub ip // <---You must edit this line!
|
||||
const unsigned int hubPort = 39500; // smartthings hub port
|
||||
|
||||
//Create a SmartThings Ethernet W5x00 object
|
||||
st::SmartThingsEthernetW5x00 smartthing(mac, ip, gateway, subnet, dnsserver, serverPort, hubIp, hubPort, messageCallout);
|
||||
|
||||
bool isDebugEnabled; // enable or disable debug in this example
|
||||
int stateLED; // state to track last set value of LED
|
||||
|
||||
//*****************************************************************************
|
||||
// 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.send("on"); // send message to cloud
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
void off()
|
||||
{
|
||||
stateLED = 0; // set state to 0 (off)
|
||||
digitalWrite(PIN_LED, LOW); // turn LED off
|
||||
smartthing.send("off"); // send message to cloud
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
// 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
|
||||
|
||||
// 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
|
||||
|
||||
//Run the SmartThings init() routine to make sure the ThingShield is connected to the ST Hub
|
||||
smartthing.init();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
//*****************************************************************************
|
||||
/// @file
|
||||
/// @brief
|
||||
/// Arduino SmartThings ThingShield On/Off with LED Example
|
||||
///
|
||||
/// Revised by Dan Ogorchock on 2017-02-10 to work with new "SmartThings v2.0" Library
|
||||
/// -Now supports automatic selectic of SoftwareSerial or HardwareSerial constructor
|
||||
/// depending on the Arduino Model.
|
||||
/// -If using an Arduino UNO R3, be sure to set the ThingShield switch to the D2/D3 position
|
||||
/// -If using an Arduino Leonardo, be sure to set the ThingShield switch to the D0/D1 position
|
||||
/// -If using an Arduino MEGA 2560, bbe sure to set the ThingShield switch to the D2/D3 position AND
|
||||
/// connect jumper wires between pins 2 & 14 as well as between pins 3 & 15.
|
||||
///
|
||||
/// DO NOT USE PINS 0,1,2,3,6 for I/O in your sketch as pins 0,1,2,3 are used by the ThingShield
|
||||
/// and USB communications to your computer. Pin 6 is reserved by the ThingShield.
|
||||
///
|
||||
/// @note UNO R3 Pins
|
||||
/// ______________
|
||||
/// | |
|
||||
/// | SW[] |
|
||||
/// |[]RST |
|
||||
/// | AREF |--
|
||||
/// | GND |--
|
||||
/// | 13 |--X Onboard LED
|
||||
/// | 12 |--
|
||||
/// | 11 |--
|
||||
/// --| 3.3V 10 |--
|
||||
/// --| 5V 9 |--
|
||||
/// --| GND 8 |--
|
||||
/// --| GND |
|
||||
/// --| Vin 7 |--
|
||||
/// | 6 |--Reserved
|
||||
/// --| A0 5 |--
|
||||
/// --| A1 ( ) 4 |--
|
||||
/// --| A2 3 |--X THING_RX
|
||||
/// --| A3 ____ 2 |--X THING_TX
|
||||
/// --| A4 | | 1 |--X THING_RX
|
||||
/// --| A5 | | 0 |--X THING_RX
|
||||
/// |____| |____|
|
||||
/// |____|
|
||||
///
|
||||
//*****************************************************************************
|
||||
#include <SmartThingsThingShield.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
|
||||
|
||||
#if defined(ARDUINO_AVR_UNO) || defined(ARDUINO_AVR_NANO) || defined(ARDUINO_AVR_MINI) //Arduino UNO, NANO, MINI
|
||||
st::SmartThingsThingShield smartthing(PIN_THING_RX, PIN_THING_TX, messageCallout); //Use Software Serial constructor
|
||||
#elif defined(ARDUINO_AVR_LEONARDO) //Arduino Leonardo
|
||||
st::SmartThingsThingShield smartthing(&Serial1, messageCallout); //Use Hardware Serial constructor w/Serial1
|
||||
#elif defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) //Arduino MEGA 1280 or 2560
|
||||
st::SmartThingsThingShield smartthing(&Serial3, messageCallout); //Use Hardware Serial constructor w/Serial3
|
||||
#else
|
||||
//assume user is using an UNO for the unknown case
|
||||
st::SmartThingsThingShield smartthing(PIN_THING_RX, PIN_THING_TX, messageCallout); //Use Software Serial constructor
|
||||
#endif
|
||||
|
||||
bool isDebugEnabled; // enable or disable debug in this example
|
||||
int stateLED; // state to track last set value of LED
|
||||
|
||||
//*****************************************************************************
|
||||
// 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, 1);
|
||||
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
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
// 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
|
||||
|
||||
// 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
|
||||
|
||||
//Run the SmartThings init() routine to make sure the ThingShield is connected to the ST Hub
|
||||
smartthing.init();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
//*****************************************************************************
|
||||
/// @file
|
||||
/// @brief
|
||||
/// Arduino SmartThings Ethernet WiFiEsp On/Off with LED Example
|
||||
///
|
||||
/// Revised by Dan Ogorchock on 2017-02-20 to work with new "SmartThings v2.0" Library
|
||||
///
|
||||
/// Notes: The Arduino MEGA 2560 communicates via Serial1 to the ESP-01 WiFi board
|
||||
/// to your home network router, then to the ST Hub, and eventually to the
|
||||
/// ST cloud servers.
|
||||
///
|
||||
/// You must connect the ESP-01 to the Arduino MEGA 2560's "Serial1"
|
||||
/// Hardware Serial port on pins 18 & 19. Google it!
|
||||
///
|
||||
/// The ESP-01 must be running a version of firmware compatible with
|
||||
/// the WiFiEsp library. Try the examples in the WiFiEsp library
|
||||
/// to make sure everything is wried correctly before trying this sketch!
|
||||
///
|
||||
//*****************************************************************************
|
||||
|
||||
#include <SmartThingsWiFiEsp.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
|
||||
|
||||
//*****************************************************************************
|
||||
// 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
|
||||
|
||||
//******************************************************************************************
|
||||
//ESP8266 WiFi Information CHANGE THIS INFORMATION ACCORDINGLY FOR YOUR NETWORK!
|
||||
//******************************************************************************************
|
||||
String str_ssid = "yourSSIDhere"; // <---You must edit this line!
|
||||
String str_password = "yourWiFiPasswordhere"; // <---You must edit this line!
|
||||
IPAddress ip(192, 168, 1, 203); // Device IP Address // <---You must edit this line!
|
||||
const unsigned int serverPort = 8090; // port to run the http server on
|
||||
|
||||
// Smartthings Hub Information
|
||||
IPAddress hubIp(192, 168, 1, 149); // smartthings hub ip // <---You must edit this line!
|
||||
const unsigned int hubPort = 39500; // smartthings hub port
|
||||
|
||||
|
||||
//Create a SmartThings Ethernet ESP8266WiFi object using the Arduino + ESP-01 constructor
|
||||
st::SmartThingsWiFiEsp smartthing(&Serial1, str_ssid, str_password, ip, serverPort, hubIp, hubPort, messageCallout);
|
||||
|
||||
bool isDebugEnabled; // enable or disable debug in this example
|
||||
int stateLED; // state to track last set value of LED
|
||||
|
||||
//*****************************************************************************
|
||||
// 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.send("on"); // send message to cloud
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
void off()
|
||||
{
|
||||
stateLED = 0; // set state to 0 (off)
|
||||
digitalWrite(PIN_LED, LOW); // turn LED off
|
||||
//smartthing.send("off"); // send message to cloud
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
// 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
|
||||
|
||||
if (isDebugEnabled)
|
||||
{ // setup debug serial port
|
||||
Serial.begin(9600); // setup serial with a baud rate of 9600
|
||||
Serial.println("");
|
||||
Serial.println("setup.."); // print out 'setup..' on start
|
||||
}
|
||||
|
||||
// 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
|
||||
|
||||
// initialize Hardware Serial UART for ESP module
|
||||
Serial1.begin(115200); //May need to adjust the baud rate for your ESP-01 (9600, 57600, 115200)
|
||||
|
||||
//Run the SmartThings init() routine to make sure the ThingShield is connected to the ST Hub
|
||||
smartthing.init();
|
||||
|
||||
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
void loop()
|
||||
{
|
||||
// run smartthing logic
|
||||
smartthing.run();
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
//*****************************************************************************
|
||||
/// @file
|
||||
/// @brief
|
||||
/// Arduino SmartThings Ethernet WiFi NINA On/Off with LED Example
|
||||
///
|
||||
/// Revised by Dan Ogorchock on 2020-01-19 to work with new "SmartThings v2.0" Library
|
||||
///
|
||||
/// Notes: The Arduino with NINA communicates via WiFi to your home network router,
|
||||
/// then to the ST Hub, and eventually to the ST cloud servers.
|
||||
///
|
||||
///
|
||||
//*****************************************************************************
|
||||
|
||||
#include <SmartThingsWiFiNINA.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
|
||||
//*****************************************************************************
|
||||
|
||||
//*****************************************************************************
|
||||
// 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
|
||||
|
||||
//******************************************************************************************
|
||||
//WiFiNINA Information CHANGE THIS INFORMATION ACCORDINGLY FOR YOUR NETWORK!
|
||||
//******************************************************************************************
|
||||
String str_ssid = "yourSSIDhere"; // <---You must edit this line!
|
||||
String str_password = "yourWiFiPasswordhere"; // <---You must edit this line!
|
||||
IPAddress ip(192, 168, 1, 202); // Device IP Address // <---You must edit this line if using static IP!
|
||||
IPAddress gateway(192, 168, 1, 1); //router gateway // <---You must edit this line if using static IP!
|
||||
IPAddress subnet(255, 255, 255, 0); //LAN subnet mask // <---You must edit this line if using static IP!
|
||||
IPAddress dnsserver(192, 168, 1, 1); //DNS server // <---You must edit this line if using static IP!
|
||||
const unsigned int serverPort = 8090; // port to run the http server on
|
||||
|
||||
// Smartthings Hub Information
|
||||
IPAddress hubIp(192, 168, 1, 149); // smartthings hub ip // <---You must edit this line!
|
||||
const unsigned int hubPort = 39500; // smartthings hub port
|
||||
|
||||
// Hubitat Hub Information
|
||||
//IPAddress hubIp(192, 168, 1, 149); // Hubitat hub ip // <---You must edit this line!
|
||||
//const unsigned int hubPort = 39501; // Hubitat hub port
|
||||
|
||||
|
||||
|
||||
//Create a SmartThings Ethernet WiFiNINA object (comment/uncomment the lines below as desired - only ONE can be active)
|
||||
// static IP
|
||||
st::SmartThingsWiFiNINA smartthing(str_ssid, str_password, ip, gateway, subnet, dnsserver, serverPort, hubIp, hubPort, messageCallout);
|
||||
// DHCP
|
||||
//st::SmartThingsWiFiNINA smartthing(str_ssid, str_password, serverPort, hubIp, hubPort, messageCallout);
|
||||
|
||||
bool isDebugEnabled; // enable or disable debug in this example
|
||||
|
||||
|
||||
//*****************************************************************************
|
||||
// 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()
|
||||
{
|
||||
digitalWrite(PIN_LED, LOW); // turn LED on
|
||||
smartthing.send("on"); // send message to cloud
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
void off()
|
||||
{
|
||||
digitalWrite(PIN_LED, HIGH); // turn LED off
|
||||
smartthing.send("off"); // send message to cloud
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
// 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;
|
||||
|
||||
if (isDebugEnabled)
|
||||
{ // setup debug serial port
|
||||
Serial.begin(9600); // setup serial with a baud rate of 9600
|
||||
Serial.println("");
|
||||
Serial.println("setup.."); // print out 'setup..' on start
|
||||
}
|
||||
|
||||
// setup hardware pins
|
||||
pinMode(PIN_LED, OUTPUT); // define PIN_LED as an output
|
||||
digitalWrite(PIN_LED, HIGH); // set value to HIGH (off)
|
||||
|
||||
//Run the SmartThings init() routine to make sure the ThingShield is connected to the ST Hub
|
||||
smartthing.init();
|
||||
|
||||
//synch up the ST cloud
|
||||
smartthing.send("off"); // send message to cloud
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
void loop()
|
||||
{
|
||||
// run smartthing logic
|
||||
smartthing.run();
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
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();
|
||||
}
|
||||
}
|
||||
111
lib/SmartThings/extras/On_Off_LED_Ethernet.device.groovy
Normal file
111
lib/SmartThings/extras/On_Off_LED_Ethernet.device.groovy
Normal file
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* Copyright 2015 SmartThings (original)
|
||||
*
|
||||
* 2017-02-11 Dan Ogorchock - Since SmartThings has abandoned the ThingShield, I have created a new
|
||||
* Arduino "SmartThings" library with support for the ThingShield,
|
||||
* W5100 Ethernet Shield, and the NodeMCU ESP8266 Board for connectivity to
|
||||
* the SmartThings cloud. This file is to be used with the On/Off Ethernet W5100
|
||||
* and ESP8266 examples found in this new library which can be found at
|
||||
*
|
||||
* https://github.com/DanielOgorchock/ST_Anything/tree/master/Arduino/libraries/SmartThings
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
metadata {
|
||||
definition (name: "On/Off Ethernet", namespace: "ogiewon", author: "Dan Ogorchock") {
|
||||
capability "Actuator"
|
||||
capability "Switch"
|
||||
capability "Sensor"
|
||||
}
|
||||
|
||||
// Simulator metadata
|
||||
simulator {
|
||||
|
||||
}
|
||||
|
||||
// Preferences
|
||||
preferences {
|
||||
input "ip", "text", title: "Arduino IP Address", description: "ip", required: true, displayDuringSetup: true
|
||||
input "port", "text", title: "Arduino Port", description: "port", required: true, displayDuringSetup: true
|
||||
input "mac", "text", title: "Arduino MAC Addr", description: "mac", required: true, displayDuringSetup: true
|
||||
}
|
||||
// UI tile definitions
|
||||
tiles {
|
||||
standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true, canChangeBackground: true) {
|
||||
state "on", label: '${name}', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#79b821"
|
||||
state "off", label: '${name}', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff"
|
||||
}
|
||||
standardTile("configure", "device.configure", inactiveLabel: false, decoration: "flat") {
|
||||
state "configure", label:'', action:"configuration.configure", icon:"st.secondary.configure"
|
||||
}
|
||||
|
||||
main (["switch"])
|
||||
details (["switch","configure"])
|
||||
}
|
||||
}
|
||||
|
||||
// parse events into attributes
|
||||
def parse(String description) {
|
||||
//log.debug "Parsing '${description}'"
|
||||
def msg = parseLanMessage(description)
|
||||
def headerString = msg.header
|
||||
|
||||
if (!headerString) {
|
||||
//log.debug "headerstring was null for some reason :("
|
||||
}
|
||||
|
||||
def bodyString = msg.body
|
||||
|
||||
if (bodyString) {
|
||||
//log.debug "BodyString: $bodyString"
|
||||
def value = bodyString
|
||||
def name = value in ["on","off"] ? "switch" : null
|
||||
def result = createEvent(name: name, value: value)
|
||||
log.debug "Parse returned ${result?.descriptionText}"
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
private getHostAddress() {
|
||||
def ip = settings.ip
|
||||
def port = settings.port
|
||||
|
||||
log.debug "Using ip: ${ip} and port: ${port} for device: ${device.id}"
|
||||
return ip + ":" + port
|
||||
}
|
||||
|
||||
def sendEthernet(message) {
|
||||
log.debug "Executing 'sendEthernet' ${message}"
|
||||
new physicalgraph.device.HubAction(
|
||||
method: "POST",
|
||||
path: "/${message}?",
|
||||
headers: [ HOST: "${getHostAddress()}" ]
|
||||
)
|
||||
}
|
||||
|
||||
// Commands sent to the device
|
||||
def on() {
|
||||
log.debug "Sending 'on'"
|
||||
sendEthernet("on")
|
||||
}
|
||||
|
||||
def off() {
|
||||
log.debug "Sending 'off'"
|
||||
sendEthernet("off")
|
||||
}
|
||||
|
||||
def configure() {
|
||||
log.debug "Executing 'configure'"
|
||||
if(device.deviceNetworkId!=settings.mac) {
|
||||
log.debug "setting device network id"
|
||||
device.deviceNetworkId = settings.mac
|
||||
}
|
||||
}
|
||||
67
lib/SmartThings/extras/On_Off_LED_ThingShield.device.groovy
Normal file
67
lib/SmartThings/extras/On_Off_LED_ThingShield.device.groovy
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Copyright 2015 SmartThings (original)
|
||||
*
|
||||
* 2017-02-11 Dan Ogorchock - Since SmartThings has abandoned the ThingShield, I have created a new
|
||||
* Arduino "SmartThings" library with support for the ThingShield,
|
||||
* W5100 Ethernet Shield, and the NodeMCU ESP8266 Board for connectivity to
|
||||
* the SmartThings cloud. This file is to be used with the On/Off ThingShield
|
||||
* example found in this new library which can be found at
|
||||
*
|
||||
* https://github.com/DanielOgorchock/ST_Anything/tree/master/Arduino/libraries/SmartThings
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
|
||||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
|
||||
* for the specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
metadata {
|
||||
definition (name: "On/Off ThingShield", namespace: "ogiewon", author: "Dan Ogorchock") {
|
||||
capability "Actuator"
|
||||
capability "Switch"
|
||||
capability "Sensor"
|
||||
}
|
||||
|
||||
// Simulator metadata
|
||||
simulator {
|
||||
status "on": "catchall: 0104 0000 01 01 0040 00 0A21 00 00 0000 0A 00 0A6F6E"
|
||||
status "off": "catchall: 0104 0000 01 01 0040 00 0A21 00 00 0000 0A 00 0A6F6666"
|
||||
|
||||
// reply messages
|
||||
reply "raw 0x0 { 00 00 0a 0a 6f 6e }": "catchall: 0104 0000 01 01 0040 00 0A21 00 00 0000 0A 00 0A6F6E"
|
||||
reply "raw 0x0 { 00 00 0a 0a 6f 66 66 }": "catchall: 0104 0000 01 01 0040 00 0A21 00 00 0000 0A 00 0A6F6666"
|
||||
}
|
||||
|
||||
// UI tile definitions
|
||||
tiles {
|
||||
standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true, canChangeBackground: true) {
|
||||
state "on", label: '${name}', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#79b821"
|
||||
state "off", label: '${name}', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff"
|
||||
}
|
||||
|
||||
main "switch"
|
||||
details "switch"
|
||||
}
|
||||
}
|
||||
|
||||
// Parse incoming device messages to generate events
|
||||
def parse(String description) {
|
||||
def value = zigbee.parse(description)?.text
|
||||
def name = value in ["on","off"] ? "switch" : null
|
||||
def result = createEvent(name: name, value: value)
|
||||
log.debug "Parse returned ${result?.descriptionText}"
|
||||
return result
|
||||
}
|
||||
|
||||
// Commands sent to the device
|
||||
def on() {
|
||||
zigbee.smartShield(text: "on").format()
|
||||
}
|
||||
|
||||
def off() {
|
||||
zigbee.smartShield(text: "off").format()
|
||||
}
|
||||
46
lib/SmartThings/info.txt
Normal file
46
lib/SmartThings/info.txt
Normal file
@@ -0,0 +1,46 @@
|
||||
--------------------------------------------------------------------------------------------------
|
||||
| Setup Instructions |
|
||||
--------------------------------------------------------------------------------------------------
|
||||
1) Copy all of the SmartThings* directories into the 'libraries' directory in your sketchbook location.
|
||||
Windows: 'My Documents\Arduino\libraries\SmartThings*'
|
||||
OSX: '~/Documents/Arduino/libraries/SmartThings*'
|
||||
|
||||
--------------------------------------------------------------------------------------------------
|
||||
| Notes |
|
||||
--------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------------------------
|
||||
| ChangeLog: |
|
||||
--------------------------------------------------------------------------------------------------
|
||||
05/25/2017 (v2.3.0) do + Added support for W5500 shield, causing library to be broken into
|
||||
multiple folders to avoid duplicate class definitions
|
||||
04/26/2017 (v2.2.0) do + Added optional argument for max transmission rate which can be queried
|
||||
by calling application. Not used internally.
|
||||
02/20/2017 (v2.1.0) do + Added support for Arduino + ESP-01 hardware combination
|
||||
02/10/2017 (v2.0.0) do ~ Totally rearchitected C++ Class structure to support ThingShield, W5100 Ethernet, and ESP8266 WiFi
|
||||
+ Added new init() function to allow object to connect to network at runtime in sketch setup() routine
|
||||
+ Revised On/Off Thingshield example to use the new library class, SmartThingsThingShield
|
||||
+ Added On/Off W5100 example to use the new SmartThingsEthernetW5100 class
|
||||
+ Added On/Off ESP8266WiFi example to use the new SmartThingsESP8266WiFi class
|
||||
01/11/2015 (v0.0.6) do + Added Hardware Serial Constructor
|
||||
+ Added support for Leonardo and MEGA
|
||||
~ Performance improvements
|
||||
~ Significant SRAM memory optimizations (minimum ~175 bytes, up to 430 bytes)
|
||||
02/11/2012 (v0.0.5) db + shieldGetLastNetworkStatus to public Class
|
||||
+ example/stLEDwithNetworkStatus
|
||||
~ renamed SmartThingsLED to stLED
|
||||
+ ascii diagram for each example documenting pins & usage
|
||||
02/08/2012 (v0.0.4) db + Add docs/Doxygen
|
||||
+ Begin Framework for working on Leonardo (incomplete)
|
||||
~ Minor format cleanup
|
||||
02/08/2012 (v0.0.3) db ! Fixed regression in last commit with rgb
|
||||
02/07/2012 (v0.0.2) db ! Fixed typo in send functions
|
||||
! Changed write to print for maximal compatibility with serial code
|
||||
dm ! Point Library to Absolute Path, Add documentation
|
||||
02/06/2012 (v0.0.1) db ! Lowered Baud to 2400 due to softwareSerial rxBuffer size issue
|
||||
+ Add shield Commands(SetLED, Leave, Find, NetworkStatus)
|
||||
+ Updated Example to also SetLED with on/off control
|
||||
+ Add Doxygen comments & Version to Lib
|
||||
02/04/2012 (unversioned) db + Initial Commit (currently depends on SoftwareSerial)
|
||||
|
||||
29
lib/SmartThings/keywords.txt
Normal file
29
lib/SmartThings/keywords.txt
Normal file
@@ -0,0 +1,29 @@
|
||||
#######################################
|
||||
# Data Types
|
||||
#######################################
|
||||
SmartThings KEYWORD1
|
||||
SmartThingsThingShield KEYWORD1
|
||||
SmartThingsEthernet KEYWORD1
|
||||
SmartThingsEthernetW5100 KEYWORD1
|
||||
SmartThingsEthernetW5500 KEYWORD1
|
||||
SmartThingsESP8266WiFi KEYWORD1
|
||||
SmartThingsESP32WiFi KEYWORD1
|
||||
SmartThingsWiFiEsp KEYWORD1
|
||||
SmartThingsWiFi101 KEYWORD1
|
||||
SmartThingsWiFiNINA KEYWORD1
|
||||
SmartThingsCallout_t KEYWORD1
|
||||
SmartThingsNetworkState_t KEYWORD1
|
||||
|
||||
#######################################
|
||||
# Public Methods / Functions
|
||||
#######################################
|
||||
run KEYWORD2
|
||||
send KEYWORD2
|
||||
init KEYWORD2
|
||||
getTransmitInterval KEYWORD2
|
||||
shieldSetLED KEYWORD2
|
||||
shieldFindNetwork KEYWORD2
|
||||
shieldLeaveNetwork KEYWORD2
|
||||
shieldGetLastNetworkState KEYWORD2
|
||||
shieldGetNetworkState KEYWORD2
|
||||
|
||||
10
lib/SmartThings/library.properties
Normal file
10
lib/SmartThings/library.properties
Normal file
@@ -0,0 +1,10 @@
|
||||
name=SmartThings
|
||||
version=2.5
|
||||
author=Dan G Ogorchock <ogiewon@gmail.com>, Daniel J Ogorchock <djogorchock@gmail.com>
|
||||
maintainer=Dan G Ogorchock <ogiewon@gmail.com>
|
||||
sentence=A library that provides multiple methods to connect to SmartThings.
|
||||
paragraph=This is a new and improved version of the SmartThings libray which now supports the original ThingShield, the W5100/W500 Ethernet shields, as well as the NodeMCU ESP8266 WiFi and ESP32 boards.
|
||||
category=Communication
|
||||
url=https://github.com/DanielOgorchock/ST_Anything/tree/master/Arduino/libraries/SmartThings
|
||||
architectures=avr,esp8266,samd,esp32
|
||||
includes=
|
||||
Reference in New Issue
Block a user