diff --git a/CrabbyHome-comp/CrabbyHome-conn/platformio.ini b/CrabbyHome-comp/CrabbyHome-conn/platformio.ini index 6869843..bc09900 100644 --- a/CrabbyHome-comp/CrabbyHome-conn/platformio.ini +++ b/CrabbyHome-comp/CrabbyHome-conn/platformio.ini @@ -8,7 +8,12 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html -[env:wemos_d1_mini32] -platform = espressif32 -board = wemos_d1_mini32 +[env:d1_mini_pro] +platform = espressif8266 +board = d1_mini_pro framework = arduino +monitor_speed = 115200 + +lib_deps = + https://git.aterve.com/Frozenverse/SimpleSerialProtocol.git + https://github.com/bblanchon/ArduinoJson.git \ No newline at end of file diff --git a/CrabbyHome-comp/CrabbyHome-conn/src/Connection.h b/CrabbyHome-comp/CrabbyHome-conn/src/Connection.h new file mode 100644 index 0000000..6cfbea6 --- /dev/null +++ b/CrabbyHome-comp/CrabbyHome-conn/src/Connection.h @@ -0,0 +1,123 @@ +#pragma once + +#include + +#include + +#include +#include + +#include + +#include + +namespace Connection { + const char* ssid = "Dragonvale"; + const char* password = "Dr4g0nSl4yr"; + const int jsonBufferSize = 1024; + + // Serial Device + SimpleSerialProtocol controller; + + // Wifi WebServer + ESP8266WebServer server(80); + + // Serial + void initSerial(); + void handleSerial(); + void onControllerPacket(Packet packet); + + // Wifi + void initWifi(); + void handleWifi(); + void sendResponse(DynamicJsonDocument response); + + void onApiStatusRoute(); + void getCurrentStateRoute(); +} + +// serial +void Connection::initSerial(){ + controller.init(0, 115200); + controller.sendPacket("controller", "serial_status", "ready"); +} + +void Connection::handleSerial(){ + controller.receivePackets(onControllerPacket); +} + +void Connection::onControllerPacket(Packet packet) { + + if(strcmp(packet.id, "access") == 0) { + if (strcmp(packet.command, "update_state") == 0) { + StateData::updateState(packet.data); + } + } +} + +// wifi +void Connection::initWifi(){ + controller.sendPacket("controller", "wifi_status", "init_connecting"); + + // Set wifi config + WiFi.hostname("KrabbyHome"); + WiFi.begin(ssid, password); + + // check connection status + while (WiFi.status() != WL_CONNECTED) { + controller.sendPacket("controller", "wifi_status", "connecting"); + delay(500); + } + + // send status + controller.sendPacket("controller", "wifi_status", "connected"); + + // Send details + char ipAdress[50]; + WiFi.localIP().toString().toCharArray(ipAdress,50); + controller.sendPacket("controller", "wifi_details", ipAdress); + + server.on("/api/status", Connection::onApiStatusRoute); + server.on("/api/currentstate", Connection::getCurrentStateRoute); + + server.begin(); +} + +void Connection::handleWifi() { + server.handleClient(); +} + +void Connection::sendResponse(DynamicJsonDocument response) { + // Prepare data to return + char serializedJson[jsonBufferSize]; + serializeJson(response, serializedJson); + + // Send response + server.send(200, "application/json", serializedJson); +} + +void Connection::onApiStatusRoute() { + // Init Json Document + DynamicJsonDocument response(jsonBufferSize); + + // Insert data + response["status"] = "success"; + response["health"] = "healthy"; + response["uptime"] = millis(); + + // Send response + sendResponse(response); +} + +void Connection::getCurrentStateRoute() { + // Init Json Document + DynamicJsonDocument response(jsonBufferSize); + + // Insert data + response["status"] = "success"; + response["state"] = StateData::state; + + + // Send response + sendResponse(response); +} \ No newline at end of file diff --git a/CrabbyHome-comp/CrabbyHome-conn/src/StateData.h b/CrabbyHome-comp/CrabbyHome-conn/src/StateData.h new file mode 100644 index 0000000..af59234 --- /dev/null +++ b/CrabbyHome-comp/CrabbyHome-conn/src/StateData.h @@ -0,0 +1,44 @@ +#pragma once +#include +namespace StateData{ + DynamicJsonDocument state(1024); + + void updateState(char* data); +} + +void StateData::updateState(char* data) { + char* strtokIndex; + char stateValueType[64]; + char stateKey[64]; + char stateValue[64]; + + // Get state value type + strtokIndex = strtok(data, ";"); + strcpy(stateValueType, strtokIndex); + + // Get state key + strtokIndex = strtok(NULL, ";"); + strcpy(stateKey, strtokIndex); + + // Get state value type + strtokIndex = strtok(NULL, ";"); + strcpy(stateValue, strtokIndex); + + if (strcmp(stateValueType, "float") == 0) { + state[stateKey] = atof(stateValue); + } + + else if (strcmp(stateValueType, "bool") == 0) { + if (strcmp(stateValue, "true") == 0) { + state[stateKey] = true; + } + + else if (strcmp(stateValue, "false") == 0) { + state[stateKey] = false; + } + } + + else if (strcmp(stateValueType, "string") == 0) { + state[stateKey] = stateValue; + } +} \ No newline at end of file diff --git a/CrabbyHome-comp/CrabbyHome-conn/src/main.cpp b/CrabbyHome-comp/CrabbyHome-conn/src/main.cpp index 58b344c..59211ab 100644 --- a/CrabbyHome-comp/CrabbyHome-conn/src/main.cpp +++ b/CrabbyHome-comp/CrabbyHome-conn/src/main.cpp @@ -1,9 +1,16 @@ #include +#include +#include void setup() { // put your setup code here, to run once: + delay(5000); + Connection::initSerial(); + Connection::initWifi(); } void loop() { // put your main code here, to run repeatedly: + Connection::handleSerial(); + Connection::handleWifi(); } \ No newline at end of file