Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <AsyncTCP.h>
- #include <WiFi.h>
- #include <ESPAsyncWebServer.h>
- using string = const char*;
- class WifiNetwork
- {
- public:
- struct Credentials
- {
- string ssid;
- string password;
- };
- private:
- const int loadingLedGpio = 32;
- const int connectionBlinkInterval = 250;
- WiFiClass wifi;
- void connect(const Credentials& credentials)
- {
- WiFi.mode(WIFI_STA);
- wifi.begin(credentials.ssid, credentials.password);
- auto lightsUp = true;
- while (wifi.status() != WL_CONNECTED)
- {
- digitalWrite(loadingLedGpio, lightsUp ? HIGH : LOW);
- Serial.print(".");
- delay(connectionBlinkInterval);
- lightsUp = !lightsUp;
- }
- digitalWrite(loadingLedGpio, LOW);
- }
- void printNetworkInfoSummary()
- {
- Serial.println(" Successfully established!");
- Serial.printf("RRSI: %d\n", RSSI());
- }
- public:
- WifiNetwork(const Credentials& credentials)
- {
- pinMode(loadingLedGpio, OUTPUT);
- Serial.write("Connecting to WiFi .");
- connect(credentials);
- printNetworkInfoSummary();
- }
- string IPAddress()
- {
- return wifi.localIP().toString().c_str();
- }
- uint8_t RSSI()
- {
- return wifi.RSSI();
- }
- };
- class SagittariusStation
- {
- private:
- AsyncWebServer server;
- string name = "\nSagittarius Station 1.0";
- static void page404(AsyncWebServerRequest* request)
- {
- request->send(
- 404,
- "text/plain",
- "404 - Page not found"
- );
- }
- public:
- SagittariusStation(WifiNetwork& network, const int port = 80) :
- server(port)
- {
- Serial.println(Name());
- Serial.printf("Starting up on %s:%d", network.IPAddress(), port);
- server.on("/", HTTP_GET, [](AsyncWebServerRequest* request)
- {
- request->send(
- 200,
- "text/plain",
- "Hello, world"
- );
- });
- server.on("/about", HTTP_GET, [this](AsyncWebServerRequest* request)
- {
- request->send(
- 200,
- "text/plain",
- name
- );
- });
- server.onNotFound(page404);
- server.begin();
- }
- string Name()
- {
- return name;
- }
- AsyncWebServer Server()
- {
- return server;
- }
- };
- void setup()
- {
- Serial.begin(115200);
- WifiNetwork net({ "Milky Way", "heslo_do_wifi" });
- SagittariusStation station(net);
- }
- void loop()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement