스마트팜/온습도보드 RS485

Receive Temperature , Humidity From Farm1st Temperature, Humidity Board by RS485, RS232 communication

ZEROWIN.TECH 2020. 11. 17. 18:05
728x90

Farm1st 온습도 보드에서 아두이노로 건구 온도, 습구 온도, 외기 온도, 습도를 전송합니다.

 

 

아두이노 RS232 과 RS 485 모듈을 통하여 데이터를 수신합니다.

 

시리얼모니터를 통하여 온도/습도 데이터를 표시합니다.

 

Farm1st 온습도 송신보드와 연결된 아두이노 보드 소스코드입니다. 

RS485 수신모듈을 부착하여 데이터 수신합니다.

#include <SoftwareSerial.h>

SoftwareSerial rx485(2,3); 

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  rx485.begin(9600);
  
}

char buffer[128] = { 0x00, };

void getline(char * buffer)
{
  uint8_t idx = 0;
  char c;
  do
  {
    while (rx485.available() == 0) ; // wait for a char this causes the blocking
    c = rx485.read();
    buffer[idx++] = c;
  }
  while (c != '\n' && c != '\r');
  buffer[idx] = 0;
}

float data[5];

void loop() {
  // put your main code here, to run repeatedly:
  #if 0
  if(0 < rx485.available())
  {
    char ch = rx485.read();
    Serial.print(ch);
  }
  #else

  getline(buffer);
  Serial.print(buffer);

 /* get the first token */
  char separator[] = ",";
  char *token;

  token = strtok(buffer, separator);
   
  // Find any more?
  int pos = 0;
  
  while(token != NULL)
  {
     // Serial.println(token);   
     data[pos] = atof(token);
     // Serial.println(data[pos]);   
     pos += 1;
     
     token = strtok(NULL, separator);
  }

  if(5 <= pos) {
    int temp = (((int)data[0] + (int)data[1] + (int)data[2] + (int)data[3]) );
    if((int)temp == (int)data[4])
    {
      Serial.print(data[0]); Serial.print(',');
      Serial.print(data[1]); Serial.print(',');
      Serial.print(data[2]); Serial.print(',');
      Serial.print(data[3]); Serial.print(' ');
      Serial.println("OK!");
    }
    else {
      Serial.print(temp);
      Serial.print(',');
      Serial.print(data[4]); Serial.println(" Failed!");  
    }
  }
  #endif

  
}

 

References

smartstore.naver.com/ic11401/products/581038959?NaPm=ct%3Dkhlias2k%7Cci%3Dcheckout%7Ctr%3Dppc%7Ctrx%3D%7Chk%3D36ce1359af3af6288ca86451250b106b85cb5aef