Arduino + MySQL Database ---- basic insert
/*
MySQL Connector/Arduino Example : basic insert
This example demonstrates how to issue an INSERT query to store data in a
table. For this, we will create a special database and table for testing.
The following are the SQL commands you will need to run in order to setup
your database for running this sketch.
CREATE DATABASE test_arduino;
CREATE TABLE database.hello_arduino (
num integer primary key auto_increment,
message char(40),
recorded timestamp
);
Here we see one database and a table with three fields; a primary key that
is an auto_increment, a string, and a timestamp. This will demonstrate how
to save a date and time of when the row was inserted, which can help you
determine when data was recorded or updated.
For more information and documentation, visit the wiki:
https://github.com/ChuckBell/MySQL_Connector_Arduino/wiki.
INSTRUCTIONS FOR USE
1) Create the database and table as shown above.
2) Change the address of the server to the IP address of the MySQL server
3) Change the user and password to a valid MySQL user and password
4) Connect a USB cable to your Arduino
5) Select the correct board and port
6) Compile and upload the sketch to your Arduino
7) Once uploaded, open Serial Monitor (use 115200 speed) and observe
8) After the sketch has run for some time, open a mysql client and issue
the command: "SELECT * FROM test_arduino.hello_arduino" to see the data
recorded. Note the field values and how the database handles both the
auto_increment and timestamp fields for us. You can clear the data with
"DELETE FROM test_arduino.hello_arduino".
Note: The MAC address can be anything so long as it is unique on your network.
Created by: Dr. Charles A. Bell
*/
#include <WiFi.h>
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
IPAddress server_addr(52,76,27,242); // IP of the MySQL *server* here Server: sql12.freemysqlhosting.net
char user[] = "sql1213q581063"; // MySQL user login username
char password[] = "PqD1QzNS6p3B"; // MySQL user login password
//Port number: 3306
//https://whatismyipaddress.com/hostname-ip
//Lookup Hostname: sql12.freemysqlhosting.net
//Lookup IPv4 Address: 52.76.27.242
// WiFi card example
char ssid[] = "7qw41e702r87"; // your SSID
char pass[] = "24q0e63173"; // your SSID Password
// Sample query
char INSERT_SQL[] = "INSERT INTO sql12358063.hello_arduino (message) VALUES ('Hello, Arduino!')";
WiFiClient client; // Use this for WiFi instead of EthernetClient
MySQL_Connection conn((Client *)&client);
void setup() {
Serial.begin(115200);
// Begin WiFi section
Serial.printf("\nConnecting to %s", ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// print out info about the connection:
Serial.println("\nConnected to network");
Serial.print("My IP address is: ");
Serial.println(WiFi.localIP());
Serial.print("Connecting to SQL... ");
if (conn.connect(server_addr, 3306, user, password)) {
delay(1000);
}
else
Serial.println("Connection failed.");
}
void loop() {
delay(2000);
Serial.println("Recording data.");
// Initiate the query class instance
MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
// Execute the query
cur_mem->execute(INSERT_SQL);
// Note: since there are no results, we do not need to read any data
// Deleting the cursor also frees up memory used
delete cur_mem;
}
訂閱:
張貼留言 (Atom)
2024_09 作業3 以Node-Red 為主
2024_09 作業3 (以Node-Red 為主 Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker 2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...
-
python pip 不是内部或外部命令 -- 解決方法 要安裝 Pyqt5 1. 首先,開啟命令提示字元。 2. 輸入 pip3 install pyqt5 好像不能執行 ! ! 錯誤顯示 : ‘ pip3 ’ 不是內部或外部命令、可執行的程式或批...
-
課程講義 下載 11/20 1) PPT 下載 + 程式下載 http://www.mediafire.com/file/cru4py7e8pptfda/106%E5%8B%A4%E7%9B%8A2-1.rar 11/27 2) PPT 下載...
-
• 認 識 PreFix、InFix、PostFix PreFix(前序式):* + 1 2 + 3 4 InFix(中序式): (1+2)*(3+4) PostFix(後序式):1 2 + 3 4 + * 後 序式的運算 例如: 運算時由 後序式的...
沒有留言:
張貼留言