Weather Station dengan ESP32
Halo!
Sekarang, aku mau melanjutkan web server yang kemarin, yaitu weather station!
Nah, web server yang digunakan ini sekarang digabung dengan sensor apapun. Kali ini aku menggunakan sensor DHT11 karena cuma punya itu:(
Nah, sumbernya dari sini:
https://randomnerdtutorials.com/esp32-web-server-with-bme280-mini-weather-station/
Tapi, sayangnya disana menggunakan sensor BME280 yang tidak kumiliki, alih-alih menggunakan sensor DHT11.
Akhirnya, saya melakukan merge antara sumber di atas dengan sumber-sumber lain di bawah ini:
1. https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ (sempat kubuat beberapa minggu lalu)
2. https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-web-server-arduino-ide/ (Katanya yang ini asynchronous web server gitu tapi aku kurang paham, jadi aku based nya tetep pakai sumber utama)
Nah, rangkaiannya aku menggunakan rangkaian dari sumber lain 1 (Tapi GPIO yang kupakai no 27)
#include <WiFi.h>
#include <Wire.h>
#include <DHT.h>
#include <Adafruit_Sensor.h>
// Replace with your network credentials
const char* ssid = "PRAWIRA";
const char* password = "ayutaris";
#define DHTPIN 27 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
void setup() {
Serial.begin(115200);
Serial.println(F("DHT11 test!"));
// default settings
// (you can also pass in a Wire library object like &Wire2)
dht.begin();
// 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,
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()) { // loop while the client's connected
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 DHT11</h1>");
client.println("<table><tr><th>MEASUREMENT</th><th>VALUE</th></tr>");
client.println("<tr><td>Humidity</td><td><span class=\"sensor\">");
float h = dht.readHumidity(); //read humidity
client.println(h);
client.println(" %</span></td></tr>");
client.println("<tr><td>Temp. Celsius</td><td><span class=\"sensor\">");
float t = dht.readTemperature(); //read temperature in celcius
client.println(t);
client.println(" *C</span></td></tr>");
client.println("<tr><td>Temp. Fahrenheit</td><td><span class=\"sensor\">");
float f = dht.readTemperature(true);
client.println(f);
client.println(" *F</span></td></tr>");
client.println("<tr><td>°C Heat index</td><td><span class=\"sensor\">");
float hic = dht.computeHeatIndex(t, h, false);
client.println(hic);
client.println(" °C</span></td></tr>");
client.println("<tr><td>°C Heat index</td><td><span class=\"sensor\">");
float hif = dht.computeHeatIndex(f, h);
client.println(hif);
client.println(" °F</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("");
}
}
Source code ini sudah compilable
Oke, dengan persiapan ini, apakah saya berhasil melakukannya?
Ternyata tidak:(((((
Permasalahan di web server ini selalu di serial monitor yang kosong, padahal kita membutuhkan serial monitor untuk mendapatkan IP address dari web server tersebut. Padahal, anatara begin di awal set up, upload speed, dan baud rate saat di serial monitor sudah ssma.
Setelah aku melakukan utak-atik dari souce code nya, ternyata kemungkinan letak kesalahanku ada di SSID dan Password. Jadi, kalau aku mengisi SSID dan password yang salah, serial monitor akan muncul titik-titik seperti yang kusebut di post web server pertama minggu lalu.
Sedangkan kalau saya masukkan SSID dan password yang benar, serial monitor akan menampilkan kosong dan terus kosong.
Atau mungkin, saya memang menggunakan ESP32 NodeMCU 32s yang katanya memang lebih tricky dalam menggunakannya.
Aku mencari tahu letak kesalahanku tapi sampai sekarang nggak ketemu, jadi tadi masih spekulasi:(
Terima kasih
Sekarang, aku mau melanjutkan web server yang kemarin, yaitu weather station!
Nah, web server yang digunakan ini sekarang digabung dengan sensor apapun. Kali ini aku menggunakan sensor DHT11 karena cuma punya itu:(
Nah, sumbernya dari sini:
https://randomnerdtutorials.com/esp32-web-server-with-bme280-mini-weather-station/
Tapi, sayangnya disana menggunakan sensor BME280 yang tidak kumiliki, alih-alih menggunakan sensor DHT11.
Akhirnya, saya melakukan merge antara sumber di atas dengan sumber-sumber lain di bawah ini:
1. https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide/ (sempat kubuat beberapa minggu lalu)
2. https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-web-server-arduino-ide/ (Katanya yang ini asynchronous web server gitu tapi aku kurang paham, jadi aku based nya tetep pakai sumber utama)
Nah, rangkaiannya aku menggunakan rangkaian dari sumber lain 1 (Tapi GPIO yang kupakai no 27)
Source codenya? Kugabung-gabungin dari yang sumber utama, disesuaiin dengan DHT11
Ini source code nya:
// Load Wi-Fi library#include <WiFi.h>
#include <Wire.h>
#include <DHT.h>
#include <Adafruit_Sensor.h>
// Replace with your network credentials
const char* ssid = "PRAWIRA";
const char* password = "ayutaris";
#define DHTPIN 27 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
void setup() {
Serial.begin(115200);
Serial.println(F("DHT11 test!"));
// default settings
// (you can also pass in a Wire library object like &Wire2)
dht.begin();
// 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,
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()) { // loop while the client's connected
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 DHT11</h1>");
client.println("<table><tr><th>MEASUREMENT</th><th>VALUE</th></tr>");
client.println("<tr><td>Humidity</td><td><span class=\"sensor\">");
float h = dht.readHumidity(); //read humidity
client.println(h);
client.println(" %</span></td></tr>");
client.println("<tr><td>Temp. Celsius</td><td><span class=\"sensor\">");
float t = dht.readTemperature(); //read temperature in celcius
client.println(t);
client.println(" *C</span></td></tr>");
client.println("<tr><td>Temp. Fahrenheit</td><td><span class=\"sensor\">");
float f = dht.readTemperature(true);
client.println(f);
client.println(" *F</span></td></tr>");
client.println("<tr><td>°C Heat index</td><td><span class=\"sensor\">");
float hic = dht.computeHeatIndex(t, h, false);
client.println(hic);
client.println(" °C</span></td></tr>");
client.println("<tr><td>°C Heat index</td><td><span class=\"sensor\">");
float hif = dht.computeHeatIndex(f, h);
client.println(hif);
client.println(" °F</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("");
}
}
Source code ini sudah compilable
Oke, dengan persiapan ini, apakah saya berhasil melakukannya?
Ternyata tidak:(((((
Permasalahan di web server ini selalu di serial monitor yang kosong, padahal kita membutuhkan serial monitor untuk mendapatkan IP address dari web server tersebut. Padahal, anatara begin di awal set up, upload speed, dan baud rate saat di serial monitor sudah ssma.
Setelah aku melakukan utak-atik dari souce code nya, ternyata kemungkinan letak kesalahanku ada di SSID dan Password. Jadi, kalau aku mengisi SSID dan password yang salah, serial monitor akan muncul titik-titik seperti yang kusebut di post web server pertama minggu lalu.
Sedangkan kalau saya masukkan SSID dan password yang benar, serial monitor akan menampilkan kosong dan terus kosong.
Atau mungkin, saya memang menggunakan ESP32 NodeMCU 32s yang katanya memang lebih tricky dalam menggunakannya.
Aku mencari tahu letak kesalahanku tapi sampai sekarang nggak ketemu, jadi tadi masih spekulasi:(
Terima kasih
Komentar
Posting Komentar