Green Robot MachineryVIBHAV
Published

RFID Data Logger

RFID card reader using LinkIt reads the value from card, communicate to node.js server over MQTT, then finally log it into a Mongo database.

IntermediateFull instructions provided5 hours2,259
RFID Data Logger

Things used in this project

Story

Read more

Schematics

Pin configuration

Code

Linkit Code

C/C++
This code is used to send the RFID card value from LinkIt One board to mqtt server using mosquitto broker
/*
***************************************
** Green Robot Machinery
***************************************
**RFID code to display the card data and send this data to mqtt server

**connections

 * Reset      9                
 * SPI SDA    10               
 * SPI MOSI   11               
 * SPI MISO   12               
 * SPI SCK    13     
*/

#include <MFRC522.h> // includes the RFID RC522 library 
#define MAXRFIDTAGS 100 // defines the max. no. of tags allowed
#define SS_PIN 10 // defines that SS pin or SDA pin is connected to pin D10
#define RST_PIN 9 // reset pin is connected to D9
#include<PubSubClient.h> // A client library that provides support for MQTT

// MediaTek LinkIt libraries for WiFi connectivity
#include <LTask.h> 
#include <LWiFi.h>
#include <LWiFiClient.h>


#include <HttpClient.h>  // library for posting HTTP requests
#include <SPI.h> //includes the Serial Peripheral Interface library


#define WIFI_AP "WiFI_NAME" // write your WiFi name in between double quotes
#define WIFI_PASSWORD "WiFI_PASSWORD" // write your WiFi Password in between double quotes
#define WIFI_AUTH LWIFI_WPA  // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.

LWiFiClient wifiClient;
byte localserver[] = {192, 168, 43, 19 }; //this is ip address of the machine where the mqtt broker is running


String clientName = String("d:quickstart:arduino:"); //client name can be of your choice  
String topicName = String("demo/device/"); //similarly topic name can be of your choice but make sure the same topic name is used in the node.js program


char rfidstr[15];
char s[100]; //stores the card data into an array

MFRC522 mfrc522(SS_PIN, RST_PIN);	// Create MFRC522 instance.
void checkRFIDval(MFRC522 mfrc522);


PubSubClient client(localserver, 1883, 0, wifiClient); //communicating the LinkIt One with the mosquitto broker (port no. 1883) running on the localserver, LinkIt One communicates to the broker via WiFi hence the WiFiClient  


void setup() {
  Serial.begin(115200);
  
  LTask.begin();
  LWiFi.begin();

  // keep retrying until connected to AP
  Serial.println("Connecting to AP");
  while(1)
  {
    status = LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD));
    if (status == 0)
    {
        Serial.println("Connection to Wifi failed. Retrying");
        delay(1000);
    }
    else
    {
        Serial.println("Connection Succeeded");
        break;
    }
  }
  SPI.begin();			// Init SPI bus
  mfrc522.PCD_Init();	// Init MFRC522 card
  Serial.println("Scan PICC to see UID and type...");

}
void loop() 
{
  Serial.println("Testing the program");
  char clientStr[34];
  clientName.toCharArray(clientStr,34);
  char topicStr[26];
  topicName.toCharArray(topicStr,26);
  
  if (!client.connected()) {
    Serial.print("Trying to connect to: ");
    Serial.println(clientStr);
    client.connect(clientStr);
  }
  if (client.connected() ) {
    String json = buildJson();
    char jsonStr[200];
    json.toCharArray(jsonStr,200);
    boolean pubresult = client.publish(topicStr,jsonStr);
    Serial.print("attempt to send ");
    Serial.println(jsonStr);
    Serial.print("to ");
    Serial.println(topicStr);
    if (pubresult)
      Serial.println("successfully sent");
    else
      Serial.println("unsuccessfully sent");
  }
  delay(5000);


      if ( ! mfrc522.PICC_IsNewCardPresent()) {
		return;
	}
   
   
      
	// Select one of the cards
	if ( ! mfrc522.PICC_ReadCardSerial()) {
		return;
	}
      
          
     checkRFIDval(mfrc522);
    
	// Dump debug info about the card. PICC_HaltA() is automatically called.
	//mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

}

String buildJson() {
  String data = "{";
  data+="\n";
  data+= "\"d\": {";
  data+="\n";
  data+="\"my name\": \"LinkIt One: RFID Test\",";
  data+="\n";
  data+="\"RFID value is\": ";
  data+=(char*)s;
  data+="\n";
  data+="}";
  data+="\n";
  data+="}";
  return data;
}


void checkRFIDval(MFRC522 mfrc522)
{
   
    char publishString[40];
    int i;
    char rfidstr[15];
    
    int tagfound = 0;
    int publish=0;
    char z;  

    for (i = 0; i < mfrc522.uid.size; i++)
    {
        
        sprintf(s,"%x",mfrc522.uid.uidByte[i]);
        strcat( &rfidstr[i] , s);
       Serial.println(s);
    }
 
}

Node.js code

JavaScript
The RFID value from mqtt server is sent to MongoDB database using this Node.js code
var mqtt=require('mqtt'); //includes mqtt server 
var mongodb=require('mongodb'); // includes mongoDB 
var mongodbClient=mongodb.MongoClient; //initialises the mongoDB client
var mongodbURI='mongodb://localhost:27017/RFIDMonitor'; //activating the MongoDB port 27017, here TempMontor is the name of the database
var deviceRoot="demo/device/"; //deviceroot is topic name given in arduino code 
var collection,client; //initialise collection and client

mongodbClient.connect(mongodbURI,setupCollection); //connect the database with collecion
     
function setupCollection(err,db) {
if(err) throw err;
collection=db.collection("test_mqtt"); //name of the collection in the database
client=mqtt.connect({ host: 'localhost', port: 1883 }); //connecting the mqtt server with the MongoDB database
client.subscribe(deviceRoot+"+"); //subscribing to the topic name 
client.on('message', insertEvent); //inserting the event
}

//function that displays the data in the MongoDataBase
function insertEvent(topic,message) {
var key=topic.replace(deviceRoot,'');

collection.update(
{ _id:key }, 
 console.log(message),
{ $push: { events: { event: {  value:message, when:new Date() } } } }, 
{ upsert:true },
function(err,docs) {
if(err) {
console.log("Insert fail")// Improve error handling		
		}
}
);

}

Credits

Green Robot Machinery

Green Robot Machinery

3 projects • 31 followers
Green Robot Machinery Private Limited
VIBHAV

VIBHAV

3 projects • 5 followers

Comments

Add projectSign up / Login