MakerRobotics
Published © GPL3+

Send e-mail - NodeMCU with Mikroelektronika Clicks

We read the data with the ambient click and in case the values become small, an e-mail message is sent.

EasyFull instructions provided1 hour2,024
Send e-mail - NodeMCU with Mikroelektronika Clicks

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
MikroElektronika Ambient click
×1
protoboard
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematics

19965054 968923779916072 2086013845 n jrew0p40im

Code

Full code

Arduino
code for Arduino IDE
#include <ESP8266WiFi.h>
byte sendEmail(int x);
byte eRcv(WiFiClientSecure client);


const char* SSID = "your wifi name"; 
const char* PASS = "your wifi password"; 
const char* user_base64 = " your e-mail adress (convert in base64encode) "; // https://www.base64encode.org/
const char* user_password_base64 = "your e-mail password (convert in base64encode) "; 
const char* from_email = "MAIL From: < your e-mail >"; 
const char* to_email = "RCPT TO: < To which the email is sent >"; 
void setup()
{
  Serial.begin(115200);
  delay(10);
  Serial.println("");
  Serial.print("Connecting To ");
  WiFi.begin(SSID, PASS);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi Connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop()
{
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  if(sensorValue <100){
       if (sendEmail(sensorValue)) Serial.println(F("Email sent"));
       else Serial.println(F("Email failed"));
  }
  delay(1000);
}

byte sendEmail(int x) 
{
  WiFiClientSecure client;
  if (client.connect("smtp.gmail.com", 465) == 1) Serial.println(F("connected"));
  else {   Serial.println(F("connection failed"));   return 0; }
  
  if (!eRcv(client)) return 0;
  Serial.println(F("--- Sending EHLO")); client.println("EHLO 1.2.3.4"); if (!eRcv(client)) return 0;
  Serial.println(F("--- Sending login")); client.println("AUTH LOGIN"); if (!eRcv(client)) return 0;
  Serial.println(F("--- Sending User base64")); client.println(user_base64); if (!eRcv(client)) return 0;
  Serial.println(F("--- Sending Password base64")); client.println(user_password_base64); if (!eRcv(client)) return 0;
  Serial.println(F("--- Sending From")); client.println(from_email); if (!eRcv(client)) return 0;
  Serial.println(F("--- Sending To")); client.println(to_email); if (!eRcv(client)) return 0;
  Serial.println(F("--- Sending DATA")); client.println(F("DATA")); if (!eRcv(client)) return 0;
  client.println(F("Subject: E-mail poruka\r\n"));
  client.println(F("******Upali svetlo gazda:\n"));
  client.println(x);
  client.println(F("."));
  if (!eRcv(client)) return 0;
  Serial.println(F("--- Sending QUIT"));
  client.println(F("QUIT"));
  if (!eRcv(client)) return 0;
  client.stop();
  return 1;
}

byte eRcv(WiFiClientSecure client)
{
  byte respCode;
  byte thisByte;
  int loopCount = 0;

  while (!client.available()) {
    delay(1);
    loopCount++;
    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')return 0;
  return 1;
}

Credits

MakerRobotics

MakerRobotics

6 projects • 87 followers
Contact

Comments