-
-
Save igrr/0a7783b7611aad9169af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
#include <ESP8266WebServer.h> | |
#include <ESP8266mDNS.h> | |
const char* ssid = "........"; | |
const char* password = "........"; | |
class Counter { | |
public: | |
void begin(ESP8266WebServer* server, const char* uri) { | |
m_server = server; | |
server->on(uri, std::bind(&Counter::handleRequest, this)); | |
} | |
protected: | |
void handleRequest() { | |
++m_counter; | |
String response; | |
response += m_counter; | |
m_server->send(200, "text/plain", response); | |
} | |
int m_counter = 0; | |
ESP8266WebServer* m_server; | |
}; | |
Counter counter; | |
ESP8266WebServer server(80); | |
void setup(void){ | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); | |
// Wait for connection | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
counter.begin(&server, "/count"); | |
server.begin(); | |
Serial.println("HTTP server started"); | |
} | |
void loop(void){ | |
server.handleClient(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment