Tutorial ESP32 – Membuat Web Server dengan ESP32

ARSAFAMILY.COM – Kali ini kita akan membuat web server menggunakan ESP32, langsung saja siapkan bahannya, hanya dengan ESP32 dan sensor BMP280. Cukup rangkaikan seperti gambar di bawah ini

Lalu upload kode berikut

#include<WiFi.h>
#include<Wire.h>
#include<SPI.h>
#include<Adafruit_BMP280.h>


#defineBMP_SCK  (13)
#defineBMP_MISO (12)
#defineBMP_MOSI (11)
#defineBMP_CS   (10)

Adafruit_BMP280 bmp;

constchar* ssid     = "NAMA_SSID";
constchar* password = "PASSWORD";

WiFiServer server(80);

String header;

unsignedlong currentTime = millis();
unsignedlong previousTime = 0;
constlong timeoutTime = 1000;

voidsetup() {
Serial.begin(9600);
unsigned status;
  status = bmp.begin(0x76);
if (!status)
  {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or try a different address!"));
while (1) delay(10);
  }

bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, Adafruit_BMP280::SAMPLING_X2, Adafruit_BMP280::SAMPLING_X16, Adafruit_BMP280::FILTER_X16, Adafruit_BMP280::STANDBY_MS_500);

Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
  }

Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}

voidloop(){
  WiFiClient client = server.available();  

if (client) {                            
    currentTime = millis();
    previousTime = currentTime;
Serial.println("New Client.");          
    String currentLine = "";                
while (client.connected() && currentTime - previousTime <= timeoutTime) {  
      currentTime = millis();
if (client.available()) {            
char c = client.read();            
Serial.write(c);                    
        header += c;
if (c == '\n') {                    
if (currentLine.length() == 0) {
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 BMP280</h1>");
client.println("<table><tr><th>MEASUREMENT</th><th>VALUE</th></tr>");
client.println("<tr><td>Temp. Celsius</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(" hPa</span></td></tr>");
client.println("<tr><td>Approx. Altitude</td><td><span class=\"sensor\">");
client.println(bmp.readAltitude(1013.25));
client.println(" m</span></td></tr>");
client.println("</body></html>");
client.println();
break;
          } else {
            currentLine = "";
          }
        } elseif (c != '\r') {
          currentLine += c;      
        }
      }
    }
    header = "";
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
  }
}
Sesuaikan kode berikut dengan nama SSID Wifi anda
constchar* ssid     = "NAMA_SSID";
constchar* password = "PASSWORD";
Jalankan ESP32, lalu buka serial monitor
Terlihat alamat IP dari ESP32 yang didapat adalah 10.10.20.18 ini tergantung dari modem wifi anda memberikan IP kepada perangkat yang terhubung.
Lalu buka web browser baik dari perangkat android/iphone maupun laptop/pc, buka alamat 10.10.20.18 di web browser, hasilnya akan seperti berikut
Data sensor BMP280 berhasil ditampilkan melalui webserver pada perangkat mobile, dan disimpulkan bahwa ESP32 berhasil menjadi web server dan perangkat mobile anda adalah client nya.