Skip to content

Instantly share code, notes, and snippets.

@ryanbekabe
Created August 23, 2022 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanbekabe/38e6fcbf71e2d4a22b808d6c75e13969 to your computer and use it in GitHub Desktop.
Save ryanbekabe/38e6fcbf71e2d4a22b808d6c75e13969 to your computer and use it in GitHub Desktop.
//18:41 23/08/2022
//NodeMCU ESP8266 Kendalikan 2 Relay Modul via Web dari WiFi NodeMCU. www.hanyajasa.com. source cncstore ap switch, Cnc Store 12345678, 337337
#include <ESP8266WiFi.h> //memasukan library NodeMCU
#define matikanrelay digitalWrite(1,LOW)
#define hidupkanrelay digitalWrite(1,HIGH)
#define matikanrelayb digitalWrite(2,LOW)
#define hidupkanrelayb digitalWrite(2,HIGH)
int saklaroff;
const char* ssid = "WiFi IoT HanyaJasa";
const char* pasw = "12345678";
WiFiServer server(80);
// Change the next 6 defines to match your matrix type and size
void setup(){
Serial.begin(115200);
WiFi.softAP(ssid, pasw);
server.begin();
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
digitalWrite(1,LOW);
digitalWrite(2,LOW);
}
void loop(){
WiFiClient client = server.available();
if (client)
{
String request = client.readStringUntil('\r');
Serial.println("********************************");
Serial.println("From the station: " + request);
client.flush();
Serial.print("Byte sent to the station: ");
if (request.indexOf("index.html") != -1)
{
client.println("<html>");
client.println("Bismillah.... Selamat datang di IoT.<br/>\r");
client.println("<a href='http://192.168.4.1/index.html/?1on'>Lampu 1 = On</a><br/>\r");
client.println("<a href='http://192.168.4.1/index.html/?1off'>Lampu 1 = Off</a><br/><br/><p></p>\r\r");
client.println("<a href='http://192.168.4.1/index.html/?2onb'>Lampu 2 = On</a><br/>\r");
client.println("<a href='http://192.168.4.1/index.html/?2offb'>Lampu 2 = Off</a><br/>\r");
client.println("</html>");
// client.println("index.html/?1on = on.\r");
// client.println("index.html/?1off = off.\r");
// client.println("index.html/?2onb = on.\r");
// client.println("index.html/?2offb = off.\r");
//String strPerintah = request.substring(11, request.indexOf("HTTP/1.1"));
//Serial.print(strPerintah);
//client.println("-dingstrPerintah-");
// if (request.indexOf("balk") != -1)
// {
// if(saklaroff)
// {
// hidupkan();
// }
// else
// {
// matikan();
// }
// }
// if (request.indexOf("hdmn") != -1)
// {
// hidupkan();
// }
// if (request.indexOf("mtmn") != -1)
// {
// matikan();
// }
}
if (request.indexOf("index.html/?1on") != -1)
{
client.println("Bismillah.... Selamat datang di IoT.\r");
client.println("hidupkan.\r");
//hidupkan();
digitalWrite(1,LOW);
}
if (request.indexOf("index.html/?1off") != -1)
{
client.println("Bismillah.... Selamat datang di IoT.\r");
client.println("matikan.\r");
//matikan();
digitalWrite(1,HIGH);
}
if (request.indexOf("index.html/?2onb") != -1)
{
client.println("Bismillah.... Selamat datang di IoT.\r");
client.println("hidupkanb.\r");
//hidupkanb();
digitalWrite(2,LOW);
}
if (request.indexOf("index.html/?2offb") != -1)
{
client.println("Bismillah.... Selamat datang di IoT.\r");
client.println("matikanb.\r");
//matikanb();
digitalWrite(2,HIGH);
}
}
}
void hidupkan()
{
//saklaroff=0;
hidupkanrelay;
}
void hidupkanb()
{
//saklaroff=0;
hidupkanrelayb;
}
void matikan()
{
//saklaroff=1;
matikanrelay;
}
void matikanb()
{
//saklaroff=1;
matikanrelayb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment