Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.35 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <SPI.h>
  3. #include <MFRC522.h>
  4. #include <ESP8266WiFi.h>
  5. #include <EEPROM.h>
  6. #include <Wire.h>
  7. #include <WiFiClient.h>
  8. #include <PubSubClient.h>
  9.  
  10. #define SS_PIN D8
  11. #define RST_PIN D1
  12. MFRC522 mfrc522(SS_PIN, RST_PIN);
  13. unsigned long cardId = 0;
  14.  
  15.  
  16.  
  17. const char* ssid = "FRITZ!Box 6490 Cable_plus";
  18. const char* password = "010166Hamburg";
  19.  
  20. const char* mqtt_server = "technight.duckdns.org";
  21. const char* mqtt_username ="homeassistant-1";
  22. const char* mqtt_password = "t4KG95TT89pLSVSbkk";
  23. const char* clientID = "MusikBox";
  24.  
  25.  
  26. // constants won't change. They're used here to set pin numbers:
  27. const int buttonPin = 2; // the number of the pushbutton pin
  28. //const int ledPin = 13; // the number of the LED pin
  29.  
  30. // Variables will change:
  31. int buttonPushCounter = 0; // counter for the number of button presses
  32. int buttonState = 0; // current state of the button
  33. int lastButtonState = 1; // previous state of the button
  34.  
  35. // RGB LED pins:
  36. int redPin = 16; // RED GPIO pin
  37. int greenPin = 0; // GREEN GPIO pin
  38. int bluePin = 4; // BLUE GPIO pin
  39.  
  40. WiFiClient espClient;
  41. PubSubClient client(espClient);
  42.  
  43.  
  44. void callback(char* topic, byte* payload, unsigned int length) {
  45. String receivedString1 = "000";
  46. String receivedString = "0";
  47. int receivedInt1;
  48. int receivedInt2;
  49. int receivedInt3;
  50. String tmpreceivedString1;
  51. String tmpreceivedString2;
  52. String tmpreceivedString3;
  53.  
  54. Serial.print("Message arrived [");
  55. Serial.print(topic);
  56. Serial.print("] ");
  57. for (int i = 0; i < length; i++) {
  58. char receivedChar = (char)payload[i];
  59. receivedString = String(receivedString+receivedChar);
  60. Serial.print(receivedChar);
  61. }
  62.  
  63. tmpreceivedString1 = receivedString.substring(0,3);
  64. receivedInt1 = tmpreceivedString1.toInt();
  65.  
  66. tmpreceivedString2 = receivedString.substring(4,7);
  67. receivedInt2 = tmpreceivedString2.toInt();
  68.  
  69. tmpreceivedString3 = receivedString.substring(8,11);
  70. receivedInt3 = tmpreceivedString3.toInt();
  71.  
  72. Serial.println();
  73. Serial.println(receivedInt1);
  74. Serial.println(receivedInt2);
  75. Serial.println(receivedInt3);
  76.  
  77.  
  78. }
  79.  
  80. // void callback(char* topic, byte* payload, unsigned int length) {
  81.  
  82. // Serial.print("Message arrived [");
  83. // Serial.print(topic);
  84. // Serial.print("] ");
  85. // for (int i = 0; i < length; i++) {
  86. // char receivedChar = (char)payload[i];
  87. // Serial.print(receivedChar);
  88. // if (receivedChar == '0')
  89. // digitalWrite(LED_BUILTIN, HIGH);
  90. // if (receivedChar == '1')
  91. // digitalWrite(LED_BUILTIN, LOW);
  92. // setColor(receivedInt1, receivedInt2, receivedInt3);
  93. // if (receivedChar == '2')
  94. // setColor(255, 000, 000);
  95. // if (receivedChar == '3')
  96. // setColor(000, 255, 000);
  97. // if (receivedChar == '4')
  98. // setColor(000, 000, 255);
  99.  
  100. // }
  101. // Serial.println();
  102. // Serial.println(receivedInt1);
  103. // Serial.println(receivedInt2);
  104. // Serial.println(receivedInt3);
  105. // Serial.println();
  106. // }
  107.  
  108.  
  109. void setup()
  110. {
  111. {
  112. SPI.begin();
  113. mfrc522.PCD_Init();
  114. client.setServer(mqtt_server, 1883);
  115. client.setCallback(callback);
  116.  
  117. // initialize the button pin as a input:
  118. pinMode(buttonPin, INPUT);
  119. // initialize the LED as an output:
  120. pinMode(LED_BUILTIN, OUTPUT);
  121. // initialize led status communication:
  122. pinMode(redPin, OUTPUT);
  123. pinMode(greenPin, OUTPUT);
  124. pinMode(bluePin, OUTPUT);
  125. digitalWrite(redPin,HIGH);
  126. delay(300);
  127. digitalWrite(redPin,LOW);
  128. delay(300);
  129. digitalWrite(redPin,HIGH);
  130. delay(300);
  131. digitalWrite(redPin,LOW);
  132. // initialize serial communication:
  133. Serial.begin(9600);
  134. }
  135.  
  136. // Connect to WiFinetwork
  137. Serial.println();
  138. Serial.println();
  139. Serial.print("Connecting to ");
  140. Serial.println(ssid);
  141.  
  142. WiFi.begin(ssid, password);
  143.  
  144. while (WiFi.status() != WL_CONNECTED) {
  145. delay(500);
  146. //Serial.begin(9600);
  147. Serial.print(".");
  148. }
  149. Serial.println("");
  150. Serial.println("WiFi connected");
  151. // Serial.begin(9600);
  152. // Start the server
  153. // server.begin();
  154. // Serial.println("Server started");
  155. // Serial.begin(9600);
  156. // Print the IP address
  157. Serial.print("Local IP: ");
  158. Serial.println(WiFi.localIP());
  159. }
  160.  
  161. void reconnect() {
  162. // Loop until we're reconnected
  163. while (!client.connected()) {
  164. Serial.println("Attempting MQTT connection...");
  165. // Attempt to connect
  166. if (client.connect(clientID, mqtt_username, mqtt_password)) {
  167. Serial.println("Connected");
  168. setColor(57, 149, 60); // green
  169. // ... and subscribe to topic
  170. client.subscribe("/technight/Interface/PlayBox/button");
  171. } else {
  172. Serial.print("failed, rc=");
  173. digitalWrite(redPin,HIGH);
  174. Serial.print(client.state());
  175. Serial.println(" try again in 5 seconds");
  176. delay(300);
  177. digitalWrite(redPin,LOW);
  178. // Wait 5 seconds before retrying
  179. delay(3000);
  180. }
  181. }
  182. }
  183.  
  184. void loop() {
  185.  
  186. // // read the state of the pushbutton value:
  187. // buttonState = digitalRead(buttonPin);
  188. //
  189. // // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  190. // if (buttonState == HIGH) {
  191. // // turn LED on:
  192. // digitalWrite(LED_BUILTIN, HIGH);
  193. // } else {
  194. // // turn LED off:
  195. // digitalWrite(LED_BUILTIN, LOW);
  196. // }
  197. //}
  198. //
  199.  
  200. // read the pushbutton input pin:
  201. buttonState = digitalRead(buttonPin);
  202.  
  203. // setColor(255, 0, 0); // red
  204. // Serial.println("ROT");
  205. // delay(2000);
  206. // setColor(255, 0, 255); // red
  207. // Serial.println("LILA");
  208. // delay(2000);
  209. // setColor(0, 255, 0); // green
  210. // Serial.println("GRÜN");
  211. // delay(2000);
  212. // setColor(0, 0, 255); // blue
  213. // Serial.println("BLAU");
  214. // delay(2000);
  215. // setColor(255, 255, 0); // yellow
  216. // Serial.println("GELB");
  217. // delay(2000);
  218. // setColor(80, 0, 80); // purple
  219. // Serial.println("PURPLE");
  220. // delay(2000);
  221. // setColor(255, 50, 0); // Orange
  222. // Serial.println("ORANGE");
  223. // delay(2000);
  224.  
  225. // compare the buttonState to its previous state
  226. if (buttonState != lastButtonState) {
  227. // if the state has changed, increment the counter
  228. if (buttonState == HIGH) {
  229. client.publish("/technight/Interface/PlayBox/button", "OFF"); //
  230. // if the current state is HIGH then the button
  231. // went from off to on:
  232. } else {
  233. // if the current state is LOW then the button
  234. // went from on to off:
  235. client.publish("/technight/Interface/PlayBox/button", "ON"); //
  236. }
  237. // Delay a little bit to avoid bouncing
  238. delay(100);
  239. }
  240. // save the current state as the last state,
  241. //for next time through the loop
  242. lastButtonState = buttonState;
  243.  
  244. {
  245. if (!client.connected()) {
  246. reconnect();
  247. }
  248. client.loop();
  249. }
  250. if (!mfrc522.PICC_IsNewCardPresent()) {
  251. return;
  252. }
  253.  
  254. if (!mfrc522.PICC_ReadCardSerial()) {
  255. return;
  256. }
  257.  
  258. cardId = getCardId();
  259.  
  260. digitalWrite(greenPin,LOW);
  261. digitalWrite(bluePin,HIGH);
  262. Serial.print("Message arrived [/technight/Interface/PlayBox] ");
  263. Serial.println(cardId);
  264.  
  265. char buffer[10];
  266. sprintf(buffer, "%lu", cardId);
  267. client.publish("/technight/Interface/PlayBox", buffer);
  268. delay(200);
  269. digitalWrite(bluePin,LOW);
  270. setColor(57, 149, 60); // green
  271.  
  272. uint8_t control = 0x00;
  273.  
  274. do {
  275. control = 0;
  276. for (int i = 0; i < 3; i++) {
  277. if (!mfrc522.PICC_IsNewCardPresent()) {
  278. if (mfrc522.PICC_ReadCardSerial()) {
  279. control |= 0x16;
  280. }
  281.  
  282. if (mfrc522.PICC_ReadCardSerial()) {
  283. control |= 0x16;
  284. }
  285.  
  286. control += 0x1;
  287. }
  288.  
  289. control += 0x4;
  290. }
  291.  
  292. delay(0);
  293. } while (control == 13 || control == 14);
  294.  
  295. reconnect();
  296.  
  297. Serial.println("Message arrived [/technight/Interface/PlayBox] 1");
  298. client.publish("/technight/Interface/PlayBox", "1");
  299. delay(500);
  300.  
  301. mfrc522.PICC_HaltA();
  302. mfrc522.PCD_StopCrypto1();
  303. }
  304.  
  305. unsigned long getCardId() {
  306. byte readCard[4];
  307. for (int i = 0; i < 4; i++) {
  308. readCard[i] = mfrc522.uid.uidByte[i];
  309. }
  310.  
  311. return (unsigned long)readCard[0] << 24
  312. | (unsigned long)readCard[1] << 16
  313. | (unsigned long)readCard[2] << 8
  314. | (unsigned long)readCard[3];
  315. }
  316.  
  317. void setColor(int red, int green, int blue)
  318. {
  319. analogWrite(redPin, red);
  320. analogWrite(greenPin, green);
  321. analogWrite(bluePin, blue);
  322. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement