Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- const char* ssid = "TheNetwork";
- const char* password = "TheNetwork";
- ESP8266WebServer server(80);
- String page = "";
- //CH 1
- int rPin1 = 16; //D0
- int sPin1 = 14; //D5
- int val1 = 0;
- int lightON1 = 0;
- int touched1 = 0;
- //CH 2
- int rPin2 = 5; //D1
- int sPin2 = 12; //D6
- int val2 = 0;
- int lightON2 = 0;
- int touched2 = 0;
- //CH 3
- int rPin3 = 4; //D2
- int sPin3 = 13; //D7
- int val3 = 0;
- int lightON3 = 0;
- int touched3 = 0;
- //CH 4
- int rPin4 = 2; //D4
- int sPin4 = 15; //D8
- int val4 = 0;
- int lightON4 = 0;
- int touched4 = 0;
- void setup(void) {
- //the HTML of the web page
- page = "<h1><center>Pipo Cloud</center></h1><p>Relay 1</p><br><p><a href=\"LED1On\"><button>ON</button></a> <a href=\"LED1Off\"><button>OFF</button></a></p><br><p>Relay 2</p><br><p><a href=\"LED2On\"><button>ON</button></a> <a href=\"LED2Off\"><button>OFF</button></a></p><br><p>Relay 3</p><br><p><a href=\"LED3On\"><button>ON</button></a> <a href=\"LED3Off\"><button>OFF</button></a></p><br><p>Relay 4</p><br><p><a href=\"LED4On\"><button>ON</button></a> <a href=\"LED4Off\"><button>OFF</button></a></p><br><p>All Relay</p><br><p><a href=\"LEDOn\"><button>ON</button></a> <a href=\"LEDOff\"><button>OFF</button></a></p><br>";
- //PinMode
- pinMode(sPin1, INPUT);
- pinMode(sPin2, INPUT);
- pinMode(sPin3, INPUT);
- pinMode(sPin4, INPUT);
- pinMode(rPin1, OUTPUT);
- pinMode(rPin2, OUTPUT);
- pinMode(rPin3, OUTPUT);
- pinMode(rPin4, OUTPUT);
- //Kondisi Utama Relay Mati
- digitalWrite(rPin1, HIGH);
- digitalWrite(rPin2, HIGH);
- digitalWrite(rPin3, HIGH);
- digitalWrite(rPin4, HIGH);
- delay(1000);
- Serial.begin(115200);
- WiFi.begin(ssid, password);
- Serial.println("");
- // Wait for connection
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to ");
- Serial.println(ssid);
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- server.on("/", []() {
- server.send(200, "text/html", page);
- });
- /*--------------------------------------------- */
- server.on("/LED1On", []() {
- server.send(200, "text/html", page);
- digitalWrite(rPin1, LOW);
- delay(1000);
- });
- server.on("/LED1Off", []() {
- server.send(200, "text/html", page);
- digitalWrite(rPin1, HIGH);
- delay(1000);
- });
- server.on("/LED2On", []() {
- server.send(200, "text/html", page);
- digitalWrite(rPin2, LOW);
- delay(1000);
- });
- server.on("/LED2Off", []() {
- server.send(200, "text/html", page);
- digitalWrite(rPin2, HIGH);
- delay(1000);
- });
- server.on("/LED3On", []() {
- server.send(200, "text/html", page);
- digitalWrite(rPin3, LOW);
- delay(1000);
- });
- server.on("/LED3Off", []() {
- server.send(200, "text/html", page);
- digitalWrite(rPin3, HIGH);
- delay(1000);
- });
- server.on("/LED4On", []() {
- server.send(200, "text/html", page);
- digitalWrite(rPin4, LOW);
- delay(1000);
- });
- server.on("/LED4Off", []() {
- server.send(200, "text/html", page);
- digitalWrite(rPin4, HIGH);
- delay(1000);
- });
- server.on("/LEDOn", []() {
- server.send(200, "text/html", page);
- digitalWrite(rPin1, LOW);
- digitalWrite(rPin2, LOW);
- digitalWrite(rPin3, LOW);
- digitalWrite(rPin4, LOW);
- delay(1000);
- });
- server.on("/LEDOff", []() {
- server.send(200, "text/html", page);
- digitalWrite(rPin1, HIGH);
- digitalWrite(rPin2, HIGH);
- digitalWrite(rPin3, HIGH);
- digitalWrite(rPin4, HIGH);
- delay(1000);
- });
- server.begin();
- Serial.println("Web server started!");
- }
- void loop(void) {
- server.handleClient();
- //CH 1
- val1 = digitalRead(sPin1);
- if (val1 == HIGH && lightON1 == LOW) {
- touched1 = 1 - touched1;
- delay(100);
- }
- lightON1 = val1;
- if (touched1 == HIGH) {
- Serial.println("Light ON");
- digitalWrite(rPin1, LOW);
- } else {
- Serial.println("Light OFF");
- digitalWrite(rPin1, HIGH);
- }
- //CH 1
- val1 = digitalRead(sPin1);
- if (val1 == HIGH && lightON1 == LOW) {
- touched1 = 1 - touched1;
- delay(100);
- }
- lightON1 = val1;
- if (touched1 == HIGH) {
- Serial.println("Light ON");
- digitalWrite(rPin1, LOW);
- } else {
- Serial.println("Light OFF");
- digitalWrite(rPin1, HIGH);
- }
- //CH 2
- val2 = digitalRead(sPin2);
- if (val2 == HIGH && lightON2 == LOW) {
- touched2 = 1 - touched2;
- delay(100);
- }
- lightON2 = val2;
- if (touched2 == HIGH) {
- Serial.println("Light ON");
- digitalWrite(rPin2, LOW);
- } else {
- Serial.println("Light OFF");
- digitalWrite(rPin2, HIGH);
- }
- //CH 3
- val3 = digitalRead(sPin3);
- if (val3 == HIGH && lightON3 == LOW) {
- touched3 = 1 - touched3;
- delay(100);
- }
- lightON3 = val3;
- if (touched3 == HIGH) {
- Serial.println("Light ON");
- digitalWrite(rPin3, LOW);
- } else {
- Serial.println("Light OFF");
- digitalWrite(rPin3, HIGH);
- }
- //CH 4
- val4 = digitalRead(sPin4);
- if (val4 == HIGH && lightON4 == LOW) {
- touched4 = 1 - touched4;
- delay(100);
- }
- lightON4 = val4;
- if (touched4 == HIGH) {
- Serial.println("Light ON");
- digitalWrite(rPin4, LOW);
- } else {
- Serial.println("Light OFF");
- digitalWrite(rPin4, HIGH);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement