1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #include <SDS011.h> #include <ESP8266WiFi.h> String api = "api 키 입력" ; const char * ssid = "와이파이 이름" ; const char * password = "비밀번호" ; const char * server = "api.thingspeak.com" ; WiFiClient client; float p10, p25; int error; SDS011 my_sds; void setup() { my_sds.begin(5, 4); Serial.begin(115200); delay(1000); Serial.print(ssid); Serial.println("에 접속중"); WiFi.begin(ssid,password); while (WiFi.status() != WL_CONNECTED) { delay(100); Serial.print("."); } Serial.println(""); Serial.println("연결되었습니다."); } void loop() { error = my_sds.read(&p25, &p10); if(client.connect(server,80)){ String postStr = api; postStr +="&field1="; postStr +=String(p25); postStr +="&field2="; postStr +=String(p10); postStr +="\r\n\r\n"; client.print("POST /update HTTP/1.1\r\n"); client.print("Host: api.thingspeak.com\r\n"); client.print("Connection: close\r\n"); client.print("X-THINGSPEAKAPIKEY: "+api+"\r\n"); client.print("Content-Type: application/x-www-form-urlencoded\r\n"); client.print("Content-Length: "); client.print(postStr.length()); client.print("\r\n\r\n"); client.print(postStr); } if(!error) { Serial.println("P2.5: " + String (p25)); Serial.println("P10: " + String (p10)); } delay(100); } | cs |
이번엔 HTTP 통신으로 POST형식으로 Request합니다.
해당 코드를 업로드 한뒤 Thingspeak 차트를 살펴봅니다.
센서를 연결하기 전 0으로 인식되다가 정상적으로 측정을 하고 앞에서 휴지를 흔들었을때
최고점을 찍고 정상화 되어가는 모습을 확인합니다.
loop문 하단에 시리얼 출력 코드도 넣어 주었기에
시리얼 모니터에도 동일한 값이 출력되는 것을 확인할 수 있습니다.
이제 Wemos D1 보드를 사용해 센서값을 읽고 그 값을 인터넷 상에 업로드 하여 확인할 수 있게 되었습니다.
이에 연동하여 환기 시스템을 제작하는 등의 응용을 시도해보기 바랍니다.