Cerca nel blog

Pagine

giovedì 17 novembre 2016

NodeMCU (ESP8266), Temperature and Humidity on Android widjet

Hello,
here I am again talking about NodeMcu and ThingSpeak, but this time with some improvement.
The idea is to build a portable device that I can plug everywhere and read the temperature and humidity data from my Android cell phone. 

 



The system has to be easily configurable,  portable and compact so I choose to use a D1 mini board.
It has to be friendly when I want to change the WiFi settings, because I don't want upload the software in the board every time I change the WiFi, in this way I can move it easily in different places.

!!! READ WITH ATTENTION !!!
DANGER
You will be working with a 220V circuit. I TAKE NO RESPONSIBILITY FOR YOU ELECTROCUTING YOURSELF. If you don't have experience with High Voltage/High Current you may not want to do this.
NEVER WORK ON A HOT (PLUGED IN) CIRCUIT! 
I take no responsibility for you or any property that may be damaged.  
You are responsible for wiring things properly! 
If you do not know how to work safely, and how to wire things in reference to a schematic do not attempt this. 


Now lets see what we need:
  • 1 cheap USB charger.
  • 1 ESP8266 D1 mini board.
  • 1 DHT22 sensor
  • 1 kohm resistor (brown, black, red, gold)
  • 10 kohm resistor (brown, black, orange, gold)
  • 1 board switch button
  • an account on ThingSpeak.com
  • Arduino IDE

First at all let's to have a look to the D1 mini board, and to the pin-out, it's compatible with Arduino, we can power the board by the 5V pin. To have more info on this board have a look here.
 


PinFunction ESP8266 Arduino Arduino IDE
TX I/O digitale, TX (serial) GPIO_1, TXD D1 1
RX I/O digitale, RX (serial) GPIO_3, RXD D3 3
A0 Input analogico ADC A0 A0
D0 I/O digital GPIO_16 D16 16
D1 I/O digital, SCL (I2C) GPIO_5 D5 5
D2 I/O digital, SDA (I2C) GPIO_4 D4 4
D3 I/O digital GPIO_0 D0 0
D4 I/O digital GPIO_2 D2 2
D5 I/O digital, SCK (SPI) GPIO_14 D14 14
D6 I/O digital, MISO (SPI) GPIO_12 D12 12
D7 I/O digital, MOSI (SPI) GPIO_13 D13 13
D8 I/O digital, SS (SPI) GPIO_15 D15 15
G Massa GND GND
5V 5V
5V
3V3 3.3V VCC 3.3V
RST Reset RST RESET



In the following photos you can see the charger that I used for this project. Opening the charger we can see the board.

On the back I soldered two cables to get the +5V and the GND from the USB connector to power the D1 board.


Now it's the time to connect the cables to the board:
A red cable to the +5V pin;
A black cable to the GND pin.
A blue cable to the pin D2 (for the DHT22 sensor);
A brown cable to the pin D1 for the WiFi reset switch.

We also need solder two resistors:
10 kohm (brown, black, orange, gold) resistor between the +5v pin and the D2 pin;
1 kohm (brown, black, red, gold) resistor between the +5V pin to the D1 pin.

 1 kohm (brown, black, red, gold) resistor
 10 kohm (brown, black, orange, gold)




You have to solder the cable between the sensor and the board, pin 1 Vcc to 3.3V board pin, pin 2 to D2 board pin, pin 4 to GND board pin.

In the following 2 pictures you can see the mini switch that I used and the pin-out.


Now it's time to make the necessaries holes in the charger box, one circular big hole for the DHT sensor, 8mm diameter hole, and a 3 mm for the switch, plus a rectangular one for the USB board plug.
The place where to make the hole depend from your charger and from the free space you have inside. In the following pictures you can see how I assembled all the parts and where I made the holes.










To configure the Arduino IDE to program the D1 board read this article.
You also need to create an account on ThingSpeak.

ThingSpeak, it is freeware and I find it very easy to manage and very useful 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"

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

For an easy Wifi configuration I used the Wifi Manager library. This library allow you to change the WiFi mode of the board by pressing the push button when you plug the system to the main power.
Let's see how it works:
  • When your board starts up, it sets it up in Station mode and tries to connect to a previously saved Access Point.
  • If this is unsuccessful (or no previous network saved) it moves the ESP into Access Point mode and spins up a DNS and WebServer (default ip 192.168.4.1).
  • Using any Wifi enabled device with a browser (computer, phone, tablet) connect to the newly created Access Point.
  • Because of the Captive Portal and the DNS server you will either get a 'Join to network' type of popup or get any domain you try to access redirected to the configuration portal
  • Choose one of the access points scanned, enter password, click save
  • The board will try to connect. If successful, it relinquishes control back to your app. If not, reconnect to AP and reconfigure. 



