From c0b5c2edd79badaf6ef9c4a899295b88c942665e Mon Sep 17 00:00:00 2001 From: "Kevin Veen-Birkenbach [aka. Frantz]" Date: Wed, 13 May 2020 12:09:49 +0200 Subject: [PATCH] Optimized template --- main/homepage_template.h | 35 ++++++--- main/main.ino | 166 +++++++++++++++++++++++---------------- 2 files changed, 125 insertions(+), 76 deletions(-) diff --git a/main/homepage_template.h b/main/homepage_template.h index 829f541..ac455c0 100644 --- a/main/homepage_template.h +++ b/main/homepage_template.h @@ -6,11 +6,14 @@ String homepage_template(void){ "" "" ""+String(titel)+"("+ String(hostname) +")" - "" - "" - "" + "" + "" + "" + "" "" "" + "" + "" "" "" "
" @@ -18,12 +21,26 @@ String homepage_template(void){ "

An REST-API Arduino microcontroller, which allows you to interact with hardware and the physical environment.

" "

Controller

" "
"; - for(const char* parameter : parameter_list) - homepage = homepage + - "
" - "" - "" - "
"; + for(const char* parameter : PARAMETER_LIST){ + String parameter_type=getParameterType(parameter); + if(parameter_type.equals("boolean")){ + homepage = homepage + + "
" + "" + "
" + "" + "
" + "
"; + }else{ + homepage = homepage + + "
" + "" + "
" + "" + "
" + "
"; + } + } homepage = homepage + "" "
" diff --git a/main/main.ino b/main/main.ino index 388689b..9dddb46 100644 --- a/main/main.ino +++ b/main/main.ino @@ -1,95 +1,104 @@ +/** + * Loading classes + */ #ifndef UNIT_TEST #include #endif - -// Infared #include #include #include #include - -// Web #include #include #include #include - -// 433 Mhz #include - -// Sensors #include - -// 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(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(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(); } }