Advertisement
jh_elec

ESP8622 - Mail

Jul 9th, 2022
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.37 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <ESP8266WiFi.h>
  3. #include <ESP_Mail_Client.h>
  4.  
  5. /*
  6.  * Werte sollten nicht geändert werden.
  7.  */
  8. #define TRIGGER_PIN     16
  9. #define ECHO_PIN        5
  10. #define SENSOR_OFFSET   1
  11.  
  12. typedef enum enumWLAN_STATUS_CODES
  13. {
  14.   WLAN_IDLE_STATUS,
  15.   WLAN_NO_SSID_AVAILABLE,
  16.   WLAN_SCAN_COMPLETED,
  17.   WLAN_CONNECTED,
  18.   WLAN_CONNECT_FAILD,
  19.   WLAN_CONNECTION_LOST,
  20.   WLAN_DISCONNECTED,
  21. };
  22.  
  23. /*
  24.  *  W-LAN Konfiguration
  25.  */
  26.  #ifndef STASSID
  27.  #define STASSID      "TEST"
  28.  #define STAPSK       "*****"
  29.  #endif
  30.  
  31. /*
  32.  * E-Mail Server Konfiguration
  33.  */
  34. #define SMTP_HOST " smtp.mail.yahoo.com"
  35. #define SMTP_PORT esp_mail_smtp_port_587
  36. #define AUTHOR_EMAIL "*******"
  37. #define AUTHOR_PASSWORD "wassolldasdenn"
  38.  
  39. /*
  40.  * Diese Werte müssen an den entsprechenden Tank angepasst werden..
  41.  * -> HEIGHT_TANK:= Höhe des Tankes
  42.  * -> DEVIATION:= Höhe von Füllinhalt max. bis  zu dem Sensor..
  43.  */
  44. #define HEIGHT_TANK     20
  45. #define DEVIATION       7
  46.  
  47.  
  48. #define GET_PERCENT(VALUE) ( 100 - ( ( ( VALUE-DEVIATION ) * 100 ) / HEIGHT_TANK ))
  49.  
  50.  
  51. SMTPSession smtp;
  52. void smtpCallback(SMTP_Status status);
  53.  
  54.  
  55. void setup()
  56. {
  57.   Serial.begin(9600);
  58.  
  59.   delay( 500 );
  60.  
  61.   Serial.println( "W-LAN Netzwerk(e) in der Naehe werden gesucht.." );
  62.  
  63.   uint8_t uiWIFIn = WiFi.scanNetworks();
  64.   Serial.print( "W-LAN Status: " );
  65.   Serial.println( WiFi.status() );
  66.   Serial.print( "Es wurde(n) " );
  67.   Serial.print( uiWIFIn );
  68.   Serial.println( " W-LAN Netzwerk(e) in der Naehe gefunden!." );
  69.   Serial.println( "" );
  70.  
  71.   Serial.println("Verbindung zum W-LAN herstellen..");
  72.   WiFi.begin( STASSID, STAPSK );
  73.  
  74.   while( WiFi.status() != WL_CONNECTED )
  75.   {
  76.     delay(1000);
  77.     Serial.print( "." );
  78.   }
  79.   Serial.println( "" );
  80.  
  81.  
  82.   Serial.print( "W-LAN Status: " );
  83.   Serial.println( WiFi.status() );
  84.   Serial.println( "" );
  85.   Serial.println( "W-LAN verbunden!." );
  86.   Serial.print( "IP Addresse: " );
  87.   Serial.println( WiFi.localIP() );
  88.   Serial.println( "" );
  89.  
  90.   //Debug Meldungen anzeigen
  91.   smtp.debug(1);
  92.  
  93.   //Ein Callback definieren welcher nach dem Senden der Mail ausgeführt werden soll.
  94.   smtp.callback(smtpCallback);
  95.  
  96.   //Mail Session
  97.   ESP_Mail_Session session;
  98.  
  99.   //Serverdaten setzen
  100.   session.server.host_name = SMTP_HOST;
  101.   session.server.port = SMTP_PORT;
  102.   session.login.email = AUTHOR_EMAIL;
  103.   session.login.password = AUTHOR_PASSWORD;
  104.   session.login.user_domain = "smtp.gmail.com";
  105.  
  106.   //Wenn keine Verbindung aufgebaut werden konnte soll die Funktion verlassen werden.
  107.   if (!smtp.connect(&session))
  108.   {
  109.     return;
  110.   }
  111.  
  112.   pinMode(TRIGGER_PIN, OUTPUT);
  113.   pinMode(ECHO_PIN, INPUT);
  114.  
  115. }
  116.  
  117. void loop()
  118. {  
  119.   Serial.println ( "********************************" );
  120.   long lDistance = GetDistance();
  121.   Serial.print( "Distanz: " );
  122.   Serial.println( lDistance );
  123.  
  124.   Serial.print( "Füllstand in %: " );
  125.   Serial.println( GET_PERCENT( lDistance ) );
  126.  
  127.   Serial.print( "W-LAN Status: " );
  128.   Serial.println( WiFi.status() );
  129.   Serial.println ( "********************************" );
  130.  
  131.   delay(1500);
  132. }
  133.  
  134.  
  135. long GetDistance()
  136. {
  137.   long lDistance = 0;
  138.   long lLength = 0;
  139.  
  140.   digitalWrite(TRIGGER_PIN, LOW);
  141.   delay(5);
  142.   digitalWrite(TRIGGER_PIN, HIGH);
  143.   delay(10);
  144.   digitalWrite(TRIGGER_PIN, LOW);
  145.  
  146.   lLength = pulseIn(ECHO_PIN, HIGH);
  147.   lDistance = ( lLength / 2 ) * 0.03432;
  148.  
  149.   return ( lDistance - SENSOR_OFFSET );
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement