Skip to content

Commit

Permalink
Merge pull request #17 from rlisle/SmartThings
Browse files Browse the repository at this point in the history
MQTT support.
  • Loading branch information
rlisle committed Sep 4, 2018
2 parents a4b8a6a + a872b2e commit 07979a6
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Photon/IoTlib/.vscode/c_cpp_properties.json
Expand Up @@ -72,5 +72,5 @@
}
}
],
"version": 3
"version": 4
}
2 changes: 1 addition & 1 deletion Photon/IoTlib/library.properties
@@ -1,5 +1,5 @@
name=IoT
version=2.1.1
version=2.2.1
author=Ron Lisle
license=BSD
sentence=Control your Photon projects using events from other Photons, Alexa and iOS devices
Expand Down
37 changes: 33 additions & 4 deletions Photon/IoTlib/src/IoT.cpp
Expand Up @@ -16,6 +16,8 @@ BSD license, check LICENSE for more information.
All text above must be included in any redistribution.
Changelog:
2018-09-04: Bridge Particle to MQTT
2018-07-07: Convert MQTT format to match SmartThings
2018-03-16: Add MQTT support
2018-01-17: Add functions for device state and type
2017-10-22: Convert to scene-like behavior
Expand Down Expand Up @@ -95,7 +97,15 @@ IoT* IoT::_instance = NULL;
void IoT::log(String msg)
{
Serial.println(msg);
Particle.publish("LOG", msg, 60, PRIVATE);
// Write to MQTT if connected
IoT* iot = IoT::getInstance();
if (iot->_mqtt != NULL && iot->_mqtt->isConnected()) {
iot->_mqtt->publish("patriot/debug", msg);

// Otherwise write to particle (limit # writes available)
} else {
Particle.publish("LOG", msg, 60, PRIVATE);
}
}

