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

Nessun commento:

Posta un commento