본문 바로가기

IOT(Arduino+Inventor)

ESP-01, DHT11, ThingSpeak [5]

외부에서 온도와 습도를 모니터링 할 수 있도록 외부 서버 를 활용합니다.

측정 주가는  약20초입니다.

서버 사이트 thingspeal.com 계정이 필요합니다.

측정 년월일날짜 시간 분초 홧수 데이터를 나타냅니다.

업로드 후 시리얼 모니터에서 확인 후 아두이노 우노는 제거 합니다.

ESP-01는 센서와 전원만 연결합니다.

소스 파일

#include <ESP8266WiFi.h>
#include <DHT.h>

#define DHTPIN 2        // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);
//hingspeak heade 파일 포함
#include <ThingSpeak.h> 

const char* ssid = "IOT999";   

const char* password = "kbs48752";   
//int keyIndex = 0;            // your network key Index number (needed only for WEP)


WiFiClient  client;
// ThingSpeak 체널 값
unsigned long myChannelNumber = 2068795;

// ThingSpeak 체널에 속하는 저장 키
const char * myWriteAPIKey = "MEL279JBB5MXABAX";

// 임시 가짜 데이터 값

int number = 0;

void setup() {
  Serial.begin(115200);  // Initialize serial

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  // wifi 모드 기능은 오직 클라이언트로만 사용
  WiFi.mode(WIFI_STA); 

// // Initialize ThingSpeak와 클라인언트 접속
  ThingSpeak.begin(client); 
  //와이 파이 연결 상태 
  if(WiFi.status() != WL_CONNECTED){
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);


    while(WiFi.status() != WL_CONNECTED){
      WiFi.begin(ssid, password);  // Connect to WPA/WPA2 network. Change this line if using open or WEP network
      Serial.print(".");
      delay(5000);     
    } 
    Serial.println("\nConnected.");
  }
  
}

void loop() {

 float h = dht.readHumidity();
 float t = dht.readTemperature(); // Read temperature as Celsius (the default)
 
// Check if any reads failed and exit early (to try again).
 if (isnan(h) || isnan(t)) {
 Serial.println("Failed to read from DHT sensor!");
 return;
 }
   
 Serial.print("Humidity % : ");
 Serial.println(h);
 Serial.print("Temperature *C: ");
 Serial.println(t);

 ThingSpeak.setField(3,h );
  ThingSpeak.setField(4, t);

  int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
    if(x == 200){
    Serial.println("Channel update successful.");
  }
  else{
    Serial.println("Problem updating channel. HTTP error code " + String(x));
  }

  delay(20000); 
}

스마트 폰 엡은

목록 ESP8266_thingSpeak를 참고합니다.

'IOT(Arduino+Inventor)' 카테고리의 다른 글

온도 센서 ds18b20  (0) 2023.04.12
esp-01S Relay[5]  (0) 2023.04.12
esp-01 DHT11 Sensor[4]  (0) 2023.04.11
ESP-01 LED Control[3]  (0) 2023.03.25
ESP-01 AT Commands[2]  (0) 2023.03.24