salve,
con un arduino uno, con shield ethernet, ho uno sketch che, dopo aver letto l'epc di un tag rfid, salva quest ultimo sulla sd di arduino e poi lo carica su un database.
Il problema è che lo sketch funziona "una volta si ed una no". Sul database viene caricata un'uscita ma, dopo una seconda lettura, l'rfid non rientra, però, con una terza lettura, il tag esce di nuovo dal database. E' come se lo sketch funzionasse solo con le uscite, rilevando gli ingressi senza però salvarli.
Lo sketch è il seguente:
#include <SPI.h>
#include <Ethernet.h>
#include <String.h>
#include <SD.h>
char act[11] = {0x0A, 0x00, 0x35, 0x01, 0x02, 0x01, 0x02, 0x06, 0x00, 0xB1, 0x63};
byte ans[11] = {0x0A, 0x00, 0x35, 0x00, 0x02, 0x01, 0x02, 0x01, 0x00, 0x92, 0x2A};
byte controllo = 0xFFFFFFFF;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 11, 13);
IPAddress server1(192, 168, 11, 3);
IPAddress server2(192, 168, 11, 15);
String msg = "";
int active = 0;
int answer = 0;
int we = 0;
bool factive = false;
String oldepc;
String epc;
String amb = "2";
int i = 0;
File myFile;
int cont;
EthernetServer server(80);
EthernetClient client1;
EthernetClient client;
void setup() {
Serial.begin(57600);
while (!Serial) {
;
}
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
}
void loop() {
client = server.available();
if (factive) {
LetturaRFID();
}
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
msg += c;
if (c == '\n') {
active = msg.indexOf("Active");
answer = msg.indexOf("AnswerEPC");
we = msg.indexOf("WriteEPC");
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<html><body><head>");
//client.print("Refresh: 5");
client.print("</head>");
client.println("The reader has changed his mode");
// client.println("The reader is in Answer mode");
// client.println(we);
if (active != -1) {
msg = "";
factive = true;
if (client1.connect(server1, 5000)) {
Serial.println("connesso antenna");
}
do {
delay(1000);
client1.write(act, 11);
Serial.println(client.read(), HEX);
} while (controllo == (client.read(), HEX));
}
if (client.available()) {
byte b = client.read();
Serial.print(b, HEX);
}
client1.stop();
client.stop();
client.print("Active");
if (answer != -1) {
msg = "";
factive = false;
if (client1.connect(server1, 5000)) {
Serial.println("connesso antenna");
}
do {
delay(1000);
client1.write(ans, 11);
Serial.println(client.read(), HEX);
} while (controllo == (client.read(), HEX));
client1.stop();
client.stop();
client.print("Answer");
}
if (we != -1) {
msg = "";
client.print("Write EPC");
}
client.print("</body></html>");
delay(1);
break;
}
}
}
client.stop();
}
}
void Caricamento()
{
if (client1.connect(server2, 80)) {
Serial.println("connesso");
client1.print("GET /html5/write_data.php?"); // This
//client.print("id="+(b,HEX)); // This
client1.print("epc=");
client1.print(epc);
client1.print("&amb=");
client1.print(amb);
//client.print(photocellReading); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
client1.println(" HTTP/1.1"); // Part of the GET request
client1.println("Host: 192.168.11.15"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
client1.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
client1.println(); // Empty line
client1.println(); // Empty line
client1.stop(); // Closing connection to server
}
else {
// If Arduino can't connect to the server (your computer or web page)
Serial.println("--> connection failed\n");
}
// Give the server some time to recieve the data and store it. I used 10 seconds here. Be advised when delaying. If u use a short delay, the server might not capture data because of Arduino transmitting new data too soon.
delay(10000);
}
void LetturaRFID() {
if (client1.connect(server1, 5000)) {
Serial.println("connesso antenna");
}
if (i < 18) {
if (client1.available()) {
byte b = client1.read();
if (i == 4 || i == 5) {
epc += String(b, HEX);
}
i++;
if (i == 18) {
client1.stop();
scrivi(epc);
leggi();
Caricamento();
Serial.println("Caricato");
SD.remove("test1.txt");
Serial.println("file svuotato");
i = 0;
epc = "";
//*}
}
}
}
}
void scrivi(String epc) {
myFile = SD.open("test1.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println(epc);
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void leggi() {
myFile = SD.open("test1.txt");
if (myFile) {
Serial.println("test1.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
L'url che compone arduino, se usato manualmente su browser, aggiorna il database sia con gli ingressi che le uscite