<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Help with MySQL library code to connect to a remote database (LilyGo T-SIM7600X)]]></title><description><![CDATA[<p dir="auto">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.<br />
Then, I have my laptop, running Debian and I can connect from this laptop to the Windows machine (where the database is) using <code>mysql</code> command line tool, either using root or another user.<br />
The code I'm using in my LilyGo T-SIM7600X device is this:</p>
<pre><code>#include &lt;WiFi.h&gt;
#include &lt;MySQL_Connection.h&gt;
#include &lt;MySQL_Cursor.h&gt;

#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(&amp;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(&amp;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(&amp;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() &gt; 0){
    while (actual != 10){
      if(Serial.available() &gt; 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(&amp;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-&gt;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.");
  }
}
</code></pre>
<p dir="auto">The issue is that the connection never happens, or at least the insertion of the data is never inserted into the database.<br />
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:<br />
<a href="https://github.com/ChuckBell/MySQL_Connector_Arduino/blob/067b345a3a9870d836bcfb89c4f709bf0561c8b8/src/MySQL_Connection.cpp#L87" rel="nofollow ugc">https://github.com/ChuckBell/MySQL_Connector_Arduino/blob/067b345a3a9870d836bcfb89c4f709bf0561c8b8/src/MySQL_Connection.cpp#L87</a></p>
<p dir="auto">So, this must be something related with authentication, although I can connect to the database using <code>mysql</code> command line tool from my laptop.</p>
<p dir="auto">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<br />
The part of the library source code I added the GOT_HERE(); macro is this:</p>
<pre><code>while (retries--)
  {
    Serial.println("...trying...");
    Serial.print("Attempt: ");
    Serial.println(retries);
    connected = client-&gt;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;
  }
</code></pre>
<p dir="auto">I commented where the code reaches and where the code does not reach the macro and also added a couple of Serial.println() lines<br />
Serial Monitor shows this:<br />
<img src="/assets/uploads/files/1724764262794-bb896dd3-ee9b-4179-9ff0-31c133f3280b-image.png" alt="bb896dd3-ee9b-4179-9ff0-31c133f3280b-image.png" class=" img-responsive img-markdown" width="606" height="347" /></p>
]]></description><link>https://www.community.lilygo.cc/topic/1729/help-with-mysql-library-code-to-connect-to-a-remote-database-lilygo-t-sim7600x</link><generator>RSS for Node</generator><lastBuildDate>Sun, 07 Jun 2026 23:33:43 GMT</lastBuildDate><atom:link href="https://www.community.lilygo.cc/topic/1729.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 27 Aug 2024 13:11:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Help with MySQL library code to connect to a remote database (LilyGo T-SIM7600X) on Sat, 07 Sep 2024 05:26:04 GMT]]></title><description><![CDATA[<p dir="auto">It seems like your connection is working fine. If you're testing further, especially for your <a href="https://allrezept.de/auberginen-fatteh-rezept/" rel="nofollow ugc">Food Website</a>, try running a few simple queries from your Debian laptop using <code>mysql</code> to ensure everything works smoothly between machines.</p>
]]></description><link>https://www.community.lilygo.cc/post/2625</link><guid isPermaLink="true">https://www.community.lilygo.cc/post/2625</guid><dc:creator><![CDATA[beck lowis]]></dc:creator><pubDate>Sat, 07 Sep 2024 05:26:04 GMT</pubDate></item></channel></rss>