hello everyone !
i'm trying to connect directly esp8266 to MySQL database and I used this code:
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <TimeLib.h>
#include <NtpClientLib.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
ESP8266WebServer server ;
WiFiClient client;
MySQL_Connection conn((Client *)&client);
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
uint8_t pin_led = 2;
char* ssid = "dorra";
char* password = "dorradorra";
char* mySsid = "ESP8266_SSID";
char* user = "root"; // MySQL user
char* passwordd = "sogeclair&"; // MySQL password
char query[128];
IPAddress ip(192,168,11,4);
IPAddress gateway(192,168,11,1);
IPAddress subnet(255,255,255,0);
IPAddress server_addr(127,0,0,1); // MySQL server IP
void toggleLED()
{
digitalWrite(pin_led,!digitalRead(pin_led));
server.send(204,"");
}
void setup()
{
pinMode(pin_led, OUTPUT);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid,password);
Serial.begin(115200);
while(WiFi.status()!=WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
WiFi.macAddress(mac_addr);
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
server.begin();
NTP.begin();
server.on("/",{server.send(200,"text/plain","Page web gneree par "+ String(mySsid) + " \n"
"Heure et Date d entree sont : "+NTP.getTimeDateString ());});
server.on("/toggle",toggleLED);
WiFi.softAPConfig(ip, gateway, subnet);
WiFi.softAP(mySsid, password);
Serial.println("Connecting to database...");
while (conn.connect(server_addr, 3306, user, passwordd) != true) {
delay(200);
Serial.print ( "trying to connect..." );
}
Serial.println("");
Serial.println("Connected to SQL Server!");
}
void loop()
{
server.handleClient();
delay(2000);
Serial.println("Recording data.");
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
cur_mem->execute(query);
// Note: since there are no results, we do not need to read any data
// Deleting the cursor also frees up memory used
delete cur_mem;
}
I can connect to My WIFI and set the esp8622 as an access piont at the same time , printing date and time on the webserver is working too but the problem is that i can't connect to MySQL Database .I wrote my provider where I has MySQL Database stored but I don't know why i can not connect to.
So,please cann someone help me with my problem ?
thank you and have a good day