I'm sending some UDP packets to an ESP8266, the Wifi Module does receive the packets and sens the data to the Arduino:
First packet:
\r\n+IDP,0,2:On\r\nOk\r\n
Second packet:
\r\n+IDP,0,3:Off\r\nOk\r\n
I can receive the first packat, but after that it's like it stops receiving...
#include<LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int LED = 13;
char inData[20]; // Will contian the UDP data
int charPos = 0; // To find a certain character in the inData array
char inChar; // conversion var
byte index = 0; // index of the inData
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
delay(2000);
Serial.print("AT+CIPMUX=1\n\r");
delay(150);
Serial.print("AT+CIPSERVER=1,2000\n\r");
delay(150);
Serial.print("AT+CIPSTO=120\n\r");
delay(150);
Serial.print("AT+CIPMUX=0\n\r");
delay(150);
while (Serial.available() > 0) {
Serial.read();
}
lcd.begin(16, 2);
lcd.print("WiFi Debugger");
delay(1000);
}
void loop() {
inData[20] = '0'; // Empty data
index = 0; // Set index bac kt o0 for a new read
while (Serial.available() > 0) {
if (index < 19) {
inChar = Serial.read();
inData[index] = inChar;
index++;
inData[index] = '\0';
}
}
lcd.setCursor(0, 1);
lcd.print(inData);
}
With the code above it just prints one (unknown, probably the carriage return) character on the second line of the LCD, however, when I put a delay of 500ms somewhere in the loop it does print the entire data of the packet send via an AT response from the wifi module, but it never refreshes...
The packets are send every 1 second from a C# console program... I tested it by connecting the module via serial directly toe the PC, and it works perfectly...
You're not waiting to receive an entire packet before printing it to the LCD. Each call to loop() your code receives only one character and then prints that one character to the LCD. The last character (a carriage return) is the last printed to the LCD so it stays there until you print something else. Adding a delay inside the while loop causes more characters to become available after you read each one.
christop:
You're not waiting to receive an entire packet before printing it to the LCD. Each call to loop() your code receives only one character and then prints that one character to the LCD. The last character (a carriage return) is the last printed to the LCD so it stays there until you print something else. Adding a delay inside the while loop causes more characters to become available after you read each one.
Why doesn't it read the whole packet? It's in the serial buffer when the wifi module sends it right (Serial.available =<> 20?)
I'm using the display to see if it really switches between the on/off packet, after that I want to make the led on pin 13 blink based on those packets (taking the 2 characters after the : (on/of))....
Adding a delay would break the idea, because I want the LED to blink based on the delay I set on the PC...
SparkyRih:
Why doesn't it read the whole packet? It's in the serial buffer when the wifi module sends it right (Serial.available =<> 20?)
No, because bytes are received one at a time. If there's one byte waiting for you to read it, Serial.available() returns 1.
Once you read that one byte (and assuming no other byte has been received in the few microseconds your code spent processing the byte), the next Serial.available() returns 0, which causes your while loop to terminate. Serial doesn't wait until a whole "packet" is received because it has no concept of "packets". YOU have to decode the data stream into packets.
christop:
No, because bytes are received one at a time. If there's one byte waiting for you to read it, Serial.available() returns 1.
Once you read that one byte (and assuming no other byte has been received in the few microseconds your code spent processing the byte), the next Serial.available() returns 0, which causes your while loop to terminate. Serial doesn't wait until a whole "packet" is received because it has no concept of "packets". YOU have to decode the data stream into packets.
That makes sense...
I guess waiting for the : to come along in the serial data and than reading the following 2-3 bytes would be a better approach right?
Edit: So I got it working now, the only issue that I'm left with is the comparison of the data string with another string... But I got a quick work around for that, will figure that out later...
115200, that's really the maximum baud rate for the arduino right (ATmega328)?