Advertisement
gabbyshimoni

ESP8266-2nd-step

Aug 28th, 2018
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2.  
  3. const char* ssid = "************"; // Update according to the router name you are connecting to
  4. const char* password = "***********"; // Update according to the password of the router
  5. const char* host = "***.***.***.***"; //it will tell you the IP once it starts up
  6. //just write it here afterwards and upload
  7.  
  8.  
  9. int ledPin = 4;
  10. int buz = 14;
  11. int pb = 12;
  12. int pbV = 0;
  13.  
  14. WiFiServer server(4023); //just pick any port number you like
  15.  
  16. void setup() {
  17.   Serial.begin(115200);
  18.   delay(10);
  19.  
  20.   // prepare GPIOs
  21.   pinMode(ledPin, OUTPUT);
  22.   pinMode(buz, OUTPUT);
  23.   pinMode(pb, INPUT);
  24.   digitalWrite(ledPin , LOW);
  25.  
  26.   // Connect to WiFi network
  27.   Serial.println();
  28.   Serial.println();
  29.   Serial.print("Connecting to ");
  30.   Serial.println(ssid);
  31.  
  32.   WiFi.begin(ssid, password);
  33.  
  34.   while (WiFi.status() != WL_CONNECTED) {
  35.     delay(500);
  36.     Serial.print(".");
  37.   }
  38.   Serial.println("");
  39.   Serial.println("WiFi connected");
  40.  
  41.   // Start the server
  42.   server.begin();
  43.   Serial.println("Server started");
  44.  
  45.   // Print the IP address
  46.   Serial.println(WiFi.localIP());
  47. }
  48.  
  49. void loop() {
  50.   // Check if a client has connected
  51.  
  52.   WiFiClient client = server.available();
  53.   if (!client) {
  54.     return;
  55.   }
  56.  
  57.   // Wait until the client sends some data
  58.   while (!client.available()) {
  59.     delay(1);
  60.   }
  61.  
  62.   // Read the first line of the request
  63.   String req = client.readStringUntil('\r');
  64.   client.flush();
  65.  
  66.   // Match the request
  67.   if (req.indexOf("") != -10) {  //checks if you're on the main page
  68.  
  69.     if (req.indexOf("/OFF") != -1) { //checks if you clicked OFF
  70.       digitalWrite(ledPin, LOW);
  71.       Serial.println("You clicked OFF");
  72.     }
  73.     if (req.indexOf("/ON") != -1) { //checks if you clicked ON
  74.       digitalWrite(ledPin, HIGH);
  75.       Serial.println("You clicked ON");
  76.     }
  77.  
  78.     if (req.indexOf("/BON") != -1) { //checks if you clicked ON
  79.       digitalWrite(buz, HIGH);
  80.       Serial.println("You clicked Buzz ON");
  81.     }
  82.     if (req.indexOf("/BOFF") != -1) { //checks if you clicked ON
  83.       digitalWrite(buz, LOW);
  84.       Serial.println("You clicked Buzz OFF");
  85.     }
  86.   }
  87.  
  88.   else {
  89.     Serial.println("invalid request");
  90.     client.stop();
  91.     return;
  92.   }
  93.  
  94.   // Prepare the response
  95.   String s = "HTTP/1.1 200 OK\r\n";
  96.   s += "Content-Type: text/html\r\n\r\n";
  97.   s += "<!DOCTYPE HTML>\r\n<html>\r\n";
  98.   s += "<br><input type=\"button\" name=\"bl\" value=\"Turn LED ON \" onclick=\"location.href='/ON'\">";
  99.   s += "<br><br><br>";
  100.   s += "<br><input type=\"button\" name=\"bl\" value=\"Turn LED OFF\" onclick=\"location.href='/OFF'\">";
  101.   s += "<br><br><br>";
  102.   s += "<br><input type=\"button\" name=\"bl\" value=\"Buzz On\" onclick=\"location.href='/BON'\">";
  103.   s += "<br><br><br>";
  104.   s += "<br><input type=\"button\" name=\"bl\" value=\"Buzz Off\" onclick=\"location.href='/BOFF'\">";
  105.   s += "<br><br><br>";
  106.  
  107.   pbV = digitalRead(pb);
  108.   if (pbV == HIGH) {
  109.     s += "<br>Button is pressed";
  110.   }
  111.   else {
  112.     s += "<br>Button is released";
  113.   }
  114.   s += "</html>\n";
  115.  
  116.   client.flush();
  117.  
  118.   // Send the response to the client
  119.   client.print(s);
  120.   delay(1);
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement