How do I get data from mysql to Arduino?

#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <avr/pgmspace.h>


int SensorPin1 = A0; //analog pin 0
int SensorPin2 = A1; //analog pin 0
int SensorPin3 = A2; //analog pin 0

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
 
IPAddress server_addr(***);  // mysql 서버 주소를 입력
char user[] = "**";              // mysql user 입력
char password[] = "***";        // mysql password 입력
 
// Sample query

EthernetClient client;
MySQL_Connection conn((Client *)&client);


 
void setup() {
  Serial.begin(9600);
  while (!Serial); // wait for serial port to connect
  Ethernet.begin(mac_addr);
  Serial.println("Connecting...");
  if (conn.connect(server_addr, 3306, user, password)) {
    delay(1000);
  }
  else
    Serial.println("Connection failed.");
}
 
 
void loop() {
  delay(2000);
  
int SensorReading1 = analogRead(SensorPin1);
int SensorReading2 = analogRead(SensorPin2);
int SensorReading3 = analogRead(SensorPin3);

int mfsr_r18 = map(SensorReading1, 0, 1024, 0, 255);
if ( mfsr_r18 >= 1 ) 
{
     const PROGMEM char UPDATE_SQL[] =  "UPDATE userdb.ParkingLotSpace set is_park='1' where P_number = 'park101' and Reserv != '1'" ;
     Serial.println("Recording data.");
     
     
  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  cur_mem->execute(UPDATE_SQL);
 
  delete cur_mem;
}
else 
{
   const PROGMEM char UPDATE_SQL[] = "UPDATE userdb.ParkingLotSpace set is_park='0' where P_number = 'park101'and Reserv != '1'";
Serial.println("Recording data.");
 
  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);

  cur_mem->execute(UPDATE_SQL);
  delete cur_mem;

}


int mfsr_r19 = map(SensorReading2, 0, 1024, 0, 255);
if ( mfsr_r19 >= 1 ) 
{
    const PROGMEM char UPDATE_SQL[] = "UPDATE userdb.ParkingLotSpace set is_park='1' where P_number = 'park102'and Reserv != '1'";

 Serial.println("Recording data.");
  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);

  cur_mem->execute(UPDATE_SQL);

  delete cur_mem;
} 
else 
{
   const PROGMEM char UPDATE_SQL[] = "UPDATE userdb.ParkingLotSpace set is_park='0' where P_number = 'park102'and Reserv != '1'";

 Serial.println("Recording data.");
  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);

  cur_mem->execute(UPDATE_SQL);

  delete cur_mem;
}

int mfsr_r20 = map(SensorReading3, 0, 1024, 0, 255);
if ( mfsr_r20 >= 1 ) 
{
    const PROGMEM char UPDATE_SQL[] = "UPDATE userdb.ParkingLotSpace set is_park='1' where P_number = 'park103'and Reserv != '1'";

 Serial.println("Recording data.");
  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);

  cur_mem->execute(UPDATE_SQL);

  delete cur_mem;
} 
else 
{
   const PROGMEM char UPDATE_SQL[] = "UPDATE userdb.ParkingLotSpace set is_park='0' where P_number = 'park103'and Reserv != '1'";

 Serial.println("Recording data.");
  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);

  cur_mem->execute(UPDATE_SQL);

  delete cur_mem;
}

Serial.println(mfsr_r18);
Serial.println(mfsr_r19);
Serial.println(mfsr_r20);

}

I want to store the parkempty value of park1 in the variable 'A' in Arduino, what should I do?

Have you seen the example sketches?

yes
I saw it, but I want to take out the data I want, but it's not working well.I'm sorry.

Can you post what you tried?

If the IS_PARK value of the table is 1, the data is taken from the table, and the condition statement is attached to Arduino, and the EMPTY value is -1.

I find ESP32 HTTP GET and HTTP POST with Arduino IDE | Random Nerd Tutorials does a good job of putting and getting data from my web sites db.

Do you know how SQL queries work?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.