If your board has a WiFi connection is not the case, and has been an error connection _______........... connection ________......., finally link is not on, please refer to my other one blog: https://blog.csdn.net/yaoyaoyao_123/article/details/103888526
As for how to buy a cloud server, and install the database server on the cloud, yet not learned. In the times we have installed the default MySql, and have created a database arduino_test, built a table test1, the specific construction of the table statement:
create table test1(
`tem` double(3,1) DEFAULT NULL,
`hem` double(3,1) DEFAULT NULL
)engine=innodb default charset=utf8 auto_increment=1;
alter database arduino_test default character set 'utf8';
SET character_set_client='utf8';
SET character_set_connection='utf8';
SET character_set_results='utf8';
Of course, in the middle there is a lot of detail, do not have time to record, no new messages can be successful.
After the preparatory work done, write Arduino IDE program:
#include <ESP8266WiFi.h> // esp8266库
#include <MySQL_Connection.h> // Arduino连接Mysql的库
#include <MySQL_Cursor.h>
IPAddress server_addr(106,12,195,127); // 安装Mysql的电脑的IP地址
char user[] = "root"; // Mysql的用户名
char password[] = "123456"; // 登陆Mysql的密码
// Mysql中添加一条数据的命令
// arduino_test,test1:刚才创建的数据和表
char INSERT_SQL[] = "INSERT INTO arduino_test.test1(tem,hem) VALUES ('%s','%s')";
char ssid[] = "TP-LINK_702"; // WiFi名
char pass[] = "weilaoshi"; // WiFi密码
WiFiClient client; // 声明一个Mysql客户端,在连接Mysql中使用
MySQL_Connection conn(&client);
MySQL_Cursor* cursor; //
int isConnection=0;
// 读取传感器的数据并写入到数据库
void readAndRecordData(){
char buff[128];// 定义存储传感器数据的数组
char tem[5];
char hem[4];
// 将传感器采集的浮点数转换为3位整数一位小数的字串放入temp
dtostrf(12.56,2,1,tem);//表示把123.56转换成整数部分3位数字,并且一位小数详情https://blog.csdn.net/qq_25827845/article/details/50717522
dtostrf(23.98,2,1,hem);
Serial.println(tem);
Serial.println(hem);
sprintf(buff,INSERT_SQL,tem,hem); // 将tem和hem中数据放入SQL中sprintf()函数详情https://blog.csdn.net/oyhb_1992/article/details/75095472
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); // 创建一个Mysql实例
cur_mem->execute(buff); // 将采集到的温湿度值插入数据库中
Serial.println("读取传感器数据,并写入数据库");
delete cur_mem; // 删除mysql实例为下次采集作准备
}
void setup()
{
Serial.begin(9600);
while (!Serial); // 等待端口的释放
Serial.printf("\nConnecting to %s", ssid);
WiFi.begin(ssid, pass); // 连接WiFi
while (WiFi.status() != WL_CONNECTED) { // 如果WiFi没有连接,一直循环打印点
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to network");
Serial.print("My IP address is: ");
Serial.println(WiFi.localIP()); // 打印开发板的IP地址
Serial.print("Connecting to SQL... ");
if (conn.connect(server_addr, 3306, user, password)) // 连接数据库
{
isConnection=1;
Serial.println("成功连接数据库---OK.");
}else{
isConnection=0;
Serial.println("连接数据库失败---FAILED.");
}
cursor = new MySQL_Cursor(&conn); // 创建一个数据库游标实例
}
void loop()
{
if(isConnection==1)
{
readAndRecordData();
delay(5000);
}
}
Procedures used in the MySQL_Connection library, you need to manually added to it, I've put the bag on the compression library:
To extract to lower Arduino library folder after download.
Upload the program to the development board, you can view the database:
Arduino stop inserting data into the database, representing a success.
Here's the query data in the database: