Created
April 26, 2024 11:07
-
-
Save DeeprajPandey/e244c1a9e981b0fc9324652a11800e5b to your computer and use it in GitHub Desktop.
ESP32 Getting Started with AsyncServer
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 <Arduino.h> | |
#include <ArduinoJson.h> | |
#include <AsyncTCP.h> | |
#include <ESPAsyncWebServer.h> | |
#include <Wifi.h> | |
#define LED 2 | |
const char *ssid = "[SSID]"; | |
const char *pswd = "[PASSWD]"; | |
// asyncwebserver on port 80 | |
AsyncWebServer server(80); | |
bool newRequest = false; | |
int lightState = 0; | |
void initWifi() { | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, pswd); | |
Serial.print("Connecting to WiFi.."); | |
// while (WiFi.waitForConnectResult()) != WL_CONNECTED) { | |
// Serial.printf("Wifi connection failed!\n"); | |
// } | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print('.'); | |
delay(1000); | |
} | |
Serial.print("IP Address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void notFound(AsyncWebServerRequest *req) { | |
req->send(404, "application/json", "{\"message\":\"Not found\"}"); | |
} | |
void setup() { | |
Serial.begin(115200); | |
pinMode(LED, OUTPUT); | |
initWifi(); | |
server.onNotFound(notFound); | |
// health-check | |
server.on("/", HTTP_GET, [](AsyncWebServerRequest *req) { | |
req->send(200, "application/json", "{\"message\":\"health ok\"}"); | |
}); | |
server.on("/get-message", HTTP_GET, [](AsyncWebServerRequest *request) { | |
StaticJsonDocument<100> data; | |
if (request->hasParam("message")) { | |
data["message"] = request->getParam("message")->value(); | |
} else { | |
data["message"] = "No message parameter"; | |
} | |
String response; | |
serializeJson(data, response); | |
request->send(200, "application/json", response); | |
}); | |
server.on("/toggle", HTTP_POST, [](AsyncWebServerRequest *req) { | |
digitalWrite(LED, lightState); | |
Serial.write("LED Toggled..."); | |
lightState = !lightState; | |
req->send(200, "application/json", "{\"message\":\"led toggled\"}"); | |
}); | |
server.begin(); | |
} | |
void loop() {} |
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
; PlatformIO Project Configuration File | |
; | |
; Build options: build flags, source filter | |
; Upload options: custom upload port, speed and extra flags | |
; Library options: dependencies, extra library storages | |
; Advanced options: extra scripting | |
; | |
; Please visit documentation for the other options and examples | |
; https://docs.platformio.org/page/projectconf.html | |
[env:esp32doit-devkit-v1] | |
platform = espressif32 | |
board = esp32doit-devkit-v1 | |
framework = arduino | |
monitor_speed = 115200 | |
lib_deps = | |
ESP Async WebServer | |
me-no-dev/AsyncTCP@^1.1.1 | |
arduino-libraries/Stepper @ ^1.1.3 | |
bblanchon/ArduinoJson@^7.0.4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment