Отправка сообщение на мыло при срабатывания пина

dronsoft
Offline
Зарегистрирован: 02.06.2011


Сталкнусля с проблемой отсылки сообщения на мыло если сработал тот или иной пин на ардуине. есть которые работают чере монитор и у них есть косяк отсылает только 1 сообщение потом не может отправить вот код 

Для работы данного кода нужна библа "Base64.h" скачать её можно тут

#include <Base64.h>
#include <SPI.h>
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>

//структура для сообщения
struct Message
{
  String From;//адрес отправителя
  String To;// адрес получателя
  String Subject;// Тема письма
  String Body;// Текст письма
};

//mac адрес устройства (можно не менять)
byte mac[] = { 
  0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x05 };

//IP адрес адруины (можно не менять)   
IPAddress ip(192,168,0,54);

// IP smtp сервера
IPAddress server(94, 100, 177, 1);// mail.ru
// IP других сереров можно найти в инете 

// логин и пароль к почтовому сервису
char login[] = "ВашЛогин";//без @mail.ru
char password[] = "ВашПароль";

// создаем экземпляр клиента
EthernetClient client;

void setup(){
  Serial.begin(9600);// UART
  Ethernet.begin(mac,ip);// Запускаем шилд
  Serial.println("Start");
}

void loop(){
  while(Serial.available()){
    char inSerialChar = 0;
    inSerialChar = Serial.read();
    if(inSerialChar == 's'){
      SendMessage();// При отправке в дуину 's', отправляем сообщение.
    }
  }
}
// метод подключения к серверу
bool connectToServer(){
  if(client.connected()){
  }
  else{
    if(client.connect(server,25)){
      return true;
    }
    else{
      return false;
    }
  }
}

// метод отправки сообщения
void SendMessage(){
  Serial.println("Please wite...");
  LogPass();
  Message message = {
    "ваш_почтовый_ящик@mail.ru",
    "почтовый_ящик_получателя@gmail.com",
    "Проба пера",//тема
    "Огромный привет от Ардуино!!!!" };//само сообщение
  client.println("MAIL From:<"+message.From+">");
  delay(250);
  client.println("RCPT To:<"+message.To+">");
  delay(250);
  client.println("DATA");
  delay(250);
  client.println("From:"+message.From);
  client.println("To:"+message.To);
  client.println("Subject:"+message.Subject);
  delay(250); 
  client.println(message.Body);
  delay(250);
  client.println(".");// по приходу точки сервер ставит сообщение в очередь 
  delay(250); // на отправку
  client.println("QUIT");// как только закрыли соединение, сообщение улетает
  delay(250);
  Serial.println("Message sending...");
}

// подключение к сереверу и авторизация
void LogPass(){
  char array[64];// массив для кодирования
  if(connectToServer()){
    client.println(F("helo 1.2.3.4"));
    delay(250);
    client.println(F("AUTH LOGIN"));
    delay(250);
    client.println(strToBase64(login));
    delay(250);
    client.println(strToBase64(password));
    delay(250);

  }
  else{
    Serial.println("Server not connected...");
  }

}

// кодировка Base64
String strToBase64(char str[]){
  char charArray[64];
  base64_encode(charArray,str,strlen(str));
  String result = charArray;
  return result;
}

Потом нашол кторый работает с пинами но у меня с ним ничегоне отсылает что не так или как сделать чтобы при срабатывания на пине или нажатии на кнопку происходила отправка сообщения.

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <util.h>
#include <SPI.h>

//
// Constants for this program
//
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin =  8;      // the number of the LED pin
const int WAIT = 300;       // delay time

//
// Variables for this program.
//
// Variable for reading the digital status
int intButtonState = 0;
// ********************************  Arduino network information
// Your Ehternet Shield MAC address
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x23, 0x86 };
// Home IP address data (if unable to use DHCP)
byte ip[] = { 192, 168, 1, 108 };  
byte subnet[] = { 255, 255, 255, 0 };
byte gateway[] = { 192, 168, 1, 1 };
// Data for COMCAST SMTP mail server - NOTE *This changes*
byte smtpServerIP[] = { 76, 96, 62, 117 };
String smtpServerName = "smtp.comcast.net";
// Login data for COMCAST (already in MIME Base64 format)
String UserName64 = "dXNlcm5hbWU=";
String Password64 = "cGFzc3dvcmQ=";
// ********************************  Mail information
String Sender = "someone@comcast.net";
String Recipient = "others@theplace.com";
String Subject = "Arduino Button Click!";
String Body = "Button attached to digital input 2 pressed down.";
// ********************************  Arduino network connection
String ServerResponse="";
EthernetClient client;



//********************************************************
//** setup
//**   This is the first function to run and will initialize all
//**   components that are connected to the Arduino.
//********************************************************
void setup()
{
  Serial.begin(9600);  // for debug
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
   // get me some ip addressage!
  vodSetupCommunications();
}

//********************************************************
//** loop
//**   This is the main function to run and will act as
//**   the control loop for the Arduino.
//********************************************************
void loop()
{
  //
  // Read the state of the pushbutton value.
  //
  intButtonState = digitalRead(buttonPin);

  //
  // Check to see the pushbutton is pressed down.
  //   IF it is, the intButtonState is HIGH
  //      - turn on the LED for visual notification
  //      - send the vodSendEmail notification
  //
  if (intButtonState == HIGH)
  {     
    // Turn LED on.    
    digitalWrite(ledPin, HIGH);
    // Send the vodSendEmail notification.
    vodSendEmail();
    // Ensure that port is still open.
    vodTestConnection();
  } 
  else
  {
    // Turn LED off.
    digitalWrite(ledPin, LOW); 
  }
}


/***********************************************
        CALLED FUNCTIONS BELOW HERE
************************************************/


//********************************************************
//** vodSetupCommunications
//**   This function is called in order to initialize
//**   the communication with the ethernet port.  It
//**   gets the IP address using DHCP, but could use a
//**   specific IP address, subnet, and gateway.
//********************************************************
void vodSetupCommunications()
{

  // 
  // Variable for this function
  //
  byte byteIPByte;
  
  //
  // Attempt to establish connection to ethernet port.
  //
  Serial.println("Trying to connect to ethernet port ...");
//  if (!Ethernet.begin(mac))
//  //if (!Ethernet.begin(mac, ip, subnet, gateway))
//  {
//    Serial.println("Failed to establish DHCP connection.");
//    // No point in carrying on, so do nothing forevermore.
//    while(true);
//  }
  /*  Non-DHCP : while ( !Ethernet.begin(mac, ip, subnet, gateway) )  */
  while ( !Ethernet.begin(mac) )
  {
    Serial.println("Failed to establish DHCP connection.");
	Serial.println("retry ...");
  }
  // Pause to ensure successful connection.
  delay(WAIT);
  
  //
  // Display the established IP address for debugging
  //
  Serial.print("My IP address: ");
  for (byteIPByte = 0; byteIPByte < 4; byteIPByte++) {
    // Print the value of each byte of the IP address.
    Serial.print(Ethernet.localIP()[byteIPByte], DEC);
    Serial.print("."); 
  }
  Serial.println();
  Serial.println("***************************************");
  Serial.println();
}

//********************************************************
//** vodSendEmail
//**   This function will connect to the SMTP server and
//**   send the email message.
//********************************************************
void vodSendEmail()
{
    Serial.println("Connecting to the SMTP server ...");
	
	//
	//  If successful connecting to the SMTP server on
	//  port 587, then pass it the information needed
	//  to send the email message.  Otherwise, display
	//  a failure message for debugging.
	//
    if (client.connect(smtpServerIP, 587))
    {
      Serial.println("Connected to the SMTP server ...");
      
      vodEthernetOut("HELO " + smtpServerName); /* say hello*/
      vodEthernetOut("AUTH LOGIN ");
      vodEthernetOut(UserName64); /* Username*/
      vodEthernetOut(Password64); /* Password */
      vodEthernetOut("MAIL From:<" + Sender +">"); /* identify sender */
      vodEthernetOut("RCPT To:<" + Recipient + ">"); /* identify recipient */
      vodEthernetOut("DATA");
      vodEthernetOut("To: " + Recipient); /*  recipient in message header */
      vodEthernetOut("From: " + Sender); /* seder name in message header */
      vodEthernetOut("Subject: "+ Subject); /* insert subject */
      vodEthernetOut(""); /* empty line */
      vodEthernetOut(Body); /* insert body */
      vodEthernetOut(""); /* empty line */
      vodEthernetOut("."); /* end mail */
      vodEthernetOut("QUIT"); /* terminate connection */
      client.println();
    }
    else
    {
      Serial.println("The SMTP connection has failed!");
    }
}

//********************************************************
//** vodEthernetOut
//**   This function will write to the ethernet port
//**   and then display the data written as well as any
//**   response for debugging purposes.
//********************************************************
 void vodEthernetOut(String m) {
   // Write the string to the ethernet port.
   client.println(m);
   // Display the string that was written.
   Serial.println(">>>" + m);
   // Pause in order to wait for a response.
   delay(WAIT);
   // Read and display any response.
   vodGetResponse();
 }
 
//********************************************************
//** vodGetResponse
//**   This function look for and display any server
//**   response for debugging purposes.
//********************************************************
  void vodGetResponse() {
    if (client.available()) {
         char c = client.read();
         while (client.available()) { // Store command char by char.
           ServerResponse +=c;
           c = client.read();
         }
    Serial.println("<<<" + ServerResponse);
    ServerResponse="";
  }
 }
 
void vodTestConnection()
{
  if (!client.connected())
  {
    Serial.println();
    Serial.println("Port closed ... reconnecting ...");
    client.stop();
    delay(WAIT);
    vodSetupCommunications();
  }
}

подскажите как можно осуществить 

MaksMS
Offline
Зарегистрирован: 11.03.2013

В конце сеанса связи с почтовым сервером надо выполнить client.stop();  - тогда должно работать нормально

dronsoft
Offline
Зарегистрирован: 02.06.2011

на втором коде сообщения вообще не отправляются. где smpt.я менял на маил.ру как обычно но сообщения не идут ( при срабатывании пина