Navigation

    LILYGO

    • Register
    • Login
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    1. Home
    2. PsySc0rpi0n
    • Profile
    • Following 0
    • Followers 0
    • Topics 2
    • Posts 2
    • Best 0
    • Groups 0

    PsySc0rpi0n

    @PsySc0rpi0n

    0
    Reputation
    1
    Profile views
    2
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    PsySc0rpi0n Unfollow Follow

    Latest posts made by PsySc0rpi0n

    • 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) using mysql 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#L87

      So, 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:
      bb896dd3-ee9b-4179-9ff0-31c133f3280b-image.png

      posted in Technical Discussion
      PsySc0rpi0n
      PsySc0rpi0n
    • Suggestion of product

      Hello.
      I'm new to LiLyGo products as the list is long.

      I would like to have some suggestions of products, according to the following needs for my project.

      1 - Need Wireless communication (to send data to a database)
      2 - Need GSM/GPRS communication for the same purpose as 1, in case 1 fails due poor connectivity.
      3 - Need GPS, for retrieving coordinates
      4 - Need at least, about 24h of battery capacity or a connection to a solar cell to be able to charge the battery if possible
      5 - If possible, no LCD to save in energy consumption

      The project is to collect data from some sensors connected to this device and send the collectecd data to a central database.

      This is a pilot project so it needs also the capability to scale up to multiple devices collecting data and send the data to the same database.

      The devices will be on the street, so, the cheaper the better, in case they are stolen (I have some ideas to prevent this though).

      I've been browsing the website but as the list is long I'm having some trouble to narrow down the list!

      Thanks
      Psy

      posted in Product information
      PsySc0rpi0n
      PsySc0rpi0n