ChuckBell / MySQL_Connector_Arduino

Database connector library for using MySQL with your Arduino projects.
332 stars 133 forks source link

MySQL connection issue using WiFiESP #28

Closed nokeeffe closed 2 years ago

nokeeffe commented 6 years ago

I am using the WiFiESP library from BPortaluri along with your MySQL connector to connect an Arduino UNO to a MySQL server. My code will connect sometimes to the MySQL server but in most cases it times out when trying to connect. I have tried increasing the MAX_CONNECT_ATTEMPTS and the delay between attempts in MySQL_Connection.cpp but this does not seem to matter. Sometimes the timeout will occur after 1 attempt and sometimes after 5 or 6. It is not an issue with the MySQL server as the connection is always successful from a remote computer. My ESP8266 and the MySQL server are connected to the same WiFi router. I have attached a screenshot of my serial monitor output and my Arduino sketch.

Any help would be greatly appreciated.

Screenshot Arduino Timeout Error.docx

`//#include // Use this for WiFi instead of Ethernet.h

include //use for ESP8266

include

include

// Emulate Serial1 on pins 2/3 if not present

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,1,4); // IP of the MySQL server here char user[] = "user1"; char password[] = "pass1";

// WiFi card example char ssid[] = "mysid"; // your network SSID (name) char pass[] = "password"; // your network password

//WiFiClient client; // Use this for WiFi instead of EthernetClient WiFiEspClient client; //Use this for ESP8266 MySQL_Connection conn((Client *)&client);

void setup() { Serial.begin(115200); while (!Serial); // wait for serial port to connect. Needed for Leonardo only // initialize serial for ESP module Serial.println("Starting...."); Serial1.begin(9600); // initialize ESP module WiFi.init(&Serial1); Serial.println("Connecting to WiFi");

// Begin WiFi section int status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); while(true); } // print out info about the connection: else { 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(1000); } else Serial.println("Connection failed."); conn.close(); }

void loop() { } `

ChuckBell commented 6 years ago

It appears you have an issue with permissions. The error, "Access denied for user" is from the MySQL server. It indicates the user@host combination + password is not permitted to connect to the server. Check your permissions again to ensure the user you specified can indeed connect to your MySQL instance via the 192.168.1.0 network.

Hint: see the reference manual about user+host access in the troubleshooting section.

Note that simply connecting to your MySQL server from another computer with root or some other account is not a valid test of permissions. You should always test using another computer on the same WiFi network as the Arduino is connecting with the same user and password included in your sketch.

If you continue to have problems and the troubleshooting tips do not help, please respond with the model of WiFi shield you are using.

On 12/5/17 1:17 PM, Niall wrote:

I am using the WiFiESP library from BPortaluri along with your MySQL connector to connect an Arduino UNO to a MySQL server. My code will connect sometimes to the MySQL server but in most cases it times out when trying to connect. I have tried increasing the MAX_CONNECT_ATTEMPTS and the delay between attempts in MySQL_Connection.cpp but this does not seem to matter. Sometimes the timeout will occur after 1 attempt and sometimes after 5 or 6. It is not an issue with the MySQL server as the connection is always successful from a remote computer. My ESP8266 and the MySQL server are connected to the same WiFi router. I have attached a screenshot of my serial monitor output and my Arduino sketch.

Any help would be greatly appreciated.

Screenshot Arduino Timeout Error.docx https://github.com/ChuckBell/MySQL_Connector_Arduino/files/1531785/Screenshot.Arduino.Timeout.Error.docx

`//#include // Use this for WiFi instead of Ethernet.h

include //use for ESP8266

include

include

// Emulate Serial1 on pins 2/3 if not present

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,1,4); // IP of the MySQL /server/ here char user[] = "user1"; char password[] = "pass1";

// WiFi card example char ssid[] = "mysid"; // your network SSID (name) char pass[] = "password"; // your network password

//WiFiClient client; // Use this for WiFi instead of EthernetClient WiFiEspClient client; //Use this for ESP8266 MySQL_Connection conn((Client *)&client);

void setup() { Serial.begin(115200); while (!Serial); // wait for serial port to connect. Needed for Leonardo only // initialize serial for ESP module Serial.println("Starting...."); Serial1.begin(9600); // initialize ESP module WiFi.init(&Serial1); Serial.println("Connecting to WiFi");

// Begin WiFi section int status = WiFi.begin(ssid, pass); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); while(true); } // print out info about the connection: else { 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(1000); } else Serial.println("Connection failed."); conn.close(); }

void loop() { } `

— 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/28, or mute the thread https://github.com/notifications/unsubscribe-auth/AH0j4JMHGVtLMc6VJrc6Qp_UHElt5AYxks5s9YizgaJpZM4Q2vFB.

nokeeffe commented 6 years ago

Thanks the swift reply. I do not think it is a permissions problem. I used the same user and password when connecting from a computer on the same network.

I used the command mysql -uuser1 -ppass1 -h192.168.1.4 and this connects within a second everytime. This is the same user and password in my Arduino sketch. The remote computer connects from 192.168.1.10 without issue. My ESP8266 module is at IP address 192.168.1.4. I have granted all privileges to user1@'%'. The other thing I see when testing is that the ESP8266 module will connect in about 1 of every 4 or 5 connection attempts.

The attached screenshot shows a successful connection in the second of 3 connection attempts to the MySQL server. It appears to be some sort of WiFi timing issue. Have you encountered something like this before? I am using a WRL-13678 ESP8266 WiFi module from Sparkfun.

Screenshot MySQL Connect 2.docx

ChuckBell commented 6 years ago

Ok. Add a delay(10000) / 10 seconds / before the conn.connect() call. I suspect your network configuration is not getting sorted before the connection to the MySQL server is attempted. The ESP chips are notorious for being slow. If the delay() helps, start reducing it by 1000 to find the minimal amount of time you need to wait for the ESP chip to get configured properly.

nokeeffe commented 6 years ago

I have found the source of the problem by placing some serial prints in the connect function in MySQL_Connection.cpp. It seems the client was making a connection to the server port. The issue was with the reading and parsing of the packet sent by the server before the authentication function was called. Adding a delay of 10msec before the read_packet() function call fixes the problem. The ESP8266 WiFi module now connects and logs in to the MySQL server correctly everytime.

I suspect that the problem may be with the ESP8266 running at 9600 Baud. This may mean that the server response packet is not fully in the buffer before it is read and parsed. My ESP8266 interface uses SoftwareSerial and this has issues running at baud rates above 9600.

The changed connect function is shown below.

Thank you for your help.

`boolean MySQL_Connection::connect(IPAddress server, int port, char user, char password) { int connected = 0; int i = -1;

// Retry up to MAX_CONNECT_ATTEMPTS times 1 second apart. do { delay(500); connected = client->connect(server, port); i++; } while (i < MAX_CONNECT_ATTEMPTS && !connected);

if (connected) { //Serial.println("Connected"); //added delay(10); //this is the fix that works read_packet(); parse_handshake_packet(); //Serial.println("Login"); //added send_authentication_packet(user, password); read_packet(); if (check_ok_packet() != 0) { parse_error_packet(); return false; } show_error(CONNECTED); Serial.println(server_version); free(server_version); // don't need it anymore return true; } return false; } `

copterino commented 6 years ago

@nokeeffe, could you please test this version of the library? Your case is pretty interesting and it would be useful to check if my edits fixed your problem.

ChuckBell commented 6 years ago

Can you also post how you've got your ESP8266 wired? I want to try to reproduce the timing issues.

nokeeffe commented 6 years ago

My ESP8266 is connected as follows: - Arduino 3.3V to ESP8266 3V (pin 8) Arduino 3.3V to ESP8266 CHPD (pin 4) Arduino GND to ESP8266 GND (pin 1) Arduino D2 (Software Serial RXD) to ESP8266 TXO (pin 2) Arduino D3 (Software Serial TXD) to ESP8266 RXI (pin 7)

Note: I use a resistor divider on the Arduino D3 output to divide the Arduino 5V output down to 3V. I use 1K in series with 2K to ground.

nokeeffe commented 6 years ago

@copterino, I have tested my Arduino sketch using your version of the library (1.2.0a). My sketch connects to the server and executes 10 table inserts. The results are good. The ESP8266 connects successfully each time - I ran the sketch about 20 times. Also I noted that the inserts to the database were much faster than with the previous version of the library (1.1.1a). The 10 inserts run in about 3 seconds. This is down from more than a minute previously. The first 10 inserts in the screenshot are from a sketch running the previous library. Inserts 11 to 20 are from a sketch running your library version.

database table capture

copterino commented 6 years ago

@nokeeffe, thank you so much for testing it. @ChuckBell, I've sent you a pull request with my changes, could you please take a look?

asetyde commented 6 years ago

@copterino Do you resolve heavy select query issue ?

copterino commented 6 years ago

@asetyde, I dont' think it is possible to fix it easy. After select query complete all results are copied into internal library structures, and only after that you can iterate rows to read the results. So the whole query result should fit into small amount of memory of the MCU. All you can do right now is to use SQL LIMIT parameter of the SELECT command. The overall idea is following (pseudo code):

myComplexQuery = "SELECT * from MyTable";
myComplexQueryCount = "SELECT COUNT(*) from MyTable";

mySqlCursor->execute(myComplexQueryCount);
row = mySqlCursor->get_next_row();
if (row)
  count = atol(row->values[0]);

for (int i = 0; i < count; i += 50)
{
  // first iteration will be like this: "SELECT * from MyTable LIMIT 0,50"
  // 0 - starting row, 50 - how many rows
  mySqlCursor->execute(myComplexQuery + " LIMIT " + str(i) + ",50");

  // Now you can process your paginated results here
}

But this approach is non-atomic and heavy to process for the database, if someone is inserting data into table while you are in the for loop you would probably get inconsistent results.

ChuckBell commented 5 years ago

I would locate where mysqld is on your system and add specific rules for that location.

On Oct 6, 2019, at 4:47 PM, michaelradhuber notifications@github.com wrote:

Just a quick note as I am suffering that issue too.

Here is the server side output from my syslog when connecting - and connection gets stuck during Login:

Oct 6 22:32:46 SHARE-SERVER systemd[1]: Stopping MySQL Community Server... Oct 6 22:32:48 SHARE-SERVER kernel: [2080962.687416] [UFW BLOCK] IN=eno1 OUT= MAC=d0:67:26:ce:56:46:00:25:83:f8:cc:00:08:00 SRC=89.248.160.193 DST=140.78.52.81 LEN=40 TOS=0x08 PREC=0x80 TTL=248 ID=29033 PROTO=TCP SPT=49108 DPT=5640 WINDOW=1024 RES=0x00 SYN URGP=0 Oct 6 22:32:50 SHARE-SERVER systemd[1]: Stopped MySQL Community Server. Oct 6 22:32:50 SHARE-SERVER systemd[1]: Starting MySQL Community Server... Oct 6 22:32:50 SHARE-SERVER kernel: [2080964.672902] audit: type=1400 audit(1570393970.720:40): apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/sys/devices/system/node/" pid=1559 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=0 ouid=0 Oct 6 22:32:50 SHARE-SERVER kernel: [2080964.687941] audit: type=1400 audit(1570393970.732:41): apparmor="DENIED" operation="capable" profile="/usr/sbin/mysqld" pid=1559 comm="mysqld" capability=2 capname="dac_read_search" Oct 6 22:32:50 SHARE-SERVER kernel: [2080964.705015] audit: type=1400 audit(1570393970.752:42): apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/sys/devices/system/node/" pid=1564 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=110 ouid=0 Oct 6 22:32:51 SHARE-SERVER systemd[1]: Started MySQL Community Server. Oct 6 22:33:01 SHARE-SERVER kernel: [2080975.250444] [UFW BLOCK] IN=eno1 OUT= MAC=d0:67:26:ce:56:46:00:25:83:f8:cc:00:08:00 SRC=51.75.52.127 DST=140.78.52.81 LEN=44 TOS=0x10 PREC=0x80 TTL=116 ID=40927 PROTO=TCP SPT=26200 DPT=1063 WINDOW=36138 RES=0x00 SYN URGP=0 Oct 6 22:34:58 SHARE-SERVER kernel: [2081092.682260] [UFW BLOCK] IN=eno1 OUT= MAC=d0:67:26:ce:56:46:00:25:83:f8:cc:00:08:00 SRC=1.174.90.129 DST=140.78.52.81 LEN=40 TOS=0x08 PREC=0x80 TTL=52 ID=36449 PROTO=TCP SPT=50682 DPT=23 WINDOW=11311 RES=0x00 SYN URGP=0 Oct 6 22:34:59 SHARE-SERVER kernel: [2081093.192464] [UFW BLOCK] IN=eno1 OUT= MAC=d0:67:26:ce:56:46:00:25:83:f8:cc:00:08:00 SRC=92.119.160.52 DST=140.78.52.81 LEN=40 TOS=0x08 PREC=0x80 TTL=242 ID=51815 PROTO=TCP SPT=49782 DPT=43094 WINDOW=1024 RES=0x00 SYN URGP=0 root@SHARE-SERVER:/var/log#

This apperas to be an APPARMOR issue. Here is more info on it

https://ixnfo.com/en/solution-apparmor-denied-operation-open-profile-usr-sbin-mysqld.html <x-msg://4/url> ....however the fix proposed did not work for me (YET). Only thing working was stopping APPARMOR (which is no solution, obv....)....

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ChuckBell/MySQL_Connector_Arduino/issues/28?email_source=notifications&email_token=AB6SHYFRFATTWXAIUZIWEFTQNJFHJA5CNFSM4EG26FA2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAOTWCA#issuecomment-538786568, or mute the thread https://github.com/notifications/unsubscribe-auth/AB6SHYDKVJRYVWH7NOQ2LMDQNJFHJANCNFSM4EG26FAQ.

michaelradhuber commented 5 years ago

I was wrong, apparmor was not the culprit. See my succ. post

Dr. Charles Bell notifications@github.com schrieb am Di., 8. Okt. 2019, 20:42:

I would locate where mysqld is on your system and add specific rules for that location.

On Oct 6, 2019, at 4:47 PM, michaelradhuber notifications@github.com wrote:

Just a quick note as I am suffering that issue too.

Here is the server side output from my syslog when connecting - and connection gets stuck during Login:

Oct 6 22:32:46 SHARE-SERVER systemd[1]: Stopping MySQL Community Server... Oct 6 22:32:48 SHARE-SERVER kernel: [2080962.687416] [UFW BLOCK] IN=eno1 OUT= MAC=d0:67:26:ce:56:46:00:25:83:f8:cc:00:08:00 SRC=89.248.160.193 DST=140.78.52.81 LEN=40 TOS=0x08 PREC=0x80 TTL=248 ID=29033 PROTO=TCP SPT=49108 DPT=5640 WINDOW=1024 RES=0x00 SYN URGP=0 Oct 6 22:32:50 SHARE-SERVER systemd[1]: Stopped MySQL Community Server. Oct 6 22:32:50 SHARE-SERVER systemd[1]: Starting MySQL Community Server... Oct 6 22:32:50 SHARE-SERVER kernel: [2080964.672902] audit: type=1400 audit(1570393970.720:40): apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/sys/devices/system/node/" pid=1559 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=0 ouid=0 Oct 6 22:32:50 SHARE-SERVER kernel: [2080964.687941] audit: type=1400 audit(1570393970.732:41): apparmor="DENIED" operation="capable" profile="/usr/sbin/mysqld" pid=1559 comm="mysqld" capability=2 capname="dac_read_search" Oct 6 22:32:50 SHARE-SERVER kernel: [2080964.705015] audit: type=1400 audit(1570393970.752:42): apparmor="DENIED" operation="open" profile="/usr/sbin/mysqld" name="/sys/devices/system/node/" pid=1564 comm="mysqld" requested_mask="r" denied_mask="r" fsuid=110 ouid=0 Oct 6 22:32:51 SHARE-SERVER systemd[1]: Started MySQL Community Server. Oct 6 22:33:01 SHARE-SERVER kernel: [2080975.250444] [UFW BLOCK] IN=eno1 OUT= MAC=d0:67:26:ce:56:46:00:25:83:f8:cc:00:08:00 SRC=51.75.52.127 DST=140.78.52.81 LEN=44 TOS=0x10 PREC=0x80 TTL=116 ID=40927 PROTO=TCP SPT=26200 DPT=1063 WINDOW=36138 RES=0x00 SYN URGP=0 Oct 6 22:34:58 SHARE-SERVER kernel: [2081092.682260] [UFW BLOCK] IN=eno1 OUT= MAC=d0:67:26:ce:56:46:00:25:83:f8:cc:00:08:00 SRC=1.174.90.129 DST=140.78.52.81 LEN=40 TOS=0x08 PREC=0x80 TTL=52 ID=36449 PROTO=TCP SPT=50682 DPT=23 WINDOW=11311 RES=0x00 SYN URGP=0 Oct 6 22:34:59 SHARE-SERVER kernel: [2081093.192464] [UFW BLOCK] IN=eno1 OUT= MAC=d0:67:26:ce:56:46:00:25:83:f8:cc:00:08:00 SRC=92.119.160.52 DST=140.78.52.81 LEN=40 TOS=0x08 PREC=0x80 TTL=242 ID=51815 PROTO=TCP SPT=49782 DPT=43094 WINDOW=1024 RES=0x00 SYN URGP=0 root@SHARE-SERVER :/var/log#

This apperas to be an APPARMOR issue. Here is more info on it

https://ixnfo.com/en/solution-apparmor-denied-operation-open-profile-usr-sbin-mysqld.html <x-msg://4/url> ....however the fix proposed did not work for me (YET). Only thing working was stopping APPARMOR (which is no solution, obv....)....

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub < https://github.com/ChuckBell/MySQL_Connector_Arduino/issues/28?email_source=notifications&email_token=AB6SHYFRFATTWXAIUZIWEFTQNJFHJA5CNFSM4EG26FA2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAOTWCA#issuecomment-538786568>, or mute the thread < https://github.com/notifications/unsubscribe-auth/AB6SHYDKVJRYVWH7NOQ2LMDQNJFHJANCNFSM4EG26FAQ .

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ChuckBell/MySQL_Connector_Arduino/issues/28?email_source=notifications&email_token=AD6YIFNDJVZF6OTQASND3MLQNTIC5A5CNFSM4EG26FA2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAVGCXQ#issuecomment-539648350, or mute the thread https://github.com/notifications/unsubscribe-auth/AD6YIFL3RJTIPH53WFLP6XTQNTIC5ANCNFSM4EG26FAQ .

JangHyeonJun2 commented 4 years ago

include

include

include

include

IPAddress server_addr(192.168.0.102); // IP of the MySQL server here char user[] = "root"; // MySQL user login username char password[] = "0653"; // MySQL user login password

char ssid[] = "구미"; // your network SSID (name) char pass[] = "tjswn0653"; // your network password (use for WPA, or use as key for WEP)

// Sample query char INSERT_SQL[] = "INSERT INTO adu.sps_test (name) VALUES ('Hello, Arduino!');";

WiFiClient client; MySQL_Connection conn((Client )&client); MySQL_Cursor cursor; void setup() { Serial.begin(115200); while (!Serial); // wait for serial port to connect int status = WiFi.begin(ssid, pass); delay(5000); if ( status != WL_CONNECTED) { Serial.println("Couldn't get a wifi connection"); while(true); } else { Serial.println("Connected to network"); IPAddress ip = WiFi.localIP(); Serial.print("My IP address is: "); Serial.println(ip); } Serial.println("Connecting..."); Serial.println("here"); delay(15000); boolean flag = conn.connect(server_addr, 3306, user, password); Serial.println(flag); delay(2000); if (conn.connect(server_addr, 3306, user, password)) { Serial.println("ok");

} else{ Serial.println("Connection failed."); conn.close(); } delay(2000); Serial.println("Recording data."); }

void loop() {

// Serial.println("Connecting..."); // Serial.println("here"); // delay(2000); // if (conn.connect(server_addr,3306,user,password)) { HereI can't keep connecting here. Can you help me? // Serial.println("ok"); // delay(10000); // } // else{ // Serial.println("Connection failed."); // conn.close(); // } // delay(2000); // Serial.println("Recording data."); //
// if (conn.connected()) { // Serial.println("Inserting data"); // cursor->execute(INSERT_SQL); //
// delay(5000); // }else{ // Serial.println("Not recording, connection failed."); // } // 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; }