Skip to content

Instantly share code, notes, and snippets.

@elktros
Last active April 7, 2018 07:16
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 elktros/7725e2a62c06dbfbf998f6c90ed656cb to your computer and use it in GitHub Desktop.
Save elktros/7725e2a62c06dbfbf998f6c90ed656cb to your computer and use it in GitHub Desktop.
Code for Controlling a Relay from anywhere in the World using ESP8266 and aREST Platform.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <aREST.h>
WiFiClient espClient;
PubSubClient client(espClient);
aREST arestVar = aREST(client);
/*Enter an Unique ID to identify the device for arest.io
A Maximum of 6 characters are supported. Must be unique.*/
char* device_id = "re1403"; // Do not use this device_id. Create your own id.
/* Enter SSID and Password of your
WiFi Network*/
const char* ssid = "SSID"; // Name of WiFi Network.
const char* password = "PASSWORD"; // Password of your WiFi Network.
void callback(char* topic, byte* payload, unsigned int length);
void setup(void)
{
Serial.begin(115200);
client.setCallback(callback);
// Give name and ID to device
arestVar.set_id(device_id);
arestVar.set_name("MyESP");
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("*");
}
Serial.println("");
Serial.println("WiFi connected");
char* out_topic = arestVar.get_topic();
}
void loop()
{
arestVar.loop(client);
}
void callback(char* topic, byte* payload, unsigned int length)
{
arestVar.handle_callback(client, topic, payload, length);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment