Hey guys, I'm having some trouble with the following:
Here's what I'm doing: I'm reading the serial port for incoming data, parsing the data (which is sent in a specific format: , for example <1-1023>), and then sending the id and the data to my web server.
I have no problem with the reading the serial port, parsing the data or sending data to the web server, but when I combine all of them I get "corrupted" data from time to time... for example:
my data ranges from 0 to 1023, and I got a couple of times something like 8830, which is over 1023. I am also getting, from time to time, a device ID that doesn't match the current device ID (which is 1).
I'm thinking the delays from sending to the web server affects the serial buffer somehow and messes up my data from time to time? Let me know if you find anything that could fix my problem ![]()
Here's my code:
#include <Ethernet.h>
#include <SPI.h>
#define SOP '<' // Start of packet
#define EOP '>' // End of packet
#define PAD '-' // Packet (device) address delimiter
// Example of a packet: <1-1022> ---> device address = 1, data = 1022
// Ethernet config
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x2A, 0x8D };
byte ip[] = { 192, 168, 2, 77 };
byte gw[] = {192, 168, 2, 1};
byte subnet[] = { 255, 255, 255, 0 };
EthernetClient client;//(server, 80);
byte server[] = { 192, 168, 2, 83 }; // Server IP
// Data config -- information to be sent to the web server
int device = 0;
int data = 0;
int index = 0;
char tmp = 0; //temporary variable to store serial data input, that will be parsed.
char in_buffer[8];
boolean end_of_packet = false;
void setup()
{
Serial.begin(9600); // Used for serial debugging
Ethernet.begin(mac, ip, gw, gw, subnet);
Serial.println("Program running...");
delay(1000); //Keeps the connection from freezing
}
void loop()
{
while (Serial.available() > 0) {
tmp = Serial.read();
if (tmp == SOP) {
index = 0;
}
else if (tmp == EOP) {
index++;
in_buffer[index] = '\0';
data = atoi(in_buffer);
end_of_packet = true;
break; //exit while loop
}
else {
in_buffer[index] = tmp;
index++;
if (tmp == PAD) {
index++;
in_buffer[index] = '\0';
device = atoi(in_buffer);
index = 0;
memset(in_buffer, 0, 8);
}
}
}
if (end_of_packet) {
index = 0;
memset(in_buffer, 0, 8);
if ((10 > device >= 1) && (1023 >= data >= 0)) { //if condition was added specifically to prevent some of the corrupted data
Serial.println("Device ID: ");
Serial.println(device);
Serial.println("Data: ");
Serial.println(data);
//delay(5000);
senddata(); // Data is sent
end_of_packet = false;
}
else {
end_of_packet = false;
}
}
}
void senddata()
{
Serial.println();
Serial.println("ATE :)");
if (client.connect(server, 80)) {
Serial.println("Connected");
client.print("GET /insertdata.php?");
client.print("device=");
client.print(device);
client.print("&data=");
client.print(data);
client.println(" HTTP/1.1");
client.println("Host: 192.168.2.83");
client.println("Connection: close");
client.println();
Serial.println();
while (client.connected()) {
while (client.available()) {
Serial.write(client.read());
}
}
}
else
{
Serial.println("Connection unsuccessful");
}
//stop client
client.stop();
while (client.status() != 0)
{
delay(5);
}
}
As you can see, I added a condition to send data so some of the corrupted data isn't sent, but I still get a device ID of 0 from time to time when it's supposed to be 1.
Thanks a lot for your help!