kJ1DqfrZxZoC 发表于 2022-12-17 13:44:28

esp8266+mysql的问题

我最近在使用esp8266的时候 打算和本机的mysql数据库产生互联
我使用了arduino的sql库

然后使用实例代码
/*
MySQL Connector/Arduino Example : connect by wifi

This example demonstrates how to connect to a MySQL server from an
Arduino using an Arduino-compatible Wifi shield. Note that "compatible"
means it must conform to the Ethernet class library or be a derivative
with the same classes and methods.

For more information and documentation, visit the wiki:
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.

INSTRUCTIONS FOR USE

1) Change the address of the server to the IP address of the MySQL server
2) Change the user and password to a valid MySQL user and password
3) Change the SSID and pass to match your WiFi network
4) Connect a USB cable to your Arduino
5) Select the correct board and port
6) Compile and upload the sketch to your Arduino
7) Once uploaded, open Serial Monitor (use 115200 speed) and observe

If you do not see messages indicating you have a connection, refer to the
manual for troubleshooting tips. The most common issues are the server is
not accessible from the network or the user name and password is incorrect.

Created by: Dr. Charles A. Bell
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>         // Use this for WiFi instead of Ethernet.h
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>

IPAddress server_addr(192,168,1,5);// IP of the MySQL *server* here
char user[] = "root";            // MySQL user login username
char password[] = "123456";      // MySQL user login password

// Sample query
char INSERT_SQL[] = "INSERT INTO arduino_test.hello (message) VALUES ('Hello, Arduino!')";

// WiFi card example
char ssid[] = "CU_md5k";         // your SSID
char pass[] = "g7yy7h63";   // your SSID Password

WiFiClient client;               // Use this for WiFi instead of EthernetClient
MySQL_Connection conn(&client);
MySQL_Cursor* cursor;

void setup()
{
Serial.begin(9600);
while (!Serial); // wait for serial port to connect. Needed for Leonardo only

// Begin WiFi section
Serial.printf("\nConnecting to %s", ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
}

// print out info about the connection:
Serial.println("\nConnected to network");
Serial.print("My IP address is: ");
Serial.println(WiFi.localIP());

Serial.print("Connecting to SQL...");
if (conn.connect(server_addr, 3306, user, password))
    Serial.println("OK.");
else
    Serial.println("FAILED.");

// create MySQL cursor object
cursor = new MySQL_Cursor(&conn);
}

void loop()
{
if (conn.connected())
    cursor->execute(INSERT_SQL);

delay(5000);
}

我已经在mysql里面创建好对应的表

ip地址也查到了

为了防止是我IP不是内网的原因 我还使用了同一WiFi下的IP地址尝试

但是我还是连接不上

我使用的arduino版本是1.8.1
esp8266 版本是1.0.0
MYSQL Connector Arduino 的库版本是1.2.0
电脑安装的sql版本是5.7.36
希望您们可以帮助我

三春牛-创客 发表于 2023-1-12 10:50:45

看不懂......

花生编程 发表于 2023-1-27 14:03:36

不知道{:7_224:}
页: [1]
查看完整版本: esp8266+mysql的问题