스마트 디바이스 실습 11주차
한세대학교 컴퓨터공학과 22학번 김선영
한세대학교 컴퓨터공학과 22학번 김선영
- Get 노드
- function 노드
- gauge 노드
- msg.payload
4. url창에 HTTP GET 요청을 서버로 보내는 주소인 'http://[IPv4주소]:1880/update-sensor?temperature=24.37' 를 입력하고 디버그가 되는지 확인
- 온도값 24.37이 출력되면 성공!
#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#define DHTPIN 13
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float tem = 0;
float hum = 0;
const char* ssid = "yourWIFIname";
const char* password = "WIFIpassword";
String serverName = "http://ip/https-severname";
unsigned long lastTime = 0;
unsigned long timerDelay = 5000;
void setup() {
delay(500);
Serial.begin(9600);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");
}
void loop() {
tem = dht.readTemperature();
hum = dht.readHumidity();
Serial.print("Temperature :");
Serial.print(tem);
Serial.print("Humidity :");
Serial.print(hum);
}
if ((millis() - lastTime) > timerDelay) {
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
String serverPath = serverName + "?Temperature=" + String(tem) + "&" + "Humidity=" + String(hum);
http.begin(serverPath.c_str()); //부동숫자 문자열로 변경
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
- http response 노드
4. url창에 HTTP GET 요청을 서버로 보내는 주소인 'http://[IPv4주소]:1880/update-sensor?temperature=24.37' 를 입력하고 디버그가 되는지 확인
status code 200과 온도값 24.37이 전송되면 성공!
status code 200이 안뜰때, 브라우저 캐시를 삭제해줌.
- Node-Red를 활용해 HTTP GET 요청을 보내는 방법을 배움.
- HTTP GET는 서버로 데이터를 전송하기 위해 많이 쓰는 방법으로 디바이스에서 모은 데이터를 서버로 보내고, 원격 서버에서 그 데이터를 처리하는 방법을 배움.
- 이런 과정을 통해 Node-Red, HTTP GET 요청, 서버와의 통신에 대한 기본적인 이해와 경험을 쌓을 수 있었음.