Cerca nel blog

Pagine

sabato 8 ottobre 2016

NodeMCU (ESP8266) posting on Ubidots (Temperature and Humidity) and 4 relays control

We already talked about NodeMCU and ThingSpeak here. Today I want to introduce you to Ubidots and intereact with it, so we are going to post on Ubidots Temperature and Humidity data but also to control 4 relays from the Ubidots account.

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);
                                                   }
}
}

venerdì 7 ottobre 2016

NodeMCU (ESP8266) posting on ThingSpeak (Temperature and Humidity) - WeatherStation



Hello,
here I am again after a long break, I am back to post a few articles on NodeMCU.
This small boards with WiFi got all my attention, I was very curious to test them so I bought two, NodeMCU ver.2 (Amica) and NodeMCU ver.3 LoLin.

The board ver.3 is much bigger than ver. 2:

If you are curious about the comparison of these boards here is a good article.

I decided to use Arduino IDE 1.6.7 to program the boards. Have a look at this link if you need to install the drivers.
I bet you are curious about the pinout...


This project will allow you to post online temperature and humidity from DHT22 sensor on ThingSpeak, it is freeware and I find it very easy to manage and very ueseful for this kind of applications.

According to its developers, "ThingSpeak is an open source Internet of Things (IoT) application and API to store and retrieve data from things using the HTTP protocol over the Internet or via a Local Area Network. ThingSpeak enables the creation of sensor logging applications, location tracking applications, and a social network of things with status updates"

So after the NodeMCU board we also need a DHT22 sensor and an account on ThingSpeak.
The sensor can be connected very easily to the board.


 



What else do we need? DHT library, you can download it here.

And finally here is the code:

#include <ESP8266WiFi.h>
#include "DHT.h"

#define DHTPIN 2  // NodeMCU Pin D4 (watch the pinout image) to connect to data pin from DHT
#define DHTTYPE DHT22

const char* ssid = "your wifi ssid name"; // your WiFi SSID
const char* password = "your password"; //your WiFi password

const int httpPort = 80;

const char* host = "api.thingspeak.com"; // ThingSpeak domain 

String ApiKey = "*********************"; // your API Key for temperature and humidity from ThingSpeak
String path = "/update?key=" + ApiKey + "&field1=";  
String path2 = "&field2=";

DHT dht(DHTPIN, DHTTYPE);

void setup() {
 
  dht.begin();
  delay(10);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

}

void loop() {

  // reading data from DHT sensor
  float h = dht.readHumidity();
  float t = dht.readTemperature();
 
  // we need this delay for the DHT sensor
  delay(1000);

  WiFiClient client;

  // connecting to ThingSpeak
  if (!client.connect(host, httpPort)) return;

  // Sending data do ThinkSpeak
  client.print(String("GET ") + path + t + path2 + h + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: keep-alive\r\n\r\n");       
                     
  client.stop(); // Closing the connection with ThingSpeak
}

Here the final result: