Advertisement
zykes

Untitled

Nov 6th, 2015
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <NeoPixelBus.h>
  3.  
  4. #include <ESP8266WiFi.h>
  5. #include <PubSubClient.h>
  6. #include <MFRC522.h>
  7.  
  8.  
  9. // CONFIG HERE
  10. const char* DEVICE = "esp-door2-1";
  11.  
  12. const char IDENTIFIER[37] = "4ce4a18f-349b-48f4-a0a0-d5033b2173ec";
  13.  
  14. const char* WIFI_SSID = "SSID";
  15. const char* WIFI_PASS= "PASS";
  16.  
  17. //const char* MQTT_HOST = "10.0.0.6";
  18. const char* MQTT_HOST = "192.168.254.8";
  19. const char* MQTT_REQ_TOPIC = "requests";
  20. const char* MQTT_RESP_TOPIC = "devices";
  21.  
  22. // Pin config
  23. #define RST_PIN D0
  24. #define SS_PIN D8 // SDA / SS
  25. const int RELAY_PIN = D1;
  26. const int LED_PIN = D2;
  27.  
  28.  
  29. // DONT MODIFY BELOW
  30. int lastRead = -1;
  31. char idBuffer[4];
  32. char msgBuffer[200];
  33. bool isReady = false;
  34.  
  35. // Neopixel status LED's
  36. #define colorSaturation 128
  37.  
  38. RgbColor red = RgbColor(colorSaturation, 0, 0);
  39. RgbColor green = RgbColor(0, colorSaturation, 0);
  40. RgbColor blue = RgbColor(0, 0, colorSaturation);
  41. RgbColor white = RgbColor(colorSaturation);
  42. RgbColor black = RgbColor(0);
  43.  
  44. NeoPixelBus statusLeds = NeoPixelBus(2, LED_PIN);
  45.  
  46. int systemLed = 0;
  47. int readerLed= 1;
  48.  
  49. WiFiClient espClient;
  50. PubSubClient client(espClient);
  51.  
  52.  
  53. MFRC522 mfrc522(SS_PIN, RST_PIN);
  54.  
  55.  
  56. void setup() {
  57. char str[200];
  58.  
  59. statusLeds.Begin();
  60. statusLeds.SetPixelColor(systemLed, red);
  61. statusLeds.SetPixelColor(readerLed, red);
  62. statusLeds.Show();
  63.  
  64. // Sets LED to RED when it's bork
  65.  
  66. Serial.begin(115200);
  67. while (!Serial);
  68.  
  69. sprintf(str, "Starting device with point %s", IDENTIFIER);
  70. Serial.println(str);
  71.  
  72. pinMode(RELAY_PIN, OUTPUT);
  73.  
  74. SPI.begin();
  75. mfrc522.PCD_Init();
  76. ShowReaderDetails();
  77.  
  78. setup_wifi();
  79.  
  80. client.setServer(MQTT_HOST, 1883);
  81. client.setCallback(callback);
  82. }
  83.  
  84. void callback(char* topic, byte* payload, unsigned int length) {
  85. Serial.print("Message arrived [");
  86. Serial.print(topic);
  87. Serial.print("] ");
  88. Serial.println("");
  89.  
  90. char *cstring = (char *) payload; // NOT WORKING
  91.  
  92. if (strcmp(cstring, "OPEN") == 0) {
  93. // Indicate that the door is opened
  94. statusLeds.SetPixelColor(readerLed, green);
  95. digitalWrite(RELAY_PIN, HIGH);
  96.  
  97. delay(4000);
  98.  
  99. statusLeds.SetPixelColor(readerLed, white);
  100. digitalWrite(RELAY_PIN, LOW);
  101.  
  102. lastRead = -1;
  103. } else if (strcmp(cstring, "DENIED") == 0) {
  104. statusLeds.SetPixelColor(readerLed, red);
  105. statusLeds.Show();
  106. delay(2000);
  107. statusLeds.SetPixelColor(readerLed, white);
  108. statusLeds.Show();
  109. lastRead = -1;
  110. }
  111. }
  112.  
  113. void ShowReaderDetails() {
  114. // Get the MFRC522 software version
  115. byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
  116. Serial.print(F("MFRC522 Software Version: 0x"));
  117. Serial.print(v, HEX);
  118. if (v == 0x91) {
  119. Serial.print(F(" = v1.0"));
  120. }ย else if (v == 0x92) {
  121. Serial.print(F(" = v2.0"));
  122. } else {
  123. Serial.print(F(" (unknown)"));
  124. }
  125. Serial.println("");
  126. // When 0x00 or 0xFF is returned, communication probably failed
  127. if ((v == 0x00) || (v == 0xFF)) {
  128. Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
  129. }
  130. }
  131.  
  132. void loop() {
  133. if (!client.connected()) {
  134. reconnect();
  135. }
  136. client.loop();
  137.  
  138. if ((lastRead == -1) && (isReady)) {
  139. bool status = getCardId();
  140.  
  141. if (status) {
  142. statusLeds.SetPixelColor(readerLed, blue);
  143. statusLeds.Show();
  144.  
  145. char cardId[50] = {};
  146. int cardIdLen = -1;
  147. get_uuid_as_string(cardId, cardIdLen);
  148.  
  149. Serial.println(cardId);
  150. }
  151. }
  152. }
  153.  
  154. void reconnect() {
  155. // Loop until we're reconnected
  156. while (!client.connected()) {
  157. Serial.println("Attempting MQTT connection...");
  158.  
  159. if (client.connect(DEVICE)) {
  160. char topic[50];
  161. sprintf(topic, "%s/%s", MQTT_RESP_TOPIC, DEVICE);
  162. client.subscribe(topic);
  163. isReady = true;
  164.  
  165. statusLeds.SetPixelColor(systemLed, white);
  166. statusLeds.SetPixelColor(readerLed, white);
  167. statusLeds.Show();
  168.  
  169. Serial.println("Ready for work!");
  170. } else {
  171. statusLeds.SetPixelColor(systemLed, red);
  172. statusLeds.SetPixelColor(readerLed, red);
  173. statusLeds.Show();
  174.  
  175. Serial.print("failed, rc=");
  176. Serial.print(client.state());
  177. Serial.println(" try again in 5 seconds");
  178. // Wait 5 seconds before retrying
  179. delay(5000);
  180. }
  181. }
  182. }
  183.  
  184. void setup_wifi() {
  185. delay(10);
  186. // We start by connecting to a WiFi network
  187. Serial.println();
  188. Serial.print("Connecting to ");
  189. Serial.println(WIFI_SSID);
  190.  
  191. WiFi.begin(WIFI_SSID, WIFI_PASS);
  192.  
  193. while (WiFi.status() != WL_CONNECTED) {
  194. delay(500);
  195. Serial.print(".");
  196. }
  197.  
  198. Serial.println("");
  199. Serial.println("WiFi connected");
  200. Serial.println("IP address: ");
  201. Serial.println(WiFi.localIP());
  202. }
  203.  
  204.  
  205. bool getCardId() {
  206. // Getting ready for Reading PICCs
  207. if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
  208. return false;
  209. }
  210. if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
  211. return false;
  212. }
  213. // There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
  214. // I think we should assume every PICC as they have 4 byte UID
  215. // Until we support 7 byte PICCs
  216. Serial.println(F("Scanned PICC's UID:"));
  217.  
  218. for (byte i = 0; i < mfrc522.uid.size; i++) {
  219. idBuffer[i] = mfrc522.uid.uidByte[i];
  220. Serial.print(idBuffer[i], HEX);
  221. Serial.print(" ");
  222. }
  223.  
  224. mfrc522.PICC_HaltA(); // Stop reading
  225.  
  226. // # Store the last read..
  227. lastRead = millis();
  228. return true;
  229. }
  230.  
  231.  
  232. void get_uuid_as_string(char* result, const unsigned result_length) {
  233. char part[4] = {};
  234. for (int i=0; i < mfrc522.uid.size; i++) {
  235. sprintf(part, "%02x", idBuffer[i]);
  236. strcat(result, part);
  237. }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement