I want to take data from receiver to my query , my sql connection is working probably until I added the receiver to Arduino , it stop the connection and sometimes give me connection failed, error 33=got packets out of order or stop retrieving data each time I upload sketch .
I'm using Xampp for database +Arduino Uno +FS1000A Wireless RF433 receiver +it receives ownerId
here is my code:
#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include<ServoTimer2.h>
#include <RH_ASK.h>//include radioHead ASK library
#include <SPI.h> // include dependant SPI libray
IPAddress ip(10, 0, 0, 12);
EthernetServer server(80);
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server_addr(10, 0, 0, 5); // IP of the MySQL server here
char user[] = "username"; // MySQL user login username
char password[] = "amna"; // MySQL user login password
EthernetClient client;
MySQL_Connection conn((Client *)&client);
const int trigPin = A2;
const int echoPin = A1;
const int servoPin = A0;
RH_ASK rf_driver; // create ASK object
double SetDelay, Input, Output, ServoOutput;
ServoTimer2 myServo; //Initialize Servo.
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
rf_driver.init(); //initialize ASK object
Serial.begin(115200);
myServo.attach(servoPin); //Attach Servo
Input = readPosition();
while (!Serial); // wait for serial port to connect
Ethernet.begin(mac_addr, ip );
Serial.println("Connecting...");
if (conn.connect(server_addr, 3306, user, password)) {
delay(1000);
// You would add your code here to run a query once on startup.
}
else
Serial.println("Connection failed.");
}
void loop() {
//set buffer to size of expected message
uint8_t buf[9];
uint8_t buflen = sizeof(buf);
// check if recevied packet is correct size
if (rf_driver.recv(buf, &buflen))
{
// message received with valid checksum
Serial.print("id: ");
Serial.println((char*)buf);
String ownerid = (char*)buf;
Serial.println(ownerid);
}
String ownerid= "908";
char select[] = "SELECT ownerID FROM parkingdatabase.logs where ownerID = %s ;";
char query[256];
sprintf(query, select, ownerid.c_str());
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Execute the query
cur_mem->execute(query);
// Fetch the columns and print them
column_names *cols = cur_mem->get_columns();
for (int f = 0; f < cols->num_fields; f++) {
Serial.print(cols->fields[f]->name);
if (f < cols->num_fields - 1) {
Serial.print(", ");
}
}
Serial.println();
// Read the rows and print them
row_values *row = NULL;
int r = 0;
do {
row = cur_mem->get_next_row();
if (row != NULL) {
r = r + 1;
for (int f = 0; f < cols->num_fields; f++) {
Serial.print(row->values[f]);
if (f < cols->num_fields - 1) {
Serial.print("+ ");
}
Input = readPosition();
ServoOutput = Input;
myServo.write(ServoOutput);
if (ServoOutput == 1600)
{
SetDelay = 3000;
}
else {
SetDelay = 100;
}
delay(SetDelay);
}
}
Serial.println();
delay(100);
} while (row != NULL);
// Deleting the cursor also frees up memory used
delete cur_mem;
//Serial.println(r);
}
float readPosition() {
delay(40);
long duration, var;
int distance;
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = duration / (29 * 2);
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
if (distance < 20) {
var = 1600;
}
else {
var = 700;
}
//Returns distance value.
return var;
}
test111.ino (3.63 KB)