Go Down

Topic: Sending an EMAIL using SENSOR INPUTS thru ESP8266 (Read 539 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


Go Up