So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By tasotountas
#61275
Code: Select all#include <SoftwareSerial.h>
 
#define DEBUG true
const int ledPin = 11;
const int ledPin1 = 10;
const int  buttonPin = 7;  //microphone
const int relay = 8;
String  str1 ="";
SoftwareSerial espSerial(2,3); // UNO 2-RX. 3 TX
int s =0;
int value = LOW;
int socket = LOW;
int socketcounter = 0;
int valuecounter = 0;
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


void setup()
{
  Serial.begin(9600);
  espSerial.begin(9600); // your esp's baud rate might be different
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(relay, OUTPUT);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  //delay(3000);
  sendData("AT+RST\r\n",1000,DEBUG); // reset module
  sendData("AT+CWMODE=3\r\n",1000,DEBUG); // configure standard node

  sendData("AT+CWJAP=\"Margie\",\"15112009\"\r\n",6000,DEBUG);
  sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
  sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
  sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
  digitalWrite(13, HIGH);
  buttonPushCounter = 0;
  socket = HIGH;
}
 
void loop()
{
    buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      buttonPushCounter++;
    } else {
//      Serial.println("off");
    }
    delay(50);
  }
  lastButtonState = buttonState;/

 
   

//  while(espSerial.available()) // check if the esp is sending a message
//  {
//  sendData("AT+RST\r\n",2000,DEBUG); // reset module
//  sendData("AT+CWMODE=3\r\n",1000,DEBUG); // configure standard node
 
//  sendData("AT+CIPSTATUS\r\n",1000, DEBUG);
//  sendData("AT+CIPCLOSE=0\r\n",1000, DEBUG);
//  sendData("AT+CIPCLOSE=1\r\n",1000, DEBUG);
//  Serial.print(value);
//  Serial.println(buttonPushCounter);
  if(espSerial.available()) // check if the esp is sending a message
  {
    if(espSerial.find("+IPD,"))
    { if (buttonPushCounter % 10 == 0) {
    //digitalWrite(ledPin, HIGH);
     send_email();
  }
  else {
 web();
  }
}
  }
}
void web(){
  delay(1000);
   
     int connectionId = espSerial.read()-48; // subtract 48 because the read() function returns
                                           // the ASCII decimal value and 0 (the first decimal number) starts at 48
     Serial.println("------------------------------------------------------------------------------------connectionId= ");
     Serial.print("connectionId= "); Serial.println(connectionId);
     
     String request = espSerial.readStringUntil('\r');
     Serial.println(request);
   
 
 if (request.indexOf("/LED_1") != -1) {
   valuecounter++;
   if (valuecounter % 2 == 0){
    digitalWrite(ledPin1, LOW);
    value = HIGH;
    delay(50);
   }
 
 else { digitalWrite(ledPin1, HIGH);
       value = LOW;
       delay(50);
 }
  }
     if (request.indexOf("/socket_1") != -1){
    socketcounter++;
    if (socketcounter % 2 == 0){
    digitalWrite(relay, LOW);
    socket = HIGH;
     delay(50);
    }
    else {digitalWrite(relay, HIGH);
    socket = LOW;
    delay(50);
   }
   }
     String webpage = "<html><head><title>Auto Gray</title><body style=\"background-color:powderblue;\"><body><h1 style=\"text-align:center;\">Hello Dio!!!</h1><h2 style=\"text-align:center;\">Gray's Talking about <button>";
     webpage += buttonPushCounter;
     webpage += "</button> times.</h2>";
     webpage += "<br>Click <a href=\"/LED_1\">here</a> to turn the E-mail sender";
      if(value == HIGH) {
       webpage += " ON";
       } else {
          webpage += " OFF ";
       }
     webpage += "<br><br>Or click <a href=\"/socket_1\">here</a> to turn the socket";
     if(socket == HIGH) {
       webpage += " ON";
       } else {
          webpage += " OFF ";
       }
     webpage += "</body></html>\r\n";
   
     String cipSend = "AT+CIPSEND=";
     cipSend += connectionId;
     cipSend += ",";
     cipSend +=webpage.length();
     cipSend +="\r\n";
     
     sendData(cipSend,1000,DEBUG);
     sendData(webpage,3000,DEBUG);
     
     delay(1000);
     String closeCommand = "AT+CIPCLOSE=";
     closeCommand+=connectionId; // append connection id
     closeCommand+="\r\n";
     
     sendData(closeCommand,1000,DEBUG);
     delay(100);   
  //}
 // }
 
  //sendData("AT+CIPSTATUS\r\n",1000, DEBUG);
 

  delay(100); //wsp
}
 void send_email()
{
  sendData("AT+CIPSTART=4,\"TCP\",\"mail.smtp2go.com\",80\r\n",2000,DEBUG);
  sendData("AT+CIPSEND=4,19\r\n",2000,DEBUG);
  sendData("EHLO 192.168.1.11\r\n",2000,DEBUG);
  sendData("AT+CIPSEND=4,12\r\n",2000,DEBUG);
  sendData("AUTH LOGIN\r\n",2000,DEBUG);
  sendData("AT+CIPSEND=4,26\r\n",2000,DEBUG);
  sendData("AT+CIPSEND=4,18\r\n",2000,DEBUG);
  sendData("aWVybeGl0d24=\r\n",2000,DEBUG);                  //base64 encoded password
  sendData("AT+CIPSEND=4,32\r\n",2000,DEBUG);
  sendData("MAIL FROM:<thismail@gmail.com>\r\n",2000,DEBUG);  // use your email address
  sendData("AT+CIPSEND=4,33\r\n",2000,DEBUG);
  sendData("RCPT To:<tt@yahoo.gr>\r\n",3000,DEBUG);
  sendData("AT+CIPSEND=4,6\r\n",2000,DEBUG);
  sendData("DATA\r\n",2000,DEBUG);
  sendData("AT+CIPSEND=4,17\r\n",2000,DEBUG);
  sendData("Testing Success\r\n",2000,DEBUG);
  sendData("AT+CIPSEND=4,3\r\n",2000,DEBUG);
  sendData(".\r\n",2000,DEBUG);
  sendData("AT+CIPSEND=4,6\r\n",2000,DEBUG);
 // sendData("AT+CIPCLOSE=4\r\n",2000,DEBUG);
  sendData("QUIT\r\n",2000,DEBUG);
 
}
void sendData(String command, const int timeout, boolean debug)
{
   
    espSerial.print(command); // send the read character to the esp8266
   
    long int time = millis();
   
    while( (millis() - time) < timeout)
    {
      while(espSerial.available())
      {
        char c = espSerial.read(); // read the next character.
        Serial.print(c);
      } 
    }
}


this is my code.it is working fine as web server OR as an alert email sender.When i put the "send_email()" void in the sketch with the "web()", the html page crashes.Please help!!!