Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #ifdef ARDUINO_ARCH_ESP32
  2. #include <WiFi.h>
  3. #include <WiFiMulti.h>
  4. WiFiMulti wifiMulti;
  5. #else
  6. #include <ESP8266WiFi.h>
  7. #include <ESP8266WiFiMulti.h>
  8. ESP8266WiFiMulti wifiMulti;
  9. #endif
  10. #include "PCD8544.h"
  11. #include "driver/gpio.h"
  12. #include <MQTT.h>
  13. #include <Wire.h>
  14. static PCD8544 lcd = PCD8544(14, 13, 27, 26, 15);
  15.  
  16. int PersonaX = 0;
  17. int PersonaXa = 0;
  18.  
  19. const char ssid1[] = "ALSWl";
  20. const char pass1[] = "25264897";
  21. const char ssid2[] = "ALSW2";
  22. const char pass2[] = "7210-3607";
  23. const char ssid3[] = "ssid";
  24. const char pass3[] = "pass";
  25.  
  26. boolean estadoCuna = false;
  27. boolean estadoPuerta = false;
  28.  
  29. WiFiClient net;
  30. MQTTClient client;
  31.  
  32.  
  33.  
  34. unsigned long lastMillis = 0;
  35.  
  36. void Conectar() {
  37. Serial.print("Conectandoo Al Wifi...");
  38.  
  39. lcd.clear();
  40. lcd.setCursor(0, 0);
  41. lcd.print("Conectando");
  42. lcd.setCursor(0, 1);
  43. lcd.print("Wifi");
  44. while (wifiMulti.run() != WL_CONNECTED) {
  45. Serial.print(".");
  46. lcd.setCursor(30, 1);
  47. lcd.print("|");
  48. delay(500);
  49. lcd.setCursor(30, 1);
  50. lcd.print("-");
  51. delay(500);
  52. }
  53.  
  54. Serial.print("\nConectado con Wifi\n Buscando MQTT");
  55. lcd.clear();
  56. lcd.setCursor(0, 0);
  57. lcd.print("Conectando");
  58. lcd.setCursor(0, 1);
  59. lcd.print("MQTT");
  60. delay (500);
  61. while (!client.connect("Pantalla", "Pulseraxd", "12345678")) {
  62. Serial.print("*");
  63. lcd.setCursor(30, 1);
  64. lcd.print("|");
  65. digitalWrite(LED_BUILTIN , 1);
  66. delay(500);
  67. lcd.setCursor(30, 1);
  68. lcd.print("-");
  69. digitalWrite(LED_BUILTIN , 0);
  70. delay(500);
  71. }
  72.  
  73. Serial.println("\nSistema Conectado");
  74. digitalWrite(LED_BUILTIN , 1);
  75. client.subscribe("/Pulsera/Puerta/Principal");
  76. client.subscribe("/Pulsera/Cuna");
  77. // client.unsubscribe("/hello");
  78. }
  79.  
  80. void MensajeRecibido(String &topic, String &payload) {
  81. Serial.println("Mensaje: " + topic + " - " + payload);
  82. if (topic == "/Pulsera/Cuna") {
  83. if (payload == "1") {
  84. estadoCuna = true;
  85. } else {
  86. estadoCuna = false;
  87. }
  88. }
  89. if (topic == "/Pulsera/Puerta/Principal") {
  90. if (payload == "1") {
  91. estadoPuerta = true;
  92. } else {
  93. estadoPuerta = false;
  94. }
  95. }
  96. }
  97.  
  98. void setup() {
  99. gpio_set_direction( GPIO_NUM_23, GPIO_MODE_OUTPUT);
  100. gpio_set_level( GPIO_NUM_23, 1);
  101. lcd.begin(84, 48);
  102. lcd.clear();
  103. lcd.setCursor(0, 0);
  104. lcd.print("Iniciando programa");
  105.  
  106. Serial.begin(115200);
  107. WiFi.mode(WIFI_STA);//Cambiar modo del Wi-Fi
  108. delay(100);
  109. wifiMulti.addAP(ssid1, pass1);
  110. wifiMulti.addAP(ssid2, pass2);
  111. wifiMulti.addAP(ssid3, pass3);
  112. pinMode(LED_BUILTIN, OUTPUT);
  113. client.begin("broker.shiftr.io", net);
  114. client.onMessage(MensajeRecibido);
  115.  
  116. Conectar();
  117. }
  118.  
  119. void loop() {
  120. client.loop();
  121. delay(10);
  122.  
  123. if (!client.connected()) {
  124. Conectar();
  125. }
  126.  
  127. if (millis() - lastMillis > 500) {
  128. lastMillis = millis();
  129. lcd.clear();
  130. lcd.setCursor (0, 1);
  131. lcd.print ("Puerta Princ.");
  132. lcd.setCursor (0, 4);
  133. lcd.print("Proyecto final");
  134.  
  135. lcd.setCursor (10, 2);
  136. if (estadoCuna) {
  137. lcd.print("Actividad");
  138. } else {
  139. lcd.print("Inactividad");
  140. }
  141.  
  142. lcd.setCursor (10, 5);
  143. if (estadoPuerta) {
  144. lcd.print("Actividad");
  145. delay(1000);
  146. } else {
  147. lcd.print("Inactividad");
  148. }
  149.  
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement