首先,感谢您的点击!我一直在研究Arduino UNO的一个非常特殊的案例,该案例必须通过以太网直接连接到承载MySQL数据库的本地(LAN)Windows Server。任务非常简单,只需从arduino验证该数据库中的一些ID。
最困难的部分是,我绝对不允许访问或修改服务器,这消除了托管微型API / PHP脚本,在本地处理与该PC的MySQL连接并向其发出HTTP请求的希望它。
因此,我决定使用Arduino/MySQL Connector,但正是出于这一目的。但是,经过一些研究,我发现它使用默认的Ethernet
库,但不支持ENC28J60
Mini Eth-Shield。
[进一步的研究使我得出了tiny hint,该Arduino/MySQL Connector
应该使UIPEthernet
与conn.connect(server_addr, 3306, user, password)
库兼容,同时也与上述屏蔽兼容。
[优点是编译错误现在消失了,缺点是Arduino在到达函数时完全挂出,永远不会发出成功或失败消息。甚至陌生人,大约一分钟后,它继续工作并打印此:
Arduino/MySQL Connector
这是代码,#include <UIPEthernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server_addr(192,168,1,8); // IP of the MySQL *server* here
char user[] = "god"; // MySQL user login username
char password[] = "ishallpass"; // MySQL user login password
// Sample query
char query[] = "SELECT nombre FROM database.clientes WHERE id = 1";
EthernetClient client;
MySQL_Connection conn((Client *)&client);
// Create an instance of the cursor passing in the connection
MySQL_Cursor cur = MySQL_Cursor(&conn);
void setup() {
Serial.begin(115200);
while (!Serial); // wait for serial port to connect
Ethernet.begin(mac_addr);
Serial.println("Connecting...");
if (conn.connect(server_addr, 3306, user, password)) {
delay(1000);
}
else
Serial.println("Connection failed.");
}
void loop() {
row_values *row = NULL;
char* nombre_cliente;
delay(1000);
Serial.println("1) Demonstrating using a cursor dynamically allocated.");
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Execute the query
cur_mem->execute(query);
// Fetch the columns (required) but we don't use them.
column_names *columns = cur_mem->get_columns();
// Read the row (we are only expecting the one)
do {
row = cur_mem->get_next_row();
if (row != NULL) {
nombre_cliente = row->values[0];
}
} while (row != NULL);
// Deleting the cursor also frees up memory used
delete cur_mem;
// Show the result
Serial.print(" CLIENTE nombre = ");
Serial.println(nombre_cliente);
delay(500);
Serial.println("2) Demonstrating using a local, global cursor.");
// Execute the query
cur.execute(query);
// Fetch the columns (required) but we don't use them.
cur.get_columns();
// Read the row (we are only expecting the one)
do {
row = cur.get_next_row();
if (row != NULL) {
nombre_cliente = row->values[0];
}
} while (row != NULL);
// Now we close the cursor to free any memory
cur.close();
// Show the result but this time do some math on it
Serial.print(" CLIENTE nombre = ");
Serial.println(nombre_cliente);
Serial.print(" CLIENTE nombre+12 = ");
Serial.println(nombre_cliente+12);
}
默认示例的修改版本。
low dynamic memory warning
[经过一些“游戏测试”后,我设法使它停止显示那些?????????如图所示,现在我遇到了另外两个问题:
"Memory Error"
。Arduino/MySQL Connector
来自串行监视器的消息。 MEMORY_ERROR
完全在下面显示的代码块上触发,位于MySQL_Packet :: read_packet()函数中:
// Check for valid packet.
if (packet_len < 0) {
show_error(PACKET_ERROR, true);
packet_len = 0;
}
buffer = (byte *)malloc(packet_len+4);
if (buffer == NULL) {
show_error(MEMORY_ERROR, true);
return;
}
删除无休止字符串??????上的缓冲区检查结果在串行端口中。
这是更新的代码,在其中我必须调整我的MySQL服务机IP,并稍微减少了内存使用。但是,我不知道如何从3个依赖项中减少看似巨大的内存消耗。
#include <UIPEthernet.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server_addr(192, 168, 1, 26);
char user[] = "god";
char password[] = "ishallpass";
char query[] = "SELECT nombre_cliente FROM database.clientes WHERE id = 1";
EthernetClient client;
MySQL_Connection conn((Client *)&client);
MySQL_Cursor cur = MySQL_Cursor(&conn);
void setup() {
Serial.begin(115200);
while (!Serial);
Ethernet.begin(mac_addr);
Serial.println(F("C"));
if (conn.connect(server_addr, 3306, user, password)) {
delay(1000);
}
else
Serial.println(F("F"));
}
void loop() {
row_values *row = NULL;
char* nombre_cliente;
delay(1000);
Serial.println(F("Q"));
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
cur_mem->execute(query);
column_names *columns = cur_mem->get_columns();
do {
row = cur_mem->get_next_row();
if (row != NULL) {
nombre_cliente = row->values[0];
}
} while (row != NULL);
delete cur_mem;
Serial.print(F("n:"));
Serial.println(nombre_cliente);
delay(500);
cur.close();
}
((我真的真的真的很指望你们。哈哈)²
GORDO COME VERGAAAAAAA Y TRAGA ESPERMAAAAAAA!