【发布时间】:2015-12-30 08:18:21
【问题描述】:
我有一个非常简单的 Arduino 项目,我正在努力解决。
我想使用带有以太网屏蔽的 Arduino 更新 MySQL 中的条目。我正在为 MySQL 服务器使用 WAMP。最终,我想每 5 分钟将温度读数发布到 mysql 服务器,但这不是我目前关心的问题。我不是 Arduino 或 MySQL 的老手,所以我想问问专业人士。
WAMP IP 地址:192.168.0.89
Arduino IP地址:192.168.0.177
MySQL 数据库。
数据库用户名:“root”
数据库密码:""
数据库名称:“wordpress”
数据库表:“读数”
要更新的表字段:“voltage1”
查询:
"更新读数 SET voltage1='12v' WHERE device_id=T1;"
Arduino 草图: (获取自:https://launchpad.net/mysql-arduino)
#include "SPI.h"
#include "Ethernet.h"
#include "sha1.h"
#include "mysql.h"
byte mac_addr[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x69, 0xAC };
byte ip_addr[] = { 192, 168, 0, 177 };
byte dns_addr[] = { 192, 168, 0, 37 };
byte gateway_addr[] = { 192, 168, 0, 37 };
byte netmask[] = { 255, 255, 255, 0 };
IPAddress server_addr(192, 168, 0, 89);
Connector my_conn; // The Connector/Arduino reference
char user[] = "root";
char password[] = ""; //the credentials are correct in my code
char INSERT_SQL[] = "UPDATE readings SET voltage1='99' WHERE device_id=T1;";
void setup() {
Serial.begin(115200);
Ethernet.begin(mac_addr, ip_addr, dns_addr, gateway_addr, netmask); //Yes, I know this is way more than necessary, but just to play it safe
delay(1000);
Serial.print("IP: ");
Serial.println(Ethernet.localIP()); // debugging
Serial.println("Connecting...");
if (my_conn.mysql_connect(server_addr, 3306, user, password)) //connect to database
{
delay(500);
my_conn.cmd_query(INSERT_SQL); //Possible problem here, i want to update not insert.
Serial.println("Query Success!");
}
else
Serial.println("Connection failed.");
}
void loop() {
}
串行监视器:
IP:192.168.0.177
正在连接...
它永远不会连接。
【问题讨论】:
-
我不明白为什么我们还在担心更新部分,而您甚至无法连接...