Cerca nel blog

Pagine

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:

Nessun commento:

Posta un commento