Meiner sieht komplett jetzt so aus:
#include <ESP8266WiFi.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
WiFiClient client; // Use this for WiFi instead of EthernetClient
MySQL_Connection conn(&client);
MySQL_Cursor* cursor;
// <= 31 Zeichen
const char *ssid = "SSID";
// >= 8 oder <= 63 Zeichen oder NULL
const char *password = "wlanPwd";
char stmt[256];
//#define REMOTE 1 // wenn RemoteDB einkommentieren
#ifdef REMOTE
char dbuser[] = "rDBUser"; // MySQL user login username
char dbpassword[] = "rDBPwd"; // MySQL user login password
char hostname[] = "rDB-Hostname";
char db[] = "rDBName"; // DB-Name
IPAddress server_ip;
#else
char dbuser[] = "lDBUser";
char dbpassword[] = "lDBPwd";
IPAddress server_ip = {192,168,178,34}; // lokale IP des DB-Servers
char hostname[] = ""; // kein Hostname!
char db[] = "lDBname";
#endif
uint32_t lastMillis;
void printRes(int res) {
if (res==1) Serial.println("OK");
else Serial.println("NOT OK");
}
void setup() {
int counter = 0; // Counter für Verbindung zum WLAN
Serial.begin(115200);
Serial.println("\nStart");
WiFi.persistent(false);
// Betrieb als Station
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay (500);
Serial.print(".");
counter++;
if (counter > 100) {
Serial.println("Kein WLAN. Restart!");
ESP.restart();
}
}
Serial.print("\nConnected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (hostname[0]) { // hostname --> remote
WiFi.hostByName(hostname, server_ip);
Serial.print("Remote-DB: ");
} else Serial.print("Local DB: ");
Serial.println(server_ip);
Serial.println("\nConnecting to database . . .");
if (conn.connect(server_ip, 3306, dbuser, dbpassword)) {
Serial.println("DB-Connection ok.");
cursor = new MySQL_Cursor(&conn);
strcpy(stmt,"use ");
strcat(stmt,db); // Auswahl der Datenbank
int res = cursor->execute(stmt); // 1=ok
Serial.print("Res: ");
printRes(res);
delay(500);
}
else {
Serial.println("DB-Connection failed.");
delay(10000);
ESP.restart();
}
}
void loop() {
}
Gruß Tommy