We can admit that with the arrival of Espressif Systems, ESPs have gotten a wider scope for affordability and availability of computing. This is really beneficial for the maker community. The latest ESP32 facilitates prototyping, also making it easier to push for production.
Why do you need to buy ESP32?
- ESP32 is capable of functioning reliably in industrial environments, with operating temperature ranging from –40°C to +125°C.
- ESP32 is highly-integrated with in-built antenna switches, RF balun, power amplifier, low-noise receive amplifier, filters, and power management modules.
- Engineered for mobile devices, wearable electronics and IoT applications, ESP32 achieves ultra-low power consumption with a combination of several types of proprietary software.
- ESP32 can perform as a completely standalone system or as a slave device to a host MCU, reducing communication stack overhead on the main application processor.
- ESP32 can interface with other systems to provide Wi-Fi and Bluetooth functionality through its SPI / SDIO or I2C / UART interfaces.
Applications of ESP32
Create a stand-alone ESP32 Web Server: Here, we explain how to control two LEDs, with the help of ESP32 and Arduino IDE. The web server will be accessible from any browser on the local network.
Connect the LEDs to GPIO pins 26 and 27. The control interface can be opened using the IP address of the ESP. We will be using the WiFi.h library to perform all the basic functionalities related to WiFi. Please, find sample code below:
// load the Wi-Fi library
#include <WiFi.h>
// fill the network credentials
const char* ssid = "________";
const char* password = “________ ";
// setting web server port number to 80
WiFiServer server(80);
// variable to store the HTTP request
String header;
// variables to store the current output state
String output26State = "off";
String output27State = "off";
// output variables to GPIO pins
const int output26 = 26;
const int output27 = 27;
void setup() {
Serial.begin(115200);
// initializing the output variables as outputs
pinMode(output26, OUTPUT);
pinMode(output27, OUTPUT);
// setting outputs to LOW
digitalWrite(output26, LOW);
digitalWrite(output27, LOW);
// connecting to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
WiFiClient client = server.available(); // check for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /26/on") >= 0) {
Serial.println("GPIO 26 on");
output26State = "on";
digitalWrite(output26, HIGH);
} else if (header.indexOf("GET /26/off") >= 0) {
Serial.println("GPIO 26 off");
output26State = "off";
digitalWrite(output26, LOW);
} else if (header.indexOf("GET /27/on") >= 0) {
Serial.println("GPIO 27 on");
output27State = "on";
digitalWrite(output27, HIGH);
} else if (header.indexOf("GET /27/off") >= 0) {
Serial.println("GPIO 27 off");
output27State = "off";
digitalWrite(output27, LOW);
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #555555;}</style></head>");
// Web Page Heading
client.println("<body><h1>ESP32 Web Server</h1>");
// Display current state, and ON/OFF buttons for GPIO 26
client.println("<p>GPIO 26 - State " + output26State + "</p>");
// If the output26State is off, it displays the ON button
if (output26State=="off") {
client.println("<p><a href=\"/26/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/26/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 27
client.println("<p>GPIO 27 - State " + output27State + "</p>");
// If the output27State is off, it displays the ON button
if (output27State=="off") {
client.println("<p><a href=\"/27/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/27/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
Note: Before uploading the code, select the appropriate board and port on the Arduino IDE.
ESP32 in AP+STA Modes:
Use ESP32 as both Station and Access Point. First, we write a program to connect to a WiFi hotspot (Access Point). We define an SSID and password for the network we want to connect our ESP to, and we define the same for our ESP to allow other devices to connect to it. The following is the command to make our ESP32 work in both AP and STA modes:
WiFi.mode(WiFi_AP_STA);
Please find sample code below
#include <WiFi.h>
const char* ssid = "________"; // the ssid/name of the WiFi, the ESP will be connected to
const char* password = "________"; // the password of that WiFi
const char* assid = "your AP name";
const char* asecret = "your password";
void setup(){
Serial.begin(115200);
WiFi.mode(WIFI_AP_STA);
//access point part
Serial.println("Creating Accesspoint");
WiFi.softAP(assid,asecret);
Serial.print("IP address:\t");
Serial.println(WiFi.softAPIP());
//station part
Serial.print("connecting to...");
Serial.println(ssid);
WiFi.begin(ssid,password);
//while connection to WIFi is not established
while(WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop(){
// put your main code here, to run repeatedly:
}
Once the code is uploaded, you can open the terminal for the connection log. You may also try connecting to your ESP32 through the Access Point created. As you have learnt the STA+AP mode, you can explore further into this and create different applications.
read more : Send an HTML mail through PHP Mailer and localhost
Happy Learning! :)