IOT 온도계 및 습도계 : 아두이노 Arduino ESP8266 WeMOS보드와 DHT 모듈을 사용하여 thingspeak에 업로드 하기

반응형

미관입니다.

아주 아주 오랫만에 아두이노(Arduino)를 가지고 놀았습니다.

뭐 아주 오랫만은 아니지만

 

ESP8266  WeMOS 보드에 DHT 실드를 올려 

정해진 시간에 온도 및 습도를 확인하여

iot 오픈 플랫폼인 ThingSpeak 에 업로드하는 작업을 했습니다.

당연 ThingSpeak 에서 그래프로 확인가능합니다. ^^

 

 

WeMOS 외 DHT 쉴드를 연결한 모습입니다.

아주 작은 크기에 와이파이까지 지원해 주니 여러모로 쓸모가 많은 제품입니다. ^^

 

 

 

WeMOS 와 DHT 쉴드

 

 

 

얘가 WeMOS D1 mini 제품입니다.

정말 크기가 작죠.. ^^

 

 

 

 

 

이 사진이 WeMOS DHT 쉴드 사진입니다.

 

 

 

 

 

먼저 ThingSpeak 의 그래프를 보실게요 ~

 

 

iot 온도계
iot 습도계

 

 

 

이런 식으로 확인이 됩니다.

 

이상 두가지 제품 모두 알리에서 구입을 했습니다.

 

 

(아래 링크는 광고입니다.)

https://www.aliexpress.com/item/32675845174.html?tracelog=rowan&rowan_id1=affedm_1_en_US_2020-12-30&rowan_msg_id=0fb8c8aeab764ec694a2013c6bf8a860&ck=in_edm_other

 

US $18.0 82% OFF|YI Home 1080p Camera 2.4G Wifi Indoor ip Camera AI Human detection Night vision Activity alerts Cameras for hom

Smarter Shopping, Better Living! Aliexpress.com

www.aliexpress.com

 

 

아래는 WeMOS 에 업로드 한 소스코드입니다.

아래에서 API Key는 ThingSpeak에서 받은 API 키를 넣어 주시고

SSID 는 본인의 와이파이 이름,

password 는 와이파이 비밀번호를 넣어 주시면 됩니다.

 

 

 

//
//  ESP8266_DHT12_WiFi_WeMos.ino (2017.8.24) --> WeMos D1 Mini version
//
//  https://playground.arduino.cc/Main/Dht
//  DHT12 Library --> https://github.com/Bobadas/DHT12_library_Arduino
//
//  Absolute Humidity --> http://biomet.ucdavis.edu/conversions/HumCon.pdf
//  Wet-bulb Temperature --> http://journals.ametsoc.org/doi/pdf/10.1175/JAMC-D-11-0143.1
//  Dew-point Temperature --> http://wahiduddin.net/calc/density_algorithms.htm
//

#include <math.h>
#include <DHT12.h>
#include <Wire.h>                             // DHT12 uses I2C (GPIO4 = SDA, GPIO5 = SCL)
DHT12 dht12;                                  // Default Unit = CELSIUS, ID = 0x5c

#include <ESP8266WiFi.h>
WiFiClient client;

String apiKey = "5D... 본인의 API 카를 넣어 주세요 S3A...";           // your ThingSpeak API key
const char* ssid = "E... 본인의 와이파이 PID 를 넣어 주세요 ...F";               // your ssid
const char* password = "n... 여기는 와이파이 비번을 넣어 주세요... ";          // your password
const char* server = "api.thingspeak.com";

const int VccPin = 0;                         // Vcc pin for the Power of DHT12
const int recordPeriod = 600;                 // Recording Period (seconds)
long sumVoltage = 0;

void setup() {
  pinMode(VccPin, OUTPUT);
  digitalWrite(VccPin, HIGH);                 // DHT12 ON

  Wire.begin();

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);
}

void loop() {
  float Temp = dht12.readTemperature();
  float RH = dht12.readHumidity();

  float AH = 2165 * RH * 0.01 * 0.6108 * exp(17.27 * Temp / (Temp + 237.3)) / (Temp + 273.16);
  float WTemp = Temp * atan(0.151977 * pow(RH + 8.313659, 0.5)) + atan(Temp + RH)
                - atan(RH - 1.676331) + 0.003918 * pow(RH, 1.5) * atan(0.023101 * RH) - 4.686035;
  float DewPoint = Temp - ((14.55 + 0.114 * Temp) * (1. - 0.01 * RH)
                           + pow((2.5 + 0.007 * Temp) * (1. - 0.01 * RH), 3) + (15.9 + 0.117 * Temp) * pow((1. - 0.01 * RH), 14));
  float DI = 0.72 * (Temp + WTemp) + 40.6;

  sumVoltage = 0;
  for (int i = 0 ; i < 100 ; i++) sumVoltage += analogRead(A0);
  float batteryVoltage = sumVoltage / 100.0 * 5.4 / 1024 * 1.0536;    // 1.0536 = Correction Factor

  digitalWrite(VccPin, LOW);                       // DHT12 OFF

  if (client.connect(server, 80)) {
    String postStr = apiKey;
    postStr += "&field1=";
    postStr += String(Temp, 1);             // Temperature (℃)
    postStr += "&field2=";
    postStr += String(RH, 1);               // Relative Humidity (%)
    postStr += "&field3=";
    postStr += String(AH, 1);               // Absolute Humidity (g/m3)
    postStr += "&field4=";
    postStr += String(WTemp, 1);            // Wet-bulb Temperature (℃)
    postStr += "&field5=";
    postStr += String(DewPoint, 1);         // Dew-point Temperature (℃)
    postStr += "&field6=";
    postStr += String(DI, 1);               // Discomfort Index
    postStr += "&field7=";
    postStr += String(batteryVoltage, 3);   // Battery Voltage (VDC)
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(postStr.length());
    client.print("\n\n");
    client.print(postStr);
  }
  client.stop();

  ESP.deepSleep((recordPeriod - 5) * 1000000 * 1.05);
}

소스코드 출처는 기억나질 않네요. 

과거 오래전에 어디서 다운 받은적이 있는데....

원작자 분께 감사의 말씀을 드립니다.

 

IOT 와 WeMOS 그리고 ThingSpeak 을 공부하시는 분들께 조금이나마 도움이 되셨기를 바랍니다. ^^

 

감사합니다.

반응형

이 글을 공유하기

댓글

Designed by JB FACTORY