-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Web API for Smart Home integration
+ Update EspAsyncWeb lib
- Loading branch information
Showing
10 changed files
with
207 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#pragma once | ||
|
||
#define CREDENTIAL_WIFI_SSID "ESP_LED" | ||
#define CREDENTIAL_WIFI_PASSWORD "12345678" | ||
|
||
#define CREDENTIAL_AUTH_USER "esp_lamp" | ||
#define CREDENTIAL_AUTH_PASSWORD "password" |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include "api.h" | ||
|
||
#include <ArduinoJson.h> | ||
|
||
#include "utils/math.h" | ||
|
||
|
||
ApiWebServer::ApiWebServer(Application &application, const char *path) : _app(application), _path(path) {} | ||
|
||
void ApiWebServer::begin(WebServer &server) { | ||
server.on((_path + String("/power")).c_str(), HTTP_GET, [this](AsyncWebServerRequest *request) { | ||
if (!request->hasArg("value")) { | ||
D_PRINT("Request power status"); | ||
return response_with_json(request, JsonPropListT{ | ||
{"status", "ok"}, | ||
{"value", _app.config.power ? 1 : 0}, | ||
}); | ||
} | ||
|
||
bool enabled = request->arg("value") == "1"; | ||
_app.set_power(enabled); | ||
|
||
response_with_json_status(request, "ok"); | ||
}); | ||
|
||
server.on((_path + String("/brightness")).c_str(), HTTP_GET, [this](AsyncWebServerRequest *request) { | ||
if (!request->hasArg("value")) { | ||
D_PRINT("Request brightness status"); | ||
return response_with_json(request, JsonPropListT{ | ||
{"status", "ok"}, | ||
{"value", map16(_app.config.max_brightness, 255, 100)}, | ||
}); | ||
} | ||
|
||
auto new_brightness = map16(request->arg("value").toInt(), 100, 255); | ||
|
||
_app.config.max_brightness = new_brightness; | ||
_app.load(); | ||
|
||
response_with_json_status(request, "ok"); | ||
}); | ||
|
||
server.on((_path + String("/color")).c_str(), HTTP_GET, [this](AsyncWebServerRequest *request) { | ||
if (!request->hasArg("value")) { | ||
D_PRINT("Request color status"); | ||
auto hsv = _app.preset().color_effect == ColorEffectEnum::SOLID | ||
? CHSV(_app.preset().speed, _app.preset().scale, 255) | ||
: CHSV(255, 255, 225); | ||
|
||
CRGB color{}; | ||
hsv2rgb_rainbow(hsv, color); | ||
|
||
return response_with_json(request, JsonPropListT{ | ||
{"status", "ok"}, | ||
{"value", static_cast<uint32_t>(color) & 0xffffff}, | ||
|
||
{"hue", hsv.hue}, | ||
{"sat", hsv.sat}, | ||
{"bri", hsv.val}, | ||
}); | ||
} | ||
|
||
PresetConfig *preset = nullptr; | ||
if (_app.preset().color_effect == ColorEffectEnum::SOLID) { | ||
preset = &_app.preset(); | ||
} else { | ||
for (int i = 0; i < _app.preset_configs.count; ++i) { | ||
if (_app.preset_configs.presets[i].color_effect == ColorEffectEnum::SOLID) { | ||
_app.change_preset(i); | ||
preset = &_app.preset_configs.presets[i]; | ||
break; | ||
} | ||
} | ||
|
||
if (preset == nullptr) { | ||
return response_with_json_status(request, "error"); | ||
} | ||
} | ||
|
||
|
||
auto new_color = CRGB(request->arg("value").toInt()); | ||
auto hsv = rgb2hsv_approximate(new_color); | ||
preset->speed = hsv.hue; | ||
preset->scale = hsv.sat; | ||
|
||
_app.load(); | ||
|
||
response_with_json(request, { | ||
{"status", "ok"}, | ||
{"hue", hsv.hue}, | ||
{"sat", hsv.sat}, | ||
{"bri", hsv.val}, | ||
}); | ||
}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
#include "constants.h" | ||
#include "application.h" | ||
|
||
#include "network/web.h" | ||
|
||
#include "utils/network.h" | ||
|
||
class ApiWebServer { | ||
Application &_app; | ||
const char *_path; | ||
|
||
public: | ||
ApiWebServer(Application &application, const char *path = "/api"); | ||
|
||
void begin(WebServer &server); | ||
}; |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <cmath> | ||
|
||
inline uint16_t map16(uint16_t value, uint16_t limit_src, uint16_t limit_dst) { | ||
value = std::max((uint16_t) 0, std::min(limit_src, value)); | ||
return (int32_t) value * limit_dst / limit_src; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include <variant> | ||
|
||
#include <ArduinoJson.h> | ||
#include <ESPAsyncWebServer.h> | ||
|
||
typedef std::variant<bool, int, long, long long, float, double, const char *> JsonPropVariantT; | ||
typedef std::initializer_list<std::pair<const char *, JsonPropVariantT>> JsonPropListT; | ||
|
||
inline void response_with_json(AsyncWebServerRequest *request, JsonDocument &doc) { | ||
auto *response = request->beginResponseStream("application/json"); | ||
serializeJson(doc, *response); | ||
request->send(response); | ||
} | ||
|
||
|
||
inline void response_with_json(AsyncWebServerRequest *request, JsonPropListT props) { | ||
JsonDocument doc; | ||
for (auto prop: props) { | ||
std::visit([&](auto &&arg) { doc[prop.first] = arg; }, prop.second); | ||
} | ||
|
||
response_with_json(request, doc); | ||
} | ||
|
||
inline void response_with_json_status(AsyncWebServerRequest *request, const char *status) { | ||
JsonDocument doc; | ||
doc["status"] = status; | ||
|
||
response_with_json(request, doc); | ||
} |