#include <WiFi.h>
#include <ESP32_MySQL.h>
#define USING_HOST_NAME true
#if USING_HOST_NAME
char server[] = "jlzem.io"; // change to your server's hostname/URL
#else
//IPAddress server(185,173,111,225);
#endif
String query = String("SELECT * from sensores;");
ESP32_MySQL_Connection conn((Client *)&client);
//ESP32_MySQL_Query sql_query = ESP32_MySQL_Query(&conn);
void runQuery() {
ESP32_MySQL_Query query_mem = ESP32_MySQL_Query(&conn);
Serial.println("\nQuery: "+ query + "\n");
if ( !query_mem.execute(query.c_str()) ) {
Serial.println("\nQuery: Erro \n");
return;
}
// Exibe os resultados
// Busca as colunas e as exibe
column_names *cols = query_mem.get_columns();
for (int f = 0; f < cols->num_fields; f++) {
Serial.print(cols->fields[f]->name);
if (f < cols->num_fields - 1) {
Serial.print(",");
}
}
Serial.println("\n--------------------");
// Le as linhas e as exibe
row_values *row = NULL;
do {
row = query_mem.get_next_row();
if (row != NULL) {
for (int f = 0; f < cols->num_fields; f++) {
Serial.print(row->values[f]);
if (f < cols->num_fields - 1) {
Serial.print(",");
}
}
Serial.println();
}
} while (row != NULL);
delay(500);
}
void setup() {
Serial.begin(9600);
while (!Serial) { }
WiFi.begin("Wokwi-GUEST", "");
while(WiFi.status() != WL_CONNECTED) {
delay(250);
}
Serial.println("\n-----------------------------------------------");
Serial.print("Endereco IP: ");
Serial.println(WiFi.localIP());
Serial.println("-----------------------------------------------");
delay(2000);
}
void loop() {
Serial.println("\nConexao: Em Andamento ...");
if (conn.connect(server, 3306, "u907186638_root", "Testes_2024", "u907186638_SmartObject")) {
//if (conn.connectNonBlocking(server, 3306, "u907186638_root", "Testes_2024", "u907186638_SmartObject") != RESULT_FAIL){
delay(500);
Serial.println("\nConexao: Aceita");
runQuery();
conn.close();
} else {
Serial.println("\nConexao: Falhou");
}
Serial.println("\nConexao: Nova em 60 segundos");
delay(60000);
}