Skip to content

Instantly share code, notes, and snippets.

@ubi-gists
Last active December 5, 2016 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ubi-gists/a4024589f5aebaf727128262f983d6f7 to your computer and use it in GitHub Desktop.
Save ubi-gists/a4024589f5aebaf727128262f983d6f7 to your computer and use it in GitHub Desktop.
/****************************************
* Include Libraries
****************************************/
#include "UbidotsESPMQTT.h"
/****************************************
* Define Constants
****************************************/
#define TOKEN "..." // Your Ubidots TOKEN
#define WIFINAME "..." //Your SSID
#define WIFIPASS "..." // Your Wifi Pass
#define MQTTCLIENTNAME "..." // Your MQTT Client Name, it must be unique so we recommend to choose a random ASCCI name
#define RELAY 12
#define LED 13
Ubidots client(TOKEN, MQTTCLIENTNAME);
/****************************************
* Auxiliar Functions
****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
if ((char)payload[0] == '1') {
digitalWrite(RELAY, HIGH); //On relay
digitalWrite(LED, LOW); //On led
}
if ((char)payload[0] == '0') {
digitalWrite(RELAY, LOW); //Off relay
digitalWrite(LED, HIGH); //Off led
}
}
/****************************************
* Main Functions
****************************************/
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
client.wifiConnection(WIFINAME, WIFIPASS);
pinMode(RELAY, OUTPUT);
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
client.begin(callback);
client.ubidotsSubscribe("relay","sonoff"); //Insert the dataSource and Variable's Labels
}
void loop() {
// put your main code here, to run repeatedly:
if(!client.connected()){
client.reconnect();
Serial.println(RELAY);
client.ubidotsSubscribe("relay","sonoff"); //Insert the dataSource and Variable's Labels
}
client.loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment