0
votes

My setup:

Android device =(WiFi)=> Linksys router (WRT54G) =(serial)=> Arduino

Android device : using sockets IO - connects to router
Linksys router : Linux sockets IO - accepts a client
Arduino : reads data serially from router. I have soldered a serial port on the router.

Serial buad rate is 9600 so things are in sync.

The issue:
I can see the server printing data properly to the console but I see strange characters in the Arduino console. Here is what I see:

From Android device: up

Router console:

<Received : up

Arduino console:

received: BELBELBELBELBELBELBELBELBELBELBELBELBELBELBEL up 
received: 

I have no idea where this BEL character is coming from?

My experimentation:
If I do this from router console window:

$> echo "hello" > /dev/tts/1

I get proper data on Arduino side i.e. I get this:

received: hello
received:

My code:
Router code

int fd;
char *portname = "/dev/tts/1";

fd = open(portname, O_WRONLY);

if (fd < 0)
{
    printf("Error : cannot open port %s\n", portname);
    return -1;
}
...
    connfd = accept(listenfd, (struct sockaddr*)&serv_addr, &len);
    printf("accept OK!\n");
    printf("accepted connection from %s\n", inet_ntoa(serv_addr.sin_addr));

    char rcvbuf[MAX_BUFF_SIZE] = { '\0' };

    while(1)
    {
        int inData = recv(connfd, rcvbuf, sizeof(rcvbuf), 0);


        if (inData <= 0)
        {
         printf("Client closed!\n");
         break;
        }

        int result = write(fd, rcvbuf, strlen(rcvbuf));

        printf("<Received : %s\n", rcvbuf);

        // reset memory otherwise we will have data
        // from previous data
        bzero((char *)rcvbuff, sizeof(rcvbuf));
     }

...

Arduino Code:

// the loop routine runs over and over again forever:
void loop() {

        while (Serial.available() > 0)
        {
          char inChar = (char)Serial.read();

          if (inChar == '\n' || inChar == '\r' || inChar == '\n\r' || inChar == '\r\n')
          {
            DATA_READ = true;
            break;
          }

          inputString += inChar;          
        }        
        // send data only when you receive data:
        if (DATA_READ) 
        {
          if (inputString[0] != ' ' || inputString[0] != '\n')
          {
            Serial.print("received: ");
            Serial.println(inputString + " " + inputString[0]);

            process_message(inputString[0]);

            // prepare for next round
            DATA_READ = false;

            Serial.flush();
            inputString = "";
          }
        }        
}

Any *pointers will be appreciated :)

EDIT
added baud rate for more info: Router

root@OpenWrt:/usr/bin# stty -F /dev/tts/1 -a
speed 9600 baud; rows 0; columns 0; line = 0; intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ; eol2 = ; swtch = ; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;

Arduino:

  Serial.begin(9600);

  delay(50);
1
Certain both sides are using the same Baud? BEL or '\7' smells like mis-matched Baud.chux - Reinstate Monica
inputString += inChar; does not look like C to me. Maybe you are using C++ ?wildplasser

1 Answers

0
votes

Are you sure there is a linefeed in the data you write from the router to the arduino, when you send it with the android app? I am not sure if any of these are the cause of your bug, but I recommend:

  1. Add the '\n' in android (or in the server).
  2. Make sure the android app (java?) is sending ascii characters, if tyou expect ascii characters in the arduino.
  3. Use int inData as length when you write in the server. Check that its length makes sense.
  4. Check int result for errors and compare against inData