logo elektroda
logo elektroda
X
logo elektroda
REKLAMA
REKLAMA
Adblock/uBlockOrigin/AdGuard mogą powodować znikanie niektórych postów z powodu nowej reguły.

Arduino ESP8266 z czujnikiem ruchu PIR HC i wysłaniem maila jako powiadomienie

kamilek2207 19 Mar 2020 23:55 1029 3
REKLAMA
  • #1 18543945
    kamilek2207
    Poziom 10  
    Posty: 19
    Ocena: 5
    Witam,
    Posiadam kod na ,,Alarm-Wemos Alarm Email V4". Zależy mi na tym aby po wykryciu ruchu z PIR HC-SR501 i wysłania go poprzez ESP8266 do mnie na maila np. RUCH na podany adres mailowy xyz.
    Jak mogę to uczynić korzystając z poczty na gmail ? Na chwilę obecną nie ma możliwości dokonania na stronie IFTTT.com Z góry serdecznie dziękuję za pomoc :-)
    Poniżej dodaje kod:
    Kod: C / C++
    Zaloguj się, aby zobaczyć kod
  • REKLAMA
  • #2 18544119
    Slawek K.
    Poziom 35  
    Posty: 3020
    Pomógł: 259
    Ocena: 1301
    Możesz skorzystać z tej biblioteki https://github.com/xreef/EMailSender

    Pozdr
  • REKLAMA
  • #3 18546693
    kamilek2207
    Poziom 10  
    Posty: 19
    Ocena: 5
    Czy tak to powinno wyglądać ?
    
    
    EMailSender emailSend("smtp.account@gmail.com", "password");
        EMailSender::EMailMessage message;
        message.subject = "Subject";
        message.message = "Hi, How are you<br>Fine.";
        Serial.println("Sending status: ");
        Serial.println(resp.code);
        Serial.println(resp.desc);
        Serial.println(resp.status);
    
    {
    
        Serial.begin(115200);
    
    
    
        const char* ssid = "ssid of your AP";
    
        const char* password = "password of your AP";
    
    
    
        connection_state = WiFiConnect(ssid, password);
    
        if(!connection_state)  // if not connected to WIFI
    
            Awaits();          // constantly trying to connect
    
    
    
        EMailSender::EMailMessage message;
    
        message.subject = "Soggetto";
    
        message.message = "Ciao come stai<br>io bene.";
    
    
    
        EMailSender::Response resp = emailSend.send("account_to_send@gmail.com", message);
    
    
    
        Serial.println("Sending status: ");
    
    
    
        Serial.println(resp.status);
    
        Serial.println(resp.code);
    
        Serial.println(resp.desc);
    
    }
    
    #include "Arduino.h"
    #include <EMailSender.h>
    #include <EMailSender.cpp>
    #include <ESP8266WiFi.h>
    const char* ssid     = "xxxx";      // SSID of local network
    const char* password = "xxxxxx";   // Password on network
    String result;
    String smoke ;
    String motion ;
    
    void setup() 
    {
      pinMode(D4, INPUT);
      pinMode(D7, INPUT);
      delay(2000);
      Serial.begin(115200);
      WiFi.hostname("ESP8266MotionSensor"); //This changes the hostname of the ESP8266 to display neatly on the network esp on router.
      WiFi.begin(ssid, password);
    }
    
    void ifttt() //Wifi connection and send all the data to IFTTT
    {
      const char host[ ]        = "maker.ifttt.com";          // maker channel of IFTTT
      const char trigger[ ]     = "motion";                   //name of the trigger you would like to send to IFTTT
      const char APIKey[ ]      = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";      //Your maker key for Webhooks on IFTTT
      Serial.print("Connect to: ");
      Serial.println(host);
      // WiFiClient to make HTTP connection
      WiFiClient client;
      if (!client.connect(host, 80)) {
        Serial.println("connection failed");
        return;
        }
    
    // Build URL
      String url = String("/trigger/") + trigger + String("/with/key/") + APIKey;
      Serial.print("Requesting URL: ");
      Serial.println(url);
    
    // Send request to IFTTT
      client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); 
      //FYI rn rn is just two new lines to comply with http format
       delay(20);
    
    // Read all the lines of the reply from server and print them to Serial
      Serial.println("Respond:");
      while(client.available()){
        String line = client.readStringUntil('\r');
        Serial.print(line);
        }
      Serial.println();
      Serial.println("closing connection");
      Serial.println(); //space things our in serial monitor for purdy
      Serial.println();
      client.stop();  //V5 added this to disconnect
      delay (20000); //This delay here just to stop spamming of emails- should be polished to a better method/location
     }
      
    void loop() //Where stuffz happens
    {
     if(digitalRead(D4)== HIGH)  // ||digitalRead(D7)== HIGH) Removed MQ2 or condition
      {
       motion = digitalRead(D4); //PIR sensor pin
       smoke = digitalRead(D7);  //MQ2 smoke sensor pin
       Serial.println("Alarm detected!"); //Send things to serial for handy dandy info
       Serial.print("motion ");
       Serial.println(motion);
       Serial.print("smoke ");
       Serial.println(smoke);
       ifttt(); //Tell IFTTT that intruder detected
      } 
      else 
      {
        //Serial.println("no trigger"); //enable for debugging of no event being triggered
      }
    }
    
  • Pomocny post
    #4 18546744
    Slawek K.
    Poziom 35  
    Posty: 3020
    Pomógł: 259
    Ocena: 1301
    Nie.
    Generalnie struktura kodu składa się z :
    - dołączonych bibliotek - #include
    - deklaracji zmiennych i konstruktorów
    - procedur i funkcji
    - setup
    - pętli loop

    Zatem jak wejdziesz do /example zalinkowanej przeze mnie biblioteki, to masz przykładowy program. Porównaj co jest zawarte w tym przykładzie i skopiuj w odpowiednie odpowiadające sobie miejsca w twoim kodzie, wg tego co napisałem powyżej i pokaż co Ci wyszło.
    Podpowiem też, że skoro jak napisałeś, że IFTTT nie działa, zatem usuń ze swojego programu część kodu związaną z obsługą tegoż.

    Pozdr
REKLAMA