Node-RED 우노빅보드 스마트팜/우노빅보드와 Node-RED 연동

Node-Red 대쉬보드로 UDP 데이터 송신

ZEROWIN.TECH 2020. 5. 28. 20:02
728x90

우노빅보드 + ESP8266 연결

우노빅보드와 ESP8266을 연결합니다.

ESP8266 명령어 수행 함수

command 변수에 명령어를 전달하면 결과값을 프린트합니다.

String esp8266Data(String command, const int timeout, boolean debug)

  {

	if (debug)
    {
		Serial.print("CMD: ");
      Serial.println(command);
    }
		
    String response = "";
    esp8266Serial.print(command);
    long int time = millis();
    while ( (time + timeout) > millis())
      {
        while (esp8266Serial.available())
          {
            char c = esp8266Serial.read();
            response += c;
          }
      }
    if (debug)
    {
      Serial.print(response);
    }

    return response;
  }

 

setup함수안에서 ESP8266 와이파이 설정을 합니다.

esp8266Serial.begin(9600);
  esp8266Data("AT+RST\r\n", 5000, DEBUG); // Reset the ESP8266
  esp8266Data("AT+CWMODE=1\r\n", 5000, DEBUG); //Set station mode Operation
  esp8266Data("AT+CWJAP=\"ZEROWIN\",\"zzzzzzzz\"\r\n", 5000, DEBUG);//Enter your WiFi network's SSID and Password.
  esp8266Data("AT+CIFSR\r\n", 5000, DEBUG);//You will get the IP Address of the ESP8266 from this command. 
  esp8266Data("AT+CIPMUX=0\r\n", 5000, DEBUG);

UDP 통신 함수

서버와 연결을 시도합니다. 서버 정보 ( 아이피 : 192.168.0.3 포트 : 50000 )

void OpenSession()
{
   esp8266Data("AT+CIPSTART=\"UDP\",\"192.168.0.3\",50000\r\n", 50000, DEBUG);
   bConnected = true;
   return; 

}

서버와 연결을 끊습니다.

void CloseSession()
{
   esp8266Serial.println("AT+CIPCLOSE");
   return;
}

 서버로 데이터를 전송합니다.

String readStr, writeStr;

void SendToClient(String s)

{

    writeStr = s;
    //Serial.println("\nAT+CIPSEND=" + String(writeStr.length() + 2));
    //delay(100);
    esp8266Serial.println("AT+CIPSEND=" + String(writeStr.length() + 2));

     // Serial.print(millis()); Serial.print("= ");

    String result = "";

    char ch = 0x00;

    while(ch != 0x00) 
    {  

      if(0 < esp8266Serial.available()) {
         ch = esp8266Serial.read();
         result += ch;
     }

     delay(1);

    };

    Serial.print("result=");
    Serial.println(result);

    delay(10);

    //Serial.println("\n" + writeStr +"\n\r");

    esp8266Serial.println(writeStr);

 

    return;

}

SendToClient 함수를 이용하여 서버로 데이터를 전달합니다.

Node-RED 데이터 수신

포트 50000 을 엽니다. 그리고 텍스트 출력과 연결합니다.

예제 동영상

시리얼모니터로 줄력되는 문장이 Node-RED 대쉬보드에 출력이 되는것을 확인 할 수 있습니다.