physical-interface/main/main.ino

306 lines
9.6 KiB
Arduino
Raw Normal View History

2020-05-13 12:09:49 +02:00
/**
* Loading classes
*/
2020-05-08 13:41:24 +02:00
#ifndef UNIT_TEST
#include <Arduino.h>
2020-05-08 13:41:24 +02:00
#endif
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#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 <NewRemoteReceiver.h>
2020-05-06 14:51:58 +02:00
#include <DHT.h>
#include "config.h"
2020-05-13 12:09:49 +02:00
/**
* Define constants
*/
2020-05-13 19:46:16 +02:00
const uint16_t PIN_RADIO_TRANSMITTER = D7;
const uint16_t PIN_RADIO_RECIEVER = D8;
const uint16_t PIN_PIR = D1;
const uint16_t PIN_DHT = D6;
const uint16_t PIN_IR_RECIEVER = D4;
const uint16_t PIN_IR_SEND = D2;
2020-05-13 12:09:49 +02:00
const uint16_t PIN_LDR = A0;
const uint16_t PIN_ACTIVE_BUZZER = D5;
2020-05-13 14:41:47 +02:00
const char* PARAMETER_PLUG_ADDRESS="plug_address";
2020-05-13 12:09:49 +02:00
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";
2020-05-13 14:41:47 +02:00
const char* PARAMETER_LIST[]={PARAMETER_PLUG_ADDRESS,PARAMETER_PLUG_ID,PARAMETER_PLUG_STATUS,PARAMETER_IR_TYPE,PARAMETER_IR_CODE,PARAMETER_IR_BITS,PARAMETER_PRE_DELAY_TIME_IN_MS,PARAMETER_SOUND};
2020-05-13 12:09:49 +02:00
/**
* 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;
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 12:09:49 +02:00
IRrecv irrecv(PIN_IR_RECIEVER);
2020-05-13 12:09:49 +02:00
/**
* 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
}
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 14:41:47 +02:00
void sendRemoteGroupSignal(boolean state, NewRemoteTransmitter transmitter)
2020-05-06 14:51:58 +02:00
{
transmitter.sendGroup(state);
2020-05-13 14:41:47 +02:00
Serial.println("The state \"" + String(state,BIN) + "\" was send to the group.");
2020-05-06 14:51:58 +02:00
}
2020-05-13 14:41:47 +02:00
void sendRemoteUnitSignal(int unit, boolean state, NewRemoteTransmitter transmitter)
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) {
Serial.println("Enable IR-sender.");
IRsend irsend(PIN_IR_SEND);
irsend.begin();
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-13 12:09:49 +02:00
/**
* 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){
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";
}
2020-05-13 14:41:47 +02:00
if(parameter==PARAMETER_PLUG_ADDRESS){
return "long";
}
2020-05-13 12:09:49 +02:00
if(
parameter==PARAMETER_SOUND ||
parameter==PARAMETER_PLUG_STATUS
){
return "boolean";
}
return "text";
}
void controller(void){
2020-05-13 19:46:16 +02:00
if(isParameterDefined(PARAMETER_SOUND)){
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-13 12:30:05 +02:00
if(isParameterDefined(PARAMETER_PLUG_ID)){
2020-05-13 14:41:47 +02:00
NewRemoteTransmitter transmitter(server.arg(PARAMETER_PLUG_ADDRESS).toInt(), PIN_RADIO_TRANSMITTER);
if(server.arg(PARAMETER_PLUG_ID).equals("0")){
sendRemoteGroupSignal(server.arg(PARAMETER_PLUG_STATUS).equals("on"),transmitter);
}else if(server.arg(PARAMETER_PLUG_ID).toInt()>0){
sendRemoteUnitSignal(server.arg(PARAMETER_PLUG_ID).toInt(),server.arg(PARAMETER_PLUG_STATUS).equals("on"),transmitter);
}
2020-05-06 17:26:39 +02:00
}
}
2020-05-13 12:09:49 +02:00
/**
* Getter functions
*/
2020-05-11 11:08:54 +02:00
String getJsonDht(void){
Serial.println("Reading DHT...");
DHT dht(PIN_DHT, DHT11);
2020-05-14 15:12:46 +02:00
delay(800); // Somehow this delay is needed to don't get "nan" values
2020-05-13 19:46:16 +02:00
String temperature = String(dht.readTemperature());
2020-05-14 15:12:46 +02:00
delay(800); // Somehow this delay is needed to don't get "nan" values
2020-05-13 19:46:16 +02:00
String humidity = String(dht.readHumidity());
return "{\"temperature_celcius\":\""+ temperature +"\",\"relative_humidity\":\""+humidity+"\"}";
2020-05-11 11:08:54 +02:00
}
String getJsonRadio(void){
Serial.println("Reading radio signal...");
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){
Serial.println("Reading PIR...");
pinMode(PIN_PIR, INPUT);
2020-05-13 19:46:16 +02:00
return "{\"motion\":\""+String((digitalRead(PIN_PIR)==HIGH)?"true":"false")+"\"}";
2020-05-11 11:08:54 +02:00
}
String getJsonSoilMoisture(void){
Serial.println("Reading soil moisture from PIN ...");
String moistures = "[";
int array_elements=(sizeof(PINS_SOIL_MOISTURE) / sizeof(uint16_t));
for (int index=0; index<array_elements; index++)
{
pinMode(PINS_SOIL_MOISTURE[index], INPUT);
moistures = moistures + "{"
"\"pin\":\"" + String(PINS_SOIL_MOISTURE[index]) + "\","
"\"is_moist\":\""+String((digitalRead(PINS_SOIL_MOISTURE[index])==LOW)?"true":"false")+"\""
"}";
if(index<(array_elements-1)){
moistures = moistures + ",";
}
}
return moistures + "]";
}
2020-05-12 11:58:36 +02:00
String getJsonLdr(void){
Serial.println("Reading LDR...");
2020-05-13 19:46:16 +02:00
return "{\"actual\":\""+String(analogRead (PIN_LDR))+"\",\"minimum\":\"0\",\"maximum\":\"1023\"}";
2020-05-12 11:58:36 +02:00
}
String getJsonIr(void){
Serial.println("Reading infared signal...");
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){
Serial.println("Generating json...");
return "{"
"\"LDR\":"+String(getJsonLdr())+","
"\"DHT\":"+String(getJsonDht())+ ","
"\"soil_moisture\":"+String(getJsonSoilMoisture())+","
"\"PIR\":"+String(getJsonPir())+","
"\"IR\":"+String(getJsonIr())+","
"\"radio\":"+String(getJsonRadio())+
"}";
2020-05-11 11:08:54 +02:00
}
#include "homepage_template.h"
void view(void){
2020-05-06 17:26:39 +02:00
if(server.arg("format")=="json"){
Serial.println("Json was called.");
server.send ( 200, "application/json", getJson());
2020-05-06 17:26:39 +02:00
}else{
Serial.println("Html was called.");
server.send ( 200, "text/html ", homepage_template());
2020-05-06 17:26:39 +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;
}
void handleRequest(void){
Serial.println("Website was called.");
2020-05-11 22:47:18 +02:00
delay(getDelayTime());
controller();
view();
2020-05-06 17:26:39 +02:00
}
2020-05-06 14:51:58 +02:00
//Arduino-Setup
void setup(void)
{
Serial.begin(9600);
Serial.println("Enable remote transmitter.");
2020-05-13 14:41:47 +02:00
NewRemoteReceiver::init(PIN_RADIO_RECIEVER, 1, setLastRecievedRadio);
2020-05-11 22:25:42 +02:00
Serial.println("Enable IR-reciever.");
irrecv.enableIRIn();
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.");
2020-05-13 19:46:16 +02:00
Serial.println("Generate test sound.");
switchSound(true);
delay(200);
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
}