2020-05-13 12:09:49 +02:00
/**
* Loading classes
*/
2020-05-08 13:41:24 +02:00
# ifndef UNIT_TEST
2020-05-11 09:35:42 +02:00
# include <Arduino.h>
2020-05-08 13:41:24 +02:00
# endif
# include <IRremoteESP8266.h>
# include <IRrecv.h>
2020-05-11 09:35:42 +02:00
# include <IRsend.h>
2020-05-08 13:41:24 +02:00
# include <IRutils.h>
2020-05-06 14:51:58 +02:00
# include <ESP8266WiFi.h>
# include <WiFiClient.h>
# include <ESP8266WebServer.h>
# include <ESP8266mDNS.h>
# include <NewRemoteTransmitter.h>
# include <DHT.h>
# include "config.h"
2020-05-11 09:35:42 +02:00
2020-05-13 12:09:49 +02:00
/**
* 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
*/
2020-05-08 13:41:24 +02:00
decode_results results ;
2020-05-11 12:24:23 +02:00
decode_type_t last_recieved_ir_type ;
2020-05-13 12:09:49 +02:00
int last_recieved_ir_code ;
2020-05-11 12:24:23 +02:00
int last_recieved_ir_bits ;
2020-05-08 13:41:24 +02:00
2020-05-13 12:09:49 +02:00
/**
* Setup classes
*/
2020-05-11 22:25:42 +02:00
MDNSResponder mdns ;
2020-05-08 13:56:18 +02:00
ESP8266WebServer server ( 80 ) ;
2020-05-13 12:09:49 +02:00
NewRemoteTransmitter transmitter ( RITTER_STANDART_GROUP_ADDRESS , PIN_RITTER ) ;
DHT dht ( PIN_DHT , DHT11 ) ;
IRrecv irrecv ( PIN_IR_RECIEVER ) ;
IRsend irsend ( PIN_IR_SEND ) ;
/**
* Setter functions
*/
void setRecievedIr ( decode_results * results ) {
2020-05-11 12:24:23 +02:00
last_recieved_ir_type = results - > decode_type ;
2020-05-13 12:09:49 +02:00
last_recieved_ir_code = results - > value ;
2020-05-11 12:24:23 +02:00
last_recieved_ir_bits = results - > bits ;
2020-05-13 12:09:49 +02:00
Serial . println ( " The code \" " + String ( last_recieved_ir_code ) + " \" was send for type \" " + String ( results - > decode_type , DEC ) + " \" with \" " + String ( results - > bits , DEC ) + " \" bits. " ) ;
2020-05-08 13:41:24 +02:00
}
2020-05-13 12:09:49 +02:00
/**
* Transmitter functions
*/
void sendRemoteGroupSignal ( int state )
2020-05-06 14:51:58 +02:00
{
transmitter . sendGroup ( state ) ;
2020-05-13 12:09:49 +02:00
Serial . println ( " The state \" " + String ( state , BIN ) + " \" was send to the group \" " + String ( RITTER_STANDART_GROUP_ADDRESS , DEC ) + " \" . " ) ;
2020-05-06 14:51:58 +02:00
}
2020-05-13 12:09:49 +02:00
void sendRemoteUnitSignal ( int unit , int state )
2020-05-06 14:51:58 +02:00
{
transmitter . sendUnit ( unit , state ) ;
2020-05-13 09:00:08 +02:00
Serial . println ( " The state \" " + String ( state , BIN ) + " \" was send to the switch \" " + String ( unit , DEC ) + " \" . " ) ;
2020-05-06 14:51:58 +02:00
}
2020-05-13 12:09:49 +02:00
void sendIrCode ( decode_type_t type , uint32_t code , uint16_t bits ) {
2020-05-11 22:25:42 +02:00
irsend . send ( type , code , bits ) ;
2020-05-13 09:00:08 +02:00
Serial . println ( " The code \" " + String ( code ) + " \" with \" " + String ( bits ) + " \" bits was send in format \" " + String ( type ) + " \" . " ) ;
}
2020-05-13 12:09:49 +02:00
/**
* Actors
*/
void makeSound ( int time_in_ms ) {
2020-05-13 09:00:08 +02:00
Serial . println ( " Making sound for \" " + String ( time_in_ms ) + " ms. " ) ;
2020-05-13 12:09:49 +02:00
pinMode ( PIN_ACTIVE_BUZZER , OUTPUT ) ;
digitalWrite ( PIN_ACTIVE_BUZZER , LOW ) ;
2020-05-13 09:00:08 +02:00
delay ( time_in_ms ) ;
2020-05-13 12:09:49 +02:00
digitalWrite ( PIN_ACTIVE_BUZZER , HIGH ) ;
pinMode ( PIN_ACTIVE_BUZZER , INPUT ) ;
2020-05-11 09:35:42 +02:00
}
2020-05-13 12:09:49 +02:00
/**
* Helper functions
*/
2020-05-11 09:35:42 +02:00
bool isParameterDefined ( String parameter_name ) {
for ( uint8_t parameter_index = 0 ; parameter_index < server . args ( ) ; parameter_index + + ) {
if ( server . argName ( parameter_index ) = = parameter_name ) {
return true ;
}
}
return false ;
}
2020-05-13 12:09:49 +02:00
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 " ;
}
2020-05-11 10:23:52 +02:00
void controller ( void ) {
2020-05-13 12:09:49 +02:00
if ( isParameterDefined ( PARAMETER_SOUND ) ) {
makeSound ( server . arg ( PARAMETER_SOUND ) . toInt ( ) ) ;
2020-05-13 09:00:08 +02:00
}
2020-05-13 12:09:49 +02:00
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 ( ) ) ;
2020-05-11 10:23:52 +02:00
}
2020-05-13 12:09:49 +02:00
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 ( ) ) ;
2020-05-11 10:23:52 +02:00
}
2020-05-06 17:26:39 +02:00
}
2020-05-11 10:23:52 +02:00
}
2020-05-13 12:09:49 +02:00
/**
* Getter functions
*/
2020-05-11 11:08:54 +02:00
String getJsonDht ( void ) {
2020-05-12 11:58:36 +02:00
return " { \" temperature_celcius \" : \" " + String ( dht . readTemperature ( ) ) + " \" , \" humidity \" : \" " + String ( dht . readHumidity ( ) ) + " \" } " ;
2020-05-11 11:08:54 +02:00
}
String getJsonPir ( void ) {
2020-05-13 12:09:49 +02:00
return " { \" motion \" : \" " + String ( digitalRead ( PIN_PIR ) ) + " \" } " ;
2020-05-11 11:08:54 +02:00
}
2020-05-12 11:58:36 +02:00
String getJsonLdr ( void ) {
2020-05-13 12:09:49 +02:00
float volt = 5.0 / 1024.0 * analogRead ( PIN_LDR ) ;
2020-05-12 18:11:35 +02:00
return " { \" input_volt \" : \" " + String ( volt ) + " \" } " ;
2020-05-12 11:58:36 +02:00
}
2020-05-11 22:25:42 +02:00
String getJsonIrLastRecieved ( void ) {
2020-05-13 12:09:49 +02:00
return " { \" last_recieved \" :{ \" bits \" : \" " + String ( last_recieved_ir_bits ) + " \" , \" type \" : \" " + String ( last_recieved_ir_type ) + " \" , \" data \" : \" " + String ( last_recieved_ir_code ) + " \" }} " ;
2020-05-11 12:24:23 +02:00
}
2020-05-11 11:08:54 +02:00
String getJson ( void ) {
2020-05-12 11:58:36 +02:00
return " { \" LDR \" : " + String ( getJsonLdr ( ) ) + " , \" DHT \" : " + String ( getJsonDht ( ) ) + " , \" PIR \" : " + String ( getJsonPir ( ) ) + " , \" IR \" : " + String ( getJsonIrLastRecieved ( ) ) + " } " ;
2020-05-11 11:08:54 +02:00
}
# include "homepage_template.h"
2020-05-11 10:23:52 +02:00
void view ( void ) {
2020-05-06 17:26:39 +02:00
if ( server . arg ( " format " ) = = " json " ) {
server . send ( 200 , " text/html " , getJson ( ) ) ;
} else {
2020-05-08 14:08:27 +02:00
server . send ( 200 , " text/html " , homepage_template ( ) ) ;
2020-05-06 17:26:39 +02:00
}
2020-05-11 10:23:52 +02:00
}
2020-05-11 22:47:18 +02:00
int getDelayTime ( void ) {
2020-05-13 12:09:49 +02:00
if ( isParameterDefined ( PARAMETER_PRE_DELAY_TIME_IN_MS ) ) {
int delay_time_in_ms = server . arg ( PARAMETER_PRE_DELAY_TIME_IN_MS ) . toInt ( ) ;
2020-05-11 22:47:18 +02:00
if ( delay_time_in_ms > 0 ) {
2020-05-13 12:09:49 +02:00
Serial . println ( " Applying delay time: " + server . arg ( PARAMETER_PRE_DELAY_TIME_IN_MS ) + " ms " ) ;
2020-05-11 22:47:18 +02:00
return delay_time_in_ms ;
}
}
return 0 ;
}
2020-05-11 10:23:52 +02:00
void handleRequest ( void ) {
Serial . println ( " Website was called. " ) ;
2020-05-11 22:47:18 +02:00
delay ( getDelayTime ( ) ) ;
2020-05-11 10:23:52 +02:00
controller ( ) ;
view ( ) ;
2020-05-06 17:26:39 +02:00
}
2020-05-06 14:51:58 +02:00
//Arduino-Setup
void setup ( void )
{
2020-05-13 12:09:49 +02:00
pinMode ( PIN_PIR , INPUT ) ;
2020-05-11 22:25:42 +02:00
Serial . println ( " Enable IR-reciever. " ) ;
irrecv . enableIRIn ( ) ;
Serial . println ( " Enable IR-sender. " ) ;
irsend . begin ( ) ;
2020-05-13 09:00:08 +02:00
Serial . println ( " Activate active buzzer. " ) ;
2020-05-13 12:09:49 +02:00
makeSound ( 1 ) ;
2020-05-08 13:56:18 +02:00
Serial . begin ( 9600 ) ;
2020-05-06 14:51:58 +02:00
Serial . println ( " Started program. " ) ;
//WiFi.softAPdisconnect(true);
WiFi . begin ( ssid , password ) ;
while ( WiFi . status ( ) ! = WL_CONNECTED ) {
Serial . print ( " . " ) ;
delay ( 500 ) ;
}
2020-05-11 12:24:23 +02:00
Serial . println ( " Connected to : " + String ( ssid ) ) ;
2020-05-12 11:22:30 +02:00
Serial . print ( " IP address: " ) ;
Serial . println ( WiFi . localIP ( ) ) ;
2020-05-11 22:25:42 +02:00
if ( mdns . begin ( hostname , WiFi . localIP ( ) ) ) {
2020-05-13 09:00:08 +02:00
Serial . println ( " MDNS responder started. Using \" " + String ( hostname ) + " \" as hostname. " ) ;
2020-05-11 22:25:42 +02:00
}
2020-05-06 14:51:58 +02:00
server . onNotFound ( handleRequest ) ;
server . begin ( ) ;
Serial . println ( " HTTP server started. " ) ;
delay ( 1000 ) ;
}
void loop ( )
{
2020-05-06 17:26:39 +02:00
server . handleClient ( ) ;
2020-05-08 13:56:18 +02:00
if ( irrecv . decode ( & results ) ) {
2020-05-13 12:09:49 +02:00
setRecievedIr ( & results ) ;
2020-05-08 13:56:18 +02:00
irrecv . resume ( ) ;
}
2020-05-06 14:51:58 +02:00
}