Created
July 9, 2020 05:16
-
-
Save cluelessperson/bb83275f2c8e08f4fbe93c1af553774b 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 <Arduino.h> | |
#include "wifi/wifi.h" | |
#include "leds/leds.h" | |
#include <TaskScheduler.h> | |
#include <ESPAsyncWebServer.h> | |
#include "AsyncJson.h" | |
#include "ArduinoJson.h" | |
void check_wifi(); | |
Scheduler ts; | |
Task wifi_task ( 5 * TASK_SECOND, TASK_FOREVER, &check_wifi, &ts, true); | |
AsyncWebServer server(80); | |
void setup() { | |
leds_setup(); | |
wifi_setup(); | |
Serial.begin(115200); | |
while ( ! Serial ) {} | |
Serial.println("ready"); | |
server.onNotFound([](AsyncWebServerRequest *request) { | |
request->send(404); | |
}); | |
server.on("/test", HTTP_POST, [](AsyncWebServerRequest *request) { | |
Serial.println("WEB REQUEST"); | |
request->send(200); | |
int args = request->args(); | |
for(int i=0;i<args;i++){ | |
Serial.printf("ARG[%s]: %s\n", request->argName(i).c_str(), request->arg(i).c_str()); | |
} | |
}); | |
AsyncCallbackJsonWebHandler* handler = new AsyncCallbackJsonWebHandler("/api/rgb", [](AsyncWebServerRequest *request, JsonVariant &json) { | |
JsonObject& jsonObj = json.as<JsonObject>(); | |
fill_solid( | |
leds, | |
NUM_LEDS, | |
CRGB( | |
jsonObj["r"], | |
jsonObj["g"], | |
jsonObj["b"] | |
)); | |
FastLED.show(); | |
}); | |
server.addHandler(handler); | |
server.begin(); | |
} | |
void loop() { | |
ts.execute(); | |
} | |
void check_wifi () { | |
if ( wifi_check() ) { | |
leds[0] = CRGB::Green; | |
FastLED.show(); | |
} else { | |
leds[0] = CRGB::Yellow; | |
FastLED.show(); | |
wifi_connect(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment