Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fredericalix/49707d47788e767ed7077470e99c5013 to your computer and use it in GitHub Desktop.
Save fredericalix/49707d47788e767ed7077470e99c5013 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include "AsyncJson.h"
#include "ArduinoJson.h"
AsyncWebServer server(80);
const char *ssid = "xxxx";
const char *password = "xxxx";
String response;
TaskHandle_t Task1;
TaskHandle_t Task2;
void notFound(AsyncWebServerRequest *request)
{
request->send(404, "application/json", "{\"message\":\"Not found\"}");
}
void setup()
{
Serial.begin(115200);
Serial.print("setup() running on core ");
Serial.println(xPortGetCoreID());
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED)
{
Serial.printf("WiFi Failed!\n");
}
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
xTaskCreatePinnedToCore(
Task1code, /* Task function. */
"Task1", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task1, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(500);
xTaskCreatePinnedToCore(
Task2code, /* Task function. */
"Task2", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task2, /* Task handle to keep track of created task */
1); /* pin task to core 1 */
delay(500);
}
void Task1code( void * pvParameters ){
Serial.print("Task1 running on core ");
Serial.println(xPortGetCoreID());
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "application/json", "{\"message\":\"Welcome\"}");
});
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";
}
response = "";
serializeJson(data, response);
request->send(200, "application/json", response);
});
AsyncCallbackJsonWebHandler *handler = new AsyncCallbackJsonWebHandler("/post-message", [](AsyncWebServerRequest *request, JsonVariant &json) {
StaticJsonDocument<200> data;
if (json.is<JsonArray>())
{
data = json.as<JsonArray>();
}
else if (json.is<JsonObject>())
{
data = json.as<JsonObject>();
}
//String response;
serializeJson(data, response);
request->send(200, "application/json", response);
Serial.println(response);
});
server.addHandler(handler);
server.onNotFound(notFound);
server.begin();
for(;;){
Serial.print("Task1 running on core ");
Serial.println(xPortGetCoreID());
delay(1000);
}
}
void Task2code( void * pvParameters ){
Serial.print("Task2 running on core ");
Serial.println(xPortGetCoreID());
for(;;){
String mess = response;
Serial.print("Task2 running on core ");
Serial.println(xPortGetCoreID());
Serial.println(mess);
delay(1000);
}
}
void loop()
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment