0% found this document useful (0 votes)
12 views52 pages

Presentation 1

Uploaded by

gunasundari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views52 pages

Presentation 1

Uploaded by

gunasundari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 52

ESP32 WIFI

COMMUNICATION
ESP32 WIFI
COMMUNICATION
ROUTE
R
• Connects local networks to other local
networks or the Internet.
• Connects devices to the network wirelessly,
using radio frequencies.
•No need of physical router or internet connection

•HTTP protocols are used for communication between


two ESP32 boards.

•HTTP communication : WI-FI AND ESPRESSIF


protocol

•HTTP client : Generating an HTTP request

•Server : Responds to HTTP request


CLIEN
T
CODE
Code to connect an ESP32 microcontroller to a wifi network and
send command to a server when button is pressed:

• Library used: WiFi.h


This library provides functionalities to connect your ESP32 microcontroller to a Wi-Fi network.
• ‘ssid’ and ‘password’ – used to store your wifi credentials.
• ‘host’- stores IP address of the server you want to communicate with.
• ‘httpPort’- Stores the port number of http communication (Default: 80)
• ‘Commands’- Holds the commands to be sent.
• ‘button’ and ‘btn_press’- GPIO Pin number where push button is inserted and btn_press tracks whether button is pressed.
• ‘Wifi.mode(WIFI_STA)’- sets the
WiFi mode to station (client mode).​
• ‘Wifi.begin(ssid,password)’-
Attempts to connect to wifi
network.​
• The while loop prints the IP address
once the wifi connection is
established.
• This part of the code ensures that
one command is being sent to the
server for each button press.

• ‘digitalRead(button)==LOW’-
Checks if button is pressed.
• If pressed, commands will be sent
using ‘send_commands()’
function (user defined).
This function
‘send_commands()’ sends
the command to the
server.
• ‘WiFiClient’- creates a
TCP connection.
• ‘client.connect(host,htt
pPort)’- Attempts to
connect to the server.
This part of the code
constructs and sends an
HTTP GET request to the
server, then waits for a
response.
This part of the code reads
the server's response to
the HTTP request and
prints it to the Serial
Monitor
SERVE
R
CODE
CODE
Code to set up an ESP32 as a web server to control an LED via WiFi

Declaration of variables:
• “WiFi.h”- library for handling WiFi functions on the ESP32.
• “Commands_Reply”- Variable to hold the response message sent back to the
client.
• “WiFiServer server(80)”- Initializes a web server on port 80, which is the default
port for HTTP communication.
This part of the code initializes the necessary settings for the ESP32 to function as a
web server and control an LED.

•“WiFi.begin(ssid,password)”- Initiates the connection to the specified WiFi


network using the provided SSID and password.
This part of the code
handles connecting the
ESP32 to a WiFi network,
starting the web server, and
providing the user with the
URL to connect to the
server.

• “server.begin()”- Starts
the web server on port
80.
• “WiFi.localIP()”- returns
the local IP address
assigned to the ESP32.
This part handles client
connection to the web server
and processes their requests.

• “server.available()”- checks
if there is a client
connected to the server.
“client.readStringUntil(‘\r’)”-
Reads the client's request line
until a carriage return
character (‘\r’) is encountered
This part of the code handles a specific command from the client to make
the LED blink.
• “if(req.indexOf(“LED_Blink”)!=-1)”- checks if the received request
‘req’ contains the substring "LED_Blink“.
• If the request is “LED_Blink”, the LED is made to blink 3 times.
• When the server receives a request containing "LED_On", it
turns the LED on, responds to the client with "LED Status : On",
and prints the status to the serial monitor.
• When the server receives a request containing "LED_Off", it
turns the LED off, responds to the client with "LED Status : Off",
and prints the status to the serial monitor.
• If the request does not
contain "LED_Blink",
"LED_On", or "LED_Off",
it is considered invalid.
• “client.stop()”- stops
the connection with the
client.
• “client.flush()”- This
ensures that any
remaining data in the
client’s output buffer is
sent and then cleared.
What is Esp Now?

• ESP-NOW is a wireless communication


protocol designed by Espressif for
ESP8266 and ESP32 microcontrollers.
• It allows devices to communicate
directly with each other using their MAC Connected

(Media Access Control) addresses.


• ESP-NOW supports peer-to-peer and
broadcast communication models,
allowing multiple devices to exchange
data efficiently and securely.
Latency

Peer-to-Peer
Communication
Encryption

Ease of
use
Data Transmission

Operability
HOW DOES IT
WORK?
Setup and Initialize
• Wi-Fi Mode Configuration
ESP-NOW requires WiFi module to be initialized even though it doesn’t
rely on it.
Device is set to station mode for the same (‘WIFI_STA’)
• Registering Callbacks
Callbacks, which are functions that handle events when data is sent or
received, are registered for sending and receiving data

