怎么把数据存到MySQL_怎样将Arduino数据直接存储到MySQL

8e34895e5f5f6e3a78167e8aa506402c.png

刻录以下内容

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

487ba38b160b16055e2cda3dc24b40fe.png

为MySQL安装Wamp服务器并将其配置为存储数据

运行wamp服务器

打开MySQL控制台

然后为您的数据创建表

createtabledata(snoint(4)primarykeyauto_increment,LDRint(4),TEMPint(4));

使用descyour_table_name显示表详细信息

descdata;

这就是数据库的全部内容,现在我们可以进行处理了……

第3步:设置处理IDE

c9a60e397a52844b641de342377070bd.png

下载并安装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步:执行程序。

7549046495fec3a6ca0e0e8f9c6b499c.png

通过单击“运行”按钮运行程序,请关闭弹出窗口。关闭窗口将停止执行,并在下面的查询中查看在MySQL中存储数据。..

select*fromdata;

查看数据插入器的数量可以使用下面的查询。

selectcount(*)fromdata;

责任编辑:wv

要将Arduino串口数据传输到MySQL,需要以下步骤: 1. 在计算机上安装MySQL数据库并启动MySQL服务。 2. 在MySQL数据库中创建一个数据库和一个表来存储串口数据。 3. 在Arduino IDE中编写一个程序,该程序将从串口读取数据并将其发送到计算机上的MySQL数据库。 4. 在计算机上编写一个程序来接收来自Arduino的串口数据并将其插入到MySQL表中。 以下是一个简单的示例程序,它从Arduino串口读取数据并将其发送到计算机上的MySQL数据库: ```c++ #include <SoftwareSerial.h> #include <MySQL_Connection.h> #include <MySQL_Cursor.h> SoftwareSerial mySerial(10, 11); // RX, TX char buffer[100]; int index = 0; IPAddress server_addr(192,168,1,100); // IP address of the MySQL server char user[] = "root"; // MySQL username char password[] = "mypassword"; // MySQL password MySQL_Connection conn((Client *)&mySerial); void setup() { Serial.begin(9600); mySerial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } if (conn.connect(server_addr, 3306, user, password)) { Serial.println("Connected to MySQL server"); } else { Serial.println("Connection failed"); } delay(1000); } void loop() { if (mySerial.available()) { char c = mySerial.read(); buffer[index++] = c; if (c == '\n') { buffer[index] = '\0'; index = 0; Serial.print("Received data: "); Serial.println(buffer); insertData(buffer); } } } void insertData(char* data) { MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn); char sql_query[255]; sprintf(sql_query, "INSERT INTO mytable (data) VALUES ('%s')", data); cur_mem->execute(sql_query); delete cur_mem; } ``` 请注意,此示例程序假定您已经在MySQL服务器上创建了一个名为“mytable”的表,并且该表包含一个名为“data”的列,以存储串口数据。 此外,您还需要在计算机上编写一个程序来接收来自Arduino的串口数据并将其插入到MySQL表中。这可以使用您选择的编程语言和MySQL客户端库完成。例如,您可以使用Python和PyMySQL库编写一个简单的程序来完成此操作。以下是一个示例程序: ```python import serial import pymysql ser = serial.Serial('/dev/ttyACM0', 9600) # replace with your serial port conn = pymysql.connect(host='localhost', port=3306, user='root', password='mypassword', database='mydatabase') cur = conn.cursor() while True: data = ser.readline().strip() print("Received data: ", data) cur.execute("INSERT INTO mytable (data) VALUES (%s)", (data.decode('utf-8'))) conn.commit() ``` 请注意,此示例程序假定您已经在MySQL服务器上创建了一个名为“mytable”的表,并且该表包含一个名为“data”的列,以存储串口数据。此外,您还需要根据您的实际设置更改程序中的串口和MySQL连接参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值