12 Mayıs 2015 Salı

Sending an email with using ESP8266 Wifi module

ESP8266 is a very useful and cheap wifi module to be as client, server and access point.

ESP8266 Quick Starter Guide : http://rancidbacon.com/files/kiwicon8/ESP8266_WiFi_Module_Quick_Start_Guide_v_1.0.4.pdf

!!!BE CAREFUL, ESP8266 WORKS UNDER 3.3 VOLT NOT 5 VOLT!!!

For sending an email, we used our server and PHP code. PHP code file is in the server file system and wifi module makes a connection with the PHP code.

PHP code :

<?php
include('DatabaseConnection.php');
$data=$_GET['data'];
$email=$_GET['email'];
require /*write here php file directory*/;
$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'localhost';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = /*write here SMTP username*/;                 // SMTP username
$mail->Password = /*write here SMTP password*/;                           // SMTP password
//$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
//$mail->Port = 587;                                    // TCP port to connect to

$mail->From = (/*write here host name*/);
$mail->addAddress($email);               // Name is optional
$mail->WordWrap = 50;     
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = /*write here mail subject*/;
$mail->Body    = /*write here mail message*/;
$mail->AltBody = /*write here altbody*/;

if($mail->send()) {
   $flag['code']=1;
}else{
   $flag['code']=0;
}
print(json_encode($flag));
mysql_close();
?>

Arduino Code :

#include <SoftwareSerial.h>
SoftwareSerial esp8266(10,11); //TX RX
#define DEBUG true

void setup()
{
  Serial.begin(9600); //serial debug
  esp8266.begin(9600); 
  //set mode needed for new boards
  
}

void loop()
{
  
  WebRequest();
  delay(5000);
}

//web request needs to be sent without the http for now, https still needs some working
void WebRequest(){

   String response;
   String cmd = "AT+CIPSTART=4,\"TCP\",\"WRITE HERE HOST NAME\",80\r\n";  //make this command: AT+CPISTART="TCP","146.227.57.195",80
   String val =  "GET /*WRITE HERE FILE NAME OF PHP*/";  //construct http GET request
   String value = "AT+CIPSEND=4,";
   value=value+val.length();
   value=value+"\r\n";
   
  sendData("AT+RST\r\n",2000,DEBUG);
  sendData("AT+CWMODE=1\r\n",1000,DEBUG);
  sendData("AT+RST\r\n",2000,DEBUG);
  sendData("AT+CIPMUX=1",500,DEBUG);
  response=sendData("AT+CWJAP=\"WIFI ACCESS POINT NAME\",\"WIFI ACCESS POINT PASSWORD\"\r\n",2000,DEBUG);
  response=sendData(cmd,2000,DEBUG);
  response = sendData(value,2000,DEBUG);
  sendData(val,5000,DEBUG);
  
}
String sendData(String command, const int timeout, boolean debug)
{
    String response = "";
    
    esp8266.print(command); // send the read character to the esp8266
    
    long int time = millis();
    
    while( (time+timeout) > millis())
    {
      while(esp8266.available())
      {
        
        // The esp has data so display its output to the serial window 
        char c = esp8266.read(); // read the next character.
        response+=c;
      }  
    }
    
    if(debug)
    {
      Serial.println(response);
    }
    
    return response;
}
boolean Contains(String s, String search) {
    int max = s.length() - search.length();

    for (int i = 0; i <= max; i++) {
        if (s.substring(i) == search) return true; // or i
    }

    return false; //or -1














3 yorum:

  1. Merhaba

    Bu ESP8266 nın versiyonunu öğrenmek için Arduino Uno ya bağlantısını gösteren bir şema bulamadım.Sabahtan beri 10 larca video izledim her biri farklı farklı.
    Rica etsem bu (besleme pinleri dışında) 6 pinin nasıl bağlanacağını vede AT Komutlarını yazarmısınız

    Ben arduinonun inputlarından birinde hareket sensöründen bir sinyal gelince Wifi me bağlanıp mailime "Evi kontrol et" gibi uyarı mesajı gelsin istiyorum

    Ne altı pinmiş arkadaş
    Cevap yazarsanız çok memnun olurum

    YanıtlaSil
    Yanıtlar
    1. Merhaba,
      Yazının üstünde koymuş olduğumuz linkten hem ARDUINO -ESP8266 bağlanışını hem de AT commandları bulabilirsin.

      http://rancidbacon.com/files/kiwicon8/ESP8266_WiFi_Module_Quick_Start_Guide_v_1.0.4.pdf

      Bu manueldeki adımları tek tek izlersen rahatlıkla bağlantıyı sağlayabilirsin.

      Yukarıdaki kod parçacığının tümü aslında senin yapmak istediğin uygulamanın işlevini görür. Fakat servisi sağlayabilmen için hostunun olması gerekli.(Yukarıdaki koda göre). Eğer hostun varsa PHP kodunu hostunda yaratıp ARDUINO kodunu da arduino ide sinde çalıştırabilirsin. Ekstra olarak yapman gerekenler beyaz arkaplanlı olan kısımda gösterilen yerleri kodda değiştirmek.
      Şimdiden kolay gelsin:)

      Sil
  2. Cevabınız için teşekkür ederim
    Benim bu pdf den anladığım:
    1-pdf in 4.sayfasındakı UNO -ESP8266 bağlantısını tavsiye ediyor
    2-Bağlantı şu şekilde olacak

    ESP8266 UNO
    ------- -------
    GND.........GND
    +3,3V.......3,3V
    TX..........TX
    RX..........TX
    CHD-PD......+3,3V

    Bağlantıyı böyle yaptıktan sonra seri monitörü açıyoruz

    Pdf deki yazıda yeni firmware in 9600 e ayarlandığı yazıyor videolarda 115200 baud olması gerektiğ söylüyor
    AYrıca host name nedir?(ne olabilir) orneğin ben gmailimdeki mailime mesaj gelmesini istiyorsam ne olmalıdır?
    Teşekkürler

    YanıtlaSil