To visualize the Temperature and Humidity data on an Android device (phone, tablet) we have to install IoT ThingSpeak Monitor Widget.

You can build more of this devices, even with more sensors, like gas sensors, light, and so on, to have a full monitoring of the house or of the environment where you plug the device.

Here the code:

#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager

// Static IP for confinguration 192.168.4.1

#include "DHT.h"

#define DHTPIN 4 // D2 for DHT sendor
#define DHTTYPE DHT22

#define TRIGGER_PIN 5 // D1 this pin will trigger the configuraton portal when set to LOW: 192.168.4.1

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

String ApiKey = "write here you API key"; // for temperature and humidity
String path = "/update?key=" + ApiKey + "&field1=";  
String path2 = "&field2=";

const int httpPort = 80;

long interval = 60000;
long previousMillis = 0;       


DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  Serial.println("\n Starting");

  pinMode(TRIGGER_PIN, INPUT);

  dht.begin();

}

void loop() {
 
  // is configuration portal requested?
  if ( digitalRead(TRIGGER_PIN) == LOW ) {
    //WiFiManager
    //Local intialization. Once its business is done, there is no need to keep it around
    WiFiManager wifiManager;

    //reset settings - for testing
    //wifiManager.resetSettings();

    //sets timeout until configuration portal gets turned off
    //useful to make it all retry or go to sleep
    //in seconds
    //wifiManager.setTimeout(120);
   
    if (!wifiManager.startConfigPortal("OnDemandAP")) {
      Serial.println("failed to connect and hit timeout");
      delay(3000);
      //reset and try again, or maybe put it to deep sleep
      ESP.reset();
      delay(5000);
    }

    //if you get here you have connected to the WiFi
    Serial.println("connected...yeey :)");
  }
 
  unsigned long currentMillis = millis();
 
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  delay(1000);
  // update interval (60 seconds)
  if(currentMillis - previousMillis > interval)
  {
  previousMillis = currentMillis;
  WiFiClient client;
 
  if (!client.connect(host, httpPort)) return; // client mode connecting to ThingSpeak

  // 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
  Serial.println("Sent data to ThingSpeak");
  }
}

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:

lunedì 12 ottobre 2015

Easy games, part 2 - Hot Game

Here again writing on Stica-Lab on the "Easy Games" section...,

This second session is dedicated to build a game that I called "hot game"; I want to build a board with few led and two temperature sensors. 
You can play in two, touching the sensors and Arduino will show you who is the hottest one :) by lighting the leds, green leds mean that there is the same temperature on both sensors.
The buzzer make the game a little bit more nice...adding the sound :)


What we need:
1) Arduino Uno board;
2) breadboard;
3) 8 led (4 reds, 2 yellows, 2 greens)
4) 8 resistors 220 ohm
5) 2 LM35 (temperature sensors)
6) buzzer
7) few cables
8) code: you can download it from here



wires connection:


Here the photo (all the necessary components are placed and connected on the breadboard):




See you to the next post :)


sabato 7 febbraio 2015

Easy games, part 1 - Musical Glove

I am back again writing on Stica-Lab, I just opened a small session called "Easy Games", to show you how easy it is to build funny stuff in few hours of work.

This first session is dedicated to a Musical Glove; I want to build a special glove that while touching  a metal board with the fingers I can hear from a speaker a different sound tone for each finger, and also for combinations of two fingers.

What we need:

1) Arduino Uno board;
2) Aluminium (or every other metal) board, size around 20x10 cm;
3) 8 Ohm Speaker (like the one inside the old desktop pc);
4) Gloves for electrician;
5) Copper tape;
5) Some cable and around 20 mini cable ties.




In the next image you can see how I connected the wires


The metal board is connected to Arduino GND (black cable), the fingers 1-2-3-4-5 are connected to Arduino pins 3-4-5-6-7 (cables yellow, green, grey, white, orange), the speaker is connected to Arduino GND and to pin 10 (red cable). 


How it works:
It plays a different note for each finger and for the combination of two fingers.