非常简单的 Arduino 更新 SQL 条目

Very Simple Arduino Update SQL Entry

我有一个非常简单的 Arduino 项目,我正在努力。
我想使用带有以太网屏蔽的 Arduino 更新 MySQL 中的一个条目。我正在为 MySQL 服务器使用 WAMP。最后,我想每 5 分钟 post 向 mysql 服务器读取一次温度读数,但这不是我目前关心的问题。我不是 Arduino 或 MySQL 的老手,所以我想我问专业人士。

WAMP IP 地址:192.168.0.89
Arduino IP地址:192.168.0.177

MySQL 数据库。
数据库用户名:"root"
数据库密码:“”
数据库名称:"wordpress"
数据库 Table:"readings"
Table 要更新的字段:"voltage1"
查询:

"UPDATE readings 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
Connecting...

它永远连接不上。

我可以告诉你,你的SQL可能是错误的。

UPDATE readings SET voltage1='12v' WHERE device_id=T1;

应该是

UPDATE readings SET voltage1='12v' WHERE device_id='T1';

抱歉,我不知道连接等其他问题。

关于

//Possible problem here, i want to update not insert.

INSERT_SQL 只是一个变量的名称,您可以使用 BOB 作为名称,它应该仍然有效,将其重命名为 QUERY 并放松。

官方的例子和你的代码很像:http://bazaar.launchpad.net/~chuck-bell/mysql-arduino/trunk/view/head:/examples/dht22_sensor_node.cpp

另外,您确定服务器上的端口 3306 已打开吗?

我相信有一种安全措施可以防止除 localhost 以外的所有人执行 运行 mysql 命令。

应打开端口 + mysql 应允许来自非 127.0.0.1 的 IP 的连接。