Advertisement
Guest User

ESP32 Server

a guest
Apr 14th, 2021
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include <AsyncTCP.h>
  2. #include <WiFi.h>
  3. #include <ESPAsyncWebServer.h>
  4.  
  5. using string = const char*;
  6.  
  7. class WifiNetwork
  8. {
  9. public:
  10.     struct Credentials
  11.     {
  12.         string ssid;
  13.         string password;
  14.     };
  15. private:
  16.     const int loadingLedGpio = 32;
  17.     const int connectionBlinkInterval = 250;
  18.     WiFiClass wifi;
  19.  
  20.     void connect(const Credentials& credentials)
  21.     {
  22.         WiFi.mode(WIFI_STA);
  23.         wifi.begin(credentials.ssid, credentials.password);
  24.         auto lightsUp = true;
  25.         while (wifi.status() != WL_CONNECTED)
  26.         {
  27.             digitalWrite(loadingLedGpio, lightsUp ? HIGH : LOW);
  28.             Serial.print(".");
  29.             delay(connectionBlinkInterval);
  30.             lightsUp = !lightsUp;
  31.         }
  32.         digitalWrite(loadingLedGpio, LOW);
  33.     }
  34.  
  35.     void printNetworkInfoSummary()
  36.     {
  37.         Serial.println(" Successfully established!");
  38.         Serial.printf("RRSI: %d\n", RSSI());
  39.     }
  40.  
  41. public:
  42.     WifiNetwork(const Credentials& credentials)
  43.     {
  44.         pinMode(loadingLedGpio, OUTPUT);
  45.         Serial.write("Connecting to WiFi .");
  46.         connect(credentials);
  47.         printNetworkInfoSummary();
  48.     }
  49.  
  50.     string IPAddress()
  51.     {
  52.         return wifi.localIP().toString().c_str();
  53.     }
  54.  
  55.     uint8_t RSSI()
  56.     {
  57.         return wifi.RSSI();
  58.     }
  59. };
  60.  
  61. class SagittariusStation
  62. {
  63. private:
  64.     AsyncWebServer server;
  65.     string name = "\nSagittarius Station 1.0";
  66.     static void page404(AsyncWebServerRequest* request)
  67.     {
  68.         request->send(
  69.             404,
  70.             "text/plain",
  71.             "404 - Page not found"
  72.         );
  73.     }
  74. public:
  75.     SagittariusStation(WifiNetwork& network, const int port = 80) :
  76.         server(port)
  77.     {
  78.         Serial.println(Name());
  79.         Serial.printf("Starting up on %s:%d", network.IPAddress(), port);
  80.         server.on("/", HTTP_GET, [](AsyncWebServerRequest* request)
  81.         {
  82.             request->send(
  83.                 200,
  84.                 "text/plain",
  85.                 "Hello, world"
  86.             );
  87.         });
  88.         server.on("/about", HTTP_GET, [this](AsyncWebServerRequest* request)
  89.         {
  90.             request->send(
  91.                 200,
  92.                 "text/plain",
  93.                 name
  94.             );
  95.         });
  96.         server.onNotFound(page404);
  97.         server.begin();
  98.     }
  99.  
  100.     string Name()
  101.     {
  102.         return name;
  103.     }
  104.  
  105.     AsyncWebServer Server()
  106.     {
  107.         return server;
  108.     }
  109. };
  110.  
  111.  
  112. void setup()
  113. {
  114.     Serial.begin(115200);
  115.  
  116.     WifiNetwork net({ "Milky Way", "heslo_do_wifi" });
  117.     SagittariusStation station(net);
  118. }
  119.  
  120. void loop()
  121. {
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement