Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ********** Arduino **********
- * Autor: Cristian Augusto Restrepo Lenis
- * Fecha: 17/11/2016
- * Descripción:
- * Enviar un correo electrónico a través de
- * SMTP2GO mediante NodeMcu cuando se vulnere la seguridad.
- * URL: https://embebidoscar.blogspot.com.co/p/proyecto-final.html
- */
- //Librerías Matriz 8x8 MAX7219 y LED 5050 CJMCU-2812-8
- #include<FastLED.h>
- #include "LedMatrix.h"
- //Librerías para enviar e-mail y conectar a internet
- #include <SPI.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266WiFiAP.h>
- #include <ESP8266WiFiGeneric.h>
- #include <ESP8266WiFiMulti.h>
- #include <ESP8266WiFiScan.h>
- #include <ESP8266WiFiSTA.h>
- #include <ESP8266WiFiType.h>
- #include <WiFiClient.h>
- #include <WiFiClientSecure.h>
- #include <WiFiServer.h>
- #include <WiFiUdp.h>
- //Fin librerías
- //Conexion WiFi y Cliente SMTP
- const char* ssid = "USC-AIREH-LIBRE";
- const char* password = "";
- //char server[] = "smtpcorp.com";
- char server[] = "mail.smtp2go.com";
- int port = 2525;
- WiFiClient client;
- //Fin Conexion WiFi y Cliente SMTP
- //LED's 5050
- #define NUM_LEDS 8
- CRGBArray<NUM_LEDS> leds;
- char val;
- //Fin LED's 5050
- //Matriz 8x8 Max7219
- #define NUMBER_OF_DEVICES 1
- #define CS_PIN 2
- LedMatrix ledMatrix = LedMatrix(NUMBER_OF_DEVICES, CS_PIN);
- int x = 0;
- //Fin Matriz 8x8 Max7219
- //Se inicializa la variable "relog" para cnfiguracion Email
- int clockMail = 0;
- //Fin inicializacion
- void setup() {
- //Otros
- pinMode(5,INPUT);
- pinMode(4,INPUT);
- //Fin Otros
- //LED's 5050
- FastLED.addLeds<NEOPIXEL,8>(leds, NUM_LEDS);
- //Fin LED'S 5050
- //Matriz 8x8 Max7219
- ledMatrix.init();
- //Mensaje que carga al inicio, cuando no se han enviado datos
- ledMatrix.setText("A L A R M");
- //Segundo mensaje
- //ledMatrix.setNextText(";)");
- //Fin Matriz 8x8 Max7219
- //Conexion WiFi y Cliente SMTP
- Serial.begin(115200);
- pinMode(12,OUTPUT);
- digitalWrite(12,HIGH);
- WiFi.begin(ssid, password);
- delay(2000);
- Serial.println("- - -");
- //Serial.println(F("Ready. Press 'e' to send."));
- Serial.println(F("Sistema de alarma activado!"));
- //Fin onexion WiFi y Cliente SMTP
- }//End Setup
- void loop(){
- //Proceso para alarma via Email en NodeMcu
- if(digitalRead(5)==LOW && digitalRead(4)==HIGH){
- ledMatrix.setNextText("i");
- sirenaLED();
- //Condicion para un solo envio de Email cada que se activa la Alarma
- if(clockMail == 2){
- smtpMail();
- }
- //Conteo para validar que se envíe una única vez el Email
- clockMail++;
- //Serial.println(clockMail);
- //Reinicializamos el contador
- // Como solo queremos enviar un correo ponemos clockMail superior a 10
- if(clockMail == 1000){
- clockMail = 11;
- }
- }
- for(int i = 0; i < NUM_LEDS/2; i++) {
- leds.fadeToBlackBy(8);
- if(digitalRead(4)==LOW){
- //Reiniciamos el relog de Email
- clockMail = 0;
- ledMatrix.setNextText("!");
- for(int i = 0; i < NUM_LEDS; i++) {
- leds.fadeToBlackBy(8);
- leds[i] = CRGB::Red;
- FastLED.delay(0);
- }
- }
- if(digitalRead(5)==HIGH && digitalRead(4)==HIGH){
- //Reiniciamos el relog de Email
- clockMail = 0;
- ledMatrix.setNextText("G");
- for(int i = 0; i < NUM_LEDS; i++) {
- leds.fadeToBlackBy(8);
- leds[i] = CRGB::Green;
- FastLED.delay(0);
- }
- }
- FastLED.delay(2);
- }
- //Fin proceso
- //Matriz 8x8 Max7219
- ledMatrix.clear();
- ledMatrix.scrollTextLeft();
- ledMatrix.drawText();
- ledMatrix.commit();
- //Fin Matriz 8x8 Max7219
- //Conexion WiFi y Cliente SMTP
- /*byte inChar;
- inChar = Serial.read();
- if(inChar == 'e'){
- if(sendEmail()) Serial.println(F("Email sent"));
- else Serial.println(F("Email failed"));
- }*/
- //Fin Conexion WiFi y Cliente SMTP
- }//End Loop
- void smtpMail(){
- sirenaLED();
- if(sendEmail()) Serial.println(F("Email sent"));
- else Serial.println(F("Email failed"));
- }//Fin SmtpMail
- void sirenaLED(){
- leds.fadeToBlackBy(8);
- leds[0] = CRGB::Blue; leds[1] = CRGB::Blue;
- leds[2] = CRGB::Blue; leds[3] = CRGB::Blue;
- leds[4] = CRGB::Black; leds[5] = CRGB::Black;
- leds[6] = CRGB::Black; leds[7] = CRGB::Black;
- FastLED.delay(46);
- leds[0] = CRGB::Black; leds[1] = CRGB::Black;
- leds[2] = CRGB::Black; leds[3] = CRGB::Black;
- leds[4] = CRGB::Red; leds[5] = CRGB::Red;
- leds[6] = CRGB::Red; leds[7] = CRGB::Red;
- FastLED.delay(46);
- leds[0] = CRGB::Blue; leds[1] = CRGB::Blue;
- leds[2] = CRGB::Blue; leds[3] = CRGB::Blue;
- leds[4] = CRGB::Black; leds[5] = CRGB::Black;
- leds[6] = CRGB::Black; leds[7] = CRGB::Black;
- FastLED.delay(46);
- leds[0] = CRGB::Black; leds[1] = CRGB::Black;
- leds[2] = CRGB::Black; leds[3] = CRGB::Black;
- leds[4] = CRGB::Red; leds[5] = CRGB::Red;
- leds[6] = CRGB::Red; leds[7] = CRGB::Red;
- FastLED.delay(46);
- leds[0] = CRGB::WhiteSmoke; leds[1] = CRGB::White;
- leds[2] = CRGB::WhiteSmoke; leds[3] = CRGB::White;
- leds[4] = CRGB::WhiteSmoke; leds[5] = CRGB::White;
- leds[6] = CRGB::WhiteSmoke; leds[7] = CRGB::White;
- FastLED.delay(25);
- }//Fin SirenaLED
- byte sendEmail(){
- byte thisByte = 0;
- byte respCode;
- if(client.connect(server,port) == 1) {
- Serial.println(F("connected"));
- } else {
- Serial.println(F("connection failed"));
- return 0;
- }
- if(!eRcv()) return 0;
- Serial.println(F("Sending hello"));
- //View IP addres
- Serial.println("___");
- Serial.println("No olvide configurar la siguiente direccion IP...");
- Serial.println(WiFi.localIP());
- Serial.println("---");
- // replace 1.2.3.4 with your Arduino's ip
- client.println("EHLO 192.168.1.30");
- if(!eRcv()) return 0;
- Serial.println(F("Sending auth login"));
- client.println("auth login");
- if(!eRcv()) return 0;
- Serial.println(F("Sending User"));
- // Change to your base64 encoded user
- //client.println("xxxx");
- client.println("ReplaceForYourUser"); //User SMTP2GO
- if(!eRcv()) return 0;
- Serial.println(F("Sending Password"));
- // change to your base64 encoded password
- //client.println("yyyy");
- client.println("ReplaceForYourPassword"); //Password SMTP2GO
- if(!eRcv()) return 0;
- // change to your email address (sender)
- Serial.println(F("Sending From"));
- //client.println("MAIL From: <[email protected]>");
- if(!eRcv()) return 0;
- // change to recipient address
- Serial.println(F("Sending To"));
- //client.println("RCPT To: <[email protected]>");
- if(!eRcv()) return 0;
- Serial.println(F("Sending DATA"));
- client.println("DATA");
- if(!eRcv()) return 0;
- Serial.println(F("Sending email"));
- // change to recipient address
- //client.println("To: You <[email protected]>");
- // change to your address
- //client.println("From: Me <[email protected]>");
- client.println("Subject: Alerta de seguridad\r\n");
- client.println("Alerta! Se ha vulnerado la seguridad");
- client.println(".");
- if(!eRcv()) return 0;
- Serial.println(F("Sending QUIT"));
- client.println("QUIT");
- if(!eRcv()) return 0;
- client.stop();
- Serial.println(F("disconnected"));
- return 1;
- }//End SendEmail
- byte eRcv(){
- byte respCode;
- byte thisByte;
- int loopCount = 0;
- while(!client.available()){
- delay(1);
- loopCount++;
- // if nothing received for 10 seconds, timeout
- if(loopCount > 10000) {
- client.stop();
- Serial.println(F("\r\nTimeout"));
- return 0;
- }
- }
- respCode = client.peek();
- while(client.available()){
- thisByte = client.read();
- Serial.write(thisByte);
- }
- if(respCode >= '4'){
- efail();
- return 0;
- }
- return 1;
- }//End ERcv
- void efail(){
- byte thisByte = 0;
- int loopCount = 0;
- client.println(F("QUIT"));
- while(!client.available()) {
- delay(1);
- loopCount++;
- // if nothing received for 10 seconds, timeout
- if(loopCount > 10000) {
- client.stop();
- Serial.println(F("\r\nTimeout"));
- return;
- }
- }
- while(client.available()){
- thisByte = client.read();
- Serial.write(thisByte);
- }
- client.stop();
- Serial.println(F("disconnected"));
- }//End Efail
Add Comment
Please, Sign In to add comment