ChuckBell / MySQL_Connector_Arduino

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

Unable to connect with Arduino Uno wifi rev. 2 #112

Closed minestrone1994 closed 2 years ago

minestrone1994 commented 4 years ago

So I managed to connect and insert data into a mariaDB10 using a Arduino MKR WiFi 1010. But when I use the exact same code on an Arduino uno wifi rev. 2 it gets stuck on connecting to database. any help would be highly appreciated, I know mariaDB is not supported.

`#include

include

include

include

include

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

IPAddress server_addr(X,X,X,X); // IP of the MySQL server here

char dbusername[] = "Arduino" ; char dbpassword[] = "pass" ; // WiFi card example char ssid[] = "ssid"; // your SSID char pass[] = "pass!"; // your SSID Password

int status = WL_IDLE_STATUS;

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

void setup() { Serial.begin(9600); Serial.print("start"); // check if the WiFi module works if (WiFi.status() == WL_NO_SHIELD) { Serial.println("WiFi shield not present"); // don't continue: while (true); }

// attempt to connect to WiFi network: while ( status != WL_CONNECTED) { Serial.print("Attempting to connect to SSID: "); Serial.println(ssid); // Connect to WPA/WPA2 network. Change this line if using open or WEP network: status = WiFi.begin(ssid, pass); delay(3000); }

printWiFiStatus();

Serial.println("Connecting..."); if (conn.connect(server_addr, 3306, dbusername, dbpassword)) { delay(3000); } else { Serial.println("Connection failed."); }

}

// Sample query char INSERT_SQL[] = "INSERT INTO 'Wetter.Datenhist' (Test) VALUES ('1')";

void loop() { delay(2000);

Serial.println("Recording data.");

// Initiate the query class instance MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); // Execute the query cur_mem->execute(INSERT_SQL); // Note: since there are no results, we do not need to read any data // Deleting the cursor also frees up memory used delete cur_mem; }

void printWiFiStatus() { // print the SSID of the network you're attached to: Serial.print("SSID: "); Serial.println(WiFi.SSID());

// print your WiFi shield's IP address: IPAddress ip = WiFi.localIP(); Serial.print("IP Address: "); Serial.println(ip);

// print the received signal strength: long rssi = WiFi.RSSI(); Serial.print("signal strength (RSSI):"); Serial.print(rssi); Serial.println(" dBm"); }`

ChuckBell commented 4 years ago

Well, aside from the usual things to check including permissions, authentication plugin, etc. there isn't much that should differ. Normally folks encounter issues running queries rather than connection issues when using non-Oracle provided binaries.

That said, this patch has been known to help in some situations. I would suggest trying it after you determine you don't have permissions or connectivity (network) issues. For example, try using a mysql client from another laptop to login to your MySQL server using the same credentials and see if it works. If it doesn't, fix those problems first.

int MySQL_Packet::wait_for_bytes(int bytes_need) { const long wait_till = millis() + MYSQL_DATA_TIMEOUT; int num = 0; long now = 0;

do { now = millis(); num += client->available();

Change that last line in the code above (MySQL_Packet.cpp) - add + before the =. Let me know if it works.

Also, be sure to get the latest code from this site. The code in the Arduino library manager is a bit behind.

If that doesn't work and you are seeing timeout errors (only if you are), try removing this loop in MySQL_Packet.cpp:

// We must wait for slow arriving packets for Ethernet shields only. // Note: comment out for WiFi shields if (wait_for_bytes(packet_len) < packet_len) { show_error(READ_TIMEOUT, true); return; }

minestrone1994 commented 4 years ago

The first patch worked. don't exactly know why the Arduino Uno wifi rev2 behaves differently than a mkr wifi 1010, but they obviously do. Thank you very much for the quick help!

hortynz commented 4 years ago

Hi, just wanted to echo the feedback.

I have an Arduino uno wifi rev. 2 I'm using WiFiNina to connect to the internet I got v1.1.1 from the Arduino libraries My code, based largely on your examples, froze at the connecting point

I followed the instructions above, downloaded your more up to date code, overwrote the library code, and re-ran. The code ran past the connecting point then failed.

I then applied the code patch above and it works fine - well, apart from some sloppy errors in my insert and select code :)

Thanks for maintaining!

ChuckBell commented 2 years ago

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