T2brozz

Wifi counter

Dec 3rd, 2018
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    
  2. #include <WiFi.h>
  3.  
  4. const char* wifi_name = "FRITZ!Box 7560 DN"; //Your Wifi name
  5. const char* wifi_pass = "420044369429418134534";                               //Your Wifi password
  6. WiFiServer server(80);                                    //Port 80
  7.  
  8. void setup() {
  9.   Serial.begin(115200);
  10.   // Let's connect to wifi network
  11.   Serial.print("Connecting to ");
  12.   Serial.print(wifi_name);
  13.   WiFi.begin(wifi_name, wifi_pass);                      //Connecting to wifi network
  14.  
  15.   while (WiFi.status() != WL_CONNECTED) {                //Waiting for the responce of wifi network
  16.     delay(500);
  17.     Serial.print(".");
  18.   }
  19.  
  20.   Serial.println("");
  21.   Serial.println("Connection Successful");
  22.   Serial.print("IP address: ");
  23.   Serial.println(WiFi.localIP());                       //Getting the IP address at which our webserver will be created
  24.   Serial.println("Put the above IP address into a browser search bar");
  25.   server.begin();                                       //Starting the server
  26. }
  27.  
  28. void loop() {
  29.   int counter=0;
  30.   WiFiClient client = server.available();               //Checking for incoming clients
  31.  
  32.   if (client) {                            
  33.     Serial.println("new client");        
  34.     String currentLine = "";                           //Storing the incoming data in the string
  35.     while (client.connected() && client.available()) { //if there is some client data available
  36.       char c = client.read();                          //read a byte
  37.       Serial.print(c);
  38.        
  39.       if (c == '\n' && currentLine.length() == 0) {     //check for newline character, if line is blank it means its the end of the client HTTP request
  40.         client.print("<title>ESP32 Webserver</title>");
  41.         client.print("<body><h1>Hello World</h1><br />");
  42.         client.print("Die Temperatur beträgt: " + std::string(++counter) + " </body>");
  43.         break;                                       //Going out of the while loop
  44.       } else if (c != '\r') {
  45.         currentLine += c;                            //if you got anything else but a carriage return character,
  46.       } else {  
  47.         currentLine = "";                            //if you got a newline, then clear currentLine
  48.       }
  49.     }
  50.     delay(1000);
  51.   }
  52.   delay(2000);
  53. }
Advertisement
Add Comment
Please, Sign In to add comment