Last active
May 4, 2016 20:04
-
-
Save LosantGists/dbe25a30b433a0962ec39da7c683719b to your computer and use it in GitHub Desktop.
how-to-connect-a-particle-photon-to-the-losant-iot-platform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This #include statement was automatically added by the Particle IDE. | |
#include "SparkJson/SparkJson.h" | |
// This #include statement was automatically added by the Particle IDE. | |
#include "MQTT/MQTT.h" | |
void setup() { | |
} | |
void loop() { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SparkJson/SparkJson.h" | |
#include "MQTT/MQTT.h" | |
#define LOSANT_BROKER "broker.losant.com" | |
#define LOSANT_DEVICE_ID "my-device-id" | |
#define LOSANT_ACCESS_KEY "my-access-key" | |
#define LOSANT_ACCESS_SECRET "my-access-secret" | |
// Topic used to subscribe to Losant commands. | |
String MQTT_TOPIC_COMMAND = | |
String::format("losant/%s/command", LOSANT_DEVICE_ID); | |
// Topic used to publish state to Losant. | |
String MQTT_TOPIC_STATE = | |
String::format("losant/%s/state", LOSANT_DEVICE_ID); | |
void setup() { | |
} | |
void loop() { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SparkJson/SparkJson.h" | |
#include "MQTT/MQTT.h" | |
#define LOSANT_BROKER "broker.losant.com" | |
#define LOSANT_DEVICE_ID "56de13f80e0a8801005a9ba6" | |
#define LOSANT_ACCESS_KEY "770c0556-16ae-4ed0-95e2-4206d3660800" | |
#define LOSANT_ACCESS_SECRET "5267b5d2e09c17e010913f81743b29ed67ea4a988d811ab3dd05447e9c47d5a7" | |
// Topic used to subscribe to Losant commands. | |
String MQTT_TOPIC_COMMAND = | |
String::format("losant/%s/command", LOSANT_DEVICE_ID); | |
// Topic used to publish state to Losant. | |
String MQTT_TOPIC_STATE = | |
String::format("losant/%s/state", LOSANT_DEVICE_ID); | |
// The Photon's onboard LED. | |
int LED = D7; | |
// Callback signature for MQTT subscriptions. | |
void callback(char* topic, byte* payload, unsigned int length); | |
// MQTT client. | |
MQTT client(LOSANT_BROKER, 1883, callback); | |
// Toggles the LED on/off whenever "toggle" command is received. | |
bool ledValue = false; | |
void callback(char* topic, byte* payload, unsigned int length) { | |
// Parse the command payload. | |
StaticJsonBuffer<200> jsonBuffer; | |
JsonObject& command = jsonBuffer.parseObject((char*)payload); | |
Serial.println("Command received:"); | |
command.printTo(Serial); | |
Serial.println(); | |
// If the command's name is "toggle", flip the LED. | |
if(String(command["name"].asString()).equals(String("toggle"))) { | |
ledValue = !ledValue; | |
digitalWrite(LED, ledValue ? HIGH : LOW); | |
Serial.println("Toggling LED"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void setup() { | |
Serial.begin(9600); | |
while(!Serial) { } | |
pinMode(LED, OUTPUT); | |
} | |
// Connects to the Losant MQTT broker. | |
void connect() { | |
Serial.print("Connecting to Losant..."); | |
while(!client.isConnected()) { | |
client.connect( | |
LOSANT_DEVICE_ID, | |
LOSANT_ACCESS_KEY, | |
LOSANT_ACCESS_SECRET); | |
if(client.isConnected()) { | |
Serial.println("connected!"); | |
client.subscribe(MQTT_TOPIC_COMMAND); | |
} | |
else { | |
Serial.print("."); | |
delay(500); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Used to only send temperature once a second. | |
int lastUpdate = millis(); | |
void loop() { | |
if (!client.isConnected()) { | |
connect(); | |
} | |
// Loop the MQTT client. | |
client.loop(); | |
int now = millis(); | |
// Publish state every second. | |
if(now - lastUpdate > 1000) { | |
lastUpdate = now; | |
// Build the json payload: | |
// { "data" : { "tempF" : val, "tempC" : val }} | |
StaticJsonBuffer<200> jsonBuffer; | |
JsonObject& root = jsonBuffer.createObject(); | |
JsonObject& state = jsonBuffer.createObject(); | |
// TODO: refer to your specific temperature sensor | |
// on how to convert the raw voltage to a temperature. | |
int tempRaw = analogRead(A0); | |
state["tempF"] = tempRaw; | |
state["tempC"] = tempRaw; | |
root["data"] = state; | |
// Get JSON string. | |
char buffer[200]; | |
root.printTo(buffer, sizeof(buffer)); | |
client.publish(MQTT_TOPIC_STATE, buffer); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is greatly appreciated as I'm considering using losant, but was a bit taken back by the learning curve. You might want to tweak your loop() function to check to see if "now" is less than "lastupdated" and set "lastupdated" to 0 (zero) if that's the case (the counter rolled over). Thanks for publishing this!