刻录以下内容
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