怎么把数据存到MySQL_怎样将Arduino数据直接存储到MySQL
刻錄以下內容
voidsetup()
{
Serial.begin(9600);
}
voidloop()
{
inti=0,j=0;
i=analogRead(A0);
j=analogRead(A1);
Serial.print(i);
Serial.print(“,”);
Serial.println(i);
}
步驟2:設置啟動MySQL
為MySQL安裝Wamp服務器并將其配置為存儲數據
運行wamp服務器
打開MySQL控制臺
然后為您的數據創建表
createtabledata(snoint(4)primarykeyauto_increment,LDRint(4),TEMPint(4));
使用descyour_table_name顯示表詳細信息
descdata;
這就是數據庫的全部內容,現在我們可以進行處理了……
第3步:設置處理IDE
下載并安裝ProcessingIDE2.2.1
將上述給定的ZIP壓縮到MyDocuments/Processing/Libraries中
現在打開正在處理的IDE和檢查庫是否已正確安裝(如上圖所示)
然后將以下代碼復制并進行處理,并自行命名
/*
ARDUINOTOMYSQLTHROUGHPROCESSING
ReadSerialmessagesfromArduinothenwriteitinMySQL.
Author:J.V.JohnsonSelvaSeptember2016
*/
importde.bezier.data.sql.*;//importtheMySQLlibrary
importprocessing.serial.*;//importtheSeriallibrary
MySQLmsql;//CreateMySQLObject
String[]a;
intend=10;//thenumber10isASCIIforlinefeed(endofserial.println),laterwewilllookforthistobreakupindividualmessages
Stringserial;//declareanewstringcalled‘serial’。Astringisasequenceofcharacters(datatypeknowas“char”)
Serialport;//Theserialport,thisisanewinstanceoftheSerialclass(anObject)
voidsetup(){
Stringuser=“root”;
Stringpass=“”;
Stringdatabase=“iot_database”;
msql=newMySQL(this,“localhost”,database,user,pass);
port=newSerial(this,Serial.list()[0],9600);//initializingtheobjectbyassigningaportandbaudrate(mustmatchthatofArduino)
port.clear();//functionfromseriallibrarythatthrowsoutthefirstreading,incasewestartedreadinginthemiddleofastringfromArduino
serial=port.readStringUntil(end);//functionthatreadsthestringfromserialportuntilaprintlnandthenassignsstringtoourstringvariable(called‘serial’)
serial=null;//initially,thestringwillbenull(empty)
}
voiddraw()
{
while(port.available()》0)
{
//aslongasthereisdatacomingfromserialport,readitandstoreit
serial=port.readStringUntil(end);
}
if(serial!=null)
{
//ifthestringisnotempty,printthefollowing
//Note:thesplitfunctionusedbelowisnotnecessaryifsendingonlyasinglevariable.However,itisusefulforparsing(separating)messageswhen
//readingfrommultipleinputsinArduino.BelowisexamplecodeforanArduinosketch
a=split(serial,‘,’);//anewarray(called‘a’)thatstoresvaluesintoseparatecells(separatedbycommasspecifiedinyourArduinoprogram)
println(a[0]);//printLDRvalue
println(a[1]);//printLM35value
function();
}
}
voidfunction()
{
if(msql.connect())
{
msql.query(“insertintodata(LDR,Temp)values(”+a[0]+“,”+a[1]+“)”);
}
else
{
//connectionfailed!
}
msql.close();//MustcloseMySQLconnectionafterExecution
}
第4步:執行程序。
通過單擊“運行”按鈕運行程序,請關閉彈出窗口。關閉窗口將停止執行,并在下面的查詢中查看在MySQL中存儲數據。..
select*fromdata;
查看數據插入器的數量可以使用下面的查詢。
selectcount(*)fromdata;
責任編輯:wv
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的怎么把数据存到MySQL_怎样将Arduino数据直接存储到MySQL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql gbk支持_mysql如何支
- 下一篇: mysql行级安全_MySQL学习笔记(