[Measured available] Arduino directly accesses mysql

Searching and searching, finally found an available Arduino direct access to mysql solution, simple and easy to get rid of the dependence on various MQTT IOT platforms. For now, consider the implementation first, and improve it later:

Test environment:
1. Arduino UNO + W5100 (test ESP8266 ESP32 when you have time)
2. Arduino IDE1.8.10 3.
Arduino online installation library mysql.h (I haven’t seen it in a few days, set up the environment on another computer again, and found the downloaded library It has actually changed. I didn’t take a closer look. It is estimated that the basic idea is
correct . Maybe the variables need to be changed, let’s talk about it...) 4.mysql-4.1.22-win32-Setup (This big hole must be an old version. OK, the new 8.0 cannot be connected. It is estimated that the security settings of the arduino library have not been fixed. Wait for the party and wait for the gods)

Test steps:

-----------------------------------------PC terminal operation------ -----------------------------------------------

1. Install mysql 4.0 on the local computer. windows10_x64_pro The actual test is no problem, and there is no need to change the firewall.
2. MySQL command line configuration, it is estimated that there are graphical, just a few commands, too lazy to look for it. Seeing the following mysql> prompt is the first step

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. Create an instance

mysql> create database IOT default character set utf8;

4. Create a table

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

5. Write some data into the table

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

6. Check and see

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)
  1. You can clear the contents of the table before the official start (DROP TABLE xxx to delete the table)
delete from dht22; 

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

  1. Upload the arduino program, the UNO program writes the data, select * from iot.dht22; You will see more and more entries increase, and you can upload the sensor readings in the future.

Attached Arduino program: the routine is simply modified, some libraries are not installed online, and it is estimated that they are not useful, such as sha1.h. Other statements in the original routine may require

Currently encountering minor problems:

  1. ℃ arduino stores garbled characters in the table, but it is not garbled characters when stored in the table from PC. . .
  2. Add NTPClient.h, you can get the time from the network. . . However, the time displayed by the serial port is different from the time written into mysql. Please post the program after you adjust it.
  3. SQL function now() can generate timestamp directly, so NTPClient.h is not needed
#include <NTPClient.h>
#include <SPI.h>
#include <Ethernet.h>
#include <stdlib.h>
#include <mysql.h>

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
**************************************/

Guess you like

Origin blog.csdn.net/jiangge12/article/details/108534624