DIY Sensor Course
입문자를 위한 ESP32 센서 만들기
1만원짜리 보드(ESP32)와 온습도 센서(DHT22)로 나만의 스마트팜 센서를 만들어보세요.
복잡한 코딩 없이, '복사'해서 '붙여넣기'하면 끝납니다.
Lecture 01: ESP32 기초와 와이파이 설정
강의 자료 PDF
회로도 및 라이브러리 가이드 (v1.2)
펌웨어 전체 코드 (Copy & Paste)
아래 코드는 DHT22 센서 값을 읽어 FarmSense 서버로 전송하는 전체 코드입니다.SSID와 PASSWORD 부분만 내 농장의 와이파이 정보로 수정하세요.
FarmSense_ESP32_Basic.inocpp
#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
// 1. 와이파이 설정 (이 부분만 수정하세요)
const char* ssid = "MY_FARM_WIFI";
const char* password = "wifi_password_1234";
// 2. 센서 설정
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// 3. 서버 설정
const char* serverUrl = "https://api.farmsense.kr/api/sensor-data/ingest/";
const char* apiKey = "YOUR_DEVICE_TOKEN";
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
if(WiFi.status() == WL_CONNECTED){
HTTPClient http;
http.begin(serverUrl);
http.addHeader("Content-Type", "application/json");
http.addHeader("Authorization", String("Bearer ") + apiKey);
// JSON 데이터 생성
String jsonPayload = String("{\"temperature\":") + t +
String(",\"humidity\":") + h + "}";
int httpResponseCode = http.POST(jsonPayload);
if(httpResponseCode > 0){
String response = http.getString();
Serial.println(httpResponseCode);
Serial.println(response);
}
http.end();
}
// 10분마다 전송
delay(600000);
}