/**
Expand Down Expand Up @@ -172,11 +182,11 @@ void IoT::begin()
}
}

void IoT::connectMQTT(byte *brokerIP)
void IoT::connectMQTT(byte *brokerIP, bool isBridge)
{
// Do we need to disconnect if previously connected?
_isBridge = isBridge;
_mqtt = new MQTT(brokerIP, 1883, globalMQTTHandler);
_mqtt->connect("PatriotIoT"); // This is NOT topic. Do we need something specific here?
_mqtt->connect("PatriotIoT"); // Unique connection ID
if (_mqtt->isConnected()) {
if(!_mqtt->subscribe(publishNameVariable)) { // Topic name
log("Unable to subscribe to MQTT");
Expand Down Expand Up @@ -254,6 +264,7 @@ void IoT::buildSupportedActivitiesVariable()

/*************************************/
/*** Particle.io Subscribe Handler ***/
/*** t:patriot m:<device>:<value> ***/
/*************************************/
void IoT::subscribeHandler(const char *eventName, const char *rawData)
{
Expand All @@ -264,6 +275,17 @@ void IoT::subscribeHandler(const char *eventName, const char *rawData)
String name = data.substring(0,colonPosition);
String state = data.substring(colonPosition+1);

// Bridge events to MQTT if this is a Bridge
// to t:particle/<eventName> m:<msg>
// eg. patriot DeskLamp:100 -> particle/patriot DeskLamp:100
if(_isBridge)
{
if (_mqtt != NULL && _mqtt->isConnected()) {
_mqtt->publish(String("particle/")+eventName, data);
}
//TODO: do we want to return at the point?
}

// See if this is a device name. If so, update it.
Device* device = _devices->getDeviceWithName(name);
if(device)
Expand Down Expand Up @@ -292,6 +314,13 @@ void IoT::mqttHandler(char* topic, byte* payload, unsigned int length) {
String name = data.substring(0,colonPosition);
String state = data.substring(colonPosition+1);

// Bridge events to Particle if this is a Bridge
if(_isBridge)
{
//TODO: Do we want to copy from MQTT to Particle?
// Probably not.
}

// See if this is a device name. If so, update it.
Device* device = _devices->getDeviceWithName(name);
if(device)
Expand Down
3 changes: 2 additions & 1 deletion Photon/IoTlib/src/IoT.h
Expand Up @@ -70,7 +70,7 @@ class IoT {
* connectMQTT(byte * brokerIP)
* Connect to an MQTT broker with specified IP
**/
void connectMQTT(byte *brokerIP);
void connectMQTT(byte *brokerIP, bool isBridge = false);

/**
* Loop needs to be called periodically
Expand All @@ -89,6 +89,7 @@ class IoT {
private:
static IoT* _instance;
bool _hasBegun;
bool _isBridge;
String _publishName;
String _proximityEvent;
String _supportedActivities[kMaxNumberActivities];
Expand Down
4 changes: 2 additions & 2 deletions Photon/Plugins/Relay/src/PatriotRelay.cpp
Expand Up @@ -47,14 +47,14 @@ void Relay::setPercent(int percent)
if(percent > 0)
{
digitalWrite(_pinNum, HIGH);
if(duration != 0)
if(_duration != 0)
{
_stopMillis = millis() + (_duration * MILLIS_PER_SECOND);
}
}
else
{
digitalWrite(pinNum, LOW);
digitalWrite(_pinNum, LOW);
_stopMillis = 0;
}
_percent = percent;
Expand Down
22 changes: 22 additions & 0 deletions Photon/Plugins/SmartThings/LICENSE
@@ -0,0 +1,22 @@
Copyright 2018 Ron Lisle

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 changes: 67 additions & 0 deletions Photon/Plugins/SmartThings/README.md
@@ -0,0 +1,67 @@
# PatriotSmartThings

A Patriot plugin to support SmartThings devices.

This library is used to support SmartThings devices.
It requires that the SmartThings MQTT Bridge be installed and working.

## Usage

Include this library in any Photon sketch that needs to support
SmartThings devices.


```
#include <IoT.h>
#include <PatriotSmartThings.h>
IoT *iot;
void setup() {
iot = IoT::getInstance();
iot->begin();
// Create your device
SmartThingsDevice *deskLamp = new SmartThingsDevice("<SmartThingsName>", "<PatriotDeviceName>");
// Add it to IoT
iot->addDevice(deskLamp);
// Add any behaviors if appropriate
iot->addBehavior(new Behavior(deskLamp, "Work at desk", '>', 0, 100));
...
}
void loop() {
iot->loop();
}
```

## Documentation

Refer to the Patriot Github repository and documentation for more
information.


## Contributing

Here's how you can make changes to this library and eventually contribute those changes back.

To get started, [clone the library from GitHub to your local machine](https://help.github.com/articles/cloning-a-repository/).

Change the name of the library in `library.properties` to something different. You can add your name at then end.

Modify the sources in <src> and <examples> with the new behavior.

To compile an example, use `particle compile examples/usage` command in [Particle CLI](https://docs.particle.io/guide/tools-and-features/cli#update-your-device-remotely) or use our [Desktop IDE](https://docs.particle.io/guide/tools-and-features/dev/#compiling-code).

After your changes are done you can upload them with `particle library upload` or `Upload` command in the IDE. This will create a private (only visible by you) library that you can use in other projects. Do `particle library add IoT_myname` to add the library to a project on your machine or add the IoT_myname library to a project on the Web IDE or Desktop IDE.

At this point, you can create a [GitHub pull request](https://help.github.com/articles/about-pull-requests/) with your changes to the original library.

If you wish to make your library public, use `particle library publish` or `Publish` command.

## LICENSE
Copyright 2018 Ron Lisle

Refer to the included LICENSE file.
11 changes: 11 additions & 0 deletions Photon/Plugins/SmartThings/library.properties
@@ -0,0 +1,11 @@
name=PatriotSmartThings
version=2.1.0
author=Ron Lisle
license=MIT
sentence=Extend Patriot IoT to support SmartThings devices.
paragraph=Patriot provides support for controlling IoT devices using Alexa and iOS devices. This plugin adds the ability to add SmartThings devices. It requires the MQTT SmartThings Bridge.
url=Lisles.net
repository=https://github.com/rlisle/Patriot
architectures=particle-photon
# Update dependencies to the latest, or the version that you've tested with.
dependencies.IoT=2.2.1
1 change: 1 addition & 0 deletions Photon/Plugins/SmartThings/src/device.h

0 comments on commit 07979a6

Please sign in to comment.