physical-interface/main/main.ino

223 lines
6.3 KiB
Arduino
Raw Normal View History

2020-05-08 13:41:24 +02:00
#ifndef UNIT_TEST
#include <Arduino.h>
2020-05-08 13:41:24 +02:00
#endif
// Infared
2020-05-08 13:41:24 +02:00
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRsend.h>
2020-05-08 13:41:24 +02:00
#include <IRutils.h>
// Web
2020-05-06 14:51:58 +02:00
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
// 433 Mhz
2020-05-06 14:51:58 +02:00
#include <NewRemoteTransmitter.h>
// Sensors
2020-05-06 14:51:58 +02:00
#include <DHT.h>
// Configuration
2020-05-06 14:51:58 +02:00
#include "config.h"
2020-05-08 13:56:18 +02:00
// Define Constants
const int pin_ritter = 13;
const int pin_pir = 14;
const int pin_tmp = 12;
const uint16_t pin_ir_reciever = 2;
const uint16_t pin_ir_send = 4;
2020-05-12 11:58:36 +02:00
const uint16_t pin_ldr = A0;
2020-05-08 13:56:18 +02:00
const unsigned long ritter_group_address = 13043702;
2020-05-11 11:55:27 +02:00
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_data";
const char* parameter_ir_bits="ir_bits";
2020-05-11 22:47:18 +02:00
const char* parameter_delay_time_in_ms="delay_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};
2020-05-08 13:41:24 +02:00
2020-05-08 13:56:18 +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;
int last_recieved_ir_data;
int last_recieved_ir_bits;
2020-05-08 13:41:24 +02:00
2020-05-08 13:56:18 +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 );
NewRemoteTransmitter transmitter(ritter_group_address, pin_ritter);
DHT dht(pin_tmp, DHT11);
IRrecv irrecv(pin_ir_reciever);
IRsend irsend(pin_ir_send);
2020-05-08 13:41:24 +02:00
2020-05-11 08:42:01 +02:00
/**
* Associative Arrays aren't possible in C++ because of Memory.
* For this reason it's necessary to implement the function like this.
* If you get the return value "UNKNOWN", adapt the return values to the ones in decode_type_t defined in IRremoteESP8266.h
**/
String getDecodeType(decode_type_t decode_type){
switch(decode_type){
2020-05-08 13:41:24 +02:00
case NEC:
return String("NEC");
case SONY:
return String("SONY");
case RC5:
return String("RC5");
case RC5X:
return String("RC5X");
case RC6:
return String("RC6");
case RCMM:
return String("RCMM");
case PANASONIC:
2020-05-11 08:42:01 +02:00
return String("PANASONIC");
2020-05-08 13:41:24 +02:00
case LG:
return String("LG");
case JVC:
return String("JVC");
case AIWA_RC_T501:
return String("AIWA_RC_T501");
case WHYNTER:
return String("WHYNTER");
}
return String("UNKNOWN");
}
void dump(decode_results *results) {
2020-05-11 12:24:23 +02:00
last_recieved_ir_type = results->decode_type;
last_recieved_ir_data = results->value;
last_recieved_ir_bits = results->bits;
2020-05-11 22:25:42 +02:00
Serial.println(getJsonIrLastRecieved());
2020-05-08 13:41:24 +02:00
}
2020-05-06 14:51:58 +02:00
// Switchs the whole group on
void setRitterGroup(int state)
{
transmitter.sendGroup(state);
Serial.print("The state \"" + String(state,BIN) + "\" was send to the group \"" + String(ritter_group_address,DEC) + "\".");
2020-05-06 14:51:58 +02:00
}
// Switchs one plug on
void setRitterSwitch(int unit, int state)
{
transmitter.sendUnit(unit, state);
Serial.print("The state \"" + String(state,BIN) + "\" was send to the switch \"" + String(unit,DEC) + "\".");
2020-05-06 14:51:58 +02:00
}
2020-05-11 22:25:42 +02:00
void setIrColor(decode_type_t type,uint32_t code, uint16_t bits) {
irsend.send(type, code, bits);
Serial.print("The code \"" + String(code) + "\" with \"" + String(bits) + "\" bits was send in format \"" + getDecodeType(type) + "\".");
}
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;
}
void controller(void){
2020-05-11 11:55:27 +02:00
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());
}
2020-05-11 11:55:27 +02:00
if(isParameterDefined(parameter_plug_id) && isParameterDefined(parameter_plug_status)){
2020-05-12 11:22:30 +02:00
if(server.arg(parameter_plug_id)=="group"){
2020-05-11 11:55:27 +02:00
setRitterGroup(server.arg(parameter_plug_status).toInt());
2020-05-12 11:22:30 +02:00
}else if(server.arg(parameter_plug_id).toInt()>0){
2020-05-11 11:55:27 +02:00
setRitterSwitch(server.arg(parameter_plug_id).toInt(),server.arg(parameter_plug_status).toInt());
}
2020-05-06 17:26:39 +02:00
}
}
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){
return "{\"motion\":\""+String(digitalRead(pin_pir))+"\"}";
}
2020-05-12 11:58:36 +02:00
String getJsonLdr(void){
2020-05-12 18:11:35 +02:00
float volt = 5.0 /1024.0 * analogRead (pin_ldr);
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-11 12:24:23 +02:00
return "{\"last_recieved\":{\"bits\":\""+String(last_recieved_ir_bits)+"\",\"type\":\""+String(last_recieved_ir_type)+"\",\"data\":\""+String(last_recieved_ir_data)+"\"}}";
}
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"
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 22:47:18 +02:00
int getDelayTime(void){
if(isParameterDefined(parameter_delay_time_in_ms)){
int delay_time_in_ms = server.arg(parameter_delay_time_in_ms).toInt();
if(delay_time_in_ms>0){
Serial.println("Applying delay time: " + server.arg(parameter_delay_time_in_ms) + "ms");
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)
{
2020-05-06 17:26:39 +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-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())) {
Serial.println("MDNS responder started.");
}
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)) {
dump(&results);
irrecv.resume();
}
2020-05-06 14:51:58 +02:00
}