Advertisement
Guest User

Kasefet code

a guest
Feb 21st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.77 KB | None | 0 0
  1. /*
  2.  WiFiEsp example: WebServerAP
  3.  A simple web server that shows the value of the analog input
  4.  pins via a web page using an ESP8266 module.
  5.  This sketch will start an access point and print the IP address of your
  6.  ESP8266 module to the Serial monitor. From there, you can open
  7.  that address in a web browser to display the web page.
  8.  The web page will be automatically refreshed each 20 seconds.
  9.  For more details see: http://yaab-arduino.blogspot.com/p/wifiesp.html
  10. */
  11.  
  12. #include "WiFiEsp.h"
  13.  
  14. #include "SoftwareSerial.h"
  15. SoftwareSerial Serial1(6, 7); // RX, TX
  16.  
  17. char ssid[] = "Kasefet";          // your network SSID (name)
  18. char pass[] = "12345678";         // your network password
  19. int status = WL_IDLE_STATUS;      // the Wifi radio's status
  20. unsigned long timer = 4294967290; // timer for program execution
  21.  
  22. WiFiEspServer server(80);
  23.  
  24. // use a ring buffer to increase speed and reduce memory allocation
  25. RingBuffer buf(8);
  26.  
  27. void setup() {
  28.   Serial.begin(115200);   // initialize serial for debugging
  29.   Serial1.begin(9600);    // initialize serial for ESP module
  30.   WiFi.init(&Serial1);    // initialize ESP module
  31.  
  32.   // PinModes and stuff
  33.   pinMode(13, OUTPUT);
  34.  
  35.   // check for the presence of the shield
  36.   if (WiFi.status() == WL_NO_SHIELD) {
  37.     Serial.println("WiFi shield not present");
  38.     while (true); // don't continue
  39.   }
  40.  
  41.   Serial.print("Attempting to start AP ");
  42.   Serial.println(ssid);
  43.  
  44.   IPAddress localIp(192, 168, 111, 111);
  45.   WiFi.configAP(localIp);
  46.  
  47.   // start access point
  48.   status = WiFi.beginAP(ssid, 10, pass, ENC_TYPE_WPA2_PSK);
  49.  
  50.   Serial.println("Access point started");
  51.   printWifiStatus();
  52.  
  53.   // start the web server on port 80
  54.   server.begin();
  55.   Serial.println("Server started");
  56. }
  57.  
  58.  
  59. void loop() {
  60.   WiFiEspClient client = server.available();  // listen for incoming clients
  61.  
  62.   if (client) {                               // if you get a client,
  63.     Serial.println("New client");             // print a message out the serial port
  64.     buf.init();                               // initialize the circular buffer
  65.     while (client.connected()) {              // loop while the client's connected
  66.       if (client.available()) {               // if there's bytes to read from the client,
  67.         char c = client.read();               // read a byte, then
  68.         buf.push(c);                          // push it to the ring buffer
  69.  
  70.         // you got two newline characters in a row
  71.         // that's the end of the HTTP request, so send a response
  72.         if (buf.endsWith("\r\n\r\n")) {
  73.           sendHttpResponse(client);
  74.           break;
  75.         }
  76.       }
  77.     }
  78.    
  79.     // give the web browser time to receive the data
  80.     delay(10);
  81.  
  82.     // close the connection
  83.     client.stop();
  84.     Serial.println("Client disconnected");
  85.   }
  86.  
  87.   if (timer < 4294967290) { // Don't change this line
  88.     // CUSTOM CODE START
  89.    
  90.     if (timer < 10000) {
  91.       digitalWrite(13, HIGH);
  92.     } else {
  93.       digitalWrite(13, LOW);
  94.     }
  95.  
  96.     // CUSTOM CODE END
  97.     timer++;
  98.   }
  99. }
  100.  
  101. void sendHttpResponse(WiFiEspClient client) {
  102.   client.print(
  103.     "HTTP/1.1 200 OK\r\n"
  104.     "Content-Type: text/html\r\n"
  105.     "Connection: close\r\n"  // the connection will be closed after completion of the response
  106.     //"Refresh: 20\r\n"        // refresh the page automatically every 20 sec
  107.     "\r\n");
  108.   client.print("Opened");
  109.   timer = 0;
  110. }
  111.  
  112. void printWifiStatus() {
  113.   // print your WiFi shield's IP address
  114.   IPAddress ip = WiFi.localIP();
  115.   Serial.print("IP Address: ");
  116.   Serial.println(ip);
  117.  
  118.   // print where to go in the browser
  119.   Serial.println();
  120.   Serial.print("To see this page in action, connect to ");
  121.   Serial.print(ssid);
  122.   Serial.print(" and open a browser to http://");
  123.   Serial.println(ip);
  124.   Serial.println();
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement