Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- const char WiFiAPPSK[] = "sparkfun";
- const int LED_PIN1 = D2;
- const int LED_PIN2 = D4;
- int val1 = 0;
- int val2 = 0;
- WiFiServer server(80);
- void setup()
- {
- initHardware();
- setupWiFi();
- server.begin();
- }
- void loop()
- {
- // Check if a client has connected
- WiFiClient client = server.available();
- if (!client) {
- return;
- }
- // Read the first line of the request
- String req = client.readStringUntil('\r');
- Serial.println(req);
- client.flush();
- // Match the request
- if (req.indexOf("/led/0") != -1) //stop
- val1 = 0;
- else if (req.indexOf("/led/1") != -1) //cala na przod
- val1 = 255;
- if (req.indexOf("/led/fast") != -1) // przyspiesz
- val1 = val1 + 20;
- if (req.indexOf("/led/slow") != -1) // zwolnij
- val1 = val - 20;
- if (val1 >= 0)
- {
- analogWrite(LED_PIN1, val1);
- Serial.println(val1);
- }
- client.flush();
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println(""); // do not forget this one
- client.println("<!DOCTYPE HTML>");
- client.println("<html>");
- client.println("<br><br>");
- client.println("Click <a href=\"/led/1\">here</a> turn the LED on pin 2 ON<br>");
- client.println("Click <a href=\"/led/0\">here</a> turn the LED on pin 2 OFF<br>");
- client.println("Click <a href=\"/led/fast\">here</a> LED will more shine<br>");
- client.println("Click <a href=\"/led/slow\">here</a> LED will less shine OFF<br>");
- client.println("</html>");
- delay(1);
- Serial.println("Client disonnected");
- }
- void setupWiFi()
- {
- WiFi.mode(WIFI_AP);
- uint8_t mac[WL_MAC_ADDR_LENGTH];
- WiFi.softAPmacAddress(mac);
- String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
- String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
- macID.toUpperCase();
- String AP_NameString = "ESP8266 Thing " + macID;
- char AP_NameChar[AP_NameString.length() + 1];
- memset(AP_NameChar, AP_NameString.length() + 1, 0);
- for (int i=0; i<AP_NameString.length(); i++)
- AP_NameChar[i] = AP_NameString.charAt(i);
- WiFi.softAP(AP_NameChar, WiFiAPPSK);
- }
- void initHardware()
- {
- Serial.begin(115200);
- pinMode(DIGITAL_PIN, INPUT_PULLUP);
- pinMode(LED_PIN1, OUTPUT);
- analogWrite(LED_PIN1, val1);
- pinMode(LED_PIN2, OUTPUT);
- analogWrite(LED_PIN2, val2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement