Go Down

Topic: Sending an EMAIL using SENSOR INPUTS thru ESP8266 (Read 3276 times) previous topic - next topic

thenoblelad

Apr 17, 2017, 03:10 am Last Edit: Apr 20, 2017, 06:28 pm by thenoblelad



NEW AND UPDATED CODE  DOWN IN   THE SECOND POST!!!
KINDLY CHECK TO AVOID CONFUSION!



*******************************************************************************



WARNING! THIS IS THE OLD POST

So I watched this "meant-to-be-easy video" by educ8ts.tv:
https://www.youtube.com/watch?v=4Z0gBG8dkqw&t=267s


Knowing that DHT.h was used to read the inputs from the temp and humidity sensor, how does one do the code to read the input with a different sensor? I think my code is okay but it does not send anything on my email whatsoever. I used a PHP script like in the video but for the web-server I used smtp2go.com because I still would like to test if it works. On my behalf, I have a vibration switch sensor plugged into WEMOS d1 mini and I would like to have ANY sort of input from the sensor(or even a certain buffer) read be sent by email.
Any sort of help would be appreciated. Thank you!

Also another question, am I meant to set-up anything else in smtp2.go.com apart from the account creation? On their website, it says "SMTP Server: mail.smtp2go.com" under "Connecting via SMTP". Because according to my serial monitor, I managed to connect in my address but it fails to connect in the SMTP server:

[/u][/b]
Code: [Select]

eir..
Connected
connected

220 mail.smtp2go.com ESMTP Exim 4.87 Mon, 17 Apr 2017 19:56:55 +0000
500 unrecognized command
500 unrecognized command
500 unrecognized command
500 Too many unrecognized commands
ΓΏ




ARDUINO CODE

Code: [Select]

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>

const char* ssid     = "eir";      // SSID of local network
const char* password = "";   // Password on network

int sensorPin = A0;    // select the input pin for the vibration sensor
int motion = 0;  // variable to store the value coming from the sensor

WiFiClient client;
char servername[] = "mail.smtp2go.com"; // remote server we will connect to
String result;

void setup() {

delay(2000);
Serial.begin(9600);
Serial.println("Connecting");
Serial.println(ssid);

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
  delay(100);
}
Serial.println("Connected");
delay(1000);
// read the value from the sensor:
motion = analogRead(sensorPin);
String motionString = String(motion, 1);
sendDataToServer(motionString);
}


void loop() {

}


void sendDataToServer(String motion)
{
if (client.connect(servername, 80)) {  //starts client connection, checks for connection
  Serial.println("connected");
  client.println("GET /send_email.php?motion=" + motion + " HTTP/1.1"); //Send data
  client.println("Host: OralBear.com");
  client.println("Connection: close");  //close 1.1 persistent connection
  client.println(); //end of get request
}
else {
  Serial.println("connection failed"); //error message if no client connect
  Serial.println();
}

while (client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
  char c = client.read(); //gets byte from ethernet buffer
  result = result + c;
}

client.stop(); //stop client
Serial.println(result);
}




PHP SCRIPT


Code: [Select]


<?php

$motion 
$_GET["motion"];


$text ="Toothbrush has been recently activated! Motion: {$motion}  ";

$admin_email "...@gmail.com";
$email "...@gmail.com";
$subject ="Toothbrush Report";


//send email
mail($admin_email,"$subject",$text,"From:" .$email);

?>


rw950431

Does the PHP script work if you call it from your browser?

Where is the PHP script hosted?  Your arduino sketch connects to mail.smtp2go.com but then supplies oralbear.com as the host string -this doesnt seem right.  Its more likely that you need to configure smtp2go as the mail server on whatever is hosting the PHP code. (Bluehost in the original Youtube link) and make your arduino code connect to that host.

PS.  Please use the code tags(</>) as it makes code easy to read

Code: [Select]
Like this


 

thenoblelad

UPDATE!

Hi! Thanks for replying! I'm really into this IoT stuff but I'm still amateur with how it mainly works.

Fortunately, I got a code to work and send an-email (finally!) without the need of a PHP script, using this:
https://arduinodiy.wordpress.com/2016/12/28/sending-mail-with-an-esp8266/#comment-2139


Now, my next problem is to have the sensor input be read correctly and will only send an email IF the sensor reads anything. I tried but that's as good as I can get. Any help would be appreciated, as always!

Code: [Select]

#include <ESP8266WiFi.h>  // the ESP8266WiFi.h  lib
const char* SSID = "";
const char* PASS = "";
char server[] = "mail.smtp2go.com";
//ADC_MODE(ADC_VCC);


int sensorPin = A0;    // select the input pin for the vibration sensor
int motion = 0;  // variable to store the value coming from the sensor

WiFiClient client;
void setup()
{
  Serial.begin(115200);
  delay(10);
  Serial.println("");
  Serial.println("");
  Serial.print("Connecting");
  Serial.println(SSID);
  WiFi.begin(SSID, PASS);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi Connected");
  Serial.print("IPess: ");
  Serial.println(WiFi.localIP());


  // The code to read the sensor input.... any suggestions?
  motion = analogRead(sensorPin);
  String motionString = String(motion, 1);
  byte ret = sendEmail(motionString);

}

void loop()
{
// I have a feeling the motion input must be here?
}

byte sendEmail(String motion)
{
  byte thisByte = 0;
  byte respCode;

  if (client.connect(server, 2525) == 1) {
    Serial.println(F("connected"));
  } else {
    Serial.println(F("connection failed"));
    return 0;
  }
  if (!eRcv()) return 0;

  Serial.println(F("Sending EHLO"));
  client.println("EHLO www.example.com");

  if (!eRcv()) return 0;
  Serial.println(F("Sending auth login"));
  client.println("auth login");

  if (!eRcv()) return 0;
  Serial.println(F("Sending User"));
  // Change to your base64, ASCII encoded user
  client.println(""); // SMTP UserID

  if (!eRcv()) return 0;
  Serial.println(F("Sending Password"));
  // change to your base64, ASCII encoded password
  client.println("");//  SMTP Passw

  if (!eRcv()) return 0;
  Serial.println(F("Sending From"));   // change to your email address (sender)
  client.println(F("MAIL From: @gmail.com"));// not important

  if (!eRcv()) return 0;   // change to recipient address
  Serial.println(F("Sending To"));
  client.println(F("RCPT To: c@my"));

  if (!eRcv()) return 0;
  Serial.println(F("Sending DATA"));
  client.println(F("DATA"));

  if (!eRcv()) return 0;
  Serial.println(F("Sending email"));   // change to recipient address
  client.println(F("To: @my"));   // change to your address
  client.println(F("From: @gmail.com"));
  client.println(F("Subject: ORAL BEARDUINO ALERT!\r\n"));

  client.println(F("Toothbrush has been recently activated!"));
  client.print(F("Motion:"));
  Serial.println(motion);



  if (!eRcv()) return 0;
  Serial.println(F("Sending QUIT"));
  client.println(F("QUIT"));

  if (!eRcv()) return 0;
  client.stop();
  Serial.println(F("disconnected"));
  return 1;
}

byte eRcv()
{
  byte respCode;
  byte thisByte;
  int loopCount = 0;
  while (!client.available())
  {
    delay(1);
    loopCount++;     // if nothing received for 10 seconds, timeout
    if (loopCount > 10000) {
      client.stop();
      Serial.println(F("\r\nTimeout"));
      return 0;
    }
  }

  respCode = client.peek();
  while (client.available())
  {
    thisByte = client.read();
    Serial.write(thisByte);
  }

  if (respCode >= '4')
  {
    //  efail();
    return 0;
  }
  return 1;
}


rw950431

As it is currently written your program only reads the sensor once when it powers on- so you should get one email each time you restart.  If you dont get any email then you still have a problem.

The loop function is where you need to place code to run all the time. So you are likely to need some code like

Code: [Select]
void loop()
{
  motion = analogRead(sensorPin);
  String motionString = String(motion, 1);
  byte ret = sendEmail(motionString);
  delay(60000); //wait one minute
}


However this will send you an email every minute regardless of the value that you read from the sensor. So you may need something that looks like


Code: [Select]
void loop()
{
  motion = analogRead(sensorPin);
  if (motion > SOME_VALUE) {  // you need to figure out what SOME_VALUE is for your sensor
    String motionString = String(motion, 1);
    byte ret = sendEmail(motionString);
  };
  delay(60000); //wait one minute
}

thenoblelad

Ah you're an angel! I should've found this out sooner as possible! Thank you very much but I was also working on it last night and came up to this conclusion:

Code: [Select]

void loop()
{
  sensorValue = analogRead(sensorPin);

  if (sensorValue >= 35) {
    byte ret = sendEmail(sensorValue);
  }

  delay(sensorValue);

}


Which would happen to look something similar to yours! hehe.... and I found out that it's not necessary to convert the "int motion" to a "string" cause what the heck lol

Thank you anyway!

rw950431


Tagaris

hello
i am from greece and i have e problem

how can i send the value off sensor in email

if send the espClient.println (sensorValue); take nothing

please help
thanks

PaulS

Quote
how can i send the value off sensor in email
Pretty simple, really.

You just need to change line 419191091998 in your sketch.

At least, that's what my crystal ball said. I'm not completely convinced that it is all that reliable.

If you want help with your code, POST YOUR CODE!
The art of getting good answers lies in asking good questions.

Tagaris

#include <ESP8266WiFi.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#define Pin D2
const char* ssid = ""; // Enter the SSID of your WiFi Network.
const char* password = "";// Enter the Password of your WiFi Network.
char server[] = "mail.smtp2go.com"; // The SMTP Server
int sensorPin = D2;    // select the input pin for the vibration sensor
int motion = 0;  // variable to store the value coming from the sensor
int temp;
float stringOne = 0;
OneWire ourWire(Pin);
DallasTemperature sensors(&ourWire);
WiFiClient espClient;
void setup()
{
  Serial.begin(115200);
  delay(10);
  Serial.println("");
  Serial.println("");
  Serial.print("Connecting To: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print("*");
  }
  Serial.println("");
  Serial.println("WiFi Connected.");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  motion = analogRead(sensorPin);
 String motionString = String(motion, 1);
 byte ret = sendEmail(motionString);
//  byte ret = sendEmail();
 }

void loop()
{
 
  sensors.requestTemperatures();       //Prepare the sensor for reading 
  temp = sensors.getTempCByIndex(0);
  motion = sensors.getTempCByIndex(0);
 //byte ret = sendEmail(motionString);
  if (motion > 15) {
     String motionString = String(motion, 1);
       
     byte ret = sendEmail(motionString);
  }
   
  Serial.println(motion);
  Serial.println(String(motion));
 // Serial.println(delay(2 * 60 * 1000UL));
  delay(5 * 60 * 1000UL);
  //delay(2 * 60 * 60 * 1000ul);
 
}

 byte sendEmail(String motion)
 
 
{
 
  if (espClient.connect(server, 2525) == 1)
  {
    Serial.println(F("connected"));
  }
  else
  {
    Serial.println(F("connection failed"));
    return 0;
  }
  if (!emailResp())
    return 0;
  //
  Serial.println(F("Sending EHLO"));
  espClient.println("EHLO www.example.com");
  if (!emailResp())
    return 0;
  //
  /*Serial.println(F("Sending TTLS"));
  espClient.println("STARTTLS");
  if (!emailResp())
  return 0;*/
  // 
  Serial.println(F("Sending auth login"));
  espClient.println("AUTH LOGIN");
  if (!emailResp())
    return 0;
  // 
  Serial.println(F("pbC5jb20="));
  // Change this to your base64, ASCII encoded username
  /*
  For example, the email address test@gmail.com would be encoded as dGVzdEBnbWFpbC5jb20=
  */
  espClient.println("BnbWFpbC5jb20="); //base64, ASCII encoded Username
  if (!emailResp())
    return 0;
  //
  Serial.println(F("MjE5NDM="));
  // change to your base64, ASCII encoded password
  /*
  For example, if your password is "testpassword" (excluding the quotes),
  it would be encoded as dGVzdHBhc3N3b3Jk
  */
  espClient.println("MjE5NDM=");//base64, ASCII encoded Password
  if (!emailResp())
    return 0;
  //
  Serial.println(F("Sending From"));
  // change to sender email address
  espClient.println(F("MAIL From: aris@gmail.com"));
  if (!emailResp())
    return 0;
  // change to recipient address
  Serial.println(F("Sending To"));
  espClient.println(F("RCPT To:aris@gmail.com"));
  if (!emailResp())
    return 0;
  //
  Serial.println(F("Sending DATA"));
  espClient.println(F("DATA"));
  if (!emailResp())
    return 0;
  Serial.println(F("Sending email"));
  // change to recipient address
  espClient.println(F("To:  aris@gmail.com"));
  // change to your address
  espClient.println(F("From: aris@gmail.com"));
  espClient.println(F("Subject: ESP8266 test e-mail\r\n"));
  espClient.println(F(" a test e-mail sent from ESP8266.\n"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ));
 espClient.println ();
  Serial.println(motion);
  //espClient.println(F(motion));
 
  //
  espClient.println(F("."));
  if (!emailResp())
    return 0;
  //
  Serial.println(F("Sending QUIT"));
  espClient.println(F("QUIT"));
  if (!emailResp())
    return 0;
  //
  espClient.stop();
  Serial.println(F("disconnected"));
  return 1;
}

byte emailResp()
{
  byte responseCode;
  byte readByte;
  int loopCount = 0;

  while (!espClient.available())
  {
    delay(1);
    loopCount++;
    // Wait for 20 seconds and if nothing is received, stop.
    if (loopCount > 20000)
    {
      espClient.stop();
      Serial.println(F("\r\nTimeout"));
      return 0;
    }
  }

  responseCode = espClient.peek();
  while (espClient.available())
  {
    readByte = espClient.read();
    Serial.write(readByte);
  }

  if (responseCode >= '4')
  {
    //  efail();
    return 0;
  }
  return 1;
}

Tagaris


PaulS

Code: [Select]
  motion = sensors.getTempCByIndex(0);
I'm at a complete loss to understand why you are storing a temperature in a variable called motion.

Code: [Select]
     String motionString = String(motion, 1);
I'm at a complete loss to understand why you want to construct a string in base 1 for the temperature.

Code: [Select]
  espClient.println(F(" a test e-mail sent from ESP8266.\n"                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ));
I do not understand why there is so much white space between the " and the ).

Code: [Select]
  Serial.println(motion);
  //espClient.println(F(motion));

The F macro is NOT used around variables which MUST be in SRAM. It is ONLY used around string literals, to keep the compiler from generating code to copy the string literal into SRAM.
The art of getting good answers lies in asking good questions.

Go Up