Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- WiFiEsp example: WebServerAP
- A simple web server that shows the value of the analog input
- pins via a web page using an ESP8266 module.
- This sketch will start an access point and print the IP address of your
- ESP8266 module to the Serial monitor. From there, you can open
- that address in a web browser to display the web page.
- The web page will be automatically refreshed each 20 seconds.
- For more details see: http://yaab-arduino.blogspot.com/p/wifiesp.html
- */
- #include "WiFiEsp.h"
- #include "SoftwareSerial.h"
- SoftwareSerial Serial1(6, 7); // RX, TX
- char ssid[] = "Kasefet"; // your network SSID (name)
- char pass[] = "12345678"; // your network password
- int status = WL_IDLE_STATUS; // the Wifi radio's status
- unsigned long timer = 4294967290; // timer for program execution
- WiFiEspServer server(80);
- // use a ring buffer to increase speed and reduce memory allocation
- RingBuffer buf(8);
- void setup() {
- Serial.begin(115200); // initialize serial for debugging
- Serial1.begin(9600); // initialize serial for ESP module
- WiFi.init(&Serial1); // initialize ESP module
- // PinModes and stuff
- pinMode(13, OUTPUT);
- // check for the presence of the shield
- if (WiFi.status() == WL_NO_SHIELD) {
- Serial.println("WiFi shield not present");
- while (true); // don't continue
- }
- Serial.print("Attempting to start AP ");
- Serial.println(ssid);
- IPAddress localIp(192, 168, 111, 111);
- WiFi.configAP(localIp);
- // start access point
- status = WiFi.beginAP(ssid, 10, pass, ENC_TYPE_WPA2_PSK);
- Serial.println("Access point started");
- printWifiStatus();
- // start the web server on port 80
- server.begin();
- Serial.println("Server started");
- }
- void loop() {
- WiFiEspClient client = server.available(); // listen for incoming clients
- if (client) { // if you get a client,
- Serial.println("New client"); // print a message out the serial port
- buf.init(); // initialize the circular buffer
- while (client.connected()) { // loop while the client's connected
- if (client.available()) { // if there's bytes to read from the client,
- char c = client.read(); // read a byte, then
- buf.push(c); // push it to the ring buffer
- // you got two newline characters in a row
- // that's the end of the HTTP request, so send a response
- if (buf.endsWith("\r\n\r\n")) {
- sendHttpResponse(client);
- break;
- }
- }
- }
- // give the web browser time to receive the data
- delay(10);
- // close the connection
- client.stop();
- Serial.println("Client disconnected");
- }
- if (timer < 4294967290) { // Don't change this line
- // CUSTOM CODE START
- if (timer < 10000) {
- digitalWrite(13, HIGH);
- } else {
- digitalWrite(13, LOW);
- }
- // CUSTOM CODE END
- timer++;
- }
- }
- void sendHttpResponse(WiFiEspClient client) {
- client.print(
- "HTTP/1.1 200 OK\r\n"
- "Content-Type: text/html\r\n"
- "Connection: close\r\n" // the connection will be closed after completion of the response
- //"Refresh: 20\r\n" // refresh the page automatically every 20 sec
- "\r\n");
- client.print("Opened");
- timer = 0;
- }
- void printWifiStatus() {
- // print your WiFi shield's IP address
- IPAddress ip = WiFi.localIP();
- Serial.print("IP Address: ");
- Serial.println(ip);
- // print where to go in the browser
- Serial.println();
- Serial.print("To see this page in action, connect to ");
- Serial.print(ssid);
- Serial.print(" and open a browser to http://");
- Serial.println(ip);
- Serial.println();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement