123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- #include "WiFiEsp.h"
- #ifndef HAVE_HWSERIAL1
- #include "SoftwareSerial.h"
- SoftwareSerial Serial1(6, 7);
- #endif
- char ssid[] = "Twim";
- char pass[] = "12345678";
- int status = WL_IDLE_STATUS;
- int ledStatus = LOW;
- WiFiEspServer server(80);
- RingBuffer buf(8);
- void setup()
- {
- pinMode(LED_BUILTIN, OUTPUT);
- Serial.begin(115200);
- Serial1.begin(9600);
- WiFi.init(&Serial1);
-
- if (WiFi.status() == WL_NO_SHIELD) {
- Serial.println("WiFi shield not present");
-
- while (true);
- }
-
- while (status != WL_CONNECTED) {
- Serial.print("Attempting to connect to WPA SSID: ");
- Serial.println(ssid);
-
- status = WiFi.begin(ssid, pass);
- }
- Serial.println("You're connected to the network");
- printWifiStatus();
-
-
- server.begin();
- }
- void loop()
- {
- WiFiEspClient client = server.available();
- if (client) {
- Serial.println("New client");
- buf.init();
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- buf.push(c);
-
-
-
-
-
-
- if (buf.endsWith("\r\n\r\n")) {
- sendHttpResponse(client);
- break;
- }
-
- if (buf.endsWith("GET /H")) {
- Serial.println("Turn led ON");
- ledStatus = HIGH;
- digitalWrite(LED_BUILTIN, HIGH);
- }
- else if (buf.endsWith("GET /L")) {
- Serial.println("Turn led OFF");
- ledStatus = LOW;
- digitalWrite(LED_BUILTIN, LOW);
- }
- }
- }
-
-
- client.stop();
- Serial.println("Client disconnected");
- }
- }
- void sendHttpResponse(WiFiEspClient client)
- {
-
-
- client.println("HTTP/1.1 200 OK");
- client.println("Content-type:text/html");
- client.println();
-
-
- client.print("The LED is ");
- client.print(ledStatus);
- client.println("<br>");
- client.println("<br>");
-
- client.println("Click <a href=\"/H\">here</a> turn the LED on<br>");
- client.println("Click <a href=\"/L\">here</a> turn the LED off<br>");
-
-
- client.println();
- }
- void printWifiStatus()
- {
-
- Serial.print("SSID: ");
- Serial.println(WiFi.SSID());
-
- IPAddress ip = WiFi.localIP();
- Serial.print("IP Address: ");
- Serial.println(ip);
-
- Serial.println();
- Serial.print("To see this page in action, open a browser to http://");
- Serial.println(ip);
- Serial.println();
- }
|