我正在尝试使用 Arduino 将一些数据发送到我的数据库。 MariaDB 安装在我的电脑上。不幸的是,我无法连接。我的第一个疑问是服务器地址应该与我的电脑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,那么您可以在该计算机上使用服务器名称 [arduino 计算机的 IP 地址] 更新代码。
关于mysql - 无法使用arduino连接到数据库服务器发送一些数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56473925/