Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <SPI.h>
  4. #include <MFRC522.h>
  5. #include <Wire.h>
  6. #include <LiquidCrystal_I2C.h>
  7.  
  8. const int SS_PIN = 4;
  9. const int RST_PIN = 5;
  10. const int LOCK = 16;
  11.  
  12. // WIFI
  13. const char* ssid = "Edelweiss";
  14. const char* password = "homesweethome";
  15. const char* mqtt_server = "192.168.0.212";
  16.  
  17. bool lockState = false;
  18.  
  19. MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
  20. WiFiClient espClient;
  21. PubSubClient client(espClient);
  22.  
  23. String getString(byte* payload, unsigned int length) {
  24.   String tmp = "";
  25.   for (int i = 0; i < length; i++)  tmp += (char)payload[i];
  26.   return tmp;
  27. }
  28.  
  29. void setup_lock() {
  30.   pinMode(LOCK, OUTPUT);
  31. }
  32.  
  33. void setup_wifi() {
  34.   Serial.println("Connecting");
  35.   WiFi.begin(ssid, password);
  36.  
  37.   while (WiFi.status() != WL_CONNECTED) {
  38.     delay(500);
  39.     Serial.print(".");
  40.   }
  41.   randomSeed(micros());
  42. }
  43.  
  44. void setup_rfid() {
  45.   SPI.begin();      // Initiate  SPI bus
  46.   mfrc522.PCD_Init();   // Initiate MFRC522
  47. }
  48.  
  49.  
  50. void findNewCards() {
  51.   if ( ! mfrc522.PICC_IsNewCardPresent() || ! mfrc522.PICC_ReadCardSerial()) return;
  52.   delay(2000);
  53. }
  54.  
  55. String getRFIDUID() {
  56.  
  57.   findNewCards();
  58.  
  59.   Serial.print("UID tag :");
  60.   String content = "";
  61.   char message_buff[100] = "";
  62.  
  63.   for (byte i = 0; i < mfrc522.uid.size; i++){
  64.     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : ""));
  65.     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  66.   }
  67.  
  68.   content.toUpperCase();
  69.   content.toCharArray(message_buff, content.length() + 1);
  70.   client.publish("cardUID", message_buff);
  71.  
  72.   return message_buff;
  73. }
  74.  
  75.  
  76. void callback(char* topic, byte * payload, unsigned int length) {
  77.  
  78.   Serial.println("Dit is de payload van de callback");
  79.   Serial.println(getString(payload, length));
  80.  
  81.   if (getString(payload, length) == "true") {
  82.     digitalWrite(LOCK, HIGH);
  83.     Serial.println("Slot open");
  84.   }
  85.  
  86.   if (getString(payload, length) == "false") {
  87.     digitalWrite(LOCK, LOW);
  88.     Serial.println("Slot dicht");
  89.   }
  90. }
  91.  
  92. void reconnect() {
  93.  
  94.   // Loop until we're reconnected
  95.   while (!client.connected()) {
  96.     Serial.println("Attempting MQTT connection...");
  97.     // Create a random client ID
  98.     String clientId = "ESP8266Client-";
  99.     clientId += String(random(0xffff), HEX);
  100.     // Attempt to connect
  101.     if (client.connect(clientId.c_str())) {
  102.       client.subscribe("acces");
  103.     }
  104.     else {
  105.       Serial.print("failed, rc=");
  106.       Serial.print(client.state());
  107.       Serial.println(" try again in 5 seconds");
  108.       // Wait 5 seconds before retrying
  109.       delay(5000);
  110.     }
  111.   }
  112. }
  113.  
  114.  
  115. void setup() {
  116.   Serial.begin(115200);
  117.   setup_wifi();
  118.   setup_rfid();
  119.   setup_lock();
  120.  
  121.   client.setServer(mqtt_server, 1883);
  122.   client.setCallback(callback);
  123. }
  124.  
  125. void loop() {
  126.   if (!client.connected()) reconnect();
  127.   client.loop();
  128.  
  129.   delay(500);
  130.   getRFIDUID();
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement