ESP

About ESP

What is ESP

The ESP32 and ESP8266 are popular series of low-cost, low-power, and highly versatile Wi-Fi and Bluetooth-enabled microcontrollers developed by Espressif Systems. These microcontrollers are widely used in various Internet of Things (IoT) applications.

The ESP8266 was the first chip released by Espressif Systems in 2014. It gained significant popularity due to its affordability, ease of use, and compatibility with Arduino development boards. The ESP8266 features a 32-bit Tensilica processor, built-in Wi-Fi connectivity, and sufficient memory to run applications.

The ESP32, released in 2016, is an enhanced version of the ESP8266. It includes a dual-core processor, increased memory capacity, and additional features such as Bluetooth connectivity, more GPIO pins, touch sensors, and support for various interfaces (SPI, I2C, UART, etc.). The ESP32 offers better performance and more capabilities compared to the ESP8266.

Both the ESP8266 and ESP32 have gained popularity in the maker and IoT communities due to their affordability, ease of use, extensive documentation, and large developer community. They can be programmed using the Arduino IDE, MicroPython, or the Espressif's native development framework called ESP-IDF. These microcontrollers are widely used for building IoT devices, home automation systems, wireless sensor networks, and various other projects that require Wi-Fi or Bluetooth connectivity.

What we are going to learn in ESP


What is ESP32

ESP32 is a series of low-cost, low-power system-on-chip microcontrollers with integrated Wi-Fi and Bluetooth capabilities. It is designed and manufactured by Espressif Systems, a company based in Shanghai, China.

The ESP32 microcontroller is a successor to the popular ESP8266 microcontroller and features a dual-core processor with more memory and I/O capabilities. It is widely used in the Internet of Things (IoT) applications, including home automation, smart devices, wearables, and robotics.

Some of the key features of the ESP32 include:

What is ESP8266

ESP8266 is a low-cost, low-power system-on-chip (SoC) microcontroller with integrated Wi-Fi capabilities. It is designed and manufactured by Espressif Systems, a company based in Shanghai, China.

The ESP8266 microcontroller became popular due to its low cost, small size, and built-in Wi-Fi connectivity, making it suitable for Internet of Things (IoT) applications such as home automation, sensor networks, and smart devices.


How They are different from each Other

Processor:

ESP8266 has a single-core processor with 32-bit RISC architecture while ESP32 has a dual-core processor with 32-bit RISC-V architecture. ESP32 has more processing power than ESP8266 and can handle more complex tasks.

Memory:

ESP32 has more memory than ESP8266, including more RAM and Flash memory. This means that ESP32 can handle larger programs and store more data than ESP8266.

Input/Output Interfaces:

ESP32 has more input/output (I/O) interfaces than ESP8266, including I2S, I2C, SPI, UART, ADC, and DAC. ESP8266 also has multiple I/O interfaces but not as many as ESP32.

Wi-Fi Connectivity:

Both microcontrollers have built-in Wi-Fi connectivity, but ESP32 has more advanced Wi-Fi features, including support for both 2.4GHz and 5GHz Wi-Fi bands, and more security protocols. ESP8266 only supports 2.4GHz Wi-Fi and fewer security protocols.

Bluetooth Connectivity:

ESP32 has built-in Bluetooth connectivity, while ESP8266 does not.

Power Consumption:

ESP32 has more power modes than ESP8266, allowing it to consume less power when running on battery or low-power applications. However, ESP8266 has a low-power deep-sleep mode that allows it to consume very little power.

Cost:

ESP8266 is generally cheaper than ESP32 due to its simpler design and fewer features. Overall, both ESP8266 and ESP32 are powerful and versatile microcontrollers that can be used for a wide range of IoT applications. The choice between the two depends on the specific requirements of the project, including processing power, memory, connectivity, and power consumption.


How They are different from Arduino

ESP and Arduino are two different platforms for building electronic projects. The main differences between them are:

Microcontroller: :

Arduino boards are based on Atmel microcontrollers such as the ATmega328P, while ESP boards are based on the Espressif Systems ESP8266 or ESP32 microcontrollers. The ESP8266 and ESP32 have built-in Wi-Fi capabilities, making them ideal for Internet of Things (IoT) applications.

Programming Language: :

Arduino uses a simplified version of C++ programming language and has its own Integrated Development Environment (IDE). On the other hand, ESP boards can be programmed using the Arduino IDE as well as the Espressif's ESP-IDF which provides low-level access to the ESP microcontroller.

Connectivity: :

ESP boards have built-in Wi-Fi capabilities, making it easier to connect to the internet and other Wi-Fi enabled devices. Arduino boards require additional hardware, such as a Wi-Fi shield, to connect to the internet or other devices.

Processing Power: :

ESP boards are generally more powerful than Arduino boards. The ESP32, for example, has two cores and can run at up to 240 MHz, while the Arduino Uno runs at 16 MHz with one core. This difference in processing power makes ESP boards better suited for complex applications.

Price: :

ESP boards are generally cheaper than Arduino boards. This is because the ESP microcontrollers are cheaper than the Atmel microcontrollers used in Arduino boards. However, the cost of the board also depends on the specific model and features.


Installing Board of ESP32 and ESP8266

Install the Latest Arduino IDE

Before you can install the board of ESP32 and ESP8266 in the Arduino IDE, you need to download and install the latest version of the Arduino IDE software. You can download it from the official Arduino website.

Add Board Manager URLs

The next step is to add the board manager URLs for ESP32 and ESP8266. To do this, open the Arduino IDE and go to File > Preferences. In the "Additional Boards Manager URLs" field, enter the following URLs:

For ESP32:

http//dl.espressif.com/dl/package_esp32_index.json

ForESP8266:

http://arduino.esp8266.com/stable/package_esp8266com_index.json

Install Board Package

After adding the board manager URLs, you need to install the board package. To do this, go to Tools > Board > Boards Manager. In the Boards Manager window, search for "ESP32" or "ESP8266" and install the package.


Select Board and Port

Once the board package is installed, you can select the board and port for your project. Go to Tools > Board and select the board you want to use, such as "ESP32 Dev Module" or "NodeMCU 1.0 (ESP-12E Module)" for ESP8266. Then, go to Tools > Port and select the port your board is connected to.


Getting Data in Serial Monitor

The serial monitor is a very useful tool that is included in the Integrated Development Environment (IDE). It allows you to view and send data to and from your ESP board via a serial connection. This can be very helpful when you are trying to debug your code or troubleshoot any problems that you are having.

To get data in the serial monitor, you need to establish a serial communication between the board and the computer. Serial communication is a way to exchange information between the two devices through a serial interface, typically via a USB cable. Connect the board to your computer via a USB cable.

int potpin = A0; void setup() { Serial.begin(9600); pinMode(potpin,INPUT); } void loop() { int val = analogRead(potpin); Serial.print("Potentiometer Value is : "); Serial.println(val); }


Turning on the Hotspots

To turn on the hotspot on an ESP8266, you need to configure it as an access point and start the Wi-Fi server.

#include <ESP8266WiFi.h> const char* ssid = "ESP8266-Access-Point"; const char* password = "123456789"; void setup() { Serial.begin(115200); WiFi.softAP(ssid, password); IPAddress IP = WiFi.softAPIP(); Serial.print("AP IP address: "); Serial.println(IP); } void loop() { }

In the code above, the WiFi.softAP() function sets up the ESP8266 as an access point with the specified SSID and password. The WiFi.softAPIP() function returns the IP address of the access point.

After uploading this code to your ESP8266, you should be able to find the ESP8266's Wi-Fi hotspot with the SSID "ESP8266-Access-Point" and connect to it with the password "123456789". Note that the ESP8266 hotspot has a default IP address of 192.168.4.1, which you can use to connect to the ESP8266 through a browser or other means.

To turn on the hotspot on an ESP32, you need to configure the ESP32 as an access point and start the Wi-Fi server.

#include <WiFi.h> const char* ssid = "ESP32-Access-Point"; const char* password = "123456789"; //you can change the ssid an d password void setup() { Serial.begin(115200); WiFi.softAP(ssid, password); IPAddress IP = WiFi.softAPIP(); Serial.print("AP IP address: "); Serial.println(IP); } void loop() { }

After uploading this code to your ESP32, you should be able to find the ESP32's Wi-Fi hotspot with the SSID "ESP32-Access-Point" and connect to it with the password "123456789". Note that the ESP32 hotspot has a default IP address of 192.168.4.1


Control via Hotspot

Controlling an ESP32 LED via a hotspot involves creating a web server on the ESP32 and hosting a web page that allows you to turn the LED on and off using a button on the webpage. Here are the general steps to achieve this:


Connecting to Wi-Fi

#include <WiFi.h> const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); } void loop() { // Your code goes here } /xmp></code><p>Make sure to replace your_SSID and your_PASSWORD with your actual WiFi network name and password.</p><p> This code will connect the ESP32 to the WiFi network and print a message to the serial monitor when the connection is established. You can add your own code in the loop() function to perform any desired actions after the connection is established.</p><b> For ESP8266</b><code><xmp> #include <ESP8266WiFi.h> const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); } void loop() { // Your code goes here }

Make sure to replace your_SSID and your_PASSWORD with your actual WiFi network name and password.

This code will connect the ESP8266 to the WiFi network and print a message to the serial monitor when the connection is established. You can add your own code in the loop() function to perform any desired actions after the connection is established.


Bluetooth in ESP32

The ESP32 is a popular microcontroller that is widely used in various IoT projects. One of the notable features of the ESP32 is its built-in Bluetooth capability. With this feature, the ESP32 can communicate with other Bluetooth devices, such as smartphones, tablets, and laptops.

The Bluetooth module on the ESP32 supports both Classic Bluetooth and Bluetooth Low Energy (BLE) modes. Classic Bluetooth allows the ESP32 to connect to other devices for audio and data transfer, while BLE allows for energy-efficient communication with other BLE-enabled devices.

Using the ESP32's built-in Bluetooth module, it is possible to create a wide range of IoT applications. For example, you can create a Bluetooth-enabled sensor network that can transmit sensor data wirelessly to a central hub for processing and analysis. You can also use the Bluetooth module to create a wireless control system for home automation or to remotely control robotic devices.

Overall, the ESP32's built-in Bluetooth capability offers a wide range of possibilities for creating innovative and powerful IoT solutions.

Turning the Bluetooth on #include <ESP8266WiFi.h> #include <BLEDevice.h> void setup() { Serial.begin(115200); BLEDevice::init("ESP32"); BLEDevice::startAdvertising(); Serial.println("Bluetooth on"); } void loop() { // Your code here }

BLEDevice::init("ESP32") function initializes the Bluetooth subsystem with a custom name, and BLEDevice::startAdvertising() function starts advertising the device's presence to other Bluetooth devices.

Once you upload this code to your ESP32 board and open the Bluetooth settings on a nearby device, you should see the ESP32 device listed as "ESP32" or whatever custom name you specified in the BLEDevice::init() function.

Note that in order to use the Bluetooth module on the ESP32, you need to have the necessary drivers and libraries installed in your development environment. You can download the ESP32 Arduino Core from the official website and install it using the Arduino IDE.



Controlling via Bluetooth

#include <BLEDevice.h> const int ledPin = 2; BLECharacteristic* pCharacteristic; bool deviceConnected = false; void setup() { Serial.begin(115200); pinMode(ledPin, OUTPUT); BLEDevice::init("ESP32 LED Control"); BLEServer *pServer = BLEDevice::createServer(); BLEService *pService = pServer->createService(BLEUUID((uint16_t)0x180D)); pCharacteristic = pService->createCharacteristic(BLEUUID((uint16_t)0x2A37), BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE); pCharacteristic->setCallbacks(new LEDCharacteristicCallback()); pService->start(); BLEAdvertising *pAdvertising = pServer->getAdvertising(); pAdvertising->start(); } void loop() { if (deviceConnected) { digitalWrite(ledPin, HIGH); delay(1000); digitalWrite(ledPin, LOW); delay(1000); } } class LEDCharacteristicCallback : public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { std::string value = pCharacteristic->getValue(); if (value.length() > 0) { int intValue = value[0]; if (intValue == 0) { digitalWrite(ledPin, LOW); } else { digitalWrite(ledPin, HIGH); } } } void onConnect(BLEServer* pServer) { deviceConnected = true; } void onDisconnect(BLEServer* pServer) { deviceConnected = false; } }

This code uses the Bluetooth Low Energy (BLE) module on the ESP32 to create a custom service with a characteristic that can be used to control an LED. When a client device connects to the ESP32, the LED starts blinking. The client device can then write a value of either 0 or 1 to the characteristic to turn the LED on or off.

The LEDCharacteristicCallback class defines the callback functions for the characteristic, including onWrite, which is called when a client writes to the characteristic, and onConnect and onDisconnect, which are called when a client connects or disconnects from the server.

To control the LED using Bluetooth, you can use a Bluetooth-enabled device, such as a smartphone or tablet, to connect to the ESP32 and write a value of either 0 or 1 to the characteristic. The ESP32 will then turn the LED on or off accordingly.


Controlling using external Bluetooth chip(same for ESP32 and ESP8266) #include <SoftwareSerial.h> SoftwareSerial BTserial(14, 12); // RX | TX const int ledPin = 2; void setup() { Serial.begin(115200); pinMode(ledPin, OUTPUT); BTserial.begin(9600); Serial.println("Bluetooth is Ready!"); } void loop() { if (BTserial.available()) { char cmd = BTserial.read(); if (cmd == '0') { digitalWrite(ledPin, LOW); Serial.println("LED OFF"); } else if (cmd == '1') { digitalWrite(ledPin, HIGH); Serial.println("LED ON"); } }

This code uses the SoftwareSerial library to create a serial port on pins 14 (RX) and 12 (TX) to communicate with the external Bluetooth chip. When the Bluetooth chip sends a value of either 0 or 1, the ESP32 will turn the LED on or off accordingly.

To use this code, you need to connect the external Bluetooth chip to the ESP using the RX and TX pins. You also need to pair the external Bluetooth chip with a Bluetooth-enabled device, such as a smartphone or tablet, and connect to the chip using a serial terminal app.

Once you're connected, you can send a value of either 0 or 1 to the chip to turn the LED on or off. The ESP will read the value from the serial port and turn the LED on or off accordingly.



Voice control via Bluetooth

We can give the voice command in esp using external Bluetooth module which convert text to string and send the string to esp board and control the system. This is also workable with Arduino board.

In this example we are controlling 3 LED light with voice.

I am using AMR voice app which is easy available on the internet. You can use any app you want.

String voice; int RED = 2; int GREEN = 3; int BLUE = 4; void RedOn(){ digitalWrite (RED, HIGH); } void RedOff(){ digitalWrite (RED, LOW); } void GreenOn(){ digitalWrite (GREEN, HIGH); } void GreenOff(){ digitalWrite (GREEN, LOW); } void BlueOn(){ digitalWrite (BLUE, HIGH); } void BlueOff(){ digitalWrite (BLUE, LOW); } void allon() { digitalWrite (RED, HIGH); digitalWrite (GREEN, HIGH); digitalWrite (BLUE, HIGH); } void alloff() { digitalWrite (RED, LOW); digitalWrite (GREEN, LOW); digitalWrite (BLUE, LOW); } void setup() { Serial.begin(9600); pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(BLUE, OUTPUT); } void loop() { while(Serial.available()) { delay(10); char c=Serial.read(); if(c=='#') {break; } voice += c; } if (voice.length() > 0) { Serial.println(voice); if (voice == "on" || voice == "all") { allon() ; } else if (voice == "off" || voice=="all off") { alloff() ; } else if(voice =="red" || voice =="red on"){ RedOn(); } else if(voice =="red off"){ RedOff(); } else if(voice =="green" || voice =="green on"){ GreenOn(); } else if( voice =="green off" ){ GreenOff(); } else if(voice =="blue" || voice =="blue on"){ BlueOn(); } else if(voice =="blue off"){ BlueOff(); } voice=""; } }



Creating a Web Server

A web server is a program that serves web pages and other content over the internet or an intranet. When you access a website, your web browser sends a request to the web server hosting that website, and the web server responds with the requested content, such as HTML files, images, videos, or other resources.

A web server runs on a computer or a server and listens for incoming requests on a specific port, usually port 80 for HTTP requests or port 443 for HTTPS requests. When a request comes in, the web server processes the request, retrieves the requested content from its file system or a database, and sends it back to the client that made the request. .

Web servers can be configured to support different protocols, such as HTTP, HTTPS, FTP, SMTP, or IMAP, and can also support different programming languages, frameworks, and libraries for building web applications. They can also handle different types of requests, such as static content requests or dynamic content requests that require server-side processing. .

Some popular web servers include Apache HTTP Server, Nginx, Microsoft IIS, and Google Web Server. In the context of IoT, microcontrollers and single-board computers like the ESP32 can also act as web servers to provide remote access to data and control of connected devices. .

Creating web server in esp32 #include <WiFi.h> #include <WebServer.h> const char* ssid = "YOUR_SSID"; const char* password = "YOUR_PASSWORD"; WebServer server(80); void handleRoot() { server.send(200, "text/plain", "Hello from ESP32!"); } void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); server.on("/", handleRoot); server.begin(); Serial.println("Web server started"); } void loop() { server.handleClient(); }

This code creates a web server on port 80 using the WebServer library. When a client connects to the server and requests the root path ("/"), the server will send a "Hello from ESP32!" message back to the client.

To use this code, you need to connect the ESP32 to your Wi-Fi network by setting the ssid and password variables to your Wi-Fi network name and password, respectively. Once the ESP32 is connected to Wi-Fi, you can open a web browser and navigate to the IP address of your ESP32 to see the "Hello from ESP32!" message.

Note that this is a very simple example and the WebServer library can be used to create more complex web servers with multiple routes and dynamic content.


Creating web server in esp8266 #include <ESP8266WiFi.h> #include <ESP8266WebServer.h> const char* ssid = "YOUR_SSID"; const char* password = "YOUR_PASSWORD"; ESP8266WebServer server(80); void handleRoot() { server.send(200, "text/plain", "Hello from ESP8266!"); } void setup() { Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); server.on("/", handleRoot); server.begin(); Serial.println("Web server started"); } void loop() { server.handleClient(); }

