使用 DNS 和 WiFi 将 Arduino 连接到远程 MySQL 数据库

Connecting Arduino to a remote MySQL database using DNS and WiFi

我目前正在尝试使用 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[] = { 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.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 函数,但它一直给我一个 'Out of scope' 错误。大多数人通过更改固件解决了这个问题,但没有帮助我。

我从 https://github.com/ChuckBell/MySQL_Connector_Arduino 获得了大部分代码 我按照指南使用 WiFi 访问 MySQL 数据库,但这不包括 DNS。

由于微软Azure只支持DNS,而ESP8266 WiFi库只支持静态IP,所以两者不能协同工作。您可以改用 google 云,因为它支持静态 IP。

WiFi 库 WiFiWebClient example 使用主机名连接到网络服务器。

char server[] = "www.google.com";    // name address for Google (using DNS)
if (client.connect(server, 80)) {

不要使用 DNSclient,它不适用于 WiFi,请改用 WiFi.hostByName。我花了 2 天时间才找到这个 gem..

IPAddress server_ip;
WiFi.hostByName("www.google.com",server_ip);
Serial.println(server_ip); // server_ip will contain the ip address of google.com