11. Node Red HTTP GET - sunbang123/Smart_device GitHub Wiki
- Gauge
- HTTP GET ์์ฒญ์ ์๋ฒ๋ก ๋ณด๋ด๊ธฐ
-
cmd๋ก Node-Red ์คํ
-
cmd์ฐฝ์์ ipconfig๋ฅผ ์ ๋ ฅํด์ IPv4์ฃผ์๋ฅผ ํ์ธํจ.

- ThingSpeak์์ ํ๋ก์ฐ๋ฅผ ๊ตฌ์ถํจ.
๋ ธ๋ ๊ตฌ์ฑ

- Get ๋ ธ๋

- function ๋ ธ๋

- gauge ๋ ธ๋

msg.payload๋ Node-RED์์ ์ฌ์ฉ๋๋ ๋ฉ์์ง ๊ฐ์ฒด์ ์์ฑ. Node-RED์์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๊ฑฐ๋ ์ฒ๋ฆฌํ ๋ msg๋ผ๋ ๋ฉ์์ง ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํจ. ์ด ๋ฉ์์ง ๊ฐ์ฒด๋ ์ฌ๋ฌ ์์ฑ์ ํฌํจํ ์ ์์ผ๋ฉฐ, msg.payload๋ ๊ทธ ์ค ํ๋. msg.payload๋ ์ผ๋ฐ์ ์ผ๋ก ๋ฉ์์ง์ ๋ํ ์ฃผ์ ๋ฐ์ดํฐ๋ฅผ ํฌํจํ๋ ์์ฑ. ์๋ฅผ ๋ค์ด, ์ผ์์์ ์์งํ ๊ฐ์ ์ ์กํ๊ฑฐ๋, ์น ์๋น์ค์์ ์์ ํ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๊ฑฐ๋, ๋ค๋ฅธ ๋ ธ๋๋ก ์ ๋ฌํ ๊ฐ์ ์ค์ ํ๋ ๋ฑ ๋ค์ํ ์ฉ๋๋ก ์ฌ์ฉ๋ ์ ์์.
- 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 GET์ Hypertext Transfer Protocol (HTTP)๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฒ์์ ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ๋ ๋ฉ์๋.
- HTTP GET ์์ฒญ์ ์๋ฒ์๊ฒ ํน์ ๋ฆฌ์์ค(์น ํ์ด์ง, ์ด๋ฏธ์ง, API ์๋ํฌ์ธํธ ๋ฑ)๋ฅผ ๊ฐ์ ธ์์ ์๋ต์ ๋ฐ๋ ๋ฐ ์ฌ์ฉ.
- ์ผ๋ฐ์ ์ผ๋ก HTTP GET ์์ฒญ์ URL(Uniform Resource Locator)์ ํตํด ์ํ.
- GET ์์ฒญ์ ์ฃผ๋ก ์ฟผ๋ฆฌ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ์์ฒญ ๋งค๊ฐ๋ณ์๋ฅผ ์๋ฒ๋ก ์ ๋ฌ. ์๋ฒ๋ GET ์์ฒญ์ ๋ฐ์ผ๋ฉด ์์ฒญ๋ ๋ฆฌ์์ค๋ฅผ ์ฐพ์์ ์๋ต์ ๋ฐํ.
-
cmd๋ก Node-Red ์คํ
-
cmd์ฐฝ์์ ipconfig๋ฅผ ์ ๋ ฅํด์ IPv4์ฃผ์๋ฅผ ํ์ธํจ.
-
ThingSpeak์์ ํ๋ก์ฐ๋ฅผ ๊ตฌ์ถํจ.
๋ ธ๋ ๊ตฌ์ฑ

- http response ๋ ธ๋

- 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 ์์ฒญ, ์๋ฒ์์ ํต์ ์ ๋ํ ๊ธฐ๋ณธ์ ์ธ ์ดํด์ ๊ฒฝํ์ ์์ ์ ์์์.