arduino如何调用mysql,【 实测可用 】Arduino 直接访问 mysql
发布日期:2021-06-24 12:27:19 浏览次数:3 分类:技术文章

本文共 2824 字,大约阅读时间需要 9 分钟。

搜啊搜,终于找到可用的 Arduino 直接访问 mysql 方案, 简单易行,摆脱对于各种 MQTT IOT 平台的依赖。目前暂时先考虑实现, 安全什么的以后完善吧:

测试环境:

1.Arduino UNO + W5100 (有空再测试 ESP8266 ESP32 )

2.Arduino IDE1.8.10

3.Arduino 在线安装库 mysql.h (几天不见,再次在另外一台电脑搭建环境,发现下载的库居然变了,没细看,估计基本思路是对的,可能变量什么的需要改一下,再说再说。。。)

4.mysql-4.1.22-win32-Setup (这个大坑,必须老版本的才行,新的8.0连接不上,估计是arduino 库 的安全设置没搞定,等等党就等大神们)

测试步骤:

-----------------------------------------PC端 操作-----------------------------------------------------

1.本地电脑安装mysql 4.0 。 windows10_x64_pro 实测没问题,也不用改防火墙什么的。

2.MySQL命令行进行配置,估计有图形化的,就几句命令,懒得去找了。 看到如下mysql> 提示符 就成功了第一步

Enter password: *****

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 15 to server version: 4.1.22-community-nt

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>

3.创建一个实例

mysql> create database IOT default character set utf8;

4.创建一个表

mysql> CREATE TABLE `DHT22`(`D_time` varchar(20) ,`D_temperature` varchar(20) ) charset=utf8;

5.写点数据进表

mysql> INSERT INTO dht22 VALUES ('2020-09-11','29.25℃');

mysql> INSERT INTO dht22 VALUES ('2020-09-11','29.25℃');

6.查询看看

mysql> select * from iot.dht22;

+------------+---------------+

| D_time | D_temperature |

+------------+---------------+

| 2020-09-11 | 29.25℃ |

| 2020-09-11 | 29.25℃ |

| 2020-09-11 | 29.25℃ |

| 2020-09-11 | 29.25℃ |

| 2020-09-11 | 29.25℃ |

+------------+---------------+

5 rows in set (0.00 sec)

正式开始前可以清除表里的内容(删除表是 DROP TABLE xxx)

delete from dht22;

------------------------------------------------Arduino----------------------------------------------------

上传arduino程序, UNO程序把数据写入, select * from iot.dht22; 会看到越来越多的条目在增加, 以后就可以上传 传感器的读数啦。

附 Arduino 程序:例程简单修改而成, 有些库没有在线安装即可,估计也有用不上的比如sha1.h, 可能原例程里其他语句需要

目前遇到小问题:

℃ arduino存进表里乱码,但从PC存进表里不是乱码。。。

加入 NTPClient.h , 可以从网络获取时间。。。不过串口输出的时间显示和写进mysql 的时间不一样,等调好了再贴程序吧

SQL 函数 now() 直接就可以生成时间戳, 所以NTPClient.h 不用了

#include

#include

#include

#include

#include

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress server_addr(10,176,29,102);

char user[] = "root";

char password[] = "mysql";

Connector my_conn; // The Connector/Arduino reference

//const char INSERT_DATA[] = "INSERT INTO iot.dht22 VALUES (CURDATE(),'10.11℃')"; // CURDATA()获取的是当前日期 "年-月-日", 2020-09-11 , 只能在sql语句中使用

const char INSERT_DATA[] = "INSERT INTO iot.dht22 VALUES (now(),'10.11℃')"; // now()也可以获取当前的时间,| 2020-09-11 20:12:12, 只能在sql语句中使用

void setup() {

Ethernet.begin(mac_addr);

Serial.begin(115200);

Serial.println("Connecting MySQL4.0...");

if (my_conn.mysql_connect(server_addr, 3306, user, password)) {

delay(1000);

}

else

Serial.println("Connection failed.");

}

void loop() {

my_conn.cmd_query(INSERT_DATA); // success insert to MySQL!!

delay(10000);

}

/*************************************

MySQL8.0 failed

MySQL4.0 OK

Connecting...

Connected to server version 4.1.22-community-nt

**************************************/

转载地址:https://blog.csdn.net/weixin_32697149/article/details/116258830 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:php数据库结构对比 微擎,禾匠数据库对比–微擎通用各类数据库结构对比教程...
下一篇:中职pHP课程标准,中等职业学校数课程标准.doc

发表评论

最新留言

很好
[***.229.124.182]2024年04月01日 23时16分42秒