Server ESP8266 (ESP-01)


Model Server ESP-01 accesat din browser. Led ON/OFF conectat la pinul 2 (GPIO2)

Server ESP-01

/*
ESP-01 Server
 */
// include library
#include <ESP8266WiFi.h>
// end include library

const char* ssid = "ssid";  //set wifi id
const char* password = "password"; //set wifi password

WiFiServer androidServer(2563);              // set port number for android netIO

// setup
void setup(){
 Serial.begin(115200);
  delay(10);

  // prepare GPIO2
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
 
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.config(IPAddress(192,168,1,122), IPAddress(255,255,255,0), IPAddress(192,168,1,1)); // static ip,dns,gateway
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  androidServer.begin();    // start the server for android netIO
Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());

}
// end setup

// main loop


void loop() {

  WiFiClient client = androidServer.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("/LED=ON") != -1) {
                              digitalWrite(2, HIGH);
                              Serial.print("LED ON");
    value = HIGH;
  }
  if (request.indexOf("/LED=OFF") != -1){
                              digitalWrite(2, LOW);  
                              Serial.print("LED OFF");
    value = LOW;
  }
 
  // 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: ");
  if(value == HIGH) {
    client.print("On");
  } else {
    client.print("Off");
  }
  client.println("<br><br>");
  client.println("Click <a href=\"/LED=ON\">here</a> Turn relay ON<br>");
  client.println("Click <a href=\"/LED=OFF\">here</a> Turn relay OFF<br>");
  client.println("</html>");

  delay(1);
  Serial.println("Client disconnected");
  Serial.println("");
}

Se poate lega ieșirea GPIO la un releu

Model schemă electronică pentru legarea unui releu la ieșirea GPIO1

0 comentarii:

Trimiteți un comentariu