Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <NeoPixelBus.h>
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <MFRC522.h>
- // CONFIG HERE
- const char* DEVICE = "esp-door2-1";
- const char IDENTIFIER[37] = "4ce4a18f-349b-48f4-a0a0-d5033b2173ec";
- const char* WIFI_SSID = "SSID";
- const char* WIFI_PASS= "PASS";
- //const char* MQTT_HOST = "10.0.0.6";
- const char* MQTT_HOST = "192.168.254.8";
- const char* MQTT_REQ_TOPIC = "requests";
- const char* MQTT_RESP_TOPIC = "devices";
- // Pin config
- #define RST_PIN D0
- #define SS_PIN D8 // SDA / SS
- const int RELAY_PIN = D1;
- const int LED_PIN = D2;
- // DONT MODIFY BELOW
- int lastRead = -1;
- char idBuffer[4];
- char msgBuffer[200];
- bool isReady = false;
- // Neopixel status LED's
- #define colorSaturation 128
- RgbColor red = RgbColor(colorSaturation, 0, 0);
- RgbColor green = RgbColor(0, colorSaturation, 0);
- RgbColor blue = RgbColor(0, 0, colorSaturation);
- RgbColor white = RgbColor(colorSaturation);
- RgbColor black = RgbColor(0);
- NeoPixelBus statusLeds = NeoPixelBus(2, LED_PIN);
- int systemLed = 0;
- int readerLed= 1;
- WiFiClient espClient;
- PubSubClient client(espClient);
- MFRC522 mfrc522(SS_PIN, RST_PIN);
- void setup() {
- char str[200];
- statusLeds.Begin();
- statusLeds.SetPixelColor(systemLed, red);
- statusLeds.SetPixelColor(readerLed, red);
- statusLeds.Show();
- // Sets LED to RED when it's bork
- Serial.begin(115200);
- while (!Serial);
- sprintf(str, "Starting device with point %s", IDENTIFIER);
- Serial.println(str);
- pinMode(RELAY_PIN, OUTPUT);
- SPI.begin();
- mfrc522.PCD_Init();
- ShowReaderDetails();
- setup_wifi();
- client.setServer(MQTT_HOST, 1883);
- client.setCallback(callback);
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- Serial.println("");
- char *cstring = (char *) payload; // NOT WORKING
- if (strcmp(cstring, "OPEN") == 0) {
- // Indicate that the door is opened
- statusLeds.SetPixelColor(readerLed, green);
- digitalWrite(RELAY_PIN, HIGH);
- delay(4000);
- statusLeds.SetPixelColor(readerLed, white);
- digitalWrite(RELAY_PIN, LOW);
- lastRead = -1;
- } else if (strcmp(cstring, "DENIED") == 0) {
- statusLeds.SetPixelColor(readerLed, red);
- statusLeds.Show();
- delay(2000);
- statusLeds.SetPixelColor(readerLed, white);
- statusLeds.Show();
- lastRead = -1;
- }
- }
- void ShowReaderDetails() {
- // Get the MFRC522 software version
- byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
- Serial.print(F("MFRC522 Software Version: 0x"));
- Serial.print(v, HEX);
- if (v == 0x91) {
- Serial.print(F(" = v1.0"));
- }ย else if (v == 0x92) {
- Serial.print(F(" = v2.0"));
- } else {
- Serial.print(F(" (unknown)"));
- }
- Serial.println("");
- // When 0x00 or 0xFF is returned, communication probably failed
- if ((v == 0x00) || (v == 0xFF)) {
- Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
- }
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- if ((lastRead == -1) && (isReady)) {
- bool status = getCardId();
- if (status) {
- statusLeds.SetPixelColor(readerLed, blue);
- statusLeds.Show();
- char cardId[50] = {};
- int cardIdLen = -1;
- get_uuid_as_string(cardId, cardIdLen);
- Serial.println(cardId);
- }
- }
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.println("Attempting MQTT connection...");
- if (client.connect(DEVICE)) {
- char topic[50];
- sprintf(topic, "%s/%s", MQTT_RESP_TOPIC, DEVICE);
- client.subscribe(topic);
- isReady = true;
- statusLeds.SetPixelColor(systemLed, white);
- statusLeds.SetPixelColor(readerLed, white);
- statusLeds.Show();
- Serial.println("Ready for work!");
- } else {
- statusLeds.SetPixelColor(systemLed, red);
- statusLeds.SetPixelColor(readerLed, red);
- statusLeds.Show();
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void setup_wifi() {
- delay(10);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(WIFI_SSID);
- WiFi.begin(WIFI_SSID, WIFI_PASS);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- bool getCardId() {
- // Getting ready for Reading PICCs
- if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
- return false;
- }
- if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
- return false;
- }
- // There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
- // I think we should assume every PICC as they have 4 byte UID
- // Until we support 7 byte PICCs
- Serial.println(F("Scanned PICC's UID:"));
- for (byte i = 0; i < mfrc522.uid.size; i++) {
- idBuffer[i] = mfrc522.uid.uidByte[i];
- Serial.print(idBuffer[i], HEX);
- Serial.print(" ");
- }
- mfrc522.PICC_HaltA(); // Stop reading
- // # Store the last read..
- lastRead = millis();
- return true;
- }
- void get_uuid_as_string(char* result, const unsigned result_length) {
- char part[4] = {};
- for (int i=0; i < mfrc522.uid.size; i++) {
- sprintf(part, "%02x", idBuffer[i]);
- strcat(result, part);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement