아두이노 MKR1000으로 DHT11 센서 데이터를 ThingSpeak에 업로드하기
부품 및 준비물
| 부품 | 수량 |
| DHT11 온습도 센서 (4핀) | 1 |
| 아두이노 MKR1000 | 1 |
프로젝트 개요
이미 ESP8266-12E로 DHT11 센서 데이터를 무선으로 ThingSpeak에 전송하고 그래프로 표시하는 코드를 개발한 상태였습니다. 이를 아두이노 MKR1000에서 작동하도록 약간만 수정하면 됩니다.
1단계: 하드웨어 연결
DHT11의 데이터 핀을 MKR1000의 핀 1(보드 뒷면에 1로 표시)에 연결합니다.
2단계: 라이브러리 설치
다음 라이브러리를 다운로드하여 아두이노 IDE에 설치하세요.
3단계: 보드 매니저에서 MKR1000 지원 추가
- 아두이노 IDE에서 도구 → 보드 → 보드 매니저를 엽니다.
- "Arduino SAMD Boards (32-bits ARM Cortex M0+)"를 검색합니다.
- 버전 1.6.3을 설치하세요. (1.6.4 버전에서는 MKR1000 보드가 설치되지 않는 문제가 있습니다.)
4단계: ThingSpeak 계정 설정 및 스케치 수정
- ThingSpeak.com에서 무료 계정을 생성합니다.
- 채널을 만들고 필드(Field)를 설정합니다: Field 1 = 습도, Field 2 = 섭씨 온도, Field 3 = 화씨 온도, Field 4 = 체감 온도(섭씨), Field 5 = 체감 온도(화씨).
- 스케치의
apiKey, ssid, pass 변수에 본인의 ThingSpeak Write API 키, 와이파이 SSID, 비밀번호를 입력합니다.
5단계: 업로드 및 실행
- 아두이노 IDE에서 도구 → 포트에서 MKR1000이 연결된 COM 포트를 선택합니다.
- 컴파일 후 업로드합니다.
- 시리얼 모니터(9600 보드레이트)를 열면 ThingSpeak로 데이터 전송 로그가 출력됩니다.
소스 코드
아래 코드는 Adafruit DHT 예제, WiFi101 WebClient, ThingSpeak 아두이노 템플릿을 결합하여 MKR1000용으로 수정한 것입니다.
/* This sketch is a combination of ADAFruits DHT sketch, WiFi101 Webclient and The arduino example from ThingSpeak Modified by Stephen Borsay for the MKR1000, feel free to use */
#include <SPI.h>
#include <WiFi101.h>
#include "DHT.h"
#define DHTPIN 1 // 데이터 핀: MKR1000 핀 1 (보드 끝에서 5번째 핀)
#define DHTTYPE DHT11 // DHT 11 사용
//#define DHTTYPE DHT21 // DHT 21
//#define DHTTYPE DHT22 // DHT 22
DHT dht(DHTPIN, DHTTYPE);
String apiKey = "YourApiKeyHere"; // ThingSpeak Write API Key
char ssid[] = "YourSSIDHERE"; // 와이파이 SSID
char pass[] = "YourPasswordHere"; // 와이파이 비밀번호
int keyIndex = 0; // WEP 사용 시 키 인덱스
char server[] = "api.thingspeak.com";
WiFiClient client;
void setup() {
Serial.begin(9600);
while (!Serial) { ; } // 네이티브 USB 포트 대기
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
while (true);
}
// 와이파이 연결 시도
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
WiFi.begin(ssid, pass); // WPA/WPA2 연결
delay(10000);
}
Serial.println("Connected to wifi");
printWifiStatus();
}
void loop() {
delay(2000); // 측정 간격
// float 변환 이슈로 int 사용 (추후 개선 예정)
int h = dht.readHumidity();
int t = dht.readTemperature(); // 섭씨
int f = dht.readTemperature(true); // 화씨
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// 체감 온도 계산
int hif = dht.computeHeatIndex(f, h);
int hic = dht.computeHeatIndex(t, h, false);
// 시리얼 출력
Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t");
Serial.print("Temperature: "); Serial.print(t); Serial.print(" *C ");
Serial.print(f); Serial.print(" *F\t");
Serial.print("Heat index: "); Serial.print(hic); Serial.print(" *C ");
Serial.print(hif); Serial.println(" *F\n");
Serial.println("\nStarting connection to server...");
if (client.connect(server, 80)) {
Serial.println("connected to server");
// ThingSpeak HTTP POST 요청 구성
String tsData = "field1=" + String(h) + "&field2=" + String(t) + "&field3=" + String(f) + "&field4=" + String(hic) + "&field5=" + String(hif);
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(tsData.length());
client.print("\n\n");
client.print(tsData);
client.stop();
delay(1000);
}
}
void printWifiStatus() {
Serial.print("SSID: "); Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: "); Serial.println(ip);
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):"); Serial.print(rssi); Serial.println(" dBm");
}
회로도
DHT 데이터 핀 → MKR1000 핀 1 (보드 끝에서 5번째 핀)
DHT VCC → MKR1000 5V
DHT GND → MKR1000 GND
참고 자료