Arduino ESP8266 Email tutorial

Hey guys, welcome to today’s tutorial. Today, we are going to learn how to send an email using an ESP8266 based board like the Wemos D1 with this Arduino ESP8266 Email Tutorial. It is a very useful and easy procedure and shouldn’t take more than 10 minutes to implement.

Having a WiFi enabled board like the ESP8266 enables us to explore and do several things which utilize the WiFI feature and the internet connectivity it makes possible. one of those things is, sending an email. This project came to mind when I was thinking about several ways through which we can get reports from a project. I felt it will be cool if a project could send hourly or per minute report based on readings from a sensor via email.

Wemos D1 ESP8266 Based Board

To demonstrate this, I connected a DHT22 temperature and humidity sensor to the Wemos D1 (Esp8266 based) board, such that, when the project boots up, it measures the temperature and the humidity and sends the data to a webserver hosted on bluehost which runs a simple 7-line PHP script that forwards the data sent to the server by the ESP8266  to my email address!

A webserver is an Important part of this project and Since I had one available (hosted on bluehost) this solution was the easiest for me to implement. It took me less than an hour to make this project work. If you don’t have a webserver available, with around 4$ per month you can get your own Bluehost account. it is of course, not important to this tutorial that you use bluehost but after using them for almost ten years without any issue, I feel they are worth recommending. Follow the link below to get a host from Bluehost.

Bluehost: http://bit.ly/BluehostAccount

Full disclosure: The link above is an affiliate link. I get a small percentage of each sale they generate. Thank you for your support!

I believe today’s tutorial is a very useful tutorial and will come in handy when building a more complex and challenging.

Required Components/parts and Where to Buy

The parts needed in order to build the Arduino ESP8266 email tutorial and where to buy them are listed below:

  1. Wemos D1: https://educ8s.tv/part/WemosD1
  2. Wemos D1 mini: https://educ8s.tv/part/D1Mini
  3. DHT22: https://educ8s.tv/part/DHT22
  4. Small Breadboard: https://educ8s.tv/part/SmallBreadboard
  5. Wires: https://educ8s.tv/part/Wires
  6. Xiaomi PowerBank: https://educ8s.tv/part/Powerbank
  7. Bluehost: http://bit.ly/BluehostAccount

Full disclosure: All of the links above are affiliate links. I get a small percentage of each sale they generate. Thank you for your support!

[adsense]

 

Schematics

The schematics for this project is quite simple as all we need to do is connect the DHT22 to the Wemos D1 ESP8266 board. For this schematics, we used the Wemos D1 mini, but you can use the Wemos D1 itself as the configuration are the same.

The Pin connections between the Wemos and the DHT22 is highlighted below to make the connection easier to understand.

DHT11 – Wemos D1

VDD – 5v

Data – D8

GND – GND

With the connections all done, we are now ready to write the code for this project.

Code

The code for this project is in two parts; the Arduino code for the Wemos D1 which reads theDHTt data and sends to the server, and the server code written in PHP which takes the data from the Wemos D1 and sends to the specified email.

To explain the code, we will start with the code for the Wemos D1 written with the Arduino IDE.

For the Arduino code, we will be using two libraries; The ESP8266wifi.h library and the DHT library. These two libraries can be installed using the library manager on the Arduino IDE.

We start the code by including these two libraries, after which we proceed to declare the ssid and password of the WiFi access point to which the Wemos should connect.

#include <ESP8266WiFi.h>
#include "DHT.h"

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

Next, we create an instance of the WiFiclient and set the link to our server as the server name.

WiFiClient client;
char servername[]="educ8s.tv";  // remote server we will connect to

Next, we declare the variables, temperature, and humidity which will be used to store corresponding data then we specify the pins of the Arduino to which the DHT sensor is connected and create an instance of the DHTTYPE specifying DHT22

float temperature;
float humidity;

#define DHTPIN D8  

#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE);

Next, we move to the void setup() function.

we start by initializing the DHT and serial communication after which we connect to the wireless access point.

void setup() {
  dht.begin();
  delay(2000);
  Serial.begin(115200);
  Serial.println("Connecting");
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
  }
  Serial.println("Connected");

All the code for this project will be run under the setup function since its just a demonstration so we read the DHT using the readsensor() command to get the temperature and humidity data. with that done, we then store those commands as a string and send them to the server using the sendDataToServer() function.

readSensor();
  String temperatureString = String(temperature,1);
  String humidityString = String(humidity,1);
  sendDataToServer(temperatureString,humidityString);
}

The voidloop() function is still included but left empty since the Arduino code will not compile without the voidloop function included.

void loop() {

}

If we were to build a project to post sensor data within an interval, for instance, It will be done under the void loop section with the required interval set as a time delay before the sensor is read and data is posted.

The second code is that of the PHP file on the webserver.

The code starts with the retrieval of the temperature and humidity data and merges them into a text.

<?php
$temperature = $_GET["Temperature"];
$humidity= $_GET["Humidity"];


$text ="Temperature: {$temperature} C Humidity: {$humidity} %";
?>

Next, the email details of the sender and that of the receiver are inputted after which the email is sent using the mail() function which takes the sender(admin) email, the subject, the body, and the receiver email address as arguments.

<?php
$admin_email 	= "youremail@something.com";
$email 	= "youremail@something.com";
$subject ="Temperature and Humidity Report";


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

The complete version of the code for the Wemos D1 and the PHP code for the server can be downloaded by clicking on the link below.

CODE OF THE PROJECT
——————–

 

 

 

With the webserver all setup, upload the Arduino code to the Wemos D1 and you should receive the data in the specified email after a few minutes. With this project, we have not only learned how to send data to an email but we have also some of the building blocks required to create an online datalogger with almost unlimited storage space as more code can be added to the PHP script to store the data from the sensor on the webserver which could then be retrieved and accessed from anywhere in the world.

That’s it for this tutorial. Thanks for reading and (or) watching. Don’t forget to subscribe to our youtube channels to get notifications when new tutorials are posted. If you have any question about this tutorial, feel free to post them in the comment section. I will be glad to help.

SUBSCRIBE ON YOUTUBE

——————–

Never miss a video: Subscribe to educ8s.tv