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

Node-RED 와 Arduino-ESP8266 TCP 통신

ZEROWIN.TECH 2020. 6. 20. 12:11
728x90

아두이노에 ESP8266을 연결하여 와이파이 통신을 합니다.

Node-RED 와 TCP 소켓을 열어 데이터를 송수신합니다.

Node-RED 에서 팬모터 / 릴레이제어 신호를 보내고,

아두이노 보드에서 스마트팜 데이터를 Node-RED로 송신합니다.

Node-RED 프로그래밍

 

Inject 노드에 텍스트와 전달 시간 간격을 설정합니다.

 

다음칸이동 문자열 추가를 하려고 버퍼로 설정하였습니다.

 이렇게 설정하면 아두이노 시리얼 모니터로 확인시 데이터 출력후 다음 라인으로 이동합니다.

팬모터 버튼구현

버튼을 누르면 문자열 명령어를 다음 노드로 전송합니다.

tcp request 노드

tcp 데이터 전송후 아두이노로부터 응답을 받습니다.

아두이노로 명령어를 전달하고

아두이노로 부터 스마트팜센서 데이터를 응답 받습니다.

Arudino 프로그래밍

Tcp 서버 (400 포트 이용)를 생성하여 Node-RED로 부터 명령어를 수신하고 스마트팜 데이터를 Node-RED로 응답합니다.

#include "WiFiEsp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(7, 6); // RX, TX
#endif

char ssid[] = "ZEROWIN";         // your network SSID (name)
char pass[] = "zzzzzzzz";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

WiFiEspServer server(400);

void setup()
{
  // initialize serial for debugging
  Serial.begin(115200);
  // initialize serial for ESP module
  Serial1.begin(9600);
  // initialize ESP module
  WiFi.init(&Serial1);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  Serial.println("You're connected to the network");
  
  printWifiStatus();

  server.begin();
}

int timeout = 0; 
char temp[32] = { 0x00, };

void loop()
{

  timeout += 1;
  
	  WiFiEspClient c = server.available();
	  if(c) {
	  	
	    Serial.println("RECV: ");

		{
		  boolean bDataRead = false;
	      while (0 < c.available()) {
	        char ch = c.read();
	        Serial.write(ch);
			bDataRead = true;
	        
	      }
		  if(bDataRead == true) {
		  	
		  	Serial.println();

			sprintf(temp, "timeout %d", timeout);
			c.print(temp);

		  }
		  
	    }
				
	    delay(10);

	    // close the connection:
	    c.stop();
	    // Serial.println("Client disconnected");
	  }

  delay(10);
}




void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

데모동영상