Peer-to-Peer Communication
• MAC Address
Each ESP device has a unique MAC address that can
be obtained using ‘WiFi.macAddress()’ which is used to
identify peers
• Adding Peers
To communicate, devices are to be added as peers by means of their MAC
address and encryption keys
Message Transmission
• Sending data

• Receiving data
Incoming data is handled by the registered callback function
Security
• Send callback
Checks if the message was successfully sent
ADVANTAGES
Low Latency

Low power consumption

Robust & Reliable

Cost Effective

Supports multiple peers

Secure & Compatible


DISADVANTAGES

 Limited Number of devices:


• Unicast - 20 devices
• ESP-NOW in encrypted mode - 6 devices
• Broadcast - unlimited devices

 Data Size Limitations: (Max - 250 bytes)

 Network Management:
• Complexity increases with number of devices

 Interference:
NICATION
MS
CLOUD
COMMUNICATION
PLATFORMS
Introduction:
Cloud communication platforms are revolutionizing how
devices and services connect and share data. These
powerful tools enable seamless integration between
microcontrollers, sensors, and online services, unlocking
a world of innovative applications and smart solutions.
How Cloud
Communication platform
works:
How Cloud
Communication platform
works: 1 Data Transmission

Data Ingestion
and processing 2

3 Data Storage

Security and
management 4
Popular platforms:
Exploring User-Friendly and Enterprise-Grade Options
Adafruit IO

Overview:
A cloud service designed to simplify data logging,
visualization, and interaction with IoT devices.

Features:
• Easy to use dashboards for visualizing data.
• Integrates seamlessly with Adafruit hardware.
• Supports MQTT and REST APIs for data communication.
• Includes triggers for automated actions.
BLYNK

Overview:
A platform designed to build mobile and web
applications for the Internet of Things..

Features:
• Drag-and-drop interface for creating custom dashboards.
• Supports a wide range of hardware platforms.
• Provides ready-to-use widgets for various functionalities.
• Real-time data monitoring and control.
THING SPEAK

Overview:
An IoT analytics platform service that allows to visualize,
and analyze live data streams in the cloud , it offers
fundamental features and its configurations can be a bit
difficult.

Features:
• Data storage and visualization in real-time.
• MATLAB integration for advanced data analysis.
• Supports HTTP and MQTT protocols.
• Ability to create public/private channels for data.
AWS IOT CORE

Overview:
A managed cloud service that lets connected devices
easily and securely interact with cloud applications
and other devices.

Features:
• Highly scalable and secure infrastructure.
• Integration with other AWS services (e.g., Lambda, S3).
• Supports Web Sockets and other protocols like http.
• Advanced device management and analytics.
Connecting Microcontrollers to Cloud
Platforms

1 Hardware Setup
Connect your microcontroller to the internet using Ethernet, Wi-
Fi, or cellular connectivity.

2 API Integration
Integrate your microcontroller's code with the cloud platform's
API to enable data exchange.

3 Real-time Monitoring
Leverage the cloud platform's dashboard to visualize sensor data
and control your device remotely.
Merits of using cloud
communication :
1 Remote Monitoring

Data storage &


analysis 2

3 Device Scalability

Security 4
IOT Solution: Simulator
Speed Bump alert system

Problem statement:
High-speed travel can miss speed breakers,
causing accidents.

Solution: Feed
Ultrasonic sensors detect speed breakers and alert
drivers, storing location in the cloud.
Code with API key integration
Model of the detector
Feed created in Adafruit API key:
MQTT Protocol:

Overview:
MQTT (Message Queuing Telemetry
Transport) is a lightweight messaging
protocol designed for small sensors and
mobile devices, optimized for high-latency
or unreliable networks.

It is widely used in IoT applications for


efficient, real-time communication.
WiFi and MQTT setup:
This segment defines the WiFi and MQTT credentials. It sets up the MQTT client and the MQTT
topics for publishing hump alerts, latitude, and longitude.
WiFi Connection Function:
This function connects the device to the specified WiFi network and prints the IP
address once connected.
MQTT Connection Function:
This function connects to the MQTT server and handles reconnection if the initial attempt
fails.
Distance Measurement Function:
This function measures the distance using the ultrasonic sensor by sending a trigger pulse
and reading the echo pulse duration.
Geolocation Retrieval Function:
This function fetches the geolocation (latitude and longitude) from an IP Geolocation API
if the device is connected to WiFi.
Publish Data Function:
This function publishes the hump alert, latitude, and longitude to the Adafruit IO feeds.
Main Loop :
• The main loop of the program repeatedly
checks the MQTT connection, reads distances
from two ultrasonic sensors, and detects
whether there is a minor or major hump based
on the distance measurements.

• If a hump is detected, the loop fetches the


current geolocation data, prints it, and sends an
alert with the location data to Adafruit IO.

• If no hump is detected, it advises to drive


carefully and pauses for 1.5 seconds before
starting the loop again.

You might also like