port to platformio

This commit is contained in:
2021-03-29 01:10:55 -07:00
parent 9a3bf84214
commit a6534bcb20
131 changed files with 1537 additions and 1148 deletions

View File

@@ -0,0 +1,163 @@
#include <Input.h>
#include <PubSubClient.h>
#include <ArduinoLog.h>
#include <ArduinoJson.h>
#include <ArduinoUniqueID.h>
WiFiClient wifi;
PubSubClient m_mqtt(wifi);
class MQTTTelemetry : public BufferedInputSource, OnlineTaskMixin {
public:
MQTTTelemetry() : BufferedInputSource("MQTT") {}
void handleEventOnline(const InputEvent& evt) override {
if (!m_mqtt.connected()) {
Log.notice("Connecting to MQTT...");
const IPAddress server(10, 0, 0, 2);
uint64_t chipid = ESP.getEfuseMac();
char deviceID[15];
snprintf(deviceID, 15, "%08X", (uint32_t)chipid);
Log.verbose("Device ID %s", deviceID);
m_mqtt.setServer(server, 1883);
m_mqtt.setBufferSize(512);
m_mqtt.setCallback(&MQTTTelemetry::s_callback);
if (m_mqtt.connect(deviceID)) {
Log.notice("Connected to MQTT");
const String deviceName = String("Renderbug ESP32 ") + (char*)deviceID;
const String rootTopic = String("homeassistant/light/renderbug/") + (char*)deviceID;
const String configTopic = rootTopic + "/config";
Log.verbose("root topic %s", rootTopic.c_str());
Log.verbose("config topic %s", configTopic.c_str());
const String statTopic = rootTopic + "/state";
const String cmdTopic = rootTopic + "/set";
const String attrTopic = rootTopic + "/attributes";
strcpy(m_statTopic, statTopic.c_str());
strcpy(m_attrTopic, attrTopic.c_str());
strcpy(m_cmdTopic, cmdTopic.c_str());
StaticJsonDocument<1024> configJson;
configJson["~"] = rootTopic;
configJson["name"] = deviceName;
configJson["ret"] = true;
configJson["unique_id"] = (char*) deviceID;
configJson["cmd_t"] = "~/set";
configJson["stat_t"] = "~/state";
configJson["json_attr_t"] = "~/attributes";
configJson["schema"] = "json";
configJson["brightness"] = true;
configJson["rgb"] = true;
configJson["dev"]["name"] = "Renderbug";
#ifdef PLATFORM_PHOTON
configJson["dev"]["mdl"] = "Photon";
#else
configJson["dev"]["mdl"] = "ESP32";
#endif
configJson["dev"]["sw"] = RENDERBUG_VERSION;
configJson["dev"]["mf"] = "Phong Robotics";
configJson["dev"]["ids"][0] = (char*)deviceID;
char buf[1024];
serializeJson(configJson, buf, sizeof(buf));
Log.verbose("Publish %s %s", configTopic.c_str(), buf);
m_mqtt.publish(configTopic.c_str(), buf);
m_mqtt.subscribe(m_cmdTopic);
} else {
Log.warning("Could not connect to MQTT");
}
} else {
if (evt.intent == InputEvent::SetPower) {
StaticJsonDocument<256> doc;
char buf[256];
doc["state"] = evt.asInt() ? "ON" : "OFF";
serializeJson(doc, buf, sizeof(buf));
m_mqtt.publish(m_statTopic, buf);
} else if (evt.intent == InputEvent::SetBrightness) {
StaticJsonDocument<256> doc;
char buf[256];
doc["brightness"] = evt.asInt();
serializeJson(doc, buf, sizeof(buf));
m_mqtt.publish(m_statTopic, buf);
} else if (evt.intent == InputEvent::SetColor) {
StaticJsonDocument<256> doc;
char buf[256];
CRGB color = evt.asRGB();
doc["color"]["r"] = color.r;
doc["color"]["g"] = color.g;
doc["color"]["b"] = color.b;
serializeJson(doc, buf, sizeof(buf));
m_mqtt.publish(m_statTopic, buf);
} else if (evt.intent == InputEvent::SetPattern) {
StaticJsonDocument<256> doc;
char buf[256];
doc["effect"] = evt.asString();
serializeJson(doc, buf, sizeof(buf));
m_mqtt.publish(m_statTopic, buf);
}
}
}
void loop() override {
BufferedInputSource::loop();
OnlineTaskMixin::loop();
}
void loopOnline() override {
m_mqtt.loop();
EVERY_N_SECONDS(10) {
char buf[254];
StaticJsonDocument<200> response;
response["fps"] = FastLED.getFPS();
response["RSSI"] = WiFi.RSSI();
response["localip"] = WiFi.localIP().toString();
response["free_ram"] = ESP.getFreeHeap();
response["os_version"] = ESP.getSdkVersion();
response["sketch_version"] = ESP.getSketchMD5();
serializeJson(response, buf, sizeof(buf));
m_mqtt.publish(m_attrTopic, buf);
}
}
private:
char m_statTopic[100];
char m_attrTopic[100];
char m_cmdTopic[100];
void callback(char* topic, byte* payload, unsigned int length) {
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload, length);
if (doc.containsKey("state")) {
if (doc["state"] == "ON") {
setEvent(InputEvent{InputEvent::SetPower, true});
} else if (doc["state"] == "OFF") {
setEvent(InputEvent{InputEvent::SetPower, false});
}
}
if (doc.containsKey("effect")) {
strcpy(m_patternBuf, doc["effect"].as<const char*>());
setEvent(InputEvent{InputEvent::SetPattern, m_patternBuf});
}
if (doc.containsKey("color")) {
uint8_t r = doc["color"]["r"];
uint8_t g = doc["color"]["g"];
uint8_t b = doc["color"]["b"];
setEvent(InputEvent{InputEvent::SetColor, CRGB(r, g, b)});
}
if (doc.containsKey("brightness")) {
setEvent(InputEvent{InputEvent::SetBrightness, (int)doc["brightness"]});
}
}
static void s_callback(char* topic, byte* payload, unsigned int length) {
Static<MQTTTelemetry>::instance()->callback(topic, payload, length);
}
char m_patternBuf[48];
};
STATIC_ALLOC(MQTTTelemetry);