Advertisement
gozkil

rfid to mqtt

Apr 4th, 2017
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.43 KB | None | 0 0
  1. /*
  2.  * --------------------------------------------------------------------------------------------------------------------
  3.  * Example sketch/program showing how to read new NUID from a PICC to serial.
  4.  * --------------------------------------------------------------------------------------------------------------------
  5.  * This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
  6.  *
  7.  *PIN CONNECTIONS :
  8.  *
  9.  * NODE MCU -   RFID READER
  10.  * -----------------------------
  11.  * D4       -   RST ( OR UNCONNECTED)
  12.  * D5       -   SCK
  13.  * D6       -   MISO
  14.  * D7       -   MOSI
  15.  * D8       -   SDA
  16.  *
  17.  * GND      -   GND
  18.  * 3.3V     -   3.3V
  19.  *
  20.  
  21.  * There are a few things you need to change for this to work: wifi ssid/ pw;  mqtt broker address, port, clientID; and the topics you want to publish to / subscribe...
  22.  */
  23.  
  24.  
  25.  
  26. #include <ESP8266WiFi.h>
  27. #include <PubSubClient.h>
  28.  
  29. //download the mfrc522 library first !
  30. #include <SPI.h>
  31. #include <MFRC522.h>
  32.  
  33. #define SS_PIN D8 //this can be arbitrary
  34. #define RST_PIN D4 //this can be arbitrary or even unconnected !
  35.  
  36. MFRC522 rfid(SS_PIN, RST_PIN); // RFID reader object
  37.  
  38. MFRC522::MIFARE_Key key;  // rfid card object
  39.  
  40. // Init array that will store new NUID (s/n)
  41. byte nuidPICC[4];
  42.  
  43.  
  44. /// wifi stuff
  45.  
  46. const char* ssid = "----";
  47. const char* password = "****";
  48.  
  49. //for the project; make sure you use the cloudmqtt - but you need to register first. See canvas for how.
  50. //you also need to change one line down in the 'reconnect' function
  51.  
  52. //const char *mqtt_server = "mXX.cloudmqtt.com";
  53. //const int mqtt_port = 17323;
  54. const char *mqtt_server = "test.mosquitto.org";
  55. const int mqtt_port = 1883;
  56. const char *mqtt_user = "test";
  57. const char *mqtt_pass = "test";
  58.  
  59. void callback(char* topic, byte* payload, unsigned int length);
  60.  
  61.  
  62. WiFiClient espClient;
  63. PubSubClient client(mqtt_server, mqtt_port, callback, espClient);
  64.  
  65. ///buffer variable
  66. char testChar[1000];
  67.  
  68. void callback(char* byteArraytopic, byte* byteArrayPayload, unsigned int length) {
  69.  
  70. // define a string for topic - they are a little easier to handle..
  71.   String topic;
  72.  
  73.   //convert topic to a string.
  74.   topic=String(byteArraytopic);
  75.  
  76.   Serial.print("Message arrived [");
  77.   Serial.print(topic);
  78.   Serial.print("] ");
  79.  
  80.   // define a string for payload - easier to use..
  81.   String payload;
  82.   // convert the payload to string..
  83.   for (int i = 0; i < length; i++) {
  84.     //Serial.print((char)payload[i]);
  85.     payload += (char)byteArrayPayload[i];
  86.   }
  87.  
  88.   Serial.println(payload);
  89.  
  90.  
  91.  
  92.   if (String(topic) == "groupXX/OLED/text")
  93.   {
  94.       // do stuff - e.g. write on OLED screen...  
  95.   }
  96.  
  97.  
  98. }
  99. void setup_wifi() {
  100.  
  101.   delay(10);
  102.   // We start by connecting to a WiFi network
  103.   Serial.println();
  104.   Serial.print("Connecting to ");
  105.   Serial.println(ssid);
  106.  
  107.  
  108.   WiFi.begin(ssid, password);
  109.   // WiFi.begin("device");
  110.  
  111.   while (WiFi.status() != WL_CONNECTED) {
  112.     delay(500);
  113.     Serial.print(".");
  114.   }
  115.  
  116.   Serial.println("");
  117.   Serial.println("WiFi connected");
  118.   Serial.println("IP address: ");
  119.   Serial.println(WiFi.localIP());
  120.   delay(1000);
  121. }
  122.  
  123.  
  124. void reconnect() {
  125.   // Loop until we're reconnected
  126.   while (!client.connected()) {
  127.     Serial.print("Attempting MQTT connection...");
  128.     // Attempt to connect:
  129.     // if you are using cloudmqtt, uncomment the following line, and comment the one below...
  130.     //  if (client.connect("myclientID","test","test")){  // make sure 'myclientID' is something unique, e.g. 'groupXX'
  131.     if (client.connect("myclientID")) { // make sure 'myclientID' is something unique, e.g. 'groupXX'  
  132.  
  133.       Serial.println("connected");
  134.       // Once connected, publish an announcement...
  135.       client.publish("groupXX/welcomeMessage", "hello world");
  136.       // ... and resubscribe to all messages coming to the 'OLED' topic...
  137.  
  138.       client.subscribe("OLED/#");
  139.  
  140.     } else {
  141.       Serial.print("failed, rc=");
  142.       Serial.print(client.state());
  143.       Serial.println(" try again in 5 seconds");
  144.       // Wait 5 seconds before retrying
  145.       delay(5000);
  146.     }
  147.   }
  148. }
  149.  
  150. void setup() {
  151. Serial.begin(115200);
  152.   setup_wifi();
  153.  
  154.   //setup the mqtt broker
  155.   client.setServer(mqtt_server, mqtt_port);
  156.   client.setCallback(callback);  
  157.  
  158.   //setup the rfid reader
  159.   SPI.begin(); // Init SPI bus
  160.   rfid.PCD_Init(); // Init MFRC522
  161.  
  162.   for (byte i = 0; i < 6; i++) {
  163.     key.keyByte[i] = 0xFF;
  164.   }
  165.  
  166. }
  167.  
  168. void loop() {
  169.  
  170.  
  171.   if (!client.connected()) {
  172.     reconnect();
  173.   }
  174.   client.loop();
  175.  
  176.  
  177.   // Look for new cards, if no new cards, go the the beginning of the loop...
  178.   if ( ! rfid.PICC_IsNewCardPresent())
  179.     return;
  180.  
  181.   // Verify if the NUID has been readed, else go the the beginning of the loop...
  182.   if ( ! rfid.PICC_ReadCardSerial())
  183.     return;
  184.  
  185.   //card type:
  186.   Serial.print(F("PICC type: "));
  187.   MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  188.   Serial.println(rfid.PICC_GetTypeName(piccType));
  189.  
  190.   // Check is the PICC of Classic MIFARE type
  191.   if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&  
  192.     piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  193.     piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  194.     Serial.println(F("Your tag is not of type MIFARE Classic."));
  195.     return;
  196.   }
  197.  
  198.   if (rfid.uid.uidByte[0] != nuidPICC[0] ||
  199.     rfid.uid.uidByte[1] != nuidPICC[1] ||
  200.     rfid.uid.uidByte[2] != nuidPICC[2] ||
  201.     rfid.uid.uidByte[3] != nuidPICC[3] ) {
  202.     Serial.println(F("A new card has been detected."));
  203.  
  204.     // Store S/N  into the buffer array
  205.     for (byte i = 0; i < 4; i++) {
  206.       nuidPICC[i] = rfid.uid.uidByte[i];
  207.     }
  208.    
  209.     Serial.println(F("The Serial number  is:"));
  210.     Serial.print(F("In hex: "));
  211.     printHex(rfid.uid.uidByte, rfid.uid.size); // see what this function does below !
  212.    
  213.   }
  214.   else Serial.println(F("Card read previously."));
  215.  
  216.   // Halt PICC
  217.   rfid.PICC_HaltA();
  218.  
  219.   // Stop encryption on PCD
  220.   rfid.PCD_StopCrypto1();
  221. }
  222.  
  223.  
  224. /**
  225.  * Helper routine to dump a byte array as hex values to Serial.
  226.  */
  227. void printHex(byte *buffer, byte bufferSize) {
  228.   String hexArray;
  229.   for (byte i = 0; i < bufferSize; i++) {
  230.     Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  231.     Serial.print(buffer[i], HEX);
  232.  
  233.     //convert from hex to String
  234.     if(buffer[i]<0x10) hexArray+="0";
  235.     hexArray+=String(buffer[i],HEX);
  236.   }
  237.  
  238.   //send to the cloud. Change the topic to be published accordingly..
  239.   client.publish("GroupXX/serialNo",hexArray.c_str());
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement