Home Control



//
Posted on February 03, 2018 at 11:23 PM

//

control listrik rumah




//Visit www.roboshala.com for more details on NodeMCU and other projects.

#include <ESP8266WiFi.h>
 
const char* ssid = "HomeSpot"; // SSID i.e. Service Set Identifier is the name of your WIFI
const char* password = "22032014"; // Your Wifi password, in case you have open network comment the whole statement.
int buttonState = 0;
String flag;


// device details
char *deviceId = "device-xxxxxxxxxxxxxxxxxx";
char *deviceUsername = "xxxxxxxxxxxxxxxxxxxxxx";
char *devicePassword = "xxxxxxxxxxxxxxxxxxxxxxxxx";
String deviceCredential = String(deviceUsername) + ":" + String(devicePassword);

WiFiClient wclient;
PubSubClient client(  "geeknesia.com", 1883,wclient);
void senddata(char* topic, String credential, char *var1, String nilai1 ) {

String pubString  = "{\"code\":\"";
 pubString += credential;
 pubString += "\",";
 pubString += "\"attributes\":{\"";
 pubString += String(var1) + "\":\"" + nilai1 + "\"";
 pubString += "}}";
 char message_buff[pubString.length() + 1];
 pubString.toCharArray(message_buff, pubString.length() + 1);
 client.publish("iot/data",message_buff);
 Serial.println(message_buff);
 //client.publish("iot/data",pubString);
}
 
int R1=D1; // GPIO13 or for NodeMCU you can directly write D7 
int R2=D2;
int R3=D3;
int R4=D4;
int R5=D5;
int R6=D6;
int R7=D7;
int R8=D8;
WiFiServer server(80); // Creates a server that listens for incoming connections on the specified port, here in this case port is 80.
 
void setup() {
  Serial.begin(115200);
  delay(10);
 
  pinMode(R1, OUTPUT);
  pinMode(R2, OUTPUT);
  pinMode(R3, OUTPUT);
  pinMode(R4, OUTPUT);
  pinMode(R5, OUTPUT);
  pinMode(R6, OUTPUT);
  pinMode(R7, OUTPUT);
  pinMode(R8, OUTPUT);
  
  digitalWrite(R1,HIGH);
  digitalWrite(R2,HIGH);
  digitalWrite(R3,HIGH);
  digitalWrite(R4,HIGH);
  digitalWrite(R5,HIGH);
  digitalWrite(R6,HIGH);
  digitalWrite(R7,HIGH);
  digitalWrite(R8,HIGH);

  
 
  // 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()); //Gets the WiFi shield's IP address and Print the IP address of serial monitor
  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');
  Serial.println(request);
  client.flush();
 
  // Match the request
 
 
  if (request.indexOf("/Relay1On") != -1)  {
    digitalWrite(R1,LOW);
     
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 1 is ON");
      client.println("</html>");
      client.stop();
      delay(1);
  }
  if (request.indexOf("/Relay1Off") != -1)  {
    digitalWrite(R1, HIGH);
   
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 1 is OFF");
      client.println("</html>");
      client.stop();
      delay(1);
  }


  if (request.indexOf("/Relay2On") != -1)  {
    digitalWrite(R2,LOW);
    
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 2 is ON");
      client.println("</html>");
      client.stop();
      delay(1);

  }
  if (request.indexOf("/Relay2Off") != -1)  {
    digitalWrite(R2, HIGH);

      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 2 is OFF");
      client.println("</html>");
      client.stop();
      delay(1);
  }
  if (request.indexOf("/Relay3On") != -1)  {
    digitalWrite(R3,LOW);

      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 3 is ON");
      client.println("</html>");
      client.stop();
      delay(1);
  }
  if (request.indexOf("/Relay3Off") != -1)  {
    digitalWrite(R3, HIGH);

      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 3 is OFF");
      client.println("</html>");
      client.stop();
      delay(1);
  }

  if (request.indexOf("/Relay4On") != -1)  {
    digitalWrite(R4,LOW);
    
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 4 is ON");
      client.println("</html>");
      client.stop();
      delay(1);
  }
  if (request.indexOf("/Relay4Off") != -1)  {
    digitalWrite(R4, HIGH);

      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 4 is OFF");
      client.println("</html>");
      client.stop();
      delay(1);
  }
  if (request.indexOf("/Relay5On") != -1)  {
    digitalWrite(R5,LOW);
    
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 5 is ON");
      client.println("</html>");
      client.stop();
      delay(1);
  }
  if (request.indexOf("/Relay5Off") != -1)  {
    digitalWrite(R5, HIGH);

      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 5 is OFF");
      client.println("</html>");
      client.stop();
      delay(1);
  }
  if (request.indexOf("/Relay6On") != -1)  {
    digitalWrite(R6,LOW);
    
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 6 is ON");
      client.println("</html>");
      client.stop();
      delay(1);
  }
  if (request.indexOf("/Relay6Off") != -1)  {
    digitalWrite(R6, HIGH);

      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 6 is OFF");
      client.println("</html>");
      client.stop();
      delay(1);
  }
  if (request.indexOf("/Relay7On") != -1)  {
    digitalWrite(R7,LOW);
    
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 7 is ON");
      client.println("</html>");
      client.stop();
      delay(1);
  }
  if (request.indexOf("/Relay7Off") != -1)  {
    digitalWrite(R7, HIGH);

      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 7 is OFF");
      client.println("</html>");
      client.stop();
      delay(1);
  }
  if (request.indexOf("/Relay8On") != -1)  {
    digitalWrite(R8,LOW);
    
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 8 is ON");
      client.println("</html>");
      client.stop();
      delay(1);
  }
  if (request.indexOf("/Relay8Off") != -1)  {
    digitalWrite(R8, HIGH);

      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 8 is OFF");
      client.println("</html>");
      client.stop();
      delay(1);
  }
  if (request.indexOf("/RelayAllOn") != -1)  {
    digitalWrite(R1,LOW);
    digitalWrite(R2,LOW);
    digitalWrite(R3,LOW);
    digitalWrite(R4,LOW);
    digitalWrite(R5,LOW);
    digitalWrite(R6,LOW);
    digitalWrite(R7,LOW);
    digitalWrite(R8,LOW);
    
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 1 is ON");
      client.println("Relay 2 is ON");
      client.println("Relay 3 is ON");
      client.println("Relay 4 is ON");
      client.println("Relay 5 is ON");
      client.println("Relay 6 is ON");
      client.println("Relay 7 is ON");
      client.println("Relay 8 is ON");
      client.println("</html>");
      client.stop();
      delay(1);
  }
  if (request.indexOf("/RelayAllOff") != -1)  {
    digitalWrite(R1,HIGH);
    digitalWrite(R2,HIGH);
    digitalWrite(R3,HIGH);
    digitalWrite(R4,HIGH);
    digitalWrite(R5,HIGH);
    digitalWrite(R6,HIGH);
    digitalWrite(R7,HIGH);
    digitalWrite(R8,HIGH);

      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println("");
      client.println("<!DOCTYPE HTML>");
      client.println("<html>");
      client.println("Relay 1 is OFF");
      client.println("Relay 2 is OFF");
      client.println("Relay 3 is OFF");
      client.println("Relay 4 is OFF");
      client.println("Relay 5 is OFF");
      client.println("Relay 6 is OFF");
      client.println("Relay 7 is OFF");
      client.println("Relay 8 is OFF");
      client.println("</html>");
      client.stop();
      delay(1);
  }
  
}
 


Leave a Comment:

Please Sign in First

 Batang