使用dns和wifi将arduino连接到远程mysql数据库

tgabmvqs  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(654)

我正在尝试使用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\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函数,但它总是给我一个“超出范围”的错误。大多数人通过改变固件修复了这个问题,但没有帮助我。
我大部分代码都是从https://github.com/chuckbell/mysql_connector_arduino 我按照指南使用wifi访问mysql数据库,但是这不包括dns。

esbemjvw

esbemjvw1#

不要使用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
mbzjlibv

mbzjlibv2#

wifi库wifiwebclient示例使用主机名连接到web服务器。

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

6ju8rftf3#

由于microsoft azure只支持dns,而esp8266 wifi库只支持静态ip,因此它们无法协同工作。你可以改用googlecloud,因为它支持静态ip。

相关问题