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>
2020-05-13 14:08:08 +02:00
# include <NewRemoteReceiver.h>
2020-05-06 14:51:58 +02:00
# include <DHT.h>
# include "config.h"
2020-05-11 09:35:42 +02:00
2020-05-13 12:09:49 +02:00
/**
* Define constants
*/
2020-05-13 14:08:08 +02:00
const uint16_t PIN_REMOTE_RECIEVER = 13 ;
const uint16_t PIN_REMOTE_TRANSMITTER = 15 ;
2020-05-13 12:09:49 +02:00
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-13 14:08:08 +02:00
unsigned int last_recieved_radio_period ;
unsigned long last_recieved_radio_address ;
unsigned long last_recieved_radio_groupBit ;
unsigned long last_recieved_radio_unit ;
unsigned long last_recieved_radio_switchType ;
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 14:08:08 +02:00
NewRemoteTransmitter transmitter ( RITTER_STANDART_GROUP_ADDRESS , PIN_REMOTE_RECIEVER ) ;
2020-05-13 12:09:49 +02:00
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 14:08:08 +02:00
void setLastRecievedRadio ( unsigned int period , unsigned long address , unsigned long groupBit , unsigned long unit , unsigned long switchType ) {
last_recieved_radio_period = period ;
last_recieved_radio_address = address ;
last_recieved_radio_groupBit = groupBit ;
last_recieved_radio_unit = unit ;
last_recieved_radio_switchType = switchType ;
Serial . println ( " Recieved radio code with the following values: address \" " + String ( address ) + " \" , period \" " + String ( period ) + " \" , unit \" " + String ( unit ) + " \" , group bit \" " + String ( groupBit ) + " \" and switch type \" " + String ( switchType ) + " \" . " ) ;
}
2020-05-13 12:09:49 +02:00
/**
* Transmitter functions
*/
2020-05-13 12:30:05 +02:00
void sendRemoteGroupSignal ( boolean 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:30:05 +02:00
void sendRemoteUnitSignal ( int unit , boolean 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
*/
2020-05-13 12:30:05 +02:00
void switchSound ( boolean status ) {
Serial . println ( " Switching sound \" " + String ( ( status ) ? ( " on " ) : ( " off " ) ) + " . " ) ;
if ( status ) {
pinMode ( PIN_ACTIVE_BUZZER , OUTPUT ) ;
digitalWrite ( PIN_ACTIVE_BUZZER , LOW ) ;
} else {
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:30:05 +02:00
switchSound ( server . arg ( PARAMETER_SOUND ) . equals ( " on " ) ) ;
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:30:05 +02:00
if ( isParameterDefined ( PARAMETER_PLUG_ID ) ) {
if ( server . arg ( PARAMETER_PLUG_ID ) . equals ( " 0 " ) ) {
sendRemoteGroupSignal ( server . arg ( PARAMETER_PLUG_STATUS ) . equals ( " on " ) ) ;
2020-05-13 12:09:49 +02:00
} else if ( server . arg ( PARAMETER_PLUG_ID ) . toInt ( ) > 0 ) {
2020-05-13 12:30:05 +02:00
sendRemoteUnitSignal ( server . arg ( PARAMETER_PLUG_ID ) . toInt ( ) , server . arg ( PARAMETER_PLUG_STATUS ) . equals ( " on " ) ) ;
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
}
2020-05-13 14:08:08 +02:00
String getJsonRadio ( void ) {
return " { \" last_recieved \" :{ \" period \" : \" " + String ( last_recieved_radio_period ) + " \" , \" address \" : \" " + String ( last_recieved_radio_address ) + " \" , \" group_bit \" : \" " + String ( last_recieved_radio_groupBit ) + " \" , \" unit \" : \" " + String ( last_recieved_radio_unit ) + " \" , \" switch_type \" : \" " + String ( last_recieved_radio_switchType ) + " \" }} " ;
}
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-13 14:08:08 +02:00
String getJsonIr ( 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-13 14:08:08 +02:00
return " { \" LDR \" : " + String ( getJsonLdr ( ) ) + " , \" DHT \" : " + String ( getJsonDht ( ) ) + " , \" PIR \" : " + String ( getJsonPir ( ) ) + " , \" IR \" : " + String ( getJsonIr ( ) ) + " , \" radio \" : " + String ( getJsonRadio ( ) ) + " } " ;
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 14:08:08 +02:00
Serial . begin ( 9600 ) ;
Serial . println ( " Activate active buzzer. " ) ;
switchSound ( true ) ;
Serial . println ( " Enable PIR. " ) ;
2020-05-13 12:09:49 +02:00
pinMode ( PIN_PIR , INPUT ) ;
2020-05-13 14:08:08 +02:00
Serial . println ( " Enable remote transmitter. " ) ;
NewRemoteReceiver : : init ( PIN_REMOTE_TRANSMITTER , 1 , setLastRecievedRadio ) ;
2020-05-11 22:25:42 +02:00
Serial . println ( " Enable IR-reciever. " ) ;
irrecv . enableIRIn ( ) ;
Serial . println ( " Enable IR-sender. " ) ;
irsend . begin ( ) ;
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 12:53:22 +02:00
Serial . println ( " MDNS responder started. " ) ;
Serial . println ( " 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 ) ;
2020-05-13 12:30:05 +02:00
switchSound ( false ) ;
2020-05-06 14:51:58 +02:00
}
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
}