首页 > 解决方案 > 无法使用 arduino 连接到数据库服务器以发送一些数据

问题描述

我正在尝试使用 Arduino 将一些数据发送到我的数据库。MariaDB 安装在我的电脑上。不幸的是,我无法连接。我的第一个疑问是服务器地址应该与我的 PC ip 地址相同吗?有人可以帮忙吗。

if (conn.connect(server_addr, 3306, user, password)) 
    { Serial.println("Connecting to database server...");
      delay(1000);     
      // Initiate the query class instance
      MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);    
      dtostrf(voltage, 6, 2, voltageNew);
      dtostrf(current, 6, 4, currentNew);     
      sprintf(querymessuremnt, INSERT_measurements,voltageNew, currentNew);

      // Execute the query 
      cur_mem->execute(querybuffer);
      delete cur_mem;
      Serial.println("Data recorded.");
    }
    else   
    Serial.println("Connection to DB failed.");
    conn.close(); 
}

标签: mysqldatabasearduino

解决方案


添加什么作为服务器名称取决于您触发您提到的代码的位置。如果您登录到您的 Arduino,您应该使用“localhost”,因为我假设您在 Arduino 上运行 MariaDB。

如果您怀疑存在阻止脚本完全工作的端口问题,请尝试将“localhost”更改为“127.0.0.1”。如果您有防火墙,您是否允许端口 3306 上的数据?

如果您正在从另一台机器上运行代码但想要访问您的 Arduino/MariaDB,那么在该机器上您使用服务器名称 [ip-adress to your arduino machine] 更新您的代码。


推荐阅读