Connect with us

ESP8266 arduino coding

Discussion in 'Microcontrollers, Programming and IoT' started by Raja Vigneshwaran, Nov 23, 2016.

  1. Raja Vigneshwaran

    Raja Vigneshwaran

    15
    0
    Oct 18, 2016
    Code:
    #include <ESP8266WiFi.h>
    const char* ssid = "Rjviki";
    const char* password = "2444666668888888";
    int ledPin = 12;
    int ledPin1 = 4;
    WiFiServer server(80);
    void setup() {
      Serial.begin(115200);
      delay(10);
      pinMode(ledPin, OUTPUT);
      digitalWrite(ledPin, LOW);
     
      pinMode(ledPin1, OUTPUT);
      digitalWrite(ledPin1, LOW);
     
      // 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
      int value = LOW;
      if (request.indexOf("/BULB=ON") != -1)  {
        digitalWrite(ledPin, HIGH);
        value = HIGH;
      }
      if (request.indexOf("/BULB=OFF") != -1)  {
        digitalWrite(ledPin, LOW);
        value = LOW;
      }
      int value1 = HIGH;
      if (request.indexOf("/FAN=OFF") != -1)  {
        digitalWrite(ledPin1, LOW);
        value1 = LOW;
      }
      if (request.indexOf("/FAN=ON") != -1)  {
        digitalWrite(ledPin1, HIGH);
        value1 = HIGH;
      }
    // 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("Toggle Bulb Switch ");
      if(value == HIGH) {
        //client.print("On");
      } else {
        //client.print("Off");
      }
      client.println("<br><br>");
      client.println("<a href=\"/BULB=ON\"\"><button>Turn On </button></a>");
      client.println("<a href=\"/BULB=OFF\"\"><button>Turn Off </button></a><br />"); 
      client.println("<br><br>");
      client.println("</html>");
      delay(1);
      Serial.println("Client disonnected");
      Serial.println("");
    
      client.print("Toggle Fan Switch ");
      if(value1 == HIGH)
      {
        //client.print("On");
      } else {
        //client.print("Off");
      }
      client.println("<br><br>");
      client.println("<a href=\"/FAN=ON\"\"><button>Turn On </button></a>");
      client.println("<a href=\"/FAN=OFF\"\"><button>Turn Off </button></a><br />"); 
      client.println("</html>");
      delay(1);
      Serial.println("Client disonnected");
      Serial.println("");
    }
    Hi !
    In my home automation I am using my ESP8266 wifi module as a client to connect to my Android Phone which is the server , How could I make the ESP module to as Server and Android phone to be Client using by altering this following code ?

    Regards,
    Raja
     
  2. Amar Dhore

    Amar Dhore

    129
    12
    Dec 2, 2015
    There are several example in the arduino for a basic client setup.

    1. WiFiServer server(80); should be WiFiclient cleint
    2. define IPAddress and the port number for the client connect.ion
    3. here instead of WiFiClient client = server.available(), you have to connect to a server using Ip and port.

    google ESP8266 client will give you lots of example.

    -
    Amar
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-