Skip to content

Instantly share code, notes, and snippets.

@ubi-gists
Last active June 20, 2019 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ubi-gists/b996294057c76dc357ff0c1fd11c5622 to your computer and use it in GitHub Desktop.
Save ubi-gists/b996294057c76dc357ff0c1fd11c5622 to your computer and use it in GitHub Desktop.
/****************************************
* Include Libraries
****************************************/
#include "UbidotsESPMQTT.h"
#include <Servo.h>
/****************************************
* Define Constants
****************************************/
#define TOKEN "assign_your_ubidots_token" // Your Ubidots TOKEN
#define WIFINAME "assign_your_wifi_ssid" //Your SSID
#define WIFIPASS "assign_your_wifi_pass" // Your Wifi Pass
Servo myservo;
int sensorPin = A0;
int ledPin = D5;
int relayPin = D3;
int state;
int lightState;
Ubidots client(TOKEN);
/****************************************
* 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(relayPin, HIGH);
}
else if ((char)payload[0]=='0'){
digitalWrite(relayPin, LOW);
}
}
/****************************************
* Main Functions
****************************************/
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
client.wifiConnection(WIFINAME, WIFIPASS);
client.begin(callback);
myservo.attach(D8);
pinMode(ledPin, OUTPUT);
pinMode(relayPin, OUTPUT);
client.ubidotsSubscribe("manual", "extra_light"); //Insert the dataSource and Variable's Labels
}
void loop() {
// put your main code here, to run repeatedly:
if(!client.connected()){
client.reconnect();
client.ubidotsSubscribe("manual", "extra_light"); //Insert the dataSource and Variable's Labels
}
state = analogRead(sensorPin);
lightState = digitalRead(ledPin);
if (state <= 50) {
digitalWrite(ledPin, HIGH);
client.add("light", lightState);
myservo.write(180);
client.add("blind", 0);
client.ubidotsPublish("Control");
Serial.println("Night - Light ON, Blinds down.");
} else {
digitalWrite(ledPin, LOW);
client.add("light", lightState);
myservo.write(0);
client.add("blind", 1);
client.ubidotsPublish("Control");
Serial.println("Day - Light OFF, Blinds up.");
}
client.add("luminosity", state); //Insert your variable Labels and the value to be sent
client.ubidotsPublish("Control");
client.loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment