physical-interface/main/main.ino

210 lines
5.5 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-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-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-08 13:56:18 +02:00
// Setup classes
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) {
uint16_t count = results->rawlen;
2020-05-11 08:42:01 +02:00
Serial.print(getDecodeType(results->decode_type));
2020-05-08 13:41:24 +02:00
serialPrintUint64(results->value, 16);
Serial.print(" (");
Serial.print(results->bits, DEC);
Serial.println(" bits)");
Serial.print("Raw (");
Serial.print(count);
Serial.print("): ");
for (uint16_t i = 1; i < count; i++) {
if (i % 100 == 0)
yield(); // Preemptive yield every 100th entry to feed the WDT.
if (i & 1) {
Serial.print(results->rawbuf[i] * RAWTICK, DEC);
} else {
Serial.write('-');
Serial.print((uint32_t) results->rawbuf[i] * RAWTICK, DEC);
}
Serial.print(" ");
}
Serial.println();
}
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
}
void setIrColor(decode_type_t type,int data, int bits) {
// irsend.send(type, data, bits);
2020-05-11 11:55:27 +02:00
Serial.print("The code \"" + String(data) + "\" with \"" + String(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)){
if(server.arg(parameter_plug_id)=="group"){
setRitterGroup(server.arg(parameter_plug_status).toInt());
}else{
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){
return "{\"temperature\":\""+String(dht.readTemperature())+"\",\"humidity\":\""+String(dht.readHumidity())+"\"}";
}
String getJsonPir(void){
return "{\"motion\":\""+String(digitalRead(pin_pir))+"\"}";
}
String getJson(void){
return "{\"DHT\":"+String(getJsonDht())+",\"PIR\":"+String(getJsonPir())+"}";
}
#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
}
}
void handleRequest(void){
Serial.println("Website was called.");
controller();
view();
2020-05-06 17:26:39 +02:00
delay(100);
}
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-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);
}
Serial.print("Connected to :");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.onNotFound(handleRequest);
server.begin();
Serial.println("HTTP server started.");
2020-05-08 13:56:18 +02:00
Serial.println("Enable IR-Reciever.");
irrecv.enableIRIn();
2020-05-06 14:51:58 +02:00
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
}