ESP32 WITH WEB SERVER
Pada percobaan kali ini, dijelaskan mengenai pembuatan web server dalam ESP32. Percobaan ini menggunakan sensor eksternal. Sensor eksternal yang digunakan merupakan sensor BMP180. Hasil pembacaan sensor akan ditampilkan pada web browser.
Pada percobaan ini, komponen dan perangkat yang dibutuhkan sebagai berikut.
1. ESP32 DEVKIT V1 Board (yang saya gunakan 30 pin)
2. Arduino IDE
4. PC / Laptop
6. Jumper wires (yang saya gunakan female to male) sebanyak 4 buah
7. Sensor BMP180
8. Breadboard
Langkah - langkah pengerjaan :
Pertama, merangkai komponen sesuai skema berikut
Keterangan :
1. GPIO GND menuju GND BMP180
2. GPIO 3V3 menuju VCC BMP180
3. GPIO D21 menuju SDA BMP180
4. GPIO D22 menuju SCL BMP180
Kedua, masukkan code program berikut pada Arduino IDE.
// Load Wi-Fi library
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>
#include <Adafruit_Sensor.h>
//uncomment the following lines if you're using SPI
/*#include <SPI.h>
#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5*/
Adafruit_BMP085 bmp;
// Replace with your network credentials
const char* ssid = "iPhone";
const char* password = "abesarkkecilukecil";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;
void setup() {
Serial.begin(115200);
bool status;
// default settings
// (you can also pass in a Wire library object like &Wire2)
//status = bmp.begin();
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085/BMP180 sensor, check wiring!");
while (1) {}
}
// Connect 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(".");
}
// 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(); // Listen for incoming clients
if (client) { // If a new client connects,
currentTime = millis();
previousTime = currentTime;
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() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
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();
// 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 table
client.println("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial;}");
client.println("table { border-collapse: collapse; width:35%; margin-left:auto; margin-right:auto; }");
client.println("th { padding: 12px; background-color: #0043af; color: white; }");
client.println("tr { border: 1px solid #ddd; padding: 12px; }");
client.println("tr:hover { background-color: #bcbcbc; }");
client.println("td { border: none; padding: 12px; }");
client.println(".sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px; }");
// Web Page Heading
client.println("</style></head><body><h1>ESP32 with BMP180</h1>");
client.println("<table><tr><th>MEASUREMENT</th><th>VALUE</th></tr>");
client.println("<tr><td>Temp. Celcius</td><td><span class=\"sensor\">");
client.println(bmp.readTemperature());
client.println(" *C</span></td></tr>");
client.println("<tr><td>Temp. Fahrenheit</td><td><span class=\"sensor\">");
client.println(1.8 * bmp.readTemperature() + 32);
client.println(" *F</span></td></tr>");
client.println("<tr><td>Pressure</td><td><span class=\"sensor\">");
client.println(bmp.readPressure() / 100.0F);
client.println(" Pa</span></td></tr>");
client.println("<tr><td>Altitude</td><td><span class=\"sensor\">");
client.println(bmp.readAltitude());
client.println(" m</span></td></tr>");
client.println("<tr><td>Pressure at sealevel (calculated)</td><td><span class=\"sensor\">");
client.println(bmp.readSealevelPressure());
client.println(" Pa</span></td></tr>");
client.println("<tr><td>Real altitude</td><td><span class=\"sensor\">");
client.println(bmp.readAltitude(102000));
client.println(" m</span></td></tr>");
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
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("");
}
}
Penjelasan code program :
1. Memasukkan library yang diperlukan.
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>
#include <Adafruit_Sensor.h>
2. Memasukkan SPI Communication karena menggunakan komunikasi I2C berupa BMP180.
#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5*/
3. Memasukkan SSID dan password di baris berikut di dalam tanda kutip ganda.
const char* ssid = "iPhone";
const char* password = "abesarkkecilukecil";
4. Mengatur web server ke port 80.
WiFiServer server(80);
5. Membuat variabel untuk menyimpan header permintaan HTTP.
String header;
Setup()
6. Memulai komunikasi serial dengan baud rate 115200 untuk keperluan debugging.
Serial.begin(115200);
7. Memeriksa bahwa sensor BME280 berhasil diinisialisasi
if (!bmp.begin()) {
Serial.println("Could not find a valid BMP085/BMP180 sensor, check wiring!");
while (1) {}
}
8. Memulai koneksi Wi-Fi dengan WiFi.begin(ssid, password); tunggu koneksi berhasil dan mencetak alamat IP di Serial Monitor.
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
Loop()
9. ESP32 selalu mendengarkan klien masuk
WiFiClient client = server.available();
10. Saat permintaan diterima klien, maka akan menyimpan data yang masuk. Loop sementara akan berjalan selama klien tetap terhubung.
if (client) { // If a new client connects,
currentTime = millis();
previousTime = currentTime;
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() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
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();
11. Menampilkan halaman web HTML.
// 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 table
client.println("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial;}");
client.println("table { border-collapse: collapse; width:35%; margin-left:auto; margin-right:auto; }");
client.println("th { padding: 12px; background-color: #0043af; color: white; }");
client.println("tr { border: 1px solid #ddd; padding: 12px; }");
client.println("tr:hover { background-color: #bcbcbc; }");
client.println("td { border: none; padding: 12px; }");
client.println(".sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px; }");
12. Menampilkan pembacaan sensor.
// Web Page Heading
client.println("</style></head><body><h1>ESP32 with BMP180</h1>");
client.println("<table><tr><th>MEASUREMENT</th><th>VALUE</th></tr>");
client.println("<tr><td>Temp. Celcius</td><td><span class=\"sensor\">");
client.println(bmp.readTemperature());
client.println(" *C</span></td></tr>");
client.println("<tr><td>Temp. Fahrenheit</td><td><span class=\"sensor\">");
client.println(1.8 * bmp.readTemperature() + 32);
client.println(" *F</span></td></tr>");
client.println("<tr><td>Pressure</td><td><span class=\"sensor\">");
client.println(bmp.readPressure() / 100.0F);
client.println(" Pa</span></td></tr>");
client.println("<tr><td>Altitude</td><td><span class=\"sensor\">");
client.println(bmp.readAltitude());
client.println(" m</span></td></tr>");
client.println("<tr><td>Pressure at sealevel (calculated)</td><td><span class=\"sensor\">");
client.println(bmp.readSealevelPressure());
client.println(" Pa</span></td></tr>");
client.println("<tr><td>Real altitude</td><td><span class=\"sensor\">");
client.println(bmp.readAltitude(102000));
client.println(" m</span></td></tr>");
client.println("</body></html>");
13. Menutup koneksi.
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
Ketiga, mengatur networks credential.
const char* ssid = "iPhone";
const char* password = "abesarkkecilukecil";
Keempat, verify dan upload program pada Arduino Ide.
Kelima, buka Serial Monitor dan pastikan berada pada baud rate 115200. Kemudian, tekan tombol EN pada ESP32.
Keenam, ESP32 akan terhubung ke Wi-Fi dan mengeluarkan alamat IP ESP32 pada Serial Monitor. Salin alamat IP tersebut untuk mengakses server web ESP32.
Ke-tujuh, IP Address yang didapatkan adalah 172.20.10.2. Maka, ketik IP Address pada web.
Ke-delapan, pastikan juga berjalan di Serial Monitor.
Berikut hasil video percobaan yang dilakukan :
Komentar
Posting Komentar