made website working
This commit is contained in:
@@ -34,6 +34,7 @@ namespace Connection {
|
||||
|
||||
void onApiStatusRoute();
|
||||
void getCurrentStateRoute();
|
||||
void onDeviceRoute();
|
||||
}
|
||||
|
||||
// serial
|
||||
@@ -79,6 +80,7 @@ void Connection::initWifi(){
|
||||
|
||||
server.on("/api/status", Connection::onApiStatusRoute);
|
||||
server.on("/api/currentstate", Connection::getCurrentStateRoute);
|
||||
server.on("/api/device", Connection::onDeviceRoute);
|
||||
|
||||
server.begin();
|
||||
}
|
||||
@@ -91,7 +93,9 @@ void Connection::sendResponse(DynamicJsonDocument response) {
|
||||
// Prepare data to return
|
||||
char serializedJson[jsonBufferSize];
|
||||
serializeJson(response, serializedJson);
|
||||
|
||||
|
||||
//added cors
|
||||
server.sendHeader("Access-Control-Allow-Origin", "*");
|
||||
// Send response
|
||||
server.send(200, "application/json", serializedJson);
|
||||
}
|
||||
@@ -120,4 +124,37 @@ void Connection::getCurrentStateRoute() {
|
||||
|
||||
// Send response
|
||||
sendResponse(response);
|
||||
}
|
||||
|
||||
void Connection::onDeviceRoute() {
|
||||
// Init Json Document
|
||||
DynamicJsonDocument response(jsonBufferSize);
|
||||
|
||||
// Check params
|
||||
if (server.arg("id").isEmpty() || server.arg("action").isEmpty() || server.arg("data").isEmpty()) {
|
||||
response["status"] = "error";
|
||||
response["msg"] = "Invalid parameter fields!";
|
||||
return sendResponse(response);
|
||||
}
|
||||
|
||||
// Get parameters from request
|
||||
int idBuffer = server.arg("id").length() + 1;
|
||||
char id[idBuffer];
|
||||
server.arg("id").toCharArray(id, idBuffer);
|
||||
|
||||
int commandBuffer = server.arg("action").length() + 1;
|
||||
char command[commandBuffer];
|
||||
server.arg("action").toCharArray(command, commandBuffer);
|
||||
|
||||
int dataBuffer = server.arg("data").length() + 1;
|
||||
char data[dataBuffer];
|
||||
server.arg("data").toCharArray(data, dataBuffer);
|
||||
|
||||
// Send packet to controller
|
||||
controller.sendPacket(id, command, data);
|
||||
|
||||
response["status"] = "success";
|
||||
response["msg"] = "Processed command!";
|
||||
|
||||
sendResponse(response);
|
||||
}
|
@@ -30,10 +30,11 @@ void Connection::handle() {
|
||||
}
|
||||
|
||||
void Connection::onNetworkerPacket(Packet packet) {
|
||||
debugger.sendPacket(packet.id, packet.command, packet.data);
|
||||
|
||||
// Handle Lights
|
||||
if (strcmp(packet.id, "main_light") == 0) {
|
||||
if (strcmp(packet.id, "ambiance_light") == 0) {
|
||||
debugger.sendPacket(packet.id, packet.command, packet.data);
|
||||
|
||||
RFTransmit::handlePacket(&networker, packet.id, packet.command, packet.data);
|
||||
}
|
||||
}
|
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include <NewRemoteTransmitter.h>
|
||||
#include <Screen.h>
|
||||
|
||||
#define RF_SEND_PIN 11
|
||||
|
||||
@@ -19,11 +20,13 @@ void RFTransmit::handlePacket(SimpleSerialProtocol *endpoint, char* id, char* ac
|
||||
if(strcmp(id, "ambiance_light") == 0){
|
||||
if (strcmp(action, "on") == 0){
|
||||
RFTransmit::send(0, rf_type_switch, rf_switch_on);
|
||||
Screen::display(id, "ON");
|
||||
endpoint->sendPacket("access", "update_state", "bool;ambiance_light;true");
|
||||
}
|
||||
|
||||
if (strcmp(action, "off") == 0){
|
||||
RFTransmit::send(0, rf_type_switch, rf_switch_off);
|
||||
Screen::display(id, "OFF");
|
||||
endpoint->sendPacket("access", "update_state", "bool;ambiance_light;false");
|
||||
}
|
||||
}
|
||||
@@ -32,11 +35,13 @@ void RFTransmit::handlePacket(SimpleSerialProtocol *endpoint, char* id, char* ac
|
||||
if(strcmp(id, "main_group") == 0){
|
||||
if (strcmp(action, "on") == 0){
|
||||
RFTransmit::send(0, rf_type_switchgroup, rf_switch_on);
|
||||
Screen::display(id, "ON");
|
||||
endpoint->sendPacket("access", "update_state", "bool;ambiance_light;true");
|
||||
}
|
||||
|
||||
if (strcmp(action, "off") == 0){
|
||||
RFTransmit::send(0, rf_type_switchgroup, rf_switch_off);
|
||||
Screen::display(id, "OFF");
|
||||
endpoint->sendPacket("access", "update_state", "bool;ambiance_light;false");
|
||||
}
|
||||
}
|
||||
|
@@ -12,8 +12,8 @@ namespace Screen{
|
||||
|
||||
void Screen::init(){
|
||||
lcd.begin();
|
||||
// Print a message to the LCD.
|
||||
lcd.backlight();
|
||||
|
||||
lcd.setCursor(3,0);
|
||||
lcd.print("CrabbyHome");
|
||||
lcd.setCursor(3,1);
|
||||
@@ -24,6 +24,8 @@ void Screen::display(char* title, char* text){
|
||||
int titleSize = strlen(title);
|
||||
int textSize = strlen(text);
|
||||
lcd.clear();
|
||||
lcd.display();
|
||||
lcd.backlight();
|
||||
|
||||
// check if text fits in screen
|
||||
if(titleSize <= 16){
|
||||
@@ -37,4 +39,8 @@ void Screen::display(char* title, char* text){
|
||||
lcd.setCursor(offset, 1);
|
||||
}
|
||||
lcd.print(text);
|
||||
delay(1000);
|
||||
|
||||
lcd.noBacklight();
|
||||
lcd.noDisplay();
|
||||
}
|
@@ -10,12 +10,9 @@ void setup() {
|
||||
Connection::init();
|
||||
Temperature::init();
|
||||
Screen::init();
|
||||
RFTransmit::send(1, "switchgroup", "true");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
// delay(500);
|
||||
Connection::handle();
|
||||
}
|
Reference in New Issue
Block a user