IoT Wireless

Serial-To-WiFi(2 UART) using WizFi250 CSI 본문

IoT Platform(Script)

Serial-To-WiFi(2 UART) using WizFi250 CSI

DH0815 2015. 6. 29. 09:14

Serial-To-WiFi using WizFi250 CSI를 조금 변경해서 UART 2개에 대한 Serial-To-WiFi 기능을 하는 C Script를 만들었다.



#include "WizFi250-CSI.h"

void main(void)
{
  struct sockaddr_in stSockAddr;
  int Res, result;
  
  // Join to AP
  if ( result = wifi_join("WizFiDemoAP", "wpa2", "12345678", 0, 0, 0)!=0 )
  {
    printf("wifi_join error : %d", result);
    return;
  }

  ////////////////////////////////////////////////////////////////////////////////////
  // TCP Socket for UART 1
  int SocketFD1 = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (-1 == SocketFD1)
  {
    printf("cannot create socket\r\n");
    return; 
  }

  // Define Server IP, Port
  memset(&stSockAddr, 0, sizeof(stSockAddr));
  stSockAddr.sin_family = AF_INET;
  stSockAddr.sin_addr.s_addr = inet_addr("192.168.3.54");
  stSockAddr.sin_port = htons(6000);  
  
  if (-1 == connect(SocketFD1, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr)))
  {
    printf("connect failed\r\n");
    close(SocketFD1);
    return;
  }
  ////////////////////////////////////////////////////////////////////////////////////
  
  ////////////////////////////////////////////////////////////////////////////////////
  // TCP Socket for UART 2
  int SocketFD2 = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (-1 == SocketFD2)
  {
    printf("cannot create socket\r\n");
    return; 
  }

  // Define Server IP, Port
  memset(&stSockAddr, 0, sizeof(stSockAddr));
  stSockAddr.sin_family = AF_INET;
  stSockAddr.sin_addr.s_addr = inet_addr("192.168.3.54");
  stSockAddr.sin_port = htons(6001);  
  
  if (-1 == connect(SocketFD2, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr)))
  {
    printf("connect failed\r\n");
    close(SocketFD2);
    return;
  }
  ////////////////////////////////////////////////////////////////////////////////////
  
  
  char szData[256];
  int length = 0;
  
  // Set GPIO12 input-pin.
  pinMode(GPIO12, 1);
  
  while(1)
  {
  	////////////////////////////////////////////////////////////////////////////////////////
  	// UART 1
    length = uart_rx(0, szData, 256);
    if ( length>0 )
    {
      pinOut(GPIO12, 0);
      result = send(SocketFD1, szData, length, 0);
      pinOut(GPIO12, 1);
      if ( result<0 )	break;
    }
  
    length = recv(SocketFD1, szData, sizeof(szData), MSG_DONTWAIT);
    if ( length==0 )
    {
      break;
    }
    else if ( length<0 )    
    {
      if ( sock_error==EAGAIN )		continue;
      
      printf("<%d,%d>", length, sock_error); 
      break;
    }
    else
    {
      pinOut(GPIO12, 0);
      uart_tx(0, szData, length);
      pinOut(GPIO12, 1);
    }
  	////////////////////////////////////////////////////////////////////////////////////////

  	////////////////////////////////////////////////////////////////////////////////////////
  	// UART 2
    length = uart_rx(1, szData, 256);
    if ( length>0 )
    {
      pinOut(GPIO12, 0);
      result = send(SocketFD2, szData, length, 0);
      pinOut(GPIO12, 1);
      if ( result<0 )	break;
    }
  
    length = recv(SocketFD2, szData, sizeof(szData), MSG_DONTWAIT);
    if ( length==0 )
    {
      break;
    }
    else if ( length<0 )    
    {
      if ( sock_error==EAGAIN )		continue;
      
      printf("<%d,%d>", length, sock_error); 
      break;
    }
    else
    {
      pinOut(GPIO12, 0);
      uart_tx(1, szData, length);
      pinOut(GPIO12, 1);
    }
  	////////////////////////////////////////////////////////////////////////////////////////

  }
   
  printf("Disconnected (%d) \r\n", length);
  close(SocketFD1);
  
  return;
}