Last active
February 8, 2022 11:08
-
-
Save kuzux/7dca33883ff22cf0ba8a12dcb0b4b60b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
time_since_last_read = 0 | |
burst_length = const | |
max_buflen = 2000 | |
recvTag() | |
loop while serial not available - waiting for an answer | |
header = serial read | |
return 0xFFFF if header != 0xA0 | |
len = serial read | |
tag = 0 | |
err = 0 | |
for i=0 to len | |
serial read | |
save the tag, whatever | |
if len == 0x0A | |
return 0xFFFF | |
if len == 0x04 | |
log error | |
return 0xFF | |
addTagToBuffer(tag) | |
buflen = get from addr 0x0002 in spi ram | |
for i=0 to buflen | |
curr = get from addr 0x0004 + 2*i | |
if curr = tag | |
return | |
if buflen == max_buflen | |
log error | |
return | |
write tag to addr 0x0004 + 2*buflen | |
buflen++ | |
write buflen to addr 0x0002 | |
scanTags() | |
num_read = 0 | |
for iter=0 to burst_length | |
for k=0 to 3 | |
switch to antenna k - we might need to wait for a response there | |
issue read command | |
tag = recvTag() | |
while tag != 0xFFFF | |
num_read++ | |
addTagToBuffer(tag) | |
tag = recvTag() | |
if num_found==0 | |
time_since_last_read++ | |
else | |
time_since_last_read = 0 | |
sendViaGprs() | |
buflen = get from addr 0x0002 in spi ram | |
if buflen == 0 | |
return | |
client connect to host & port | |
send post request to resource | |
auth header | |
connection close | |
headers done | |
start writing body | |
for i=0 to buflen | |
tag = get from addr 0x0004 + 2*i | |
send tag in decimal | |
if i != buflen-1 | |
send ',' character | |
done writing body | |
client stop | |
// we do a fire and forget type of request | |
loop() | |
if time_since_last_read > some const | |
activateGsmSerial() - only one software serial can actively work, apparently | |
sendViaGprs() | |
time_since_last_read = 0 | |
delay(100) - is this even necessary? | |
else | |
activateRfidSerial() | |
scanTags() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#include <SPI.h> | |
#define rfidRx 3 | |
#define rfidTx 2 | |
#define commRx 5 | |
#define commTx 4 | |
// sram pins: | |
#define sramCs 10 | |
// mosi 11 | |
// miso 12 | |
// sck 13 | |
SoftwareSerial rfid(rfidRx, rfidTx); | |
SoftwareSerial comm(commRx, commTx); | |
int time_since_last_read = 0; | |
// spi ram commands | |
#define WRMR 1 // Write to the Mode Register | |
#define READ 3 // Read command | |
#define WRITE 2 // Write command | |
void sramSetMode(char mode){ | |
digitalWrite(sramCs, LOW); | |
SPI.transfer(WRMR); | |
SPI.transfer(mode); | |
digitalWrite(sramCs, HIGH); | |
} | |
byte sramReadByte(int address) { | |
char read_byte; | |
SPI.transfer(READ); | |
SPI.transfer((byte)(address >> 8)); | |
SPI.transfer((byte)address); | |
SPI.transfer((byte)0x00); | |
read_byte = SPI.transfer(0x00); | |
return read_byte; | |
} | |
void sramWriteByte(int address, byte data_byte) { | |
SPI.transfer(WRITE); | |
SPI.transfer((byte)(address >> 8)); | |
SPI.transfer((byte)adress); | |
SPI.transfer((byte)0x00); | |
SPI.transfer(data_byte); | |
} | |
int sramReadWord(int address) { | |
digitalWrite(sramCs, LOW); | |
byte hi = sramReadByte(address); | |
byte lo = sramReadByte(address+1); | |
digitalWrite(sramCs, HIGH); | |
return (hi << 8) | lo; | |
} | |
void sramWriteWord(uint32_t address, int data) { | |
byte hi = (byte)(data >> 8) | |
byte lo = (byte)(data & 0xFF); | |
digitalWrite(sramCs, LOW); | |
sramWriteByte(address, hi); | |
sramWriteByte(address+1, lo); | |
digitalWrite(sramCs, HIGH); | |
} | |
void setup() { | |
pinMode(rfidRx, INPUT); | |
pinMode(commRx, INPUT); | |
pinMode(rfidTx, OUTPUT); | |
pinMode(commTx, OUTPUT); | |
pinMode(sramCs, OUTPUT); | |
rfid.begin(115200); | |
comm.begin(115200); // maybe use a differnt baud rate here? | |
Serial.begin(9600); | |
SPI.begin(); | |
sramSetMode(0x00); // transferring individual bytes | |
} | |
byte inventory[] = { 0xA0, 0x04, 0xFF, 0x89, 0x01, 0x00 }; | |
// receives a message, which includes a length byte (always byte 1). | |
// loops until that byte, writing checksum to the end | |
void calculateChecksum(byte* msg) { | |
byte len = msg[1]; | |
byte sum = 0x0; | |
for(byte i=0; i<len+1; i++) { | |
sum += msg[i]; | |
} | |
sum = (~sum) + 1; | |
msg[len+1] = sum; | |
} | |
// blocks until we can read from the rfid module | |
// returns 0xFFFF on error | |
int recvTag() { | |
while(!rfid.available()); | |
byte header = rfid.read(); | |
byte len = rfid.read(); | |
byte pc_msb = 0; | |
byte pc_lsb = 0; | |
byte err_code = 0; // only valid in error case | |
for(byte i=0; i<len; i++) { | |
byte tmp = rfid.read(); | |
if(i==2) err_code=tmp; | |
if(i==3) pc_msb=tmp; | |
if(i==4) pc_lsb=tmp; | |
} | |
if(len == 0x04) { | |
Serial.print("scan error: 0x"); | |
Serial.print(err_code, HEX); | |
return 0xFFFF; | |
} else if(len == 0x0a) { | |
Serial.println("done scanning"); | |
return 0xFFFF; | |
} else { | |
int pc = (pc_msb << 8) | pc_lsb; | |
Serial.print("scanned card: "); | |
Serial.println(pc); | |
return pc; | |
} | |
} | |
// the buffer holds unique tags - checks if the | |
void addToBuffer(int tag) { | |
int buflen = sramReadWord(0x0002); | |
for(int i=0; i<buflen; i++) { | |
int curr = sramReadWord(0x0004 + 2*i); | |
if(curr == tag) return; | |
} | |
if(buflen == 2000) { | |
Serial.println("tag buffer full"); | |
return; | |
} | |
sramWriteWord(0x0004 + 2*buflen, tag); | |
buflen++; | |
sramWriteWord(0x0002, buflen); | |
} | |
void scanTags() { | |
calculateChecksum(inventory); | |
int num_read = 0; | |
for(byte i=0; i<10; i++) { // burst length might be changed | |
for(byte j=0; j<sizeof(inventory; j++)) rfid.write(inventory[j]); | |
int tag = recvTag(); | |
while(tag != 0xFFFF) { | |
num_read++; | |
addToBuffer(tag); | |
tag = recvTag(); | |
} | |
} | |
if(num_read == 0) time_since_last_read++; | |
else time_since_last_read = 0; | |
} | |
void sendHttpRequest() { | |
int buflen = sramReadWord(0x0002); | |
if(buflen == 0) return; | |
// TODO: Actually send the http request - not sure which http library / module we're using | |
buflen = 0; | |
} | |
void loop() { | |
if(time_since_last_read > 10) { | |
comm.listen(); | |
sendHttpRequest(); | |
time_since_last_read = 0; // so that we go back to | |
delay(100); | |
} else { | |
rfid.listen(); | |
scanTags(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* readerConnector just connects to a com port with 115200 baud | |
creates an rfidReaderHelper (with the com port) | |
* we sleep after initializing connection | |
* a listener and observer is attached to rfid reader helper | |
listener => looks like low-level stuff (for serial communication) | |
observer => higher level (tagRead / tagEnd stuff) | |
* we send a real-time inventory command | |
0xa0, 0x04, 0xff, 0x89, 0x01, <checksum> | |
got this via rfidReaderHelper.sendMessage -> MessageTran ctor | |
* we get a bunch of reads and then an end message | |
I think we need to distinguish it by length (= 0x0a) | |
find out via RXObserver.processRealTimeInventory | |
0xa0, <length>, <don't care>, <dont care>, <PC>x2, <EPC>xN, <RSSI>, <checksum> | |
length of EPC = length - 7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #define _SS_MAX_RX_BUFF 1024 // increase software serial buffer size when we have more ram | |
#include <SoftwareSerial.h> | |
#define rfidRx 3 | |
#define rfidTx 2 | |
SoftwareSerial rfid(rfidRx, rfidTx); | |
void setup() { | |
pinMode(rfidRx, INPUT); | |
pinMode(rfidTx, OUTPUT); | |
rfid.begin(115200); | |
Serial.begin(9600); | |
} | |
// receives a message, which includes a length byte (always byte 1). | |
// loops until that byte, writing checksum to the end | |
void calculateChecksum(byte* msg) { | |
byte len = msg[1]; | |
byte sum = 0x0; | |
for(byte i=0; i<len+1; i++) { | |
sum += msg[i]; | |
} | |
sum = (~sum) + 1; | |
msg[len+1] = sum; | |
} | |
byte reset[] = { 0xA0, 0x03, 0xFF, 0x70, 0x00 }; | |
byte version_req[] = { 0xA0, 0x03, 0xFF, 0x72, 0x00 }; | |
byte inventory[] = { 0xA0, 0x04, 0xFF, 0x89, 0x01, 0x00 }; | |
/* | |
// this should beep the buzzer on reader | |
void loop() { | |
calculateChecksum(reset); | |
for(byte i=0; i<sizeof(reset); i++) { | |
rfid.write(reset[i]); | |
} | |
for(;;); | |
} | |
// this should print the version onto the serial console | |
void loop() { | |
// send request for firmware version | |
calculateChecksum(version_req); | |
for(byte i=0; i<sizeof(version_req); i++) { | |
rfid.write(version_req[i]); | |
} | |
// wait for a reply | |
byte resp = 0x00; | |
do { | |
delay(10); | |
resp = rfid.read(); | |
} while(resp == 0xFF); | |
// we have already read the first byte (header), now read the length | |
byte len = rfid.read(); | |
byte major = 0; | |
byte minor = 0; | |
for(byte i=0; i<len; i++) { | |
byte tmp = rfid.read(); | |
if(i==2) major=tmp; | |
if(i==3) minor=tmp; | |
} | |
// now let's write the version info onto serial console | |
Serial.print("Firmware version: "); | |
Serial.print(major); | |
Serial.print("."); | |
Serial.println(minor); | |
// go to sleep | |
for(;;); | |
} | |
*/ | |
void loop() { | |
calculateChecksum(inventory); | |
for(byte i=0; i<sizeof(inventory); i++) { | |
rfid.write(inventory[i]); | |
} | |
for(;;) { | |
// wait for some msg | |
while(!rfid.available()); | |
byte header = rfid.read(); | |
byte len = rfid.read(); | |
byte pc_msb = 0; | |
byte pc_lsb = 0; | |
byte err_code = 0; // only valid in error case | |
for(byte i=0; i<len; i++) { | |
byte tmp = rfid.read(); | |
if(i==2) err_code=tmp; | |
if(i==3) pc_msb=tmp; | |
if(i==4) pc_lsb=tmp; | |
} | |
if(len == 0x04) { | |
Serial.print("scan error: 0x"); | |
Serial.print(err_code, HEX); | |
break; | |
} else if(len == 0x0a) { | |
Serial.println("done scanning"); | |
break; | |
} else { | |
int pc = (pc_msb << 8) | pc_lsb; | |
Serial.print("scanned card: "); | |
Serial.println(pc); | |
} | |
} | |
for(;;); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment