Advertisement
MBrendecke

Arduino

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