noam76

esp arduino

Sep 15th, 2017
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.26 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #define LED_PIN 16
  3.  
  4. const char* ssid = "*****";
  5. const char* password = "******";
  6. int state = 0;
  7. WiFiServer server(80); //Initialize the server on Port 80
  8.  
  9. void setup()
  10. {
  11.  Serial.begin(115200); //Start communication between the ESP8266-12E and the monitor window
  12.  delay(10);
  13.  
  14.  pinMode(LED_PIN, OUTPUT); //GPIO16 is an OUTPUT pin;
  15.  digitalWrite(LED_PIN, LOW); //Initial state is ON
  16.  
  17.  WiFi.begin(ssid, password); // Provide the (SSID, password);
  18.  while (WiFi.status() != WL_CONNECTED) {
  19.     delay(500);
  20.     Serial.print(".");
  21.   }
  22.   Serial.println("");
  23.   Serial.println("WiFi connected");
  24.  server.begin(); // Start the HTTP Server
  25.  
  26.  //Looking under the hood
  27.  Serial.print("Server IP is: "); // Print the IP to the monitor window
  28.  Serial.println(WiFi.localIP());
  29. }
  30.  
  31. void loop()
  32. {
  33.  WiFiClient client = server.available();
  34.  if (!client)
  35.  {
  36.   return;
  37.  }
  38.  //Looking under the hood
  39.  Serial.println("Somebody has connected :)");
  40.  
  41.  //Read what the browser has sent into a String class and print the request to the monitor
  42.  String request = client.readStringUntil('\r');
  43. //Looking under the hood
  44.  Serial.println(request);
  45.  // Handle the Request
  46.  if (request.indexOf("/OFF") != -1)
  47.  {
  48.   digitalWrite(LED_PIN, HIGH);
  49.   Serial.println(request.indexOf("/ON"));
  50.  }
  51.  else if (request.indexOf("/ON") != -1)
  52.  {
  53.   digitalWrite(LED_PIN, LOW);
  54.   Serial.println(request.indexOf("/OFF"));
  55.  }
  56.  if (request.indexOf("/Green_BT") == 4)
  57.  {
  58.   if(state !=0)
  59.   {
  60.    state = 0;
  61.    Serial.println("Turn OFF");
  62.   }
  63.   else
  64.   {
  65.    state = 1;
  66.    Serial.println("Turn ON");
  67.   }
  68.   Serial.println("Green is pressed");
  69.   Serial.println(request.indexOf("/Green_BT"));
  70.  }
  71.  Serial.println(request.indexOf("/name_field"));
  72.  
  73.  //Prepare the HTML document to respond and add buttons:
  74.  String s = "HTTP/1.1 200 OK\r\n";
  75.  s += "Content-Type: text/html\r\n\r\n";
  76.  s += "<!DOCTYPE HTML>\r\n<html>\r\n";
  77.  
  78.  s += "<style> .button { background-color: #4CAF50; border: none; color: white; padding: 16px 32px; text-align: center; text-decoration: none; display: inline-block;mfont-size: 16px; margin: 4px 2px; -webkit-transition-duration: 0.4s; transition-duration: 0.4s; cursor: pointer;}";
  79.  s += " .button1 { background-color: #4CAF50; color: white; border: 2px solid #4CAF50;}";
  80.  s += " .button1:hover { background-color: white; color: black;}";
  81.  s += "</style>";
  82.  
  83.  s += "<br><input type=\"button\" name=\"b1\" value=\"Turn LED ON\" onclick=\"location.href='/ON'\">";
  84.  s += "<br><br><br>";
  85.  s += "<input type=\"button\" name=\"b1\" value=\"Turn LED OFF\" onclick=\"location.href='/OFF'\">";
  86.  s += "<br><br><br>";
  87.  s += "<button class=\"button button1\" onclick=\"location.href='/Green_BT'\">Green</button>";
  88.  s += "<br><br><br>";
  89.  s += "<form action=\"/team_name_url/\" method=\"post\">";
  90.  s += "<label for=\"team_name\">Enter name: </label>";
  91.  s += "<input id=\"team_name\" type=\"text\" name=\"name_field\" placeholder=\"Default name for team.\">";
  92.  s += "<input type=\"submit\" value=\"OK\">";
  93.  s += "</form>";
  94.  s += "</html>\n";
  95.  
  96.  //Serve the HTML document to the browser.
  97.  client.flush(); //clear previous info in the stream
  98.  client.print(s); // Send the response to the client
  99.  delay(1);
  100.  Serial.println("Client disonnected"); //Looking under the hood
  101. }
Advertisement
Add Comment
Please, Sign In to add comment