2022-12-17 13:44:28 [显示全部楼层]
3617浏览
查看: 3617|回复: 2

[求助] esp8266+mysql的问题

[复制链接]
我最近在使用esp8266的时候 打算和本机的mysql数据库产生互联
我使用了Arduino的sql库
esp8266+mysql的问题图1
然后使用实例代码
/*
  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里面创建好对应的表
esp8266+mysql的问题图2
ip地址也查到了
esp8266+mysql的问题图3
为了防止是我IP不是内网的原因 我还使用了同一WiFi下的IP地址尝试
esp8266+mysql的问题图4
但是我还是连接不上
esp8266+mysql的问题图5
我使用的arduino版本是1.8.1
esp8266 版本是1.0.0
MYSQL Connector Arduino 的库版本是1.2.0
电脑安装的sql版本是5.7.36
希望您们可以帮助我
SatDecember-202212174845..png

三春牛-创客  初级技神

发表于 2023-1-12 10:50:45

回帖奖励 +1 创造力

看不懂......
回复

使用道具 举报

花生编程  中级技匠

发表于 2023-1-27 14:03:36

不知道
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
关于楼主

楼主的其它帖子

上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail