[c] uts_iot
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- const char* ssid = "FARIS";
- const char* password = "banjarsari1406";
- const char* host = "192.168.1.21"; // Alamat URL lengkap ke skrip PHP
- int kursi1 = D5; // GPIO14
- int kursi2 = D6; // GPIO12
- int kursi3 = D7; // GPIO13
- WiFiServer server(80);
- int count_kursi1 = 0;
- int count_kursi2 = 0;
- int count_kursi3 = 0;
- void setup() {
- Serial.begin(115200);
- delay(10);
- pinMode(kursi1, OUTPUT);
- pinMode(kursi2, OUTPUT);
- pinMode(kursi3, OUTPUT);
- digitalWrite(kursi1, LOW);
- digitalWrite(kursi2, LOW);
- digitalWrite(kursi3, LOW);
- // Connect to WiFi network
- Serial.println();
- Serial.println();
- 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");
- // Start the server
- server.begin();
- Serial.println("Server started");
- // Print the IP address
- Serial.print("Use this URL to connect: ");
- Serial.print("http://");
- Serial.print(WiFi.localIP());
- Serial.println("/");
- }
- void loop() {
- // Check if a client has connected
- WiFiClient client = server.available();
- if (!client) {
- return;
- }
- // Wait until the client sends some data
- Serial.println("new client");
- while (!client.available()) {
- delay(1);
- }
- // Read the first line of the request
- String request = client.readStringUntil('\r');
- // /LED=ON
- Serial.println(request);
- client.flush();
- // Match the request
- // request = GET /LED=OFF HTTP/1.1
- if (request.indexOf("/KURSI1=ON") != -1) {
- digitalWrite(kursi1, HIGH);
- count_kursi1++;
- sendToServer(1, 1, client); // Kirim data ke server PHP
- // Update jumlah_nyala setiap kali tombol ON ditekan
- client.println("<p>Kursi 1 ON - Total Hidup: " + String(count_kursi1) + "</p>");
- }
- if (request.indexOf("/KURSI1=OFF") != -1) {
- digitalWrite(kursi1, LOW);
- sendToServer(1, 0, client); // Kirim data ke server PHP
- }
- if (request.indexOf("/KURSI2=ON") != -1) {
- digitalWrite(kursi2, HIGH);
- count_kursi2++;
- sendToServer(2, 1, client); // Kirim data ke server PHP
- // Update jumlah_nyala setiap kali tombol ON ditekan
- client.println("<p>Kursi 2 ON - Total Hidup: " + String(count_kursi2) + "</p>");
- }
- if (request.indexOf("/KURSI2=OFF") != -1) {
- digitalWrite(kursi2, LOW);
- sendToServer(2, 0, client); // Kirim data ke server PHP
- }
- if (request.indexOf("/KURSI3=ON") != -1) {
- digitalWrite(kursi3, HIGH);
- count_kursi3++;
- sendToServer(3, 1, client); // Kirim data ke server PHP
- // Update jumlah_nyala setiap kali tombol ON ditekan
- client.println("<p>Kursi 3 ON - Total Hidup: " + String(count_kursi3) + "</p>");
- }
- if (request.indexOf("/KURSI3=OFF") != -1) {
- digitalWrite(kursi3, LOW);
- sendToServer(3, 0, client); // Kirim data ke server PHP
- }
- // Set ledPin according to the request
- //digitalWrite(ledPin, value);
- // Return the response
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println(""); // do not forget this one
- client.println("<!DOCTYPE HTML>");
- client.println("<html>");
- // Add CSS style for buttons
- client.println("<style>");
- client.println("button {");
- client.println(" display: inline-block;");
- client.println(" padding: 10px 20px;");
- client.println(" font-size: 16px;");
- client.println(" cursor: pointer;");
- client.println(" text-align: center;");
- client.println(" text-decoration: none;");
- client.println(" outline: none;");
- client.println(" color: #fff;");
- client.println(" background-color: #4CAF50;");
- client.println(" border: none;");
- client.println(" border-radius: 15px;");
- client.println("}");
- client.println("</style>");
- client.println("<h1>Control Kursi LED</h1>");
- // Add buttons with style
- client.print("<p>Kursi 1 ON - Total Hidup: ");
- client.print(count_kursi1);
- client.println("</p>");
- client.println("<a href=\"/KURSI1=ON\"><button style=\"background-color: #4CAF50;\">Kursi 1 ON</button></a>");
- client.println("<a href=\"/KURSI1=OFF\"><button style=\"background-color: #f44336;\">Kursi 1 OFF</button></a><br />");
- client.print("<p>Kursi 2 ON - Total Hidup: ");
- client.print(count_kursi2);
- client.println("</p>");
- client.println("<a href=\"/KURSI2=ON\"><button style=\"background-color: #4CAF50;\">Kursi 2 ON</button></a>");
- client.println("<a href=\"/KURSI2=OFF\"><button style=\"background-color: #f44336;\">Kursi 2 OFF</button></a><br />");
- client.print("<p>Kursi 3 ON - Total Hidup: ");
- client.print(count_kursi3);
- client.println("</p>");
- client.println("<a href=\"/KURSI3=ON\"><button style=\"background-color: #4CAF50;\">Kursi 3 ON</button></a>");
- client.println("<a href=\"/KURSI3=OFF\"><button style=\"background-color: #f44336;\">Kursi 3 OFF</button></a><br />");
- client.println("</html>");
- delay(1);
- Serial.println("Client disconnected");
- Serial.println("");
- }
- void sendToServer(int kursi, int status, WiFiClient client) {
- if (!client.connect(host, 80)) {
- Serial.println("Koneksi ke server gagal");
- return;
- }
- Serial.println("Koneksi ke server berhasil");
- // Buat URL untuk dikirim ke server PHP
- String url = "/iot/uts.php?kursi=" + String(kursi) + "&status=" + String(status);
- // Kirim HTTP request ke server PHP
- client.print(String("GET ") + url + " HTTP/1.1\r\n" +
- "Host: " + host + "\r\n" +
- "Connection: close\r\n\r\n");
- delay(10);
- // Baca respons dari server
- while (client.available()) {
- String line = client.readStringUntil('\r');
- Serial.print(line);
- }
- client.stop();
- }
Editor
You can edit this paste and save as new: