dirakit community
Published © CC BY

kontrol led maju dan mundur nodemcu

Belajar membuat lampu berjalan maju dan mundur,dengan peralatan seadanya,beberapa led,resistor comotan

AdvancedFull instructions provided2 hours240
kontrol led maju dan mundur nodemcu

Things used in this project

Story

Read more

Code

Source Code

Arduino
#include <ESP8266WiFi.h> 
const char* ssid = "isi nama routermu"; 
const char* password = "password routermu"; 
int ledPins1[]={D0,D1,D2,D3,D4,D5,D6,D7}; 
int ledPins2[]={D7,D6,D5,D4,D3,D2,D1,D0}; 
WiFiServer server(80); 
void setup() 
{   
    Serial.begin(115200);   
    delay(10);   
    for (int i =0; i <8; i++)   
    {     
        pinMode(ledPins1[i],OUTPUT);
        pinMode(ledPins2[i],OUTPUT);   
    }   

    // 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');   
    Serial.println(request);   
    client.flush();   

    // Match the request maju  
    {   int value = LOW;  
        if (request.indexOf("/LED=maju")!= -1 )     
        for(int i =0; i<8; i++)   
        {     
            digitalWrite(ledPins1[i],HIGH);     
            delay(1000);     
            digitalWrite(ledPins1[i],LOW);   
        }   
        
        for(int i =7; i<1; i--)   
        {     
            digitalWrite(ledPins1[i],HIGH);     
            delay(1000);     
            digitalWrite(ledPins1[i],LOW);   
        }  
    }   

    // Match the request mundur   
    {   
        int value = HIGH;  
        if (request.indexOf("/LED=mundur")!= -1 )     
        for(int i =0; i<8; i++)   
        {     
            digitalWrite(ledPins2[i],HIGH);     
            delay(1000);     
            digitalWrite(ledPins2[i],LOW);   
        }

        for(int i =7; i<1; i--)   
        {     
            digitalWrite(ledPins2[i],HIGH);     
            delay(1000);     
            digitalWrite(ledPins2[i],LOW);   
        }   
    }   

    // 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>");   
    client.print("Led pin is now: ");   
    client.println("<br><br>");   
    client.println("<a href=\"/LED=maju\"\"><button>maju </button></a>");   
    client.println("<a href=\"/LED=mundur\"\"><button>mundur </button></a><br />");     client.println("</html>");   
    delay(1);   
    Serial.println("Client disonnected");   
    Serial.println(""); 
}

Credits

dirakit community

dirakit community

23 projects • 109 followers
Indonesia IoT Community by Informatics UIN Sunan Kalijaga Yogyakarta

Comments