Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * --------------------------------------------------------------------------------------------------------------------
- * Example sketch/program showing how to read new NUID from a PICC to serial.
- * --------------------------------------------------------------------------------------------------------------------
- * This is a MFRC522 library example; for further details and other examples see: https://github.com/miguelbalboa/rfid
- *
- *PIN CONNECTIONS :
- *
- * NODE MCU - RFID READER
- * -----------------------------
- * D4 - RST ( OR UNCONNECTED)
- * D5 - SCK
- * D6 - MISO
- * D7 - MOSI
- * D8 - SDA
- *
- * GND - GND
- * 3.3V - 3.3V
- *
- * 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...
- */
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- //download the mfrc522 library first !
- #include <SPI.h>
- #include <MFRC522.h>
- #define SS_PIN D8 //this can be arbitrary
- #define RST_PIN D4 //this can be arbitrary or even unconnected !
- MFRC522 rfid(SS_PIN, RST_PIN); // RFID reader object
- MFRC522::MIFARE_Key key; // rfid card object
- // Init array that will store new NUID (s/n)
- byte nuidPICC[4];
- /// wifi stuff
- const char* ssid = "----";
- const char* password = "****";
- //for the project; make sure you use the cloudmqtt - but you need to register first. See canvas for how.
- //you also need to change one line down in the 'reconnect' function
- //const char *mqtt_server = "mXX.cloudmqtt.com";
- //const int mqtt_port = 17323;
- const char *mqtt_server = "test.mosquitto.org";
- const int mqtt_port = 1883;
- const char *mqtt_user = "test";
- const char *mqtt_pass = "test";
- void callback(char* topic, byte* payload, unsigned int length);
- WiFiClient espClient;
- PubSubClient client(mqtt_server, mqtt_port, callback, espClient);
- ///buffer variable
- char testChar[1000];
- void callback(char* byteArraytopic, byte* byteArrayPayload, unsigned int length) {
- // define a string for topic - they are a little easier to handle..
- String topic;
- //convert topic to a string.
- topic=String(byteArraytopic);
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- // define a string for payload - easier to use..
- String payload;
- // convert the payload to string..
- for (int i = 0; i < length; i++) {
- //Serial.print((char)payload[i]);
- payload += (char)byteArrayPayload[i];
- }
- Serial.println(payload);
- if (String(topic) == "groupXX/OLED/text")
- {
- // do stuff - e.g. write on OLED screen...
- }
- }
- void setup_wifi() {
- delay(10);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- // WiFi.begin("device");
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- delay(1000);
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect:
- // if you are using cloudmqtt, uncomment the following line, and comment the one below...
- // if (client.connect("myclientID","test","test")){ // make sure 'myclientID' is something unique, e.g. 'groupXX'
- if (client.connect("myclientID")) { // make sure 'myclientID' is something unique, e.g. 'groupXX'
- Serial.println("connected");
- // Once connected, publish an announcement...
- client.publish("groupXX/welcomeMessage", "hello world");
- // ... and resubscribe to all messages coming to the 'OLED' topic...
- client.subscribe("OLED/#");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void setup() {
- Serial.begin(115200);
- setup_wifi();
- //setup the mqtt broker
- client.setServer(mqtt_server, mqtt_port);
- client.setCallback(callback);
- //setup the rfid reader
- SPI.begin(); // Init SPI bus
- rfid.PCD_Init(); // Init MFRC522
- for (byte i = 0; i < 6; i++) {
- key.keyByte[i] = 0xFF;
- }
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- // Look for new cards, if no new cards, go the the beginning of the loop...
- if ( ! rfid.PICC_IsNewCardPresent())
- return;
- // Verify if the NUID has been readed, else go the the beginning of the loop...
- if ( ! rfid.PICC_ReadCardSerial())
- return;
- //card type:
- Serial.print(F("PICC type: "));
- MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
- Serial.println(rfid.PICC_GetTypeName(piccType));
- // Check is the PICC of Classic MIFARE type
- if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
- piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
- piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
- Serial.println(F("Your tag is not of type MIFARE Classic."));
- return;
- }
- if (rfid.uid.uidByte[0] != nuidPICC[0] ||
- rfid.uid.uidByte[1] != nuidPICC[1] ||
- rfid.uid.uidByte[2] != nuidPICC[2] ||
- rfid.uid.uidByte[3] != nuidPICC[3] ) {
- Serial.println(F("A new card has been detected."));
- // Store S/N into the buffer array
- for (byte i = 0; i < 4; i++) {
- nuidPICC[i] = rfid.uid.uidByte[i];
- }
- Serial.println(F("The Serial number is:"));
- Serial.print(F("In hex: "));
- printHex(rfid.uid.uidByte, rfid.uid.size); // see what this function does below !
- }
- else Serial.println(F("Card read previously."));
- // Halt PICC
- rfid.PICC_HaltA();
- // Stop encryption on PCD
- rfid.PCD_StopCrypto1();
- }
- /**
- * Helper routine to dump a byte array as hex values to Serial.
- */
- void printHex(byte *buffer, byte bufferSize) {
- String hexArray;
- for (byte i = 0; i < bufferSize; i++) {
- Serial.print(buffer[i] < 0x10 ? " 0" : " ");
- Serial.print(buffer[i], HEX);
- //convert from hex to String
- if(buffer[i]<0x10) hexArray+="0";
- hexArray+=String(buffer[i],HEX);
- }
- //send to the cloud. Change the topic to be published accordingly..
- client.publish("GroupXX/serialNo",hexArray.c_str());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement