我正在尝试使用Arduino将一些数据发送到我的数据库。MariaDB 已安装在我的 PC 中。不幸的是,我无法连接。我的第一个疑问是服务器地址应该与我的 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();
}
添加什么作为服务器名称,取决于触发上述代码的位置。如果您登录到Arduino,则应使用"localhost",因为我假设您在Arduino上运行MariaDB。
如果您怀疑存在阻止脚本完全工作的端口问题,请尝试将"localhost"更改为"127.0.0.1"。如果您有防火墙,是否允许端口 3306 上的数据?
如果您从另一台机器运行代码,但想要访问您的 Arduino/MariaDB,那么在该机器上,您可以使用服务器名称 [ip-aaddress 到 arduino 机器] 更新代码。