Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. /*
  2. Base de proyectos
  3. */
  4. #include <ESP8266WiFi.h>
  5. #include <PubSubClient.h>
  6. #include <TinyGPS++.h>
  7. #include <SoftwareSerial.h>
  8.  
  9. #define usuarioServidorMQTT "upbpnxzc" // usuario para acceder al broker
  10. #define claveServidorMQTT "OSPLsP4Gq96a" // clave del usuario para acceder al broker
  11. #define servidorMQTT "m11.cloudmqtt.com"
  12. #define puertoMQTT 14224
  13. #define topicoActualizacion "/update"
  14. #define topicoConexion "/conection" //topico que utilizara la opción de birth de mqtt
  15. #define mensajeConexion "1"
  16. #define mensajeDesconexion "0"
  17. #define ledAlarma D3
  18. WiFiClient clienteWifi;
  19. PubSubClient client(clienteWifi);
  20.  
  21. int inputPin = D2; // choose the input pin (for PIR sensor)
  22. int pirState = LOW; // we start, assuming no motion detected
  23. int val = 0; // variable for reading the pin status
  24.  
  25. const char* clienteMQTT = "HackerspaceX";
  26. static const int RXPin = D5, TXPin = D6;
  27. static const uint32_t GPSBaud = 9600;
  28. boolean estadoLed = true;
  29. boolean parpadeoLed = false;
  30. long contadorLed;
  31. long contadorLed2;
  32. int tiempoParpadeo = 500;
  33. // The TinyGPS++ object
  34. TinyGPSPlus gps;
  35. SoftwareSerial ss(RXPin, TXPin)
  36.  
  37.  
  38. void callback(char* topic, byte* payload, unsigned int length) {
  39. // handle message arrived
  40. Serial.println("mensaje entrante");
  41. Serial.println(topic);
  42. if (String(topic) == "/alarmaOn") {
  43. Serial.println("activando alarma");
  44. parpadeoLed = true;
  45. }
  46. if (String(topic) == "/alarmaOff") {
  47. digitalWrite(ledAlarma,LOW);
  48. Serial.println("desactivando alarma");
  49. parpadeoLed = false;
  50. }
  51. }
  52.  
  53. long lastReconnectAttempt = 0;
  54.  
  55. boolean reconnect() {
  56. if (client.connect(clienteMQTT, usuarioServidorMQTT, claveServidorMQTT, topicoConexion, 2, 1, mensajeDesconexion)) {
  57. client.subscribe(topicoActualizacion);
  58. client.subscribe("/alarmaOn");
  59. client.subscribe("/alarmaOff");
  60. client.subscribe(topicoActualizacion);
  61. client.publish(topicoConexion, mensajeConexion);
  62.  
  63. }
  64. return client.connected();
  65. }
  66.  
  67. void setup()
  68. {
  69. pinMode(inputPin, INPUT); // declare sensor as input
  70.  
  71. Serial.begin(115200);
  72. ss.begin(GPSBaud);
  73. delay(2000);
  74. pinMode(ledAlarma, OUTPUT);
  75. pinMode(LED_BUILTIN, OUTPUT);
  76. digitalWrite(ledAlarma, HIGH);
  77. delay(1000);
  78. digitalWrite(ledAlarma, LOW);
  79. delay(1000);
  80. WiFi.begin("HUAWEI P10 1", "cokin124");
  81. while (WiFi.status() != WL_CONNECTED) {
  82. delay(500);
  83. Serial.print(".");
  84. }
  85. Serial.println("");
  86. Serial.println("WiFi connected");
  87. Serial.println("IP address: ");
  88. Serial.println(WiFi.localIP());
  89. Serial.println("iniciando program " );
  90. client.setServer(servidorMQTT, puertoMQTT);
  91. client.setCallback(callback);
  92. lastReconnectAttempt = 0;
  93. }
  94.  
  95.  
  96. void loop()
  97. {
  98. if (!client.connected()) {
  99. long now = millis();
  100. if (now - lastReconnectAttempt > 5000) {
  101. lastReconnectAttempt = now;
  102. // Attempt to reconnect
  103. if (reconnect()) {
  104. lastReconnectAttempt = 0;
  105. }
  106. }
  107. } else {
  108.  
  109. client.loop();
  110. while (ss.available() > 0)
  111. if (gps.encode(ss.read()))
  112. displayInfo();
  113.  
  114. if (millis() > 5000 && gps.charsProcessed() < 10)
  115. {
  116. Serial.println(F("GPS no detectado, revisar cableado."));
  117. while (true);
  118. }
  119.  
  120. val = digitalRead(inputPin); // read input value
  121. if (val == HIGH) { // check if the input is HIGH
  122. digitalWrite(LED_BUILTIN, HIGH); // turn LED ON
  123. if (pirState == LOW) {
  124. // we have just turned on
  125. Serial.println("Motion detected!");
  126. client.publish("/movimiento", "movimiento detectado ");
  127. // We only want to print on the output change, not state
  128. pirState = HIGH;
  129. }
  130. } else {
  131. digitalWrite(LED_BUILTIN, LOW); // turn LED OFF
  132. if (pirState == HIGH) {
  133. // we have just turned of
  134. Serial.println("Motion ended!");
  135. client.publish("/movimiento", "movimiento terminado");
  136. // We only want to print on the output change, not state
  137. pirState = LOW;
  138. }
  139. }
  140. }
  141.  
  142. }
  143.  
  144. void displayInfo()
  145. {
  146. Serial.print(F("Localización: "));
  147. if (gps.location.isValid())
  148. {
  149. Serial.print(gps.location.lat(), 6);
  150. Serial.print(F(","));
  151. Serial.println(gps.location.lng(), 6);
  152. String localizacion = String(gps.location.lat(), 6) + ',' + String(gps.location.lng(), 6);
  153. char dato[localizacion.length() + 1];
  154. localizacion.toCharArray(dato, localizacion.length());
  155. client.publish("/gps", dato);
  156. }
  157. else
  158. {
  159. Serial.println(F("INVALIDO"));
  160.  
  161. client.publish("/gps", "sin señal");
  162. }
  163. contadorLed = millis();
  164. if (contadorLed - contadorLed2 > tiempoParpadeo && parpadeoLed == true) {
  165. contadorLed2 = contadorLed;
  166. Serial.println(estadoLed);
  167. digitalWrite(ledAlarma, estadoLed);
  168. estadoLed = !estadoLed;
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement