From 88695dee49f2f1de7379879851fbc51d1b973a73 Mon Sep 17 00:00:00 2001 From: Elias Waschin-Scholvin <> Date: Mon, 28 Jun 2021 12:09:06 +0200 Subject: [PATCH] Added first sketch --- arduino/sketch_main/sketch_main.ino | 196 ++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 arduino/sketch_main/sketch_main.ino diff --git a/arduino/sketch_main/sketch_main.ino b/arduino/sketch_main/sketch_main.ino new file mode 100644 index 0000000..e6cb2e7 --- /dev/null +++ b/arduino/sketch_main/sketch_main.ino @@ -0,0 +1,196 @@ +#include <SPI.h> +#include <MFRC522.h> +#include <WiFiNINA_Generic.h> + +// ----- WIFI ------- +char ssid[] = "Lab@SmartLab_2G"; +char pass[] = "its.0021!7A"; +int status = WL_IDLE_STATUS; // the Wi-Fi radio's status +int ledState = LOW; //ledState used to set the LED +unsigned long previousMillisInfo = 0; //will store last time Wi-Fi information was updated +unsigned long previousMillisLED = 0; // will store the last time LED was updated +const int intervalInfo = 5000; // interval at which to update the board information + +// ----- RFID------- +const uint8_t RST_PIN = 9; // reset pin +const uint8_t SS_PIN = 10; // serial data pin +#define RESET_TIME 3000 +MFRC522 rfid(SS_PIN, RST_PIN); // create MFRC522 instance +MFRC522::MIFARE_Key key; + +// Init array that will store new NUID +byte nuidPICC[4]; +unsigned long lastReadTS; + +void connectToWifi() { + // set the LED as output + pinMode(LED_BUILTIN, OUTPUT); + + // attempt to connect to Wi-Fi network: + while (status != WL_CONNECTED) { + Serial.print("Attempting to connect to network: "); + Serial.println(ssid); + // Connect to WPA/WPA2 network: + status = WiFi.begin(ssid, pass); + + // wait 10 seconds for connection: + delay(10000); + } + + // you're connected now, so print out the data: + Serial.println("You're connected to the network"); + Serial.println("---------------------------------------"); +} + +void printWifiInfo(){ + unsigned long currentMillisInfo = millis(); + + // check if the time after the last update is bigger the interval + if (currentMillisInfo - previousMillisInfo >= intervalInfo) { + previousMillisInfo = currentMillisInfo; + + Serial.println("Board Information:"); + // print your board's IP address: + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + // print your network's SSID: + Serial.println(); + Serial.println("Network Information:"); + Serial.print("SSID: "); + Serial.println(WiFi.SSID()); + + // print the received signal strength: + long rssi = WiFi.RSSI(); + Serial.print("signal strength (RSSI):"); + Serial.println(rssi); + Serial.println("---------------------------------------"); + } + + unsigned long currentMillisLED = millis(); + + // measure the signal strength and convert it into a time interval + int intervalLED = WiFi.RSSI() * -10; + + // check if the time after the last blink is bigger the interval + if (currentMillisLED - previousMillisLED >= intervalLED) { + previousMillisLED = currentMillisLED; + + // if the LED is off turn it on and vice-versa: + if (ledState == LOW) { + ledState = HIGH; + } else { + ledState = LOW; + } + + // set the LED with the ledState of the variable: + digitalWrite(LED_BUILTIN, ledState); + } +} + +void initRFID(){ + SPI.begin(); // Init SPI bus + rfid.PCD_Init(); // Init MFRC522 + + for (byte i = 0; i < 6; i++) { + key.keyByte[i] = 0xFF; + } + + Serial.println(F("This code scan the MIFARE Classsic NUID.")); + Serial.print(F("Using the following key:")); + printHex(key.keyByte, MFRC522::MF_KEY_SIZE); +} + +void readRFID() { + // Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle. + if ( ! rfid.PICC_IsNewCardPresent()) + return; + + // Verify if the NUID has been readed + if ( ! rfid.PICC_ReadCardSerial()) + return; + + 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; + } + + unsigned long test = millis() - lastReadTS; + Serial.println("Milis "); + Serial.println(test); + + 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] || + millis() - lastReadTS > RESET_TIME + ) { + Serial.println(F("A new card has been detected.")); + + // Store NUID into nuidPICC array + for (byte i = 0; i < 4; i++) { + nuidPICC[i] = rfid.uid.uidByte[i]; + } + + Serial.println(F("The NUID tag is:")); + Serial.print(F("In hex: ")); + printHex(rfid.uid.uidByte, rfid.uid.size); + Serial.println(); + Serial.print(F("In dec: ")); + printDec(rfid.uid.uidByte, rfid.uid.size); + Serial.println(); + } + else Serial.println(F("Card read previously.")); + + lastReadTS = millis(); + + // Halt PICC + rfid.PICC_HaltA(); + + // Stop encryption on PCD + rfid.PCD_StopCrypto1(); +} + + +void setup() { + Serial.begin(9600); // start serial connection + + while (!Serial); + + connectToWifi(); + initRFID(); + printWifiInfo(); +} + +void loop() { + readRFID(); +} + + +/** + * Helper routine to dump a byte array as hex values to Serial. + */ +void printHex(byte *buffer, byte bufferSize) { + for (byte i = 0; i < bufferSize; i++) { + Serial.print(buffer[i] < 0x10 ? " 0" : " "); + Serial.print(buffer[i], HEX); + } +} + +/** + * Helper routine to dump a byte array as dec values to Serial. + */ +void printDec(byte *buffer, byte bufferSize) { + for (byte i = 0; i < bufferSize; i++) { + Serial.print(buffer[i] < 0x10 ? " 0" : " "); + Serial.print(buffer[i], DEC); + } +} -- GitLab