Last active
November 7, 2019 20:08
-
-
Save ubi-gists/b6b05165b04fc52d217113bee75f2e52 to your computer and use it in GitHub Desktop.
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
/* | |
* The sample code requires the ESP8266 v.2.0.0 library for proper operation. | |
* (https://github.com/ubidots/ubidots-esp8266/releases/tag/2.0.0) | |
* However, is strongly recommended to migrate the code to the latest library version. | |
* (https://github.com/ubidots/ubidots-esp8266) | |
*/ | |
#include "UbidotsMicroESP8266.h" | |
#include "DHT.h" | |
#define DHTPIN 14 // what digital pin we're connected to | |
#define DHTTYPE DHT11 // DHT 11 | |
DHT dht(DHTPIN, DHTTYPE); | |
#define ID "My_humedity_id" // Put here your Ubidots variable ID | |
#define ID2 "My_Temperature_id" // Put here your Ubidots variable ID | |
#define ID3 "My_heat_id" // Put here your Ubidots variable ID | |
#define ID4 "My_relay_id" // Put here your Ubidots variable ID | |
#define TOKEN "MY_TOKEN" // Put here your Ubidots TOKEN | |
#define WIFISSID "MY_SSID" | |
#define PASSWORD "MY_WIFI_PASSWORD" | |
#define RELAY 12 | |
#define LED 13 | |
Ubidots client(TOKEN); | |
void setup(){ | |
Serial.begin(115200); | |
dht.begin(); | |
delay(10); | |
pinMode(RELAY, OUTPUT); | |
pinMode(LED, OUTPUT); | |
digitalWrite(LED, HIGH); | |
client.wifiConnection(WIFISSID, PASSWORD); | |
} | |
void loop(){ | |
// Wait a few seconds between measurements. | |
delay(2000); | |
// Reading temperature or humidity takes about 250 milliseconds! | |
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) | |
float h = dht.readHumidity(); | |
// Read temperature as Celsius (the default) | |
float t = dht.readTemperature(); | |
// Check if any reads failed and exit early (to try again). | |
if (isnan(h) || isnan(t)) { | |
Serial.println("Failed to read from DHT sensor!"); | |
return; | |
} | |
// Compute heat index in Celsius (isFahreheit = false) | |
float hic = dht.computeHeatIndex(t, h, false); | |
client.add(ID, h); | |
client.add(ID2, t); | |
client.add(ID3, hic); | |
client.sendAll(); | |
float setRelay = client.getValue(ID4); | |
if (setRelay == 1.00) { | |
digitalWrite(RELAY, HIGH); //On relay | |
digitalWrite(LED, LOW); //On led | |
} | |
if (setRelay == 0.00) { | |
digitalWrite(RELAY, LOW); //Off relay | |
digitalWrite(LED, HIGH); //Off led | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment