Help with MySQL library code to connect to a remote database (LilyGo T-SIM7600X)
-
Ok, so I have a Windows machine with the SQL database and MySQL Workbench 8.0. The database have 1 table with 1 row only, just for testing purposes.
Then, I have my laptop, running Debian and I can connect from this laptop to the Windows machine (where the database is) usingmysql
command line tool, either using root or another user.
The code I'm using in my LilyGo T-SIM7600X device is this:#include <WiFi.h> #include <MySQL_Connection.h> #include <MySQL_Cursor.h> #define SOUND_SPEED 0.034 #define CM_TO_INCH 0.039701 #define GOT_HERE() do {Serial.print('['); Serial.print(__LINE__); Serial.println(']'); Serial.flush();} while(0) IPAddress server_addr(192, 168, 1, 157); // Server IP address char user[] = "testuser"; // MySQL user char password[] = "testuser-passwd"; // MySQL password char ssid[] = "Work_WiFi"; // Wi-Fi SSID char pass[] = "Work_WiFi_passwd"; // Wi-Fi password char INSERT_DATA[] = "INSERT INTO sensorsdara (sensorsId, latitude, longitude, distance, weight, lastUpdate, description) VALUES ('%s', %.8f, %.8f, %d, %.1f, '%s', '%s')"; WiFiClient Client; MySQL_Connection conn(&Client); MySQL_Cursor* cursor; const int trigPin = 13; const int echoPin = 14; long duration = 0; float distanceInch = 0; void setup() { Serial.begin(115200); Serial.println("Starting the system..."); checkWiFi(); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { int sensorsId = 1; // Assuming sensorsId is an integer float latitude = 40.12236870; float longitude = -7.75648210; float weight = 98.5; int distance = 56; char timestamp[] = "19:22:37"; char description[] = "Contentor XYZ, marca AAA"; ReadData(&sensorsId, latitude, longitude, distance, weight, timestamp, description); digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = duration * SOUND_SPEED / 2; ReadData(&sensorsId, latitude, longitude, distance, weight, timestamp, description); delay(100); SendData(sensorsId, latitude, longitude, distance, weight, timestamp, description); Serial.println(); } void ReadData(int* sensorsId, float latitude, float longitude, int distance, float weight, char timestamp[], char description[]){ char mensagem[20]; byte actual, i = 0; actual = 255; if(Serial.available() > 0){ while (actual != 10){ if(Serial.available() > 0){ actual = Serial.read(); mensagem[i] = (char) actual; } } i = 0; *sensorsId = atoi(strtok(mensagem, "|")); //*temp = atof(strtok(NULL, "|")); } } void SendData(int sensorsID, float latitude, float longitude, int distance, float weight, char timestamp[], char description[]) { char query[256]; char sensorsId[10] = "sensorABC"; checkWiFi(); Serial.print("Connecting to MySQL database... "); if (conn.connect(server_addr, 3306, user, password, "database_name")) { Serial.println("Connected successfully."); delay(1000); MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); // Prepare the SQL query sprintf(query, INSERT_DATA, sensorsId, latitude, longitude, distance, weight, timestamp, description); // Print the query for debugging purposes Serial.print("Executing query: "); Serial.println(query); // Execute the query if (cur_mem->execute(query)) { Serial.println("Query executed successfully, data inserted."); } else { Serial.println("Failed to execute query."); } // Free up memory used by the cursor delete cur_mem; } else { Serial.println("Failed to connect to MySQL database."); } conn.close(); } void checkWiFi() { Serial.print("Checking WiFi connection... "); if (WiFi.status() != WL_CONNECTED) { Serial.println("Not connected. Trying to connect..."); WiFi.disconnect(); delay(1000); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nConnected to WiFi."); } else { Serial.println("Already connected to WiFi."); } }
The issue is that the connection never happens, or at least the insertion of the data is never inserted into the database.
So, I went a bit deeper and started to add that known macro that tells us the line of code where the macro is used in the source code of the library MySQL_Connection.h to see where the code would hang and I came to the conclusion that the code hangs here:
https://github.com/ChuckBell/MySQL_Connector_Arduino/blob/067b345a3a9870d836bcfb89c4f709bf0561c8b8/src/MySQL_Connection.cpp#L87So, this must be something related with authentication, although I can connect to the database using
mysql
command line tool from my laptop.So, I need some help to try to figure out what may be wrong with the code or the authentication or maybe something wrong with the database settings/permissions
The part of the library source code I added the GOT_HERE(); macro is this:while (retries--) { Serial.println("...trying..."); Serial.print("Attempt: "); Serial.println(retries); connected = client->connect(server, port); GOT_HERE(); // We do reach here if (connected != SUCCESS) { GOT_HERE(); // We do not reach here Serial.print("...got: "); Serial.print(connected); Serial.println(" retrying..."); delay(CONNECT_DELAY_MS); } else { GOT_HERE(); // We do reach here break; } } if (connected != SUCCESS){ GOT_HERE(); // We do not reach here return false; } GOT_HERE(); // We do reach here read_packet(); GOT_HERE(); // We do reach here parse_handshake_packet(); GOT_HERE(); Serial.print("User: "), Serial.println(user); Serial.print("Passwd: "), Serial.println(password); Serial.print("DB: "), Serial.println(db); send_authentication_packet(user, password, db); GOT_HERE(); // We do not reach here read_packet(); GOT_HERE(); // We do not reach here if (get_packet_type() != MYSQL_OK_PACKET) { parse_error_packet(); return false; }
I commented where the code reaches and where the code does not reach the macro and also added a couple of Serial.println() lines
Serial Monitor shows this:
-
It seems like your connection is working fine. If you're testing further, especially for your Food Website, try running a few simple queries from your Debian laptop using
mysql
to ensure everything works smoothly between machines.