目前,我正在尝试使用ESP8266和DNS将我的MySQL连接到Azure MySQL数据库。似乎WiFi库本机不支持DNS,只有IP但是Azure不支持静态IP,因此我需要使用DNS。
这是我到目前为止掌握的代码:
#include <WiFi.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <Dns.h>
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char hostname[] = "something.mysql.database.azure.com"; //replace something with database name
IPAddress server_ip;
char user[] = "root"; // MySQL user login username
char password[] = "secret"; // MySQL user login password
// WiFi card example
char ssid[] = "WiFiSSID"; // your SSID
char pass[] = "secret"; // your SSID Password
WiFiClient client; // Use this for WiFi instead of EthernetClient
MySQL_Connection conn((Client *)&client);
void setup() {
Serial.begin(115200);
while (!Serial); // wait for serial port to connect. Needed for Leonardo only
// Begin WiFi section
int status = WiFi.begin(ssid, pass);
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while(true);
}
// print out info about the connection:
else {
Serial.println("Connected to network");
IPAddress ip = WiFi.localIP();
// Particle.process(); Seemingly need to call this for WiFi.dnsServerIP() to be available but gives "out of scope error".
DNSClient dns;
dns.begin(WiFi.dnsServerIP());
dns.getHostByName(hostname, server_ip);
}
// End WiFi section
Serial.println("Connecting...");
if (conn.connect(server_ip, 3306, user, password)) {
delay(1000);
}
else
Serial.println("Connection failed.");
conn.close();
}
void loop() {
}这是我正在犯的错误:
Arduino: 1.8.5 (Windows 10), Board: "SparkFun ESP8266 Thing Dev, 80 MHz, 512K (no SPIFFS), v2 Lower Memory, Disabled, None, Only Sketch, 115200"
C:\Users\Mathi\OneDrive\Documents\Arduino\wifi_hostname_sketchB\wifi_hostname_sketchB.ino: In function 'void setup()':
wifi_hostname_sketchB:65: error: 'class WiFiClass' has no member named 'dnsServerIP'
dns.begin(WiFi.dnsServerIP());
^
Multiple libraries were found for "Ethernet.h"
Used: C:\Users\Mathi\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.4.1\libraries\Ethernet
Not used: D:\Programs\Arduino IDE\Arduino\libraries\Ethernet
exit status 1
'class WiFiClass' has no member named 'dnsServerIP'
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.我尝试过使用Particle.process函数,但是它一直给我一个“超出范围”的错误。大多数人通过修改固件来修复这个问题,但是没有帮助我。
我从阿杜伊诺获得了大部分代码--我遵循了使用WiFi访问MySQL数据库的指南,但是这不包括DNS。
发布于 2018-08-08 21:58:38
不要使用DNSclient,它不会与WiFi一起工作,而是使用WiFi.hostByName。我花了两天才找到这颗宝石。
IPAddress server_ip;
WiFi.hostByName("www.google.com",server_ip);
Serial.println(server_ip); // server_ip will contain the ip address of google.com发布于 2018-06-07 14:26:27
WiFi库WiFiWebClient实例使用主机名连接到web服务器。
char server[] = "www.google.com"; // name address for Google (using DNS)
if (client.connect(server, 80)) {发布于 2018-06-06 09:05:46
由于microsoft只支持DNS,而ESP8266 WiFi库只支持静态IP,因此它们不会一起工作。您可以使用google,因为它支持静态IP。
https://stackoverflow.com/questions/49976592
复制相似问题