augusto0112

AlarmaNodeMcu

Nov 18th, 2016
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.46 KB | None | 0 0
  1. /*
  2.  * ********** Arduino **********
  3.  * Autor: Cristian Augusto Restrepo Lenis
  4.  * Fecha: 17/11/2016
  5.  * Descripción:
  6.  * Enviar un correo electrónico a través de
  7.  * SMTP2GO mediante NodeMcu cuando se vulnere la seguridad.
  8.  * URL: https://embebidoscar.blogspot.com.co/p/proyecto-final.html
  9. */
  10.  
  11. //Librerías Matriz 8x8 MAX7219 y LED 5050 CJMCU-2812-8
  12. #include<FastLED.h>
  13. #include "LedMatrix.h"
  14.  
  15. //Librerías para enviar e-mail y conectar a internet
  16. #include <SPI.h>
  17. #include <ESP8266WiFi.h>
  18. #include <ESP8266WiFiAP.h>
  19. #include <ESP8266WiFiGeneric.h>
  20. #include <ESP8266WiFiMulti.h>
  21. #include <ESP8266WiFiScan.h>
  22. #include <ESP8266WiFiSTA.h>
  23. #include <ESP8266WiFiType.h>
  24. #include <WiFiClient.h>
  25. #include <WiFiClientSecure.h>
  26. #include <WiFiServer.h>
  27. #include <WiFiUdp.h>
  28. //Fin librerías
  29.  
  30. //Conexion WiFi y Cliente SMTP
  31. const char* ssid = "USC-AIREH-LIBRE";
  32. const char* password = "";
  33. //char server[] = "smtpcorp.com";
  34. char server[] = "mail.smtp2go.com";
  35. int port = 2525;
  36. WiFiClient client;
  37. //Fin Conexion WiFi y Cliente SMTP
  38.  
  39. //LED's 5050
  40. #define NUM_LEDS 8
  41. CRGBArray<NUM_LEDS> leds;
  42. char val;
  43. //Fin LED's 5050
  44.  
  45. //Matriz 8x8 Max7219
  46. #define NUMBER_OF_DEVICES 1
  47. #define CS_PIN 2
  48. LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CS_PIN);
  49. int x = 0;
  50. //Fin Matriz 8x8 Max7219
  51.  
  52. //Se inicializa la variable "relog" para cnfiguracion Email
  53. int clockMail = 0;
  54. //Fin inicializacion
  55.  
  56. void setup() {
  57.   //Otros
  58.   pinMode(5,INPUT);
  59.   pinMode(4,INPUT);
  60.   //Fin Otros
  61.  
  62.   //LED's 5050
  63.   FastLED.addLeds<NEOPIXEL,8>(leds, NUM_LEDS);
  64.   //Fin LED'S 5050
  65.  
  66.   //Matriz 8x8 Max7219
  67.   ledMatrix.init();
  68.   //Mensaje que carga al inicio, cuando no se han enviado datos
  69.   ledMatrix.setText("A L A R M");
  70.   //Segundo mensaje
  71.   //ledMatrix.setNextText(";)");
  72.   //Fin Matriz 8x8 Max7219
  73.  
  74.   //Conexion WiFi y Cliente SMTP
  75.   Serial.begin(115200);
  76.   pinMode(12,OUTPUT);
  77.   digitalWrite(12,HIGH);
  78.   WiFi.begin(ssid, password);
  79.   delay(2000);
  80.   Serial.println("- - -");
  81.   //Serial.println(F("Ready. Press 'e' to send."));
  82.   Serial.println(F("Sistema de alarma activado!"));
  83.   //Fin onexion WiFi y Cliente SMTP
  84. }//End Setup
  85.  
  86. void loop(){
  87.   //Proceso para alarma via Email en NodeMcu
  88.   if(digitalRead(5)==LOW && digitalRead(4)==HIGH){
  89.       ledMatrix.setNextText("i");
  90.       sirenaLED();
  91.       //Condicion para un solo envio de Email cada que se activa la Alarma
  92.       if(clockMail == 2){
  93.         smtpMail();
  94.       }
  95.       //Conteo para validar que se envíe una única vez el Email
  96.       clockMail++;
  97.       //Serial.println(clockMail);
  98.       //Reinicializamos el contador
  99.       //  Como solo queremos enviar un correo ponemos clockMail superior a 10
  100.       if(clockMail == 1000){
  101.         clockMail = 11;
  102.       }
  103.   }
  104.   for(int i = 0; i < NUM_LEDS/2; i++) {
  105.     leds.fadeToBlackBy(8);
  106.   if(digitalRead(4)==LOW){
  107.     //Reiniciamos el relog de Email
  108.     clockMail = 0;
  109.     ledMatrix.setNextText("!");
  110.     for(int i = 0; i < NUM_LEDS; i++) {
  111.         leds.fadeToBlackBy(8);
  112.         leds[i] = CRGB::Red;
  113.         FastLED.delay(0);
  114.       }
  115.   }
  116.   if(digitalRead(5)==HIGH && digitalRead(4)==HIGH){
  117.     //Reiniciamos el relog de Email
  118.     clockMail = 0;
  119.     ledMatrix.setNextText("G");
  120.       for(int i = 0; i < NUM_LEDS; i++) {
  121.         leds.fadeToBlackBy(8);
  122.         leds[i] = CRGB::Green;
  123.         FastLED.delay(0);
  124.   }
  125.   }
  126.   FastLED.delay(2);
  127.   }
  128.   //Fin proceso
  129.  
  130.   //Matriz 8x8 Max7219
  131.   ledMatrix.clear();
  132.   ledMatrix.scrollTextLeft();
  133.   ledMatrix.drawText();
  134.   ledMatrix.commit();
  135.   //Fin Matriz 8x8 Max7219
  136.  
  137.   //Conexion WiFi y Cliente SMTP
  138.   /*byte inChar;
  139.   inChar = Serial.read();
  140.   if(inChar == 'e'){
  141.       if(sendEmail()) Serial.println(F("Email sent"));
  142.       else Serial.println(F("Email failed"));
  143.   }*/
  144.   //Fin Conexion WiFi y Cliente SMTP
  145.  
  146. }//End Loop
  147. void smtpMail(){
  148.   sirenaLED();
  149.   if(sendEmail()) Serial.println(F("Email sent"));
  150.   else Serial.println(F("Email failed"));
  151. }//Fin SmtpMail
  152.  
  153. void sirenaLED(){
  154.   leds.fadeToBlackBy(8);
  155.      
  156.       leds[0] = CRGB::Blue; leds[1] = CRGB::Blue;
  157.       leds[2] = CRGB::Blue; leds[3] = CRGB::Blue;
  158.       leds[4] = CRGB::Black; leds[5] = CRGB::Black;
  159.       leds[6] = CRGB::Black; leds[7] = CRGB::Black;
  160.         FastLED.delay(46);
  161.        
  162.       leds[0] = CRGB::Black; leds[1] = CRGB::Black;
  163.       leds[2] = CRGB::Black; leds[3] = CRGB::Black;
  164.       leds[4] = CRGB::Red; leds[5] = CRGB::Red;
  165.       leds[6] = CRGB::Red; leds[7] = CRGB::Red;
  166.         FastLED.delay(46);
  167.        
  168.       leds[0] = CRGB::Blue; leds[1] = CRGB::Blue;
  169.       leds[2] = CRGB::Blue; leds[3] = CRGB::Blue;
  170.       leds[4] = CRGB::Black; leds[5] = CRGB::Black;
  171.       leds[6] = CRGB::Black; leds[7] = CRGB::Black;
  172.         FastLED.delay(46);
  173.        
  174.       leds[0] = CRGB::Black; leds[1] = CRGB::Black;
  175.       leds[2] = CRGB::Black; leds[3] = CRGB::Black;
  176.       leds[4] = CRGB::Red; leds[5] = CRGB::Red;
  177.       leds[6] = CRGB::Red; leds[7] = CRGB::Red;
  178.         FastLED.delay(46);
  179.        
  180.       leds[0] = CRGB::WhiteSmoke; leds[1] = CRGB::White;
  181.       leds[2] = CRGB::WhiteSmoke; leds[3] = CRGB::White;
  182.       leds[4] = CRGB::WhiteSmoke; leds[5] = CRGB::White;
  183.       leds[6] = CRGB::WhiteSmoke; leds[7] = CRGB::White;
  184.         FastLED.delay(25);
  185. }//Fin SirenaLED
  186.  
  187. byte sendEmail(){
  188.   byte thisByte = 0;
  189.   byte respCode;
  190.  
  191.   if(client.connect(server,port) == 1) {
  192.     Serial.println(F("connected"));
  193.   } else {
  194.     Serial.println(F("connection failed"));
  195.     return 0;
  196.   }
  197.  
  198.   if(!eRcv()) return 0;
  199.  
  200.   Serial.println(F("Sending hello"));
  201.   //View IP addres
  202.   Serial.println("___");
  203.   Serial.println("No olvide configurar la siguiente direccion IP...");
  204.   Serial.println(WiFi.localIP());
  205.   Serial.println("---");
  206. // replace 1.2.3.4 with your Arduino's ip
  207.   client.println("EHLO 192.168.1.30");
  208.   if(!eRcv()) return 0;
  209.  
  210.   Serial.println(F("Sending auth login"));
  211.   client.println("auth login");
  212.   if(!eRcv()) return 0;
  213.  
  214.   Serial.println(F("Sending User"));
  215. // Change to your base64 encoded user
  216.   //client.println("xxxx");
  217.   client.println("ReplaceForYourUser"); //User SMTP2GO
  218.  
  219.   if(!eRcv()) return 0;
  220.  
  221.   Serial.println(F("Sending Password"));
  222. // change to your base64 encoded password
  223.   //client.println("yyyy");
  224.   client.println("ReplaceForYourPassword"); //Password SMTP2GO
  225.  
  226.   if(!eRcv()) return 0;
  227.  
  228. // change to your email address (sender)
  229.   Serial.println(F("Sending From"));
  230.   //client.println("MAIL From: <[email protected]>");
  231.   client.println("MAIL From: <[email protected]>");
  232.  
  233.   if(!eRcv()) return 0;
  234.  
  235. // change to recipient address
  236.   Serial.println(F("Sending To"));
  237.   //client.println("RCPT To: <[email protected]>");
  238.   client.println("RCPT To: <[email protected]>");
  239.   if(!eRcv()) return 0;
  240.  
  241.   Serial.println(F("Sending DATA"));
  242.   client.println("DATA");
  243.   if(!eRcv()) return 0;
  244.  
  245.   Serial.println(F("Sending email"));
  246.  
  247. // change to recipient address
  248.   //client.println("To: You <[email protected]>");
  249.   client.println("To: Cliente <[email protected]>");
  250.  
  251. // change to your address
  252.   //client.println("From: Me <[email protected]>");
  253.   client.println("From: Alarma <[email protected]>");
  254.  
  255.   client.println("Subject: Alerta de seguridad\r\n");
  256.  
  257.   client.println("Alerta! Se ha vulnerado la seguridad");
  258.  
  259.   client.println(".");
  260.  
  261.   if(!eRcv()) return 0;
  262.  
  263.   Serial.println(F("Sending QUIT"));
  264.   client.println("QUIT");
  265.   if(!eRcv()) return 0;
  266.  
  267.   client.stop();
  268.  
  269.   Serial.println(F("disconnected"));
  270.  
  271.   return 1;
  272. }//End SendEmail
  273.  
  274. byte eRcv(){
  275.   byte respCode;
  276.   byte thisByte;
  277.   int loopCount = 0;
  278.  
  279.   while(!client.available()){
  280.     delay(1);
  281.     loopCount++;
  282.  
  283.     // if nothing received for 10 seconds, timeout
  284.     if(loopCount > 10000) {
  285.       client.stop();
  286.       Serial.println(F("\r\nTimeout"));
  287.       return 0;
  288.     }
  289.   }
  290.  
  291.   respCode = client.peek();
  292.  
  293.   while(client.available()){  
  294.     thisByte = client.read();    
  295.     Serial.write(thisByte);
  296.   }
  297.  
  298.   if(respCode >= '4'){
  299.     efail();
  300.     return 0;  
  301.   }
  302.   return 1;
  303. }//End ERcv
  304.  
  305. void efail(){
  306.   byte thisByte = 0;
  307.   int loopCount = 0;
  308.  
  309.   client.println(F("QUIT"));
  310.  
  311.   while(!client.available()) {
  312.     delay(1);
  313.     loopCount++;
  314.  
  315.     // if nothing received for 10 seconds, timeout
  316.     if(loopCount > 10000) {
  317.       client.stop();
  318.       Serial.println(F("\r\nTimeout"));
  319.       return;
  320.     }
  321.   }
  322.  
  323.   while(client.available()){  
  324.     thisByte = client.read();    
  325.     Serial.write(thisByte);
  326.   }
  327.  
  328.   client.stop();
  329.  
  330.   Serial.println(F("disconnected"));
  331. }//End Efail
Add Comment
Please, Sign In to add comment