ChuckBell / MySQL_Connector_Arduino

Database connector library for using MySQL with your Arduino projects.
331 stars 132 forks source link

Arduino MySQL Connector Insert working fine but Select returns Null or timeouts #126

Closed franastley closed 2 years ago

franastley commented 4 years ago

Hi,

I am trying to retrieve Data from a MySQL on a NAS server on my local network. I am able to insert data with the code below: `//#include // Use this for WiFi instead of Ethernet.h

include //use for ESP8266

include

include

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server_addr(192,168,xx,xx); // IP of the MySQL server here char user[] = "francis"; char password[] = "xxx"; String head_count; // WiFi card example char ssid[] = "PintoLoco"; // your network SSID (name) char pass[] = "xxxx"; // your network password

//WiFiClient client; // Use this for WiFi instead of EthernetClient WiFiEspClient client; //Use this for ESP8266 MySQL_Connection conn((Client )&client); char query[] = "INSERT INTO Peceras.Parameters(Device,Value) Values ('Test',6);"; row_values row ; char test; void setup() {

Serial.println("Starting...."); Serial.begin(115200); // initialize ESP module WiFi.init(&Serial); Serial.println("Connecting to WiFi");

// Begin WiFi section int status = WiFi.begin(ssid, pass); while(status != WL_CONNECTED){ status = WiFi.begin(ssid, pass); } // print out info about the connection:

Serial.println("Connected to network"); IPAddress ip = WiFi.localIP(); Serial.print("My IP address is: "); Serial.println(ip);

// End WiFi section

Serial.println("Connecting..."); if (conn.connect(server_addr,3306,user,password)) { delay(5000);

} else{ Serial.println("Connection failed."); }}

void loop() { MySQL_Cursor cur_mem = new MySQL_Cursor(&conn); // Execute the query cur_mem->execute(query); // Fetch the columns (required) but we don't use them. column_names columns = cur_mem->get_columns();

delete cur_mem;

// Show the result Serial.print(" NYC pop = "); Serial.println(head_count);

delay(5000); }`

However when i try selecting a value from the same table and database i sometimes get a null value or a timeout 50, disconnection 3. The code for the select is the following: `#include

include

include

ifndef HAVE_HWSERIAL1

include "SoftwareSerial.h"

SoftwareSerial Serial1(2, 3); // RX, TX

endif

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server_addr(192,168,50,28); char user[] = ""; char password[] = "";

char ssid[] = ""; char pass[] = "";

WiFiEspClient client; MySQL_Connection conn( (Client ) &client ); MySQL_Cursor cur_mem ;

// PPK: some database systems are case sensitive // so Peceras.Parameters is not the same as peceras.parameters // check if the tables and fields are named correctly. char query[] = "SELECT Value FROM Peceras.Parameters";

void setup() { Serial.begin(115200);

Serial.println( "Connecting to WiFi" ); WiFi.init( &Serial ); int status = WiFi.begin( ssid, pass ); while( status != WL_CONNECTED ) { status = WiFi.begin(ssid, pass); }

Serial.println( "Connected to network" ); IPAddress ip = WiFi.localIP(); Serial.print( "My IP address is: " ); Serial.println( ip );

Serial.println( "Connecting DB ..." ); if ( conn.connect( server_addr, 3306, user, password ) ) { Serial.println( "DB connected." ); cur_mem = new MySQL_Cursor( &conn ); row_values row = NULL; long head_count = NULL; column_names cols = cur_mem->get_columns();

cur_mem->execute(query);

do 
{
  row = cur_mem->get_next_row();
  if ( row != NULL ) 
  {
    for ( int f = 0; f < cols->num_fields; f++ ) 
    {
      head_count = atoi( row->values[f] );
      Serial.print( "Ingestelde Temperatuur:  ");
      Serial.println( head_count );
      if ( f < cols->num_fields - 1 ) 
      {
        Serial.println(',');
      }
    }
    Serial.println();
  }
} while (row != NULL);

// Deleting the cursor also frees up memory used
// PPK: I'm not sure if this is a good idea, I don't know
delete cur_mem;

} else { Serial.println( "DB Connection failed." ); } }

void loop() { // PPK: I would recommend to do the select in the setup section // It looks like you want to run it just once }`

I have tried changing the readUntil(1000 to 2000 and 5000 in the EspDrv.h file from the WifiESP Library and still same timeout result. I have checked my user has access and its host is a wildcard to discard that issue. My only guess would be memory? although I only have one record in the table at the moment.

Any Ideas?

ChuckBell commented 4 years ago

Move get_columns AFTER the execute. Try that.

On Mar 17, 2020, at 8:30 PM, franastley notifications@github.com wrote:

Hi,

I am trying to retrieve Data from a MySQL on a NAS server on my local network. I am able to insert data with the code below: `//#include // Use this for WiFi instead of Ethernet.h

include //use for ESP8266

include

include

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server_addr(192,168,xx,xx); // IP of the MySQL server here char user[] = "francis"; char password[] = "xxx"; String head_count; // WiFi card example char ssid[] = "PintoLoco"; // your network SSID (name) char pass[] = "xxxx"; // your network password

//WiFiClient client; // Use this for WiFi instead of EthernetClient WiFiEspClient client; //Use this for ESP8266 MySQL_Connection conn((Client )&client); char query[] = "INSERT INTO Peceras.Parameters(Device,Value) Values ('Test',6);"; row_values row ; char test; void setup() {

Serial.println("Starting...."); Serial.begin(115200); // initialize ESP module WiFi.init(&Serial); Serial.println("Connecting to WiFi");

// Begin WiFi section int status = WiFi.begin(ssid, pass); while(status != WL_CONNECTED){ status = WiFi.begin(ssid, pass); } // print out info about the connection:

Serial.println("Connected to network"); IPAddress ip = WiFi.localIP(); Serial.print("My IP address is: "); Serial.println(ip);

// End WiFi section

Serial.println("Connecting..."); if (conn.connect(server_addr,3306,user,password)) { delay(5000);

} else{ Serial.println("Connection failed."); }}

void loop() { MySQL_Cursor cur_mem = new MySQL_Cursor(&conn); // Execute the query cur_mem->execute(query); // Fetch the columns (required) but we don't use them. column_names columns = cur_mem->get_columns();

delete cur_mem;

// Show the result Serial.print(" NYC pop = "); Serial.println(head_count);

delay(5000); }`

However when i try selecting a value from the same table and database i sometimes get a null value or a timeout 50, disconnection 3. The code for the select is the following: `#include

include

include

ifndef HAVE_HWSERIAL1

include "SoftwareSerial.h"

SoftwareSerial Serial1(2, 3); // RX, TX

endif

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server_addr(192,168,50,28); char user[] = "francis"; char password[] = "Karate9250";

char ssid[] = "PintoLoco"; char pass[] = "richmond123";

WiFiEspClient client; MySQL_Connection conn( (Client ) &client ); MySQL_Cursor cur_mem ;

// PPK: some database systems are case sensitive // so Peceras.Parameters is not the same as peceras.parameters // check if the tables and fields are named correctly. char query[] = "SELECT Value FROM Peceras.Parameters";

void setup() { Serial.begin(115200);

Serial.println( "Connecting to WiFi" ); WiFi.init( &Serial ); int status = WiFi.begin( ssid, pass ); while( status != WL_CONNECTED ) { status = WiFi.begin(ssid, pass); }

Serial.println( "Connected to network" ); IPAddress ip = WiFi.localIP(); Serial.print( "My IP address is: " ); Serial.println( ip );

Serial.println( "Connecting DB ..." ); if ( conn.connect( server_addr, 3306, user, password ) ) { Serial.println( "DB connected." ); cur_mem = new MySQL_Cursor( &conn ); row_values row = NULL; long head_count = NULL; column_names cols = cur_mem->get_columns();

cur_mem->execute(query);

do { row = cur_mem->get_next_row(); if ( row != NULL ) { for ( int f = 0; f < cols->num_fields; f++ ) { head_count = atoi( row->values[f] ); Serial.print( "Ingestelde Temperatuur: "); Serial.println( head_count ); if ( f < cols->num_fields - 1 ) { Serial.println(','); } } Serial.println(); } } while (row != NULL);

// Deleting the cursor also frees up memory used // PPK: I'm not sure if this is a good idea, I don't know delete cur_mem; } else { Serial.println( "DB Connection failed." ); } }

void loop() { // PPK: I would recommend to do the select in the setup section // It looks like you want to run it just once }`

I have tried changing the readUntil(1000 to 2000 and 5000 in the EspDrv.h file from the WifiESP Library and still same timeout result. I have checked my user has access and its host is a wildcard to discard that issue. My only guess would be memory? although I only have one record in the table at the moment.

Any Ideas?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ChuckBell/MySQL_Connector_Arduino/issues/126, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB6SHYERMEXPLKM5Q6C66LLRIAIZJANCNFSM4LOAIHNQ.

franastley commented 4 years ago

Thanks Chuck, sadly same result. Got only Nulls. Could it be the type of board i am using? I am using a Arduino ESP8266 Wifi Shiald from WangTongze connected to an Arduino UNO

ChuckBell commented 4 years ago

Possibly. Do you have another shield to try? I’m working through a related issue with a similar WiFi Shield. If I find any solution, I’ll share in case it helps. ;)

On Mar 17, 2020, at 8:44 PM, franastley notifications@github.com wrote:

Thanks Chuck, sadly same result. Got only Nulls. Could it be the type of board i am using? I am using a Arduino ESP8266 Wifi Shiald from WangTongze connected to an Arduino UNO

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ChuckBell/MySQL_Connector_Arduino/issues/126#issuecomment-600368331, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB6SHYDVYOQIWHJ2RC2OHZLRIAKQBANCNFSM4LOAIHNQ.

ChuckBell commented 4 years ago

Yep, changing shields for me worked. I’d try another WiFi shield.

On Mar 17, 2020, at 8:46 PM, Charles Bell drcharlesbell@gmail.com wrote:

Possibly. Do you have another shield to try? I’m working through a related issue with a similar WiFi Shield. If I find any solution, I’ll share in case it helps. ;)

On Mar 17, 2020, at 8:44 PM, franastley <notifications@github.com mailto:notifications@github.com> wrote:

Thanks Chuck, sadly same result. Got only Nulls. Could it be the type of board i am using? I am using a Arduino ESP8266 Wifi Shiald from WangTongze connected to an Arduino UNO

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ChuckBell/MySQL_Connector_Arduino/issues/126#issuecomment-600368331, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB6SHYDVYOQIWHJ2RC2OHZLRIAKQBANCNFSM4LOAIHNQ.

franastley commented 4 years ago

Thanks Chuck I'll Buy another one then. which one did you try to buy that if possible

ChuckBell commented 4 years ago

I’m using a Uno WifI rev2 if that helps. :)

On Mar 17, 2020, at 9:11 PM, franastley notifications@github.com wrote:

Thanks Chuck I'll Buy another one then. which one did you try to buy that if possible

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ChuckBell/MySQL_Connector_Arduino/issues/126#issuecomment-600374276, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB6SHYEMRTBYJ7U3FIXBEBTRIANTRANCNFSM4LOAIHNQ.

Git-RWK commented 4 years ago

hi franastley.. next time remove your wifi and db user credentials from your code before posting here..

ChuckBell commented 2 years ago

Closed due to inactivity. Please open a new ticket if this is still relevant.