ESP 01 DHT11 and the ThingSpeak Cloud Write Data

Arduino tutorial: This module uses the ESP-01 or the ESP-01S as the master control, and the DHT11 ... Humidity Monitoring Using the ESP-01 & DHT and the ThingSpeak Cloud.

 DHT22 Temperature Humidity Sensor

Code

/*
 
 * DHT Temperature and humidity monitoring using ESP01 and the thingspeak 
 * Description: This examples connects the ESP to wifi, and sends Temperature and humidity to thingspeak IoT platfom over HTTPS GET Request.
 * https://thingspeak.com
 * github: https://github.com/mathworks/thingspeak-arduino
 * DHT11 library (DHT11) found at https://github.com/winlinvip/SimpleDHT
 * More videos : https://www.youtube.com/channel/UCCC8DuqicBtP3A_aC53HYDQ/videos
 */

// includes
#include "ESP8266WiFi.h"
#include "WiFiClientSecure.h"
#include "SimpleDHT.h"

// user config: 
const char* wifi_ssid = "MySSID";             // replace MySSID with your WiFi network name
const char* wifi_password = "MyPassword";         // replace MyPassword with your WiFi password
const char* apiKeyIn = "APIKEYIN";      // replace APIKEYIN with your channel write API Key
const unsigned int writeInterval = 25000; // write interval (in ms)

// thingspeak config.
const char* https_host = "api.thingspeak.com";         // thingspeak host name
const int https_port = 443;                        // https port 
// DHT config.
int pinDHT11 = 2;
SimpleDHT11 dht11(pinDHT11);
// create thingspeak client
WiFiClientSecure client;

void getTem()
{
  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    delay(100);
    return;
  }
  if (!client.connect(https_host, https_port)) {
  delay(200);
  return;
  }
  // Create a URL for the request
  String url = "/update?api_key=";
  url += apiKeyIn;
    url += "&field1=";
  url += ((int)temperature);
  url += "&field2=";
  url += ((int)humidity);
  

  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + https_host + "\r\n" +
               "Connection: close\r\n\r\n");

  client.connected();
}
void setup() {
  WiFi.begin(wifi_ssid, wifi_password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  // Set Client to insecure
  client.setInsecure();
}

void loop() {
         getTem();
  delay(20000);

}

No comments:

Post a Comment