What is Ubidots?
Ubidots offers a platform for developers that enables them to easily capture sensor data and turn it into useful information. Use the Ubidots platform to send data to the cloud from any Internet-enabled device. You can then Configure actions and alerts based on your real-time data and unlock the value of your data through visual tools. Ubidots offers a REST API that allows you to read and write data to the resources available: data sources, variables, values, events and insights. The API supports both HTTP and HTTPS and an API Key is required.
After configuring the Ubidots account this is how it will look:
Temperature and Humidity from DHT22 sensor and 4 switchs to control the relays.
So for this project we need a NodeMCU board, a DHT22 or 11 sensor, a 4 relays board and a Ubidots account. To program the board I used Arduino IDE 1.6.7, you can find more detail on NodeMCU board and how to connect the DHT sensor here.
I connected the 4 relays board to the digital pins of NodeMCU, D3, D5, D5 and D7. Then you have to connect the board's VCC pin to the 5V pin (in some NodeMCU board it is written "Vin" and you can use it as +5V. Don't forget to connect the GND pin.
The last relay is programmed to work like a switch button, so it can be used to open a gate or other applications.
In the end it is also possible to program events, like if the temperature is less than 15°C to send an sms alert or an email or both.
In the end everything is working fine, but Ubidots it's not totaly freeware, so check out if it fits to your project and to your finances.
I noticed also quite a long delay, like one minute, from when you push the button on the web browser to the switch on the relay.
The save Ubidots credits the code upload and get the datas every minute and only if there is a new value.
Finnaly here is the code:
#include "DHT.h"
#include "UbidotsMicroESP8266.h"#define DHTPIN 4
#define DHTTYPE DHT22
#define TOKEN "your ubidots token" // Ubidots Token
#define WIFISSID "your WiFi ssid"
#define PASSWORD "your wifi password"
int opengate_pin = 13; // D07 relay 4
int kitchenlight_pin = 12; // D06 relay 3
int gardenlight_pin = 14; // D05 relay 2
int hallwaylight_pin = 0; // D03 relay 1
int interval = 60000;
DHT dht(DHTPIN, DHTTYPE);
float temp_h = 0.0;
float temp_t = 0.0;
Ubidots client(TOKEN);
void setup() {
Serial.begin(115200);
dht.begin();
delay(10);
pinMode(hallwaylight_pin, OUTPUT);
digitalWrite(hallwaylight_pin, HIGH);
pinMode(kitchenlight_pin, OUTPUT);
digitalWrite(kitchenlight_pin, HIGH);
pinMode(gardenlight_pin, OUTPUT);
digitalWrite(gardenlight_pin, HIGH);
pinMode(opengate_pin, OUTPUT);
digitalWrite(opengate_pin, HIGH);
client.wifiConnection(WIFISSID, PASSWORD);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
delay(1000);
// update interval (60 seconds)
if(millis() >= interval)
{
// this 3 following conditions permit to send less data to ubidots in this way we can save credits
if (t != temp_t) client.add("your temperature ubidots variable name", t); //
if (h != temp_h) client.add("your humidity ubidots variable name", h); //
if (t != temp_t || h!= temp_h) client.sendAll(true); // send only new values
temp_t = t;
temp_h = h;
if (client.getValue("relay 1 variable name from ubidots")) digitalWrite(hallwaylight_pin, LOW);
else digitalWrite(hallwaylight_pin, HIGH);
if (client.getValue("relay 2 variable name from ubidots")) digitalWrite(kitchenlight_pin, LOW);
else digitalWrite(kitchenlight_pin, HIGH);
if (client.getValue("relay 3 variable name from ubidots")) digitalWrite(gardenlight_pin, LOW);
else digitalWrite(gardenlight_pin, HIGH);
// this work like a push button, useful to open a gate or a ring belt.
if (client.getValue("relay 4 variable name from ubidots")) {digitalWrite(opengate_pin, LOW);delay(500);
digitalWrite(opengate_pin, HIGH);
}
}
}
Nessun commento:
Posta un commento