步骤1:设置arduino和rfid rc-522(物理连接)
只需将arduino与rfid连接-rc522,如上图所示。
警告:仅提供3.3v电压,否则模块将烧坏
为uno/nano和mega插脚 strong》
rc522 module uno/nano mega
sda d10 d9
sck d13 d52
mosi d11 d51
miso d12 d50
irq n/a n/a
gnd gnd gnd
rst d9 d8
3.3v 3.3v 3.3v
第2步:arduino代码。
复制以下代码,然后将其上传到您的arduino
/*
pinout:
rc522 module uno/nano mega
sda d10 d9
sck d13 d52
mosi d11 d51
miso d12 d50
irq n/a n/a
gnd gnd gnd
rst d9 d8
3.3v 3.3v 3.3v
*/
/* include the standard arduino spi library */
#include
/* include the rfid library */
#include
/* define the dio used for the sda (ss) and rst (reset) pins. */
#define sda_dio 9
#define reset_dio 8
/* create an instance of the rfid library */
rfid rc522(sda_dio, reset_dio);
int reader=0;
void setup()
{
serial.begin(9600);
/* enable the spi interface */
spi.begin();
/* initialise the rfid reader */
rc522.init();
}
void loop()
{
/* temporary loop counter */
byte i;
/* has a card been detected? */
if (rc522.iscard())
{
/* if so then get its serial number */
rc522.readcardserial();
/* output the serial number to the uart */
for(i = 0; i 《= 2; i++)
{
serial.print(rc522.sernum[i],dec);
//serial.print(rc522.sernum[i],hex);
}
serial.print(“,”);
serial.print(reader++);
serial.println();
}
delay(1000);
}
步骤3:设置mysql
为mysql安装wamp服务器并将其配置为存储数据(
运行wamp服务器打开mysql控制台
选择数据库
然后为您的数据创建表
create table rfid(id int(8),token int(1),name varchar(20),amount int(4));
现在查看此链接以了解如何获取您的rfid标签值,然后使用以下代码插入数据。不要忘记将id值替换为您的rfid标签值
insert into rfid values(3756178,1,‘pencil’,20);
使用令牌值作为 1 ,以便在首次读取标签值后,它将自动更改为 2 读取未插入数据库的卡时,不要使用 0 作为令牌值,它将分配0,然后将其显示为“未知卡”。
步骤4:设置处理ide
下载并安装处理ide 2.2.1
将上述给定的zip提取到mydocuments/processing/libraries
现在打开处理的ide,并检查库是否正确安装(如上图所示)
然后复制以下代码进行处理并自行命名
import de.bezier.data.sql.*;
import processing.serial.*;
//import java.math.biginteger;
// created 2005-05-10 by fjenett
// updated fjenett 20080605
mysql dbconnection;
string s=“ ”;
int wheight=700;
int wwidth=1200;
long id;
int token;
int amount;
int total=0;
string[] a={“null”,“null”};
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;
string curr,prev,name;
pfont f;
void setup()
{
//size( wwidth,wheight );
size(700,500);
f=createfont(“arial”,24,true);
// this example assumes that you are running the
// mysql server locally (on “localhost”)。
//
// replace --username--, --password-- with your mysql-account.
//
string user = “root”;
string pass = “”;
// name of the database to use
//
string database = “iot_database”;
// name of the table that will be created
string table = “”;
// connect to database of server “localhost”
dbconnection = 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;
}
void draw()
{
background(255);
textfont(f,24);
fill(0);
text(“total amount rs:”,400,400);
text(total,585,400);
data();
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)
{
prev=curr;
curr=a[1];
a = split(serial, ‘,’); //a new array (called ‘a’) that stores values into separate cells (separated by commas specified in your arduino program)
if((curr).equals(prev))
{
//
}
else
{
//println(“curr”,curr);
//println(“prev”,prev);
function();
}
}
}
void function()
{
if ( dbconnection.connect() )
{
// now read it back out
//
dbconnection.query( “select * from rfid where id=”+a[0]+“” );
while (dbconnection.next())
{
id = dbconnection.getint(“id”);
token = dbconnection.getint(“token”);
amount = dbconnection.getint(“amount”);
}
if(token==0)
{
println(“ok”);
textfont(f,54);
fill(255,0,0,160);
text(“unknown item detected”,50,300);
delay(2000);
}
else if(token==1)
{
total=total+amount;
dbconnection.query(“update rfid set token=2 where id=”+a[0]+“” );
println(“ok”);
textfont(f,24);
fill(255,0,0,160);
//text(“item added”,10,30);
delay(1000);
}
else if(token==2)
{
total=total-amount;
dbconnection.query(“update rfid set token=1 where id=”+a[0]+“” );
println(“ok”);
textfont(f,24);
fill(255,0,0,160);
//text(“item removed”,10,30);
delay(1000);
}
else
{
}
dbconnection.close();
}
else
{
// connection failed !
}
}
void data()
{
int position=100;
if ( dbconnection.connect() )
{
dbconnection.query( “select * from rfid where token=2”);
while (dbconnection.next())
{
name = dbconnection.getstring(“name”);
amount = dbconnection.getint(“amount”);
textfont(f,24);
fill(0,0,255,160);
text(name,10,position);
fill(0,0,0,160);
text(amount,215,position);
position=position+30;
}
}
dbconnection.close();
}
第5步:执行程序
通过单击运行按钮运行程序,请关闭弹出窗口,关闭窗口将停止执行,并在以下查询中查看在mysql中存储的数据。..
英伟达股价能否凭借AI进一步上涨到500美元?
315遥控接收发射电路图
MS2630—Sub-1 GHz、低噪声放大器芯片
VS10X无线混合信号采集仪
贸泽即日起备货优化物联网边缘应用能效的Silicon Labs EFM32PG22 MCU
怎样使用RFID RC-522和Arduino创建一个简单的超市应用程序
DDR的基础知识
变频器输出电压有1000多伏是怎么回事?
【数据库数据恢复】oracle数据库误truncate table的数据恢复案例
裸机下运行Hello,World【常见错误解决方案】
如何用宝塔Liunx面板设置优化服务器性能?
如何正确的理解工业互联网
人工智能蓬勃发展还缺了点什么
谷歌回国,AI人才争夺正式开启
现代逆变器系统的结构功能及其对功率因数校正
索尼PS5开发机照片曝光 深V造型亮眼
猜测Redmi Note 8 Pro的6G+128G售价,竟只比上一代涨了200元?
微软语音助手微软小娜:人工智能大热 小娜不再只是语音助手
使用仿真皮电子皮肤的机器人:可自由移动,人对机器人的操控将会更便捷
GTC23 | 3 月 22 日预告 — 在线观看派对:重塑软件定义汽车的电子架构