Optimized template

This commit is contained in:
Kevin Veen-Birkenbach 2020-05-13 12:09:49 +02:00
parent 95e4889c73
commit c0b5c2edd7
2 changed files with 125 additions and 76 deletions

View File

@ -6,11 +6,14 @@ String homepage_template(void){
"<head>"
"<meta charset=\"UTF-8\">"
"<title>"+String(titel)+"("+ String(hostname) +")</title>"
"<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css\">"
"<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js\"></script>"
"<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js\"></script>"
"<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css\" integrity=\"sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm\" crossorigin=\"anonymous\">"
"<script src=\"https://code.jquery.com/jquery-3.2.1.slim.min.js\" integrity=\"sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN\" crossorigin=\"anonymous\"></script>"
"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js\" integrity=\"sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q\" crossorigin=\"anonymous\"></script>"
"<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js\" integrity=\"sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl\" crossorigin=\"anonymous\"></script>"
"<link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css\">"
"<script src=\"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js\"></script>"
"<link href=\"https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css\" rel=\"stylesheet\">"
"<script src=\"https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js\"></script>"
"</head>"
"<body>"
"<div class=\"container\">"
@ -18,12 +21,26 @@ String homepage_template(void){
"<p>An REST-API Arduino microcontroller, which allows you to interact with hardware and the physical environment.</p>"
"<h2>Controller <i class=\"fas fa-cogs\"></i></h2>"
"<form method=\"get\" >";
for(const char* parameter : parameter_list)
homepage = homepage +
"<div class=\"form-group\">"
"<label for=\""+ parameter +"\">"+ parameter +":</label>"
"<input type=\"number\" class= \"form-control\" id=\""+ parameter +"\" value=\"" + server.arg(parameter) +"\" name=\""+ parameter +"\">"
"</div>";
for(const char* parameter : PARAMETER_LIST){
String parameter_type=getParameterType(parameter);
if(parameter_type.equals("boolean")){
homepage = homepage +
"<div class=\"form-row\">"
"<label for=\""+ parameter +"\" class=\"col-sm-2 col-form-label\">"+ parameter +":</label>"
"<div class=\"col-sm-10\" >"
"<input type=\"checkbox\" data-toggle=\"toggle\" name=\""+ parameter +"\" " + ((server.arg(parameter)=="on") ? String("checked=\"checked\""):String("")) + ">"
"</div>"
"</div>";
}else{
homepage = homepage +
"<div class=\"form-group row\" >"
"<label for=\""+ parameter +"\" class=\"col-sm-2 col-form-label\">"+ parameter +":</label>"
"<div class=\"col-sm-10\" >"
"<input type=\"" + ((parameter_type=="integer") ? String("number") : String("text")) + "\" class= \"form-control\" id=\""+ parameter +"\" value=\"" + server.arg(parameter) +"\" name=\""+ parameter +"\">"
"</div>"
"</div>";
}
}
homepage = homepage +
"<input type=\"submit\" class=\"btn btn-secondary\">"
"</form>"

View File

@ -1,95 +1,104 @@
/**
* Loading classes
*/
#ifndef UNIT_TEST
#include <Arduino.h>
#endif
// Infared
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRsend.h>
#include <IRutils.h>
// Web
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
// 433 Mhz
#include <NewRemoteTransmitter.h>
// Sensors
#include <DHT.h>
// Configuration
#include "config.h"
// Define Constants
const uint16_t pin_ritter = 13;
const uint16_t pin_pir = 14;
const uint16_t pin_tmp = 12;
const uint16_t pin_ir_reciever = 2;
const uint16_t pin_ir_send = 4;
const uint16_t pin_ldr = A0;
const uint16_t pin_active_buzzer = D5;
const unsigned long ritter_group_address = 13043702;
const char* parameter_plug_id="plug_id";
const char* parameter_plug_status="plug_status";
const char* parameter_ir_type="ir_type";
const char* parameter_ir_data="ir_code";
const char* parameter_ir_bits="ir_bits";
const char* parameter_delay_time_in_ms="delay_time_in_ms";
const char* parameter_sound_time_in_ms="sound_time_in_ms";
const char* parameter_list[]={parameter_plug_id,parameter_plug_status,parameter_ir_type,parameter_ir_data,parameter_ir_bits,parameter_delay_time_in_ms,parameter_sound_time_in_ms};
/**
* Define constants
*/
const uint16_t PIN_RITTER = 13;
const uint16_t PIN_PIR = 14;
const uint16_t PIN_DHT = 12;
const uint16_t PIN_IR_RECIEVER = 2;
const uint16_t PIN_IR_SEND = 4;
const uint16_t PIN_LDR = A0;
const uint16_t PIN_ACTIVE_BUZZER = D5;
const unsigned long RITTER_STANDART_GROUP_ADDRESS = 13043702;
const char* PARAMETER_PLUG_ID="plug_id";
const char* PARAMETER_PLUG_STATUS="plug_enabled";
const char* PARAMETER_IR_TYPE="ir_type";
const char* PARAMETER_IR_CODE="ir_code";
const char* PARAMETER_IR_BITS="ir_bits";
const char* PARAMETER_PRE_DELAY_TIME_IN_MS="pre_delay_time_in_ms";
const char* PARAMETER_SOUND="sound_enabled";
const char* PARAMETER_LIST[]={PARAMETER_PLUG_ID,PARAMETER_PLUG_STATUS,PARAMETER_IR_TYPE,PARAMETER_IR_CODE,PARAMETER_IR_BITS,PARAMETER_PRE_DELAY_TIME_IN_MS,PARAMETER_SOUND};
// Define variables
/**
* Define variables
*/
decode_results results;
decode_type_t last_recieved_ir_type;
int last_recieved_ir_data;
int last_recieved_ir_code;
int last_recieved_ir_bits;
// Setup classes
/**
* Setup classes
*/
MDNSResponder mdns;
ESP8266WebServer server ( 80 );
NewRemoteTransmitter transmitter(ritter_group_address, pin_ritter);
DHT dht(pin_tmp, DHT11);
IRrecv irrecv(pin_ir_reciever);
IRsend irsend(pin_ir_send);
NewRemoteTransmitter transmitter(RITTER_STANDART_GROUP_ADDRESS, PIN_RITTER);
DHT dht(PIN_DHT, DHT11);
IRrecv irrecv(PIN_IR_RECIEVER);
IRsend irsend(PIN_IR_SEND);
void dump(decode_results *results) {
/**
* Setter functions
*/
void setRecievedIr(decode_results *results) {
last_recieved_ir_type = results->decode_type;
last_recieved_ir_data = results->value;
last_recieved_ir_code = results->value;
last_recieved_ir_bits = results->bits;
Serial.println(getJsonIrLastRecieved());
Serial.println("The code \"" + String(last_recieved_ir_code) + "\" was send for type \"" + String(results->decode_type,DEC) + "\" with \"" + String(results->bits,DEC) + "\" bits.");
}
// Switchs the whole group on
void setRitterGroup(int state)
/**
* Transmitter functions
*/
void sendRemoteGroupSignal(int state)
{
transmitter.sendGroup(state);
Serial.println("The state \"" + String(state,BIN) + "\" was send to the group \"" + String(ritter_group_address,DEC) + "\".");
Serial.println("The state \"" + String(state,BIN) + "\" was send to the group \"" + String(RITTER_STANDART_GROUP_ADDRESS,DEC) + "\".");
}
// Switchs one plug on
void setRitterSwitch(int unit, int state)
void sendRemoteUnitSignal(int unit, int state)
{
transmitter.sendUnit(unit, state);
Serial.println("The state \"" + String(state,BIN) + "\" was send to the switch \"" + String(unit,DEC) + "\".");
}
void setIrColor(decode_type_t type,uint32_t code, uint16_t bits) {
void sendIrCode(decode_type_t type,uint32_t code, uint16_t bits) {
irsend.send(type, code, bits);
Serial.println("The code \"" + String(code) + "\" with \"" + String(bits) + "\" bits was send in format \"" + String(type) + "\".");
}
void setSound(int time_in_ms){
/**
* Actors
*/
void makeSound(int time_in_ms){
Serial.println("Making sound for \"" + String(time_in_ms) + "ms.");
pinMode(pin_active_buzzer,OUTPUT);
digitalWrite(pin_active_buzzer,LOW);
pinMode(PIN_ACTIVE_BUZZER,OUTPUT);
digitalWrite(PIN_ACTIVE_BUZZER,LOW);
delay(time_in_ms);
digitalWrite(pin_active_buzzer,HIGH);
pinMode(pin_active_buzzer,INPUT);
digitalWrite(PIN_ACTIVE_BUZZER,HIGH);
pinMode(PIN_ACTIVE_BUZZER,INPUT);
}
/**
* Helper functions
*/
bool isParameterDefined(String parameter_name){
for (uint8_t parameter_index = 0; parameter_index < server.args(); parameter_index++) {
if(server.argName(parameter_index)==parameter_name){
@ -99,38 +108,61 @@ bool isParameterDefined(String parameter_name){
return false;
}
String getParameterType(const char* parameter){
if(
parameter==PARAMETER_PLUG_ID ||
parameter==PARAMETER_IR_TYPE ||
parameter==PARAMETER_IR_CODE ||
parameter==PARAMETER_PRE_DELAY_TIME_IN_MS ||
parameter==PARAMETER_IR_BITS ||
parameter==PARAMETER_PLUG_ID
){
return "integer";
}
if(
parameter==PARAMETER_SOUND ||
parameter==PARAMETER_PLUG_STATUS
){
return "boolean";
}
return "text";
}
void controller(void){
if(isParameterDefined(parameter_sound_time_in_ms)){
setSound(server.arg(parameter_sound_time_in_ms).toInt());
if(isParameterDefined(PARAMETER_SOUND)){
makeSound(server.arg(PARAMETER_SOUND).toInt());
}
if(isParameterDefined(parameter_ir_type) && isParameterDefined(parameter_ir_data) && isParameterDefined(parameter_ir_bits)){
setIrColor(static_cast<decode_type_t>(server.arg(parameter_ir_type).toInt()),server.arg(parameter_ir_data).toInt(),server.arg(parameter_ir_bits).toInt());
if(isParameterDefined(PARAMETER_IR_TYPE) && isParameterDefined(PARAMETER_IR_CODE) && isParameterDefined(PARAMETER_IR_BITS)){
sendIrCode(static_cast<decode_type_t>(server.arg(PARAMETER_IR_TYPE).toInt()),server.arg(PARAMETER_IR_CODE).toInt(),server.arg(PARAMETER_IR_BITS).toInt());
}
if(isParameterDefined(parameter_plug_id) && isParameterDefined(parameter_plug_status)){
if(server.arg(parameter_plug_id)=="group"){
setRitterGroup(server.arg(parameter_plug_status).toInt());
}else if(server.arg(parameter_plug_id).toInt()>0){
setRitterSwitch(server.arg(parameter_plug_id).toInt(),server.arg(parameter_plug_status).toInt());
if(isParameterDefined(PARAMETER_PLUG_ID) && isParameterDefined(PARAMETER_PLUG_STATUS)){
if(server.arg(PARAMETER_PLUG_ID)=="group"){
sendRemoteGroupSignal(server.arg(PARAMETER_PLUG_STATUS).toInt());
}else if(server.arg(PARAMETER_PLUG_ID).toInt()>0){
sendRemoteUnitSignal(server.arg(PARAMETER_PLUG_ID).toInt(),server.arg(PARAMETER_PLUG_STATUS).toInt());
}
}
}
/**
* Getter functions
*/
String getJsonDht(void){
return "{\"temperature_celcius\":\""+String(dht.readTemperature())+"\",\"humidity\":\""+String(dht.readHumidity())+"\"}";
}
String getJsonPir(void){
return "{\"motion\":\""+String(digitalRead(pin_pir))+"\"}";
return "{\"motion\":\""+String(digitalRead(PIN_PIR))+"\"}";
}
String getJsonLdr(void){
float volt = 5.0 /1024.0 * analogRead (pin_ldr);
float volt = 5.0 /1024.0 * analogRead (PIN_LDR);
return "{\"input_volt\":\""+String(volt)+"\"}";
}
String getJsonIrLastRecieved(void){
return "{\"last_recieved\":{\"bits\":\""+String(last_recieved_ir_bits)+"\",\"type\":\""+String(last_recieved_ir_type)+"\",\"data\":\""+String(last_recieved_ir_data)+"\"}}";
return "{\"last_recieved\":{\"bits\":\""+String(last_recieved_ir_bits)+"\",\"type\":\""+String(last_recieved_ir_type)+"\",\"data\":\""+String(last_recieved_ir_code)+"\"}}";
}
String getJson(void){
@ -148,10 +180,10 @@ void view(void){
}
int getDelayTime(void){
if(isParameterDefined(parameter_delay_time_in_ms)){
int delay_time_in_ms = server.arg(parameter_delay_time_in_ms).toInt();
if(isParameterDefined(PARAMETER_PRE_DELAY_TIME_IN_MS)){
int delay_time_in_ms = server.arg(PARAMETER_PRE_DELAY_TIME_IN_MS).toInt();
if(delay_time_in_ms>0){
Serial.println("Applying delay time: " + server.arg(parameter_delay_time_in_ms) + "ms");
Serial.println("Applying delay time: " + server.arg(PARAMETER_PRE_DELAY_TIME_IN_MS) + "ms");
return delay_time_in_ms;
}
}
@ -168,13 +200,13 @@ void handleRequest(void){
//Arduino-Setup
void setup(void)
{
pinMode(pin_pir, INPUT);
pinMode(PIN_PIR, INPUT);
Serial.println("Enable IR-reciever.");
irrecv.enableIRIn();
Serial.println("Enable IR-sender.");
irsend.begin();
Serial.println("Activate active buzzer.");
setSound(1);
makeSound(1);
Serial.begin(9600);
Serial.println("Started program.");
//WiFi.softAPdisconnect(true);
@ -199,7 +231,7 @@ void loop()
{
server.handleClient();
if (irrecv.decode(&results)) {
dump(&results);
setRecievedIr(&results);
irrecv.resume();
}
}