Guys, I'm reading a 125KHz RFID (RDM6300) via hardware serial 1 (mega 2560) and then attempting to post it into SQL using ethernet. Now, the posting is fine... looks working, but it's adding some extra bytes... at the beginning and ending. This is creating issues when working with php and sql later. My codes are given below...
Here, say I'm trying to get "0200719455B2" but I'm getting " 0200719455B2 ". Need to cut down those 3 bits.
#include <SPI.h>
#include <Ethernet.h>#define buzzer 13
String str_rx = "";
String data;char c;
char tagID[14];
int machineID;// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(10,10,20,9); // numeric IP for Google (no DNS)
String serverIP = "10.10.20.9";
//char server[] = "www.google.com"; // name address for Google (using DNS)// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(172,16,20,253);
IPAddress subnet(255,255,255,0);
IPAddress gateway(172,16,20,1);
IPAddress dnsServer(10,100,200,6);// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial1.begin(9600); // start serial1 to RFID reader for Issue
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer, LOW);
Serial.println("Powered UP");
// start the Ethernet connection:
Ethernet.begin(mac, ip, dnsServer, gateway, subnet);
Serial.print("Local IP: ");
Serial.println(Ethernet.localIP());// give the Ethernet shield a second to initialize:
delay(2000);
Serial.println("Ethernet UP and Ready...");
Serial.println();}
void loop() {
digitalWrite(buzzer, LOW);
if (Serial1.available() > 0) {
machineID = 1;
digitalWrite(buzzer, HIGH);
delay(500);
for (int z = 0 ; z < 14 ; z++) // read the rest of the tag
{
tagID[z] = Serial1.read();
}
Serial1.flush();
Serial.print("Machine ID: ");
Serial.println(machineID);data = String(tagID);
Serial.print("Found Data:");
Serial.println(tagID);
postData();
delay(1500);
}/*
while (Serial.available() > 0){
str_rx = String(Serial.readStringUntil('\n'));
data = str_rx;
Serial.print("Found Data: ");
Serial.println(str_rx);
str_rx = "";postData();
delay(1000);}
*/}
void postData(){
Serial.println("Connecting to server...");
// if you get a connection, report back via serial:
client.connect(server, 80);
Serial.println("Connected!");
// Make a HTTP request:
Serial.println("Posting...");
client.println("GET /log_posting.php?tag=" + data + "&machineID=" + machineID + " HTTP/1.1");
Serial.println("GET /log_posting.php?tag=" + data + "&machineID=" + machineID + " HTTP/1.1");
client.println("Host: " + serverIP);
Serial.println("Host: " + serverIP);
client.println("Connection: close");
Serial.println("Connection: close");
client.println();Serial.println("Posted. Disconnecting from Server.");
Serial.println();
client.stop();/*
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}*/
}
and Serial output is:
Machine ID: 1
Found Data:[]0200719455B2[][]
Connecting to server...
Connected!
Posting...
GET /log_posting.php?tag=[]0200719455B2[][]&machineID=1 HTTP/1.1
Host: 10.100.200.9
Connection: close
Posted. Disconnecting from Server.
Where [] is the white space or some bytes...
And SQL gives same. Any ideas??