我的ESP32實做書籍:https://youyouyou.pixnet.net/blog/post/121105860
博客來網址:https://www.books.com.tw/products/0010901195
本篇已有更新版,敬請參閱:https://youyouyou.pixnet.net/blog/post/120275917
Arduino存入資料庫很有多方法,目前常見的還是以http get或post方式,例如php或asp.net,或者第三方轉接的例如IFTTT。
今天簡單介紹如何直接存入mysql的方法,其實沒什麼大工程,用Mysql library而已。個人比較不建議這種方式,畢竟這樣等於是把mysql放到Internet中,反之如果透過http get post修改或新增資料,權限跟內容由web程式控制,有較高的安全性。
以下以二個部份說明
一、環境設定:安裝mysql及wokbench
二、程式開發:在Arduino IDE中撰寫程式
一、環境設定
為了測試是否可行,以下介紹安裝相關測試軟體,mysql最大的好處是,他是免費的,執行效能也不差,另外就是workbench,這是懶人的福音,如果你跟我一樣,對cmd界面不熟悉,你一定要安裝workbench,他提供你完整的mysql圖形界面操作模式,真心推薦。
1.安裝mysql伺服器,安裝方式網路非常多介紹,請自行google,或參考這篇: https://jerrynest.io/windows-mysql-installer/ ,下載的網址: https://dev.mysql.com/downloads/installer/ ,題外話,如果你想用cluster版,可參考本人本篇文章: http://youyouyou.pixnet.net/blog/post/119326123
2.安裝workbench,下載網址: https://www.mysql.com/products/workbench/
3.將workbench連線到mysql資料庫,這個步驟的目的在於開放使用者可進行遠端登入,讓arduino以遠端更新資料庫資料
開啟workbench,一般都會建立一個連線是直接連線到本機端mysql,直接打開即可。
開啟後,要調整使用權限,你可以建立一個新的使用者,或者修改root使用者由原本的localhost改為%,代表該使用者可以從網路上任何地點連進資料庫來進行修改。
要注意的是,你若是用新建使用者,那記得要去調權限Administrative Roles跟能使用的資料庫Schema Privileges。
完成後按下方的Apply即可存檔。
4.建立資料庫
4.1透過workbench建立資料庫,由於本範例將把dht11數值存入,因此建立一個資料庫名稱為tempandhumd
再來建立資料表,在剛建好的資料庫前方+號點開,在table上按右鍵,選擇Create Table。
除了溫度、濕度之外,可用自動編號當作主鍵,最後在給一個更新時間,因此有四個欄位
id:整數,自動編號
temp:整數
humd:整數
updatetime:timestamp,並給予預設值:CURRENT_TIMESTAMP,目的是這樣當資料新增時,直接給時間,就不用在程式裡面還要寫入時間
雖然有四個欄位,但實際上我們僅須給temp跟humd即可,id與updatetime都是sql資料庫直接給值得。這樣比較簡單。
二、程式開發
完成資料庫設定後,就可以來arduino寫程式,為了直接寫入mysql,可直接匯入mysql 的library。本案例搭配ESP32,他的好處可參考本文: ESP32 Arduino開發環境架設(取代Arduino UNO及ESP8266首選)
露天可搜尋:https://goo.gl/JCyV8i
安裝"mysql connector arduino" by Dr. Charles Bell
搜尋關鍵字mysql,即可找到所需的library,此時把他安裝起來。
我修改好的arduino mysql範例程式請參閱以下程式,其中要注意的是第14行的Server ip這裡要用逗號:「,」做區隔,而不是傳統的點「.」做區隔。
另外第59行為寫入資料庫SQLString,為方便測試,我直接給定數值寫入資料庫,其中溫度=35而濕度=60。
char INSERT_SQL[] = "INSERT INTO tempandhumd.datalog (temp,humd) VALUES (35,60)";
執行後,至資料庫則發現資料已寫入資料庫中
接下來就靠各位把DHT11連上,並將資料更新到第59行的VALUES內了。
/* | |
* You need to download mysql library by arudino IDE/Sketch/Include Library/manage Libraries. | |
* Search "MySQL Connector Arduino" by Dr. Charles Bell, and install it. | |
*/ | |
#include <WiFi.h> | |
#include <MySQL_Connection.h> | |
#include <MySQL_Cursor.h> | |
const char ssid[] = "YourSSid"; // change to your WIFI SSID | |
const char password[] = "YourPassword";// change to your WIFI Password | |
IPAddress server_addr(192,168,100,100); // change to you server ip, note its form split by "," not "." | |
int MYSQLPort =3306; //mysql port default is 3306 | |
char user[] = "root"; // Your MySQL user login username(default is root),and note to change MYSQL user root can access from local to internet(%) | |
char pass[] = "MYSQLpassword"; // Your MYSQL password | |
WiFiClient client; | |
MySQL_Connection conn((Client *)&client); | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
//try to connect to WIFI | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
//try to connect to mysql server | |
if (conn.connect(server_addr, 3306, user, pass)) { | |
delay(1000); | |
} | |
else{ | |
Serial.println("Connection failed."); | |
} | |
delay(2000); | |
//insert, change database name and values by string and char[] | |
char INSERT_SQL[] = "INSERT INTO tempandhumd.datalog (temp,humd) VALUES ('30','80')"; | |
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); | |
//execute | |
cur_mem->execute(INSERT_SQL); | |
delete cur_mem; | |
Serial.println("Data Saved."); | |
} | |
void loop() { | |
//do nothing | |
} |
有一個地方可能要注意的是,SQL的Connection要記得斷線,不然connection pool會暴掉
請參考這篇https://github.com/ChuckBell/MySQL_Connector_Arduino/blob/master/examples/connect_disconnect/connect_disconnect.ino