ChuckBell / MySQL_Connector_Arduino

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

cur_mem->execute(query) sometimes freezes #79

Closed axenov048 closed 5 years ago

axenov048 commented 5 years ago

Hello. Sketch is uploaded to NodeMCU. Everything works fine, but at some point the code stops being executed and stops at the line cur_mem-> execute (query). NodeMCU on the wifi network continues to be displayed, but no data is sent to the database. This can happen in an hour, in two hours, in a day, in three days. Please tell me what could be the problem. Thanks you.

Devices: -> NodeMCU - HX711 - load cell Power supply: 5v 2a charger from samsung, tried to connect to a powerbank

curmem->execute(query); <-- sometimes freezes_

#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include "HX711.h"

float calibration_factor = -3.79;          // калибровка!
float ounces;
float tare = 681.501;                           //Собственный вес 
float weight;
float h;
long lastMsg = 0;
const char* id_tbl = "value";

//WiFi setting
const char* ssid = "mywifi";
const char* password_wifi = "passwifi";

//MySQL database
char user[] = "db_user";                                // MySQL user login username
char password[] = "db_password";               // MySQL user login password
char hostname[] = "db_hostname";              // change to your server's hostname/URL

// SQL запрос
 char INSERT_SQL[] = "INSERT INTO f0256568_ovoshi.%s (scale, drain) VALUES (%05.3f, %05.3f)";
 char query[128];

WiFiClient client;
MySQL_Connection conn((Client *)&client);
HX711 scale;

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Подключение к ");
  Serial.println(ssid);

  WiFi.begin(ssid, password_wifi);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi подключен");
  Serial.println("IP адрес: ");
  Serial.println(WiFi.localIP());
  setup_db();
}

void setup_db() {
  IPAddress server_ip;

  WiFi.hostByName(hostname, server_ip);
  Serial.println(server_ip);
  Serial.println("Подключение к базе данных");

  while (conn.connect(server_ip, 3306, user, password) != true) {
    delay(200);
    Serial.print ( "." );
  }

  Serial.println("");
  Serial.println("Соединение с SQL сервером установлено!");   
}

void setup() {
  Serial.begin(115200);   
  setup_wifi();
  scale.begin(D1, D2);
  scale.set_scale();
  scale.set_scale(calibration_factor);       //Применяем калибровку                        
}

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    delay(500);
    setup_wifi();
  }

  long now = millis(); 
  if (now - lastMsg > 60000) {
    lastMsg = now;

    weight = scale.get_units(50), 1;                          //среднее показания
    weight = (weight * 0.035274 - tare) / 1000;       //перевод унций в граммы

    Serial.print("Вес: ");
    Serial.println(weight);   

    h = weight + 1;        
    Serial.print("Дренаж: ");
    Serial.println(h);

    sprintf(query, INSERT_SQL, id_tbl, weight, h);
     Serial.println("Запись данных в базу.");
      Serial.println(query);

    MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
    cur_mem->execute(query); // <-- sometimes freezes
    delete cur_mem;   
  }
}
ChuckBell commented 5 years ago

The most likely cause is running out of memory. You should try reducing the complexity of your sketch and reducing the number of variables used and be sure you clean up any allocated memory.

Another possibility is loosing connectivity to the server. One way to treat this is to move the connect/disconnect to the loop() function. See the docs for an example of how to do this.

On Thu, Jan 3, 2019 at 10:42 axenov048 notifications@github.com wrote:

Hello. Sketch is uploaded to NodeMCU. Everything works fine, but at some point the code stops being executed and stops at the line cur_mem-> execute (query). NodeMCU on the wifi network continues to be displayed, but no data is sent to the database. This can happen in an hour, in two hours, in a day, in three days. Please tell me what could be the problem. Thanks you.

Devices: -> NodeMCU - HX711 - load cell Power supply: 5v 2a charger from samsung, tried to connect to a powerbank cur_mem->execute(query); <-- sometimes freezes

include

include

include

include

include "HX711.h"

float calibration_factor = -3.79; // калибровка!

float ounces;

float tare = 681.501; //Собственный вес

float weight;

float h;

long lastMsg = 0;

const char* id_tbl = "value";

//WiFi setting

const char* ssid = "mywifi";

const char* password_wifi = "passwifi";

//MySQL database

char user[] = "db_user"; // MySQL user login username

char password[] = "db_password"; // MySQL user login password

char hostname[] = "db_hostname"; // change to your server's hostname/URL

// SQL запрос

char INSERT_SQL[] = "INSERT INTO f0256568_ovoshi.%s (scale, drain) VALUES (%05.3f, %05.3f)";

char query[128];

WiFiClient client;

MySQL_Connection conn((Client *)&client);

HX711 scale;

void setup_wifi() {

delay(10);

// We start by connecting to a WiFi network

Serial.println();

Serial.print("Подключение к ");

Serial.println(ssid);

WiFi.begin(ssid, password_wifi);

while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

}

Serial.println("");

Serial.println("WiFi подключен");

Serial.println("IP адрес: ");

Serial.println(WiFi.localIP());

setup_db();

}

void setup_db() {

IPAddress server_ip;

WiFi.hostByName(hostname, server_ip);

Serial.println(server_ip);

Serial.println("Подключение к базе данных");

while (conn.connect(server_ip, 3306, user, password) != true) {

delay(200);

Serial.print ( "." );

}

Serial.println("");

Serial.println("Соединение с SQL сервером установлено!");

}

void setup() {

Serial.begin(115200);

setup_wifi();

scale.begin(D1, D2);

scale.set_scale();

scale.set_scale(calibration_factor); //Применяем калибровку

}

void loop() {

if (WiFi.status() != WL_CONNECTED) {

delay(500);

setup_wifi();

}

long now = millis();

if (now - lastMsg > 60000) {

lastMsg = now;

weight = scale.get_units(50), 1;                          //среднее показания

weight = (weight * 0.035274 - tare) / 1000;       //перевод унций в граммы

Serial.print("Вес: ");

Serial.println(weight);

h = weight + 1;

Serial.print("Дренаж: ");

Serial.println(h);

sprintf(query, INSERT_SQL, id_tbl, weight, h);

 Serial.println("Запись данных в базу.");

  Serial.println(query);

MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);

cur_mem->execute(query); // <-- sometimes freezes

delete cur_mem;

}

}

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

axenov048 commented 5 years ago

Thanks @ChuckBell. now changed the loop a bit. Now every minute there is a connection to the database, sending data and closing the connection. Let's see what happens. next step I will try to reduce the number of variables.

axenov048 commented 5 years ago

One way to treat this is to move the connect/disconnect to the loop() .

It helped. Thank.