Skip to content
Snippets Groups Projects
RFID_IoT_BS_final.ino 4.05 KiB
Newer Older
#include <SPI.h>          //include Serial Peripheral Interface for communication with Arduino as master and MF522 as slave
#include <MFRC522.h>      //include MFRC522 library for communication with MF522-AN (based on MFRC522)
#include <ESP8266WiFi.h>  //include WiFI library
 
#define RST_PIN   D2      // set pin for reset
#define SS_PIN    D4      // set pin for slave select
 
MFRC522 mfrc522(SS_PIN, RST_PIN);   // initiate instance of MFRC522 for MF522-AN

const char server[] = "bs19-team34.bas.uni-koblenz.de";     // configure network credentials (server URL)
const char* ssid     = "BS2019";        // configure network credentials (SSID)
const char* password = "IoTinBS2019";    // configure network credentials (PW)
WiFiClient client;                   

 
void setup() {            // initial general configuration
  Serial.begin(9600);     // initialize serial communication
  SPI.begin();            // initialize SPI communication
  mfrc522.PCD_Init();     // initialize MF522-AN hardware module

  Serial.print("[CONNECTING TO WiFi with Network SSID: ");         // inform about connection try
  Serial.print(ssid);                                             // print configured SSID
  Serial.print("] "); 
  WiFi.begin(ssid, password);                                       // start connection process
  while (WiFi.status() != WL_CONNECTED) {                           // try and indicate connecting
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("[CONNECTION SUCCESSFUL]");      // inform about successful connection
  Serial.println("");     
}

void loop() {             // start the continuous readiness of the system  
  delay(1000);
  String tagID ="";     // initialize tagID as empty String in every loop pass

  if (mfrc522.PICC_IsNewCardPresent() && mfrc522.PICC_ReadCardSerial() ) {    // start as soon as a new RFID tag was found and can be read
    tagID = "";                                                               // clean last tagID string as protection against incorrectly initialized tagID
    for (byte i = 0; i < mfrc522.uid.size; i++) {                             
      tagID = tagID + mfrc522.uid.uidByte[i];                                 // add new bytes to the tagID as long as they are available
    } 

    Serial.println("RFID data captured: {\"tagID\":\""+tagID+"\"}");          // output complete tagID in serial (for control)
 
    mfrc522.PICC_HaltA();   // as soon as tag information has been completely read out and tag is still within range, 
                            // put tag to sleep and wait for transmission
   
    Serial.println("[CONNECTING TO SERVER...]");      // inform about connection try
    if (client.connect(server, 80)) {                 // connect to configured server and execute code if successful 
      delay(1000);
      Serial.println("[CONNECTION SUCCESSFUL]");      // inform about successful connection
      Serial.println("[TRANSMITTING RFID DATA...]");  // inform about transmission try
    
      String data = "action=append&tagId=" + tagID;   // prepare RFID tag data to be transmitted
      client.println("POST /api/rfid.php HTTP/1.1");  // transfer html content
      client.print("Host: bs19-team34.bas.uni-koblenz.de\r\n");                 
      client.println("User-Agent: ESP8266/1.0");
      client.println("Connection: close"); 
      client.println("Content-Type: application/x-www-form-urlencoded");
      client.print("Content-Length: ");
      client.print(data.length());
      client.println();
      client.println();
      client.print(data);                               // tranfser prepared RFID tag data
   
      delay(500);
      Serial.println("[TRANSMISSION SUCCESSFUL]");      // inform about successful transmission
      client.stop();                                    // close connection to server 
      // delay(500);
      Serial.println("[CONNECTION TO SERVER CLOSED]");  // inform about successful connection closure
      Serial.println(""); 
      } else {
      Serial.println("[ERROR: CONNECTION NOT SUCCESSFUL ! ! !]"); // inform about connection problem