Find file History
Latest commit 49c6074 Sep 15, 2016 @mpous mpous committed on GitHub Update Photon_Fortune_Cookie.ino
Permalink
..
Failed to load latest commit information.
Fortune_Cookie Update Photon_Fortune_Cookie.ino Sep 15, 2016
Photon_Code.ino merged Electron & Photon Jul 26, 2016
README.md

README.md

Photon Particle MQTT

Photon particle example to connect to http://thethings.iO. By using a button connected to the digital port D1, the particle sends a random value between 0 and 9 once the button is pressed. Here's the fritzing diagram of the connections: alt tag

Installation

First navigate to particle build and create a new project. alt tag

Clone this repository and copy all the code from the .ino file to the Particle IDE.

Code Explanation

Include de header for the MQTT library.

#include "MQTT/MQTT.h"

Declare and implement the callback in order to receive messages from thethings.io.

void callback(char* topic, byte* payload, unsigned int length) {
    // handle message arrived
    String text = "";
    for (int i = 0; i < length; i++)
        text.concat((char)payload[i]);
    Serial.println(text);
}

Instantiate the mqtt client

MQTT client("mqtt.thethings.io", 1883, callback);

Change TOKEN with your token. Change the WIFI_SSID with your SSID. Change the WIFI_PASSWORD with your wifi password.

int a;
String TOKEN = "YOUR TOKEN";
String TOPIC = "v2/things/"+TOKEN;
String WIFI_SSID = "yourwifiSSID";
String WIFI_PASSWORD = "yourpassword";

In the setup function, D1 pin is set to INPUT. Then the particle is set to connect to your wifi and to the thethings.io mqtt server.

void setup() {
    pinMode(D1,INPUT_PULLDOWN);
    digitalWrite(D1, LOW);
    WiFi.on();
    WiFi.setCredentials(WIFI_SSID, WIFI_PASSWORD); 
    while(!WiFi.ready()) {
        Serial.print(".");
    }
    Serial.println("");
    Serial.print("Connected ssid");
    while (!client.connect("photonclient")) {
        Serial.println("Could not subscribe, retrying...");
    }

Later, it subscribes to the same topic were it publishes the messages, in order to receive messages from thethings.io.

    client.subscribe(TOPIC);
}

In loop function, if the button is pressed, a random value between 0 and 9 is generated. A message is created with a resource key named randValue and with "a" value. Then the message is published to thethings.io. Then it assures that the mqtt client is still connected.

void loop() {
    if (digitalRead(D1) == HIGH) {
        a = random(0,10);
        String message = "{\"values\":[{\"key\":\"randValue\",\"value\":"+String(a, DEC)+"}]}";
        client.publish(TOPIC, message);
        delay(200);         // ADDED A LITTLE BIT DELAY TO REDUCE BOUNCES
    }
    if (client.isConnected()) {
        client.loop();
    }
}