Skip to content

Instantly share code, notes, and snippets.

@jarodreyes
Created December 15, 2017 00:58
Show Gist options
  • Save jarodreyes/dd297ff54011824aa20ba547b22b4d55 to your computer and use it in GitHub Desktop.
Save jarodreyes/dd297ff54011824aa20ba547b22b4d55 to your computer and use it in GitHub Desktop.
owls.ino
// This #include statement was automatically added by the Particle IDE.
#undef MBEDTLS_ECDSA_C
// This #include statement was automatically added by the Particle IDE.
#include <MQTT-TLS.h>
// This #include statement was automatically added by the Particle IDE.
#include <ArduinoJson.h>
#include "certificate.h"
/*
* Sync Settings
*
* Enter a Sync Key & Password, your document unique name,
* and a device name.
*/
const char* sync_key = "KYx";
const char* sync_password = "nnnnnn";
const char* sync_document = "sync/docs/BoardLED";
const char* sync_device_name = "jreyes_owl_key";
/* LED Setting - on the Electron, the LED is Pin 7 */
const uint8_t LED_PIN = D5;
/* Sync server and MQTT setup; you probably don't have to change these. */
char* mqtt_server = "mqtt-sync.us1.twilio.com";
uint16_t mqtt_port = 8883;
const uint16_t maxMQTTpackageSize = 512;
void callback(char* topic, byte* payload, unsigned int length);
MQTT client(
mqtt_server,
mqtt_port,
callback
);
/*
* Our Twilio Connected Devices message handling callback. This is passed as a
* callback function when we subscribe to the document, and any messages will
* appear here.
*/
void callback(char* topic, byte* payload, unsigned int length)
{
std::unique_ptr<char []> msg(new char[length+1]());
memcpy (msg.get(), payload, length);
Serial.print("Message arrived on topic "); Serial.println(msg.get());
StaticJsonBuffer<maxMQTTpackageSize> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(msg.get());
const char* led_command_raw = root["led"];
String led_command(led_command_raw);
if (led_command == "ON") {
digitalWrite(LED_PIN, HIGH);
Serial.println("LED turned on!");
} else {
digitalWrite(LED_PIN, LOW);
Serial.println("LED turned off!");
}
}
/*
* This function connects to Sync via MQTT. We connect using the device keys
* from above, and immediately check the server's certificate fingerprint.
*
* If everything works, we subscribe to the document topic and return.
*/
void connect_mqtt()
{
// Connect to Sync
Serial.println("Connecting to Sync...");
Serial.println(client.connect(
"owl-jarod",
sync_key,
sync_password
));
if (client.isConnected()) {
client.subscribe(sync_document);
Serial.print("Subscribed to "); Serial.println(sync_document);
}
Serial.print("Client Connected:");
Serial.println(client.isConnected());
}
/* In setup, we configure our LED, enabled serial, and connect to Sync */
void setup()
{
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(115200);
// Ensure we are talking to Twilio
client.enableTls(root_ca, sizeof(root_ca));
// Connect to Sync
Serial.println("Connecting to Sync...");
Serial.println(client.connect(
"owl-jarod",
sync_key,
sync_password
));
if (client.isConnected()) {
client.subscribe(sync_document);
Serial.print("Subscribed to "); Serial.println(sync_document);
}
Serial.print("Client Connected:");
Serial.println(client.isConnected());
}
/*
* Our loop constantly checks we are still connected and runs the
* heartbeat/yield function.
*/
void loop()
{
if (client.isConnected()) {
Serial.println("Connected Successfully!");
client.loop();
} else {
delay(1000);
connect_mqtt();
}
delay(1000);
// Here's an example of publishing from the Particle Electron.
// Uncomment until the end of the function to send a 'msg' back to Sync
// every 2 minutes!
// static uint32_t now = millis();
// if (millis() > now + (2*(1000*60))) {
// Serial.println("Sending 2 minute ON message to Twilio!");
// client.publish(
// sync_document,
// "{\"msg\":\"Ahoy World!\",\"led\":\"ON\"}"
// );
// now = millis();
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment