스마트 디바이스 실습 11주차

Node Red HTTP GET

한세대학교 컴퓨터공학과 22학번 김선영


실습 보고서

실습 목차
  1. Gauge
  2. HTTP GET 요청을 서버로 보내기

실습1 : Gauge

실습 과정

  1. cmd로 Node-Red 실행
  2. cmd창에서 ipconfig를 입력해서 IPv4주소를 확인함.
  3. Responsive image

  4. ThingSpeak에서 플로우를 구축함.
Responsive image

- Get 노드

Responsive image

- function 노드

Responsive image

- gauge 노드

Responsive image

- msg.payload

  1. Node-RED에서 사용되는 메시지 객체의 속성.
  2. Node-RED에서 데이터를 전달하거나 처리할 때 msg라는 메시지 객체를 사용함.
  3. 이 메시지 객체는 여러 속성을 포함할 수 있으며, msg.payload는 그 중 하나.
  4. msg.payload는 일반적으로 메시지에 대한 주요 데이터를 포함하는 속성. 예를 들어, 센서에서 수집한 값을 전송하거나, 웹 서비스에서 수신한 데이터를 처리하거나, 다른 노드로 전달할 값을 설정하는 등 다양한 용도로 사용될 수 있음.

4. url창에 HTTP GET 요청을 서버로 보내는 주소인 'http://[IPv4주소]:1880/update-sensor?temperature=24.37' 를 입력하고 디버그가 되는지 확인

Responsive image Responsive image

- 온도값 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();
        }
    }

실습2 : HTTP GET 요청을 서버로 보내기

Responsive image
HTTP GET
  1. HTTP GET은 Hypertext Transfer Protocol (HTTP)를 사용하여 서버에서 데이터를 요청하는 메서드.
  2. HTTP GET 요청은 서버에게 특정 리소스(웹 페이지, 이미지, API 엔드포인트 등)를 가져와서 응답을 받는 데 사용.
  3. 일반적으로 HTTP GET 요청은 URL(Uniform Resource Locator)을 통해 수행.
  4. GET 요청은 주로 쿼리 매개변수를 사용하여 요청 매개변수를 서버로 전달.
  5. 서버는 GET 요청을 받으면 요청된 리소스를 찾아서 응답을 반환.
실습 과정

  1. cmd로 Node-Red 실행
  2. cmd창에서 ipconfig를 입력해서 IPv4주소를 확인함.
  3. ThingSpeak에서 플로우를 구축함.
Responsive image

- http response 노드

Responsive image

4. url창에 HTTP GET 요청을 서버로 보내는 주소인 'http://[IPv4주소]:1880/update-sensor?temperature=24.37' 를 입력하고 디버그가 되는지 확인

Responsive image

status code 200과 온도값 24.37이 전송되면 성공!

Responsive image

status code 200이 안뜰때, 브라우저 캐시를 삭제해줌.


실습을 통해 느낀점

- Node-Red를 활용해 HTTP GET 요청을 보내는 방법을 배움.

- HTTP GET는 서버로 데이터를 전송하기 위해 많이 쓰는 방법으로 디바이스에서 모은 데이터를 서버로 보내고, 원격 서버에서 그 데이터를 처리하는 방법을 배움.

- 이런 과정을 통해 Node-Red, HTTP GET 요청, 서버와의 통신에 대한 기본적인 이해와 경험을 쌓을 수 있었음.


Previous   /   Next