This code creates a web server on port 80 using the ESP8266WebServer library. When a client connects to the server and requests the root path ("/"), the server will send a "Hello from ESP8266!" message back to the client.

To use this code, you need to connect the ESP8266 to your Wi-Fi network by setting the ssid and password variables to your Wi-Fi network name and password, respectively. Once the ESP8266 is connected to Wi-Fi, you can open a web browser and navigate to the IP address of your ESP8266 to see the "Hello from ESP8266!" message.

Note that this is a very simple example and the ESP8266WebServer library can be used to create more complex web servers with multiple routes and dynamic content.



Pulse Wave Modulation

Pulse Width Modulation (PWM) is a common technique used in electronics to control the amount of power delivered to a load, such as an LED, motor, or heater. It works by rapidly turning a voltage source on and off at a specific frequency, and varying the width of the on-time or duty cycle of the pulse wave. This allows us to effectively adjust the amount of power that the load receives without changing the voltage level.

In Microcontroller, PWM is commonly used to control the brightness of an LED, the speed of a motor, or the temperature of a heater. The Arduino has built-in hardware support for PWM on certain pins, allowing us to easily generate pulse waves of varying duty cycles.

To use PWM in Microcontroller, we can use the analogWrite() function, which sends a PWM signal to a specific pin. The analogWrite() function takes two arguments: the first is the pin number, and the second is the duty cycle, which can range from 0 (always off) to 255 (always on). .

NOTE: PWM is also supported in Arduino boards

const int ledPin = D2; // the pin number of the LED int brightness = 0; // initial LED brightness int fadeAmount = 5; // amount to fade the LED by void setup() { pinMode(ledPin, OUTPUT); // set the LED pin as an output } void loop() { analogWrite(ledPin, brightness); // set the LED brightness using PWM brightness += fadeAmount; // increase or decrease the LED brightness if (brightness <= 0 || brightness >= 255) { // if brightness is at min or max fadeAmount = -fadeAmount; // reverse the fade direction } delay(30); // wait for a short period before updating the brightness again }

This code sets up an LED on pin 9 and uses the analogWrite() function to set the LED brightness using PWM. The LED brightness is increased or decreased by a fixed amount (fadeAmount) each time the loop() function is called, and the direction of the fade is reversed when the brightness reaches the minimum or maximum level.

To use this code, connect an LED to pin 9 of your Arduino board (with a current-limiting resistor if necessary), upload the code to the board, and watch as the LED fades in and out smoothly. You can adjust the fadeAmount variable to change the speed of the fade, and the delay() function to change the delay between brightness updates.

Note that PWM can also be used to control the speed of a motor, the temperature of a heater, or the intensity of a speaker, among other things. The analogWrite() function can be used to set the PWM output on any pin that supports PWM, which can vary depending on the specific Arduino board you are using. Check your board's documentation to see which pins support PWM.



Getting updates via OTA

OTA (Over-The-Air) update allows you to upload new firmware to your ESP32 or ESP8266 board wirelessly, without the need to physically connect it to your computer with a USB cable.

#include <WiFi.h> #include <ESPmDNS.h> #include <WiFiUdp.h> #include <ArduinoOTA.h> const char* ssid = "YourWiFiSSID"; // your WiFi network SSID const char* password = "YourWiFiPassword"; // your WiFi network password void setup() { Serial.begin(115200); // initialize serial communication at 115200 baud rate Serial.println("Booting..."); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("Connection Failed! Rebooting..."); delay(5000); ESP.restart(); } // initialize OTA ArduinoOTA.onStart([]() { Serial.println("OTA Starting..."); }); ArduinoOTA.onEnd([]() { Serial.println("\nOTA End"); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { Serial.printf("OTA Progress: %u%%\r", (progress / (total / 100))); }); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("OTA Error[%u]: ", error); if (error == OTA_AUTH_ERROR) { Serial.println("Auth Failed"); } else if (error == OTA_BEGIN_ERROR) { Serial.println("Begin Failed"); } else if (error == OTA_CONNECT_ERROR) { Serial.println("Connect Failed"); } else if (error == OTA_RECEIVE_ERROR) { Serial.println("Receive Failed"); } else if (error == OTA_END_ERROR) { Serial.println("End Failed"); } }); ArduinoOTA.begin(); Serial.println("OTA Ready"); Serial.print("IP address: "); Serial.println(WiFi.localIP()); } void loop() { ArduinoOTA.handle(); }