步驟1:設置Arduino
刻錄以下內容
void setup()
{
Serial.begin(9600);
}
void loop()
{
int i=0,j=0;
j=analogRead(A1);
Serial.print(i);
Serial.print(“,”);
Serial.println(i);
}
步驟2:設置啟動MySQL
為MySQL安裝Wamp服務器并將其配置為存儲數據
運行wamp服務器
打開MySQL控制臺
選擇數據庫
然后為您的數據創建表
create table data(sno int(4) primary key auto_increment,LDR int(4),TEMP int(4));
使用desc your_table_name顯示表詳細信息
desc data;
這就是數據庫的全部內容,現在我們可以進行處理了……
第3步:設置處理IDE
下載并安裝Processing IDE 2.2.1
將上述給定的ZIP壓縮到MyDocuments/Processing/Libraries中
現在打開正在處理的IDE和檢查庫是否已正確安裝(如上圖所示)
然后將以下代碼復制并進行處理,并自行命名
/*
ARDUINO TO MYSQL THROUGH PROCESSING
Read Serial messages from Arduino then write it in MySQL.
Author : J.V.JohnsonSelva September 2016
*/
import de.bezier.data.sql.*; //import the MySQL library
import processing.serial.*; //import the Serial library
MySQL msql; //Create MySQL Object
String[] a;
int end = 10; // the number 10 is ASCII for linefeed (end of serial.println), later we will look for this to break up individual messages
String serial; // declare a new string called ‘serial’ 。 A string is a sequence of characters (data type know as “char”)
Serial port; // The serial port, this is a new instance of the Serial class (an Object)
void setup() {
String user = “root”;
String pass = “”;
String database = “iot_database”;
msql = new MySQL( this, “localhost”, database, user, pass );
port = new Serial(this, Serial.list()[0], 9600); // initializing the object by assigning a port and baud rate (must match that of Arduino)
port.clear(); // function from serial library that throws out the first reading, in case we started reading in the middle of a string from Arduino
serial = port.readStringUntil(end); // function that reads the string from serial port until a println and then assigns string to our string variable (called ‘serial’)
serial = null; // initially, the string will be null (empty)
}
void draw()
{
while (port.available() 》 0)
{
//as long as there is data coming from serial port, read it and store it
serial = port.readStringUntil(end);
}
if (serial != null)
{
//if the string is not empty, print the following
//Note: the split function used below is not necessary if sending only a single variable. However, it is useful for parsing (separating) messages when
//reading from multiple inputs in Arduino. Below is example code for an Arduino sketch
a = split(serial, ‘,’); //a new array (called ‘a’) that stores values into separate cells (separated by commas specified in your Arduino program)
println(a[0]); //print LDR value
println(a[1]); //print LM35 value
function();
}
}
void function()
{
if ( msql.connect() )
{
msql.query( “insert into data(LDR,Temp)values(”+a[0]+“,”+a[1]+“)” );
}
else
{
// connection failed !
}
msql.close(); //Must close MySQL connection after Execution
}
第4步:執行程序。
通過單擊“運行”按鈕運行程序,請關閉彈出窗口。關閉窗口將停止執行,并在下面的查詢中查看在MySQL中存儲數據。..
select * from data;
查看數據插入器的數量可以使用下面的查詢。
select count(*) from data;
責任編輯:wv
-
MySQL
+關注
關注
1文章
831瀏覽量
26760 -
Arduino
+關注
關注
188文章
6477瀏覽量
187950
發布評論請先 登錄
相關推薦
從Delphi、C++ Builder和Lazarus連接到MySQL數據庫
![從Delphi、C++ Builder和Lazarus連接到<b class='flag-5'>MySQL</b><b class='flag-5'>數據</b>庫](https://file1.elecfans.com/web3/M00/06/A5/wKgZO2eN5IqANOPPAAAbvvhWjM0611.png)
適用于MySQL和MariaDB的Python連接器:可靠的MySQL數據連接器和數據庫
![適用于<b class='flag-5'>MySQL</b>和MariaDB的Python連接器:可靠的<b class='flag-5'>MySQL</b><b class='flag-5'>數據</b>連接器和<b class='flag-5'>數據</b>庫](https://file1.elecfans.com/web3/M00/06/57/wKgZPGeJ2kmAcWpWAAAh1ecL_LM122.png)
MySQL數據庫的安裝
![<b class='flag-5'>MySQL</b><b class='flag-5'>數據</b>庫的安裝](https://file1.elecfans.com/web3/M00/05/E2/wKgZPGeF2XWAe83fAAAW9lhgvGk652.jpg)
香港云服務器怎么部署MySQL數據庫?
IG902如何上傳數據到MQTT云平臺EMQX ?
在FX3S上如何通過USB和GPIF將數據存儲到eMMC中?
為什么無法在nodemcu和arduino mega之間交換數據?
求助,在esp-idf中使用arduino作為組件后怎樣使用arduino的庫?
Redis與MySQL協同升級企業緩存
![Redis與<b class='flag-5'>MySQL</b>協同升級企業緩存](https://file.elecfans.com/web2/M00/3F/D7/poYBAGJqPMKAEXjWAAAOpepuZJ8475.jpg)
評論