ChuckBell / MySQL_Connector_Arduino

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

ERROR: Class requires connected server. #187

Closed Darryl0912 closed 2 years ago

Darryl0912 commented 2 years ago

Connected to server version 5.7.21-1ubuntu1 Sample OK: 29 C, 74 %H Disconnected. Data Saved. Sample OK: 29 C, 73 %H ERROR: Class requires connected server. Data Saved.

May I know why am I facing this error, how can I solve this problem?

Here is my Arduino coding

include

include

include

include

const char ssid[] = "vivo 1919";// change to your WIFI SSID const char password[] = "11597829ab";// change to your WIFI Password

IPAddress server_addr(160,16,84,67); // change to you server ip, note its form split by "," not "." //const char server_addr = "160.16.84.67"; int MYSQLPort = 3306; //mysql port default is 3306 char user[] = "gpbl2203";// Your MySQL user login username(default is root),and note to change MYSQL user root can access from local to internet(%) char pass[] = "MaCDMRwN:;#~UKE";// Your MYSQL password

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

int pinDHT11 = 4 ; // remmeber to change you connection pin on the ESP32 SimpleDHT11 dht11(pinDHT11); void setup() { Serial.begin(115200); delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid);

WiFi.begin(ssid, password); //try to connect to WIFI while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP());

//try to connect to mysql server if (conn.connect(server_addr, MYSQLPort, user, pass)) { delay(1000); } else { Serial.println("Connection failed."); } delay(2000);

}

void loop() {

byte temperature = 0; byte humidity = 0; int err = SimpleDHTErrSuccess; if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { Serial.print("Read DHT11 failed, err="); Serial.println(err); delay(1000); return; } Serial.print("Sample OK: "); Serial.print((int)temperature); Serial.print(" *C, "); Serial.print((int)humidity); Serial.println(" %H");

String INSERT_SQL = "INSERT INTO db_gpbl2203.datalog (temp,humd) VALUES ('" + String((int)temperature) + "','" + String((int)humidity) + "')"; MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); cur_mem->execute(INSERT_SQL.c_str());//execute SQL delete cur_mem; conn.close();// close the connection Serial.println("Data Saved."); delay(3000); }

ChuckBell commented 2 years ago

You are closing the connection in the loop. Remove that statement and you should have success.

On Feb 24, 2022, at 10:48 PM, Darryl0912 @.***> wrote:

Reopened #187.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.

Darryl0912 commented 2 years ago

You are closing the connection in the loop. Remove that statement and you should have success. On Feb 24, 2022, at 10:48 PM, Darryl0912 @.***> wrote: Reopened #187. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.

Thank you so much and really appreciate your quick reply, have a nice day ahead 😊❤️