WIFI Interfacing with Arduino
What is a WIFI?
Wi-Fi (short for Wireless Fidelity) is a wireless communication technology that allows devices to connect to a local area network (LAN) and the internet using radio waves, typically in the 2.4 GHz or 5 GHz frequency bands.
Arduino boards like Uno and Mega do not have built-in Wi-Fi, but Wi-Fi capability can be added using external Wi-Fi modules.
Module | Features |
---|---|
ESP8266 | Low-cost Wi-Fi module with AT commands or full programming via Arduino IDE |
What are AT Commands?
AT commands (short for Attention commands) are text-based instructions used to control modems and communication modules like GSM, Bluetooth, and Wi-Fi modules (e.g., ESP8266).
AT Commands for ESP8266 (Wi-Fi Module)
Command | Description |
---|---|
AT |
Check if the module is working |
AT+GMR |
Get firmware version |
AT+CWMODE=1 |
Set Wi-Fi mode to station |
AT+CWJAP="SSID","PWD" |
Connect to Wi-Fi network |
AT+CIFSR |
Get IP address |
AT+CIPSTART=... |
Start TCP or UDP connection |
AT+CIPSEND=... |
Send data over a network |
Connection of WIFI module With Arduino.
How to link WiFi to an HTML page?
In HTML code we use a linking tag known as an anchor tag and the href attribute which specifies the url.(Hypertext reference)to link to.
Below is an example from the given HTML code. After connecting WiFi to the router you will get an IP address you just have to copy the IP address and paste it into the anchor tag.
eg :- <a href="http://192.168.1.13/?LED=ON" class="control-button">Turn LED 1 ON</a>
Task 1:- To turn on/off LED from the website using wifi.
Code:-
#include <Arduino.h>
const int ledPin = 13;
String buffer = "";
void sendCommand(const String& cmd, int waitTime = 2000) {
Serial1.println(cmd);
delay(waitTime);
while (Serial1.available()) {
char c = Serial1.read();
Serial.write(c); // Debug output
}
}
void setup() {
Serial.begin(9600); // Serial Monitor
Serial1.begin(115200); // ESP8266 default baud rate
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
delay(2000);
Serial.println("\n--- Connecting to ESP8266 ---");
sendCommand("AT", 1000);
sendCommand("AT+CWMODE=1", 1000); // Station mode
// Replace with your WiFi credentials
sendCommand("AT+CWJAP=\"Your_ SSID\",\"Your_PASSWORD\"", 8000);
// Get and display IP address
Serial.println("--- Getting IP ---");
Serial1.println("AT+CIFSR");
delay(2000);
while (Serial1.available()) {
char c = Serial1.read();
Serial.write(c); // Print IP address
}
// Start TCP Server
sendCommand("AT+CIPMUX=1", 500);
sendCommand("AT+CIPSERVER=1, 80", 1000);
}
void loop() {
while (Serial1.available()) {
char c = Serial1.read();
buffer += c;
Serial.write(c); // Debug
if (buffer.indexOf("LED=ON") != -1) {
digitalWrite(ledPin, HIGH);
sendResponse("LED turned ON");
buffer = "";
} else if (buffer.indexOf("LED=OFF") != -1) {
digitalWrite(ledPin, LOW);
sendResponse("LED turned OFF");
buffer = "";
} else if (buffer.indexOf("\r\n\r\n") != -1) {
buffer = "";
}
}
}
void sendResponse(String msg) {
int idIndex = buffer.indexOf(",CONNECT");
if (idIndex == -1) return;
char connId = buffer.charAt(idIndex - 1); // Connection ID
String response = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n" + msg + "\r\n";
String sendCmd = "AT+CIPSEND=" + String(connId) + "," + response.length();
Serial1.println(sendCmd);
delay(100);
Serial1.print(response);
delay(100);
Serial1.println("AT+CIPCLOSE=" + String(connId));
}
Output:-
HTML Code:-
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>LED Control Panel</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f7f9;
color: #333;
text-align: center;
padding: 50px;
}
h1 {
margin-bottom: 40px;
color: #2c3e50;
}
.button-group {
display: flex;
justify-content: center;
gap: 20px;
flex-wrap: wrap;
}
.control-button {
background-color: #3498db;
color: white;
border: none;
padding: 15px 30px;
font-size: 30px;
border-radius: 8px;
cursor: pointer;
text-decoration : none;
}
.control-button:hover {
background-color: #2980b9;
}
.off-button {
background-color: #e74c3c;
}
.off-button:hover {
background-color: #c0392b;
}
</style>
</head>
<body>
<h1>LED Control Panel</h1>
<div class="button-group">
<a href="http://192.168.1.13/?LED=ON" class="control-button">Turn LED 1 ON</a>
<a href="http://192.168.1.13/?LED=OFF" class="control-button off-button">Turn LED 1 OFF</a>
<a href="http://192.168.1.13/?LED1=ON" class="control-button">Turn LED 2 ON</a>
<a href="http://192.168.1.13/?LED1=OFF" class="control-button off-button">Turn LED 2 OFF</a>
</div>
</body>
</html>
HTML Output :-
0 Comments