ChuckBell / MySQL_Connector_Arduino

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

Conectivity Error using UIPEthernet (ENC28J60) #135

Closed Pra3t0r5 closed 2 years ago

Pra3t0r5 commented 4 years ago

Full stack-overflow post

Transcript:

Issues

  1. Incompatibility with EtherCard ENC28J60 Library (Fixed changing my dependency and MySQL_Packet dependency for UIPEthernet. Suggested HERE )
  2. A low dynamic memory warning from Arduino IDE compiler.
  3. A Memory Error message in the Serial Monitor coming from the Arduino/MySQL Connector.
  4. Connectivity problems to the database over static TCP protocol. (see this comment below)

The MEMORY_ERROR is triggered exactly at the code block shown below, located in MySQL_Packet::read_packet() function:

 // Check for valid packet.
  if (packet_len < 0) {
    show_error(PACKET_ERROR, true);
    packet_len = 0;
  }
  buffer = (byte *)malloc(packet_len+4);
  if (buffer == NULL) {
    show_error(MEMORY_ERROR, true);
    return;
  }

Hardware

Database (to connect)

Expected Behavior

Code

#include <UIPEthernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server_addr(192, 168, 1, 26);
char user[] = "god";
char password[] = "ishallpass";
char query[] = "SELECT nombre_cliente FROM arduinokinap.clientes WHERE id = 1";

EthernetClient client;
MySQL_Connection conn((Client *)&client);
MySQL_Cursor cur = MySQL_Cursor(&conn);

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Ethernet.begin(mac_addr);
  Serial.println(F("C"));
  if (conn.connect(server_addr, 3306, user, password)) {
    delay(1000);
  }
  else
    Serial.println(F("F"));
}

void loop() {
  row_values *row = NULL;
  char* nombre_cliente;
  delay(1000);
  Serial.println(F("Q"));
  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  cur_mem->execute(query);
  column_names *columns = cur_mem->get_columns();
  do {
    row = cur_mem->get_next_row();
    if (row != NULL) {
      nombre_cliente = row->values[0];
    } else {      
      Serial.println(F("E"));
    }
  } while (row != NULL);
  delete cur_mem;
  Serial.print(F("n:"));
  Serial.println(nombre_cliente);
  delay(500);
  cur.close();
}
Pra3t0r5 commented 4 years ago

I've decided to add these lines to MySQL_Packet.cpp in key places show_error(packet_len, true); show_error(buffer, true); And now i'm getting this behavior in serial monitor:

19:07:18.166 -> ⸮⸮0⸮0⸮0⸮0⸮0⸮0⸮0⸮0⸮0⸮0⸮0⸮0⸮0⸮0⸮0⸮⸮'⸮0⸮x'⸮R'⸮0⸮0⸮0⸮0⸮0⸮0ERROR: Timeout waiting for client.
19:07:18.199 -> ⸮0ERROR: Timeout waiting for client.
19:07:18.199 -> Memory error.

This is the modified code:

while (avail_bytes < packet_len)
  {
    avail_bytes = wait_for_client();
  }
  // Check for valid packet.
  if (packet_len < 0)
  {
    show_error(PACKET_ERROR, true);
    packet_len = 0;
  }
  buffer = (byte *)malloc(packet_len + 4);
  show_error(buffer, true);
  if (buffer == NULL)
  {
    show_error(packet_len, true);
    show_error(MEMORY_ERROR, true);
    return;
  }
Pra3t0r5 commented 4 years ago

I've been tweaking the program and libraries a bit more, setting UIPEthernet to disable UDP and beginning the program connection as static saved a lot of memory.

Sketch uses 19182 bytes (59%) of program storage space. Maximum is 32256 bytes. Global variables use 1346 bytes (65%) of dynamic memory, leaving 702 bytes for local variables. Maximum is 2048 bytes.

However, now that the program flows without getting stucked, i'm clearly facing a connectivity problem.

18:00:47.074 -> Connecting
18:00:47.074 -> ...trying...
18:01:12.049 -> ...got: 0 retrying...
18:01:12.546 -> ...trying...
18:01:37.607 -> ...got: 0 retrying...
18:01:38.104 -> ...trying...
18:02:03.165 -> ...got: 0 retrying...
18:02:03.661 -> Connection Failed

I've tested the connection in every possible way from other devices in LAN and it works perfectly.

Current state of the program:

#include <UIPEthernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server_addr(192, 168, 1, 8);
char user[] = "god";
char password[] = "ishallpass";
char default_db[] = "arduinokinap";
char query[] = "SELECT nombre_cliente FROM arduinokinap.clientes WHERE id = 1";

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

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Ethernet.begin(mac_addr, server_addr);
  Serial.println(F("Connecting"));
  if (conn.connect(server_addr, 3306, user, password, default_db)) {
    delay(1000);
    row_values *row = NULL;
    char* nombre_cliente;
    delay(1000);
    Serial.println(F("Querying"));
    MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
    cur_mem->execute(query);
    column_names *columns = cur_mem->get_columns();
    do {
      row = cur_mem->get_next_row();
      if (row != NULL) {
        nombre_cliente = row->values[0];
      } else {
        Serial.println(F("Error querying"));
      }
    } while (row != NULL);
    delete cur_mem;
    Serial.print(F("name: "));
    Serial.println(nombre_cliente);
    delay(500);
  }
  else
    Serial.println(F("Connection Failed"));
}

void loop() {
}
ChuckBell commented 2 years ago

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