fixed connection added state
This commit is contained in:
parent
b03422e292
commit
416b75c424
@ -8,7 +8,12 @@
|
|||||||
; Please visit documentation for the other options and examples
|
; Please visit documentation for the other options and examples
|
||||||
; https://docs.platformio.org/page/projectconf.html
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
[env:wemos_d1_mini32]
|
[env:d1_mini_pro]
|
||||||
platform = espressif32
|
platform = espressif8266
|
||||||
board = wemos_d1_mini32
|
board = d1_mini_pro
|
||||||
framework = arduino
|
framework = arduino
|
||||||
|
monitor_speed = 115200
|
||||||
|
|
||||||
|
lib_deps =
|
||||||
|
https://git.aterve.com/Frozenverse/SimpleSerialProtocol.git
|
||||||
|
https://github.com/bblanchon/ArduinoJson.git
|
123
CrabbyHome-comp/CrabbyHome-conn/src/Connection.h
Normal file
123
CrabbyHome-comp/CrabbyHome-conn/src/Connection.h
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ESP8266WebServer.h>
|
||||||
|
|
||||||
|
#include <SimpleSerialProtocol.h>
|
||||||
|
|
||||||
|
#include <StateData.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
44
CrabbyHome-comp/CrabbyHome-conn/src/StateData.h
Normal file
44
CrabbyHome-comp/CrabbyHome-conn/src/StateData.h
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Arduino.h>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,16 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <Connection.h>
|
||||||
|
#include <StateData.h>
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
// put your setup code here, to run once:
|
// put your setup code here, to run once:
|
||||||
|
delay(5000);
|
||||||
|
Connection::initSerial();
|
||||||
|
Connection::initWifi();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
// put your main code here, to run repeatedly:
|
// put your main code here, to run repeatedly:
|
||||||
|
Connection::handleSerial();
|
||||||
|
Connection::handleWifi();
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user