使用DNS和WiFi将Arduino连接到远程MySQL数据库
我目前正在尝试使用WiFi和DNS将我的ESP8266连接到Azure MySQL数据库。看起来WiFi库不支持DNS,只有IP,但是Azure不支持静态IP,因此我需要使用DNS 这是我目前掌握的代码:使用DNS和WiFi将Arduino连接到远程MySQL数据库,mysql,dns,arduino,wifi,esp8266,Mysql,Dns,Arduino,Wifi,Esp8266,我目前正在尝试使用WiFi和DNS将我的ESP8266连接到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
#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() {
}
我尝试过使用Particle.process函数,但它总是给我一个“超出范围”的错误。大多数人通过改变固件修复了这个问题,但没有帮助我
我大部分代码都是从
我按照指南使用WiFi访问MySQL数据库,但这不包括DNS。因为microsoft Azure只支持DNS,而ESP8266 WiFi库只支持静态IP,所以它们不能一起工作。你可以改用谷歌云,因为它支持静态IP WiFi库使用主机名连接到web服务器
char server[] = "www.google.com"; // name address for Google (using DNS)
if (client.connect(server, 80)) {
不要使用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
也许只要试试WiFi.hostByName(主机名、服务器ip)获取服务器IP?我将在何处实现此功能?这不是正确答案。ESP8266 Wifi库不支持DNS。见下面王克的答案。@JohnSmith,不是这个。函数WiFiClient.connect()可以将服务器名称作为参数,然后解析名称“幕后”。看到我的答案了吗
IPAddress server_ip;
WiFi.hostByName("www.google.com",server_ip);
Serial.println(server_ip); // server_ip will contain the ip address of google.com