Created
November 13, 2019 04:17
-
-
Save ajikamaludin/5bb305df376b1dba00cf154247bece7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
#include <ESP8266WebServer.h> | |
#include <ESP8266mDNS.h> | |
#include <ESP8266HTTPClient.h> | |
MDNSResponder mdns; | |
ESP8266WebServer server(80); | |
HTTPClient http; | |
// Koneksi WiFi | |
const char* ssid = "NETD"; | |
const char* password = "aji00014"; | |
// Respone Default HTTPServer | |
String webPage = "200"; | |
String notFound = "404"; | |
// URL Akses Ke HTTP Remote | |
String url = ""; | |
// Control Pin List | |
int gpio0_pin = 0; | |
int gpio1_pin = 2; | |
// Status | |
String gpio0_status = "0"; | |
String gpio1_status = "0"; | |
// Membuat Notifikasi Ke Server | |
void notifServer(){ | |
url += "http://"; | |
url += WiFi.gatewayIP().toString(); | |
//url += "192.168.100.5"; | |
url += "/apiv2/notif/"; | |
url += WiFi.localIP().toString(); | |
url += "/2"; | |
http.begin(url); | |
int httpCode = http.GET(); | |
if(httpCode > 0) | |
{ | |
Serial.printf("[HTTP GET] code: %d\n", httpCode); | |
if(httpCode == HTTP_CODE_OK) { | |
String payload = http.getString(); | |
Serial.println(payload); | |
} | |
} | |
else | |
{ | |
Serial.printf("[HTTP GET] failed, error: %s , \n", http.errorToString(httpCode).c_str()); | |
} | |
http.end(); | |
} | |
//Handle NotFound WebServer | |
void handleNotFound() { | |
server.send ( 404, "text/plain", notFound); | |
} | |
void lampStatus1(){ | |
server.send ( 200, "text/plain", gpio0_status); | |
} | |
void lampStatus2(){ | |
server.send ( 200, "text/plain", gpio1_status); | |
} | |
// Lampu Pin OFF | |
void handle1Off(){ | |
pinMode(gpio0_pin, INPUT); | |
gpio0_status = "0"; | |
} | |
// Lampu Pin ON | |
void handle1On(){ | |
pinMode(gpio0_pin, OUTPUT); | |
gpio0_status = "1"; | |
} | |
// Lampu Pin OFF | |
void handle2Off(){ | |
pinMode(gpio1_pin, INPUT); | |
gpio1_status = "0"; | |
} | |
// Lampu Pin ON | |
void handle2On(){ | |
pinMode(gpio1_pin, OUTPUT); | |
gpio1_status = "1"; | |
} | |
void setup(void){ | |
delay(1000); | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); | |
Serial.println(""); | |
// Wait for connection | |
Serial.println("Trying Connecting. . . "); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("#"); | |
} | |
// Print Status When Connected | |
Serial.println(""); | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
Serial.print("IP Address - "); | |
Serial.println(WiFi.localIP()); | |
Serial.print("Gateway - "); | |
Serial.println(WiFi.gatewayIP()); | |
// Call Notif | |
notifServer(); | |
Serial.printf("%s \n",url.c_str()); | |
if (mdns.begin("esp8266", WiFi.localIP())) { | |
Serial.println("MDNS responder started"); | |
} | |
// Handle HTTP Request | |
server.on("/", [](){ | |
server.send(200, "text/html", webPage); | |
}); | |
server.on("/io1On", [](){ | |
server.send(200, "text/html", webPage); | |
handle1On(); | |
delay(1000); | |
}); | |
server.on("/io1Off", [](){ | |
server.send(200, "text/html", webPage); | |
handle1Off(); | |
delay(1000); | |
}); | |
server.on("/io1Status", [](){ | |
lampStatus1(); | |
delay(1000); | |
}); | |
server.on("/io2On", [](){ | |
server.send(200, "text/html", webPage); | |
handle2On(); | |
delay(1000); | |
}); | |
server.on("/io2Off", [](){ | |
server.send(200, "text/html", webPage); | |
handle2Off(); | |
delay(1000); | |
}); | |
server.on("/io2Status", [](){ | |
lampStatus2(); | |
delay(1000); | |
}); | |
server.onNotFound(handleNotFound); | |
server.begin(); | |
Serial.println("HTTP server started"); | |
} | |
void loop(void){ | |
server.handleClient(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment