我正在尝试使用 esp8266 mcu 连接到 MySQL。它在其串行端口中接收一个 json 文档,将其反序列化并将其发送到 sql。就目前情况而言,我无法与 mysql 建立通信。我已经检查并仔细检查了登录凭据,还在服务器端将 mcu 的 IP 地址列入了白名单。
#include "defines.h"
#include "Credentials.h"
#include <MySQL_Generic.h>
#include <ArduinoJson.h>
#define USING_HOST_NAME false
#if USING_HOST_NAME
// Optional using hostname, and Ethernet built-in DNS lookup
char server[] = "your_account.ddns.net"; // change to your servers hostname/URL
#else
IPAddress server(34,30,245,67);
#endif
uint16_t server_port = 3306; //3306;
char default_database[] = "submon";
char default_table1[] = "feeder_states";
char default_table2[] = "btu_params";
MySQL_Connection conn((Client *)&client);
MySQL_Query *query_mem;
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000); // wait for serial port to connect
MYSQL_DISPLAY1("\nStarting Basic_Insert_WiFi on", BOARD_NAME);
MYSQL_DISPLAY(MYSQL_MARIADB_GENERIC_VERSION);
// Remember to initialize your WiFi module
#if ( USING_WIFI_ESP8266_AT || USING_WIFIESPAT_LIB )
#if ( USING_WIFI_ESP8266_AT )
MYSQL_DISPLAY("Using ESP8266_AT/ESP8266_AT_WebServer Library");
#elif ( USING_WIFIESPAT_LIB )
MYSQL_DISPLAY("Using WiFiEspAT Library");
#endif
// initialize serial for ESP module
EspSerial.begin(115200);
// initialize ESP module
WiFi.init(&EspSerial);
MYSQL_DISPLAY(F("WiFi shield init done"));
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD)
{
MYSQL_DISPLAY(F("WiFi shield not present"));
// dont continue
while (true);
}
#endif
// Begin WiFi section
MYSQL_DISPLAY1("Connecting to", ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
MYSQL_DISPLAY0(".");
}
// print out info about the connection:
MYSQL_DISPLAY1("Connected to network. My IP address is:", WiFi.localIP());
MYSQL_DISPLAY3("Connecting to SQL Server @", server, ", Port =", server_port);
MYSQL_DISPLAY5("User =", user, ", PW =", password, ", DB =", default_database);
}
void runInsert()
{
if (Serial.available() > 0)
{
String jsonDocument = Serial.readStringUntil('\n');
Serial.print("data received");
// Parse the JSON document
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, jsonDocument);
// Extract values from JSON document
int feeder1 = doc["feeder1"];
int feeder2 = doc["feeder2"];
int feeder3 = doc["feeder3"];
int feeder4 = doc["feeder4"];
int feeder5 = doc["feeder5"];
int feeder6 = doc["feeder6"];
int feeder7 = doc["feeder7"];
int feeder8 = doc["feeder8"];
int feeder9 = doc["feeder9"];
int feeder10 = doc["feeder10"];
int feeder11 = doc["feeder11"];
int branch_id = doc["branch_id"];
int company_id = doc["company_id"];
float BatteryVoltage = doc["BatteryVoltage"];
float BatteryCurrent = doc["BatteryCurrent"];
bool DCEarthFault = doc["DCEarthFault"];
if (error)
{
MYSQL_DISPLAY("JSON parsing failed!");
return;
}
// Initiate the query class instance
MySQL_Query query_mem = MySQL_Query(&conn);
delay(10000);
if (conn.connected())
{
String INSERT_SQL1 = String("INSERT INTO ") + default_database + "." + default_table1
+ "(branch_id, company_id, feeder1,feeder2, feeder3, feeder4, feeder5, feeder6, feeder7, feeder8, feeder9, feeder10, feeder11) VALUES ('" + branch_id + "','" + company_id + "','" + feeder1 + "', '" + feeder2 + "', '" + feeder3 + "', '" + feeder4 + "', '" + feeder5 + "', '" + feeder6 + "', '" + feeder7 + "', '" + feeder8 + "', '" + feeder9 + "', '" + feeder10 + "', '" + feeder11 + "')";
String INSERT_SQL2 = String("INSERT INTO ") + default_database + "." + default_table2
+ "(BatteryVoltage, BatteryCurrent, DCEarthFault,company_id, branch_id) VALUES ('" + BatteryVoltage + "','" + BatteryCurrent + "', '" + DCEarthFault + "', '" + company_id + "', '" + branch_id + "' )";
// Execute the first query
MYSQL_DISPLAY(INSERT_SQL1);
Serial.print("connection to db successful");
if ( !query_mem.execute(INSERT_SQL1.c_str()) )
{
MYSQL_DISPLAY("Insert error in Table 1");
}
else
{
MYSQL_DISPLAY("Data Inserted in Table 1.");
}
// Execute the second query
MYSQL_DISPLAY(INSERT_SQL2);
if ( !query_mem.execute(INSERT_SQL2.c_str()) )
{
MYSQL_DISPLAY("Insert error in Table 2");
}
else
{
MYSQL_DISPLAY("Data Inserted in Table 2.");
}
// Close the connection
conn.close();
}
else
{
MYSQL_DISPLAY("Error: Could not connect to SQL Server.");
}
}
}
void loop()
{
runInsert();
}
串行监视器