physical-interface/main/main.ino

98 lines
2.3 KiB
Arduino
Raw Normal View History

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>
2020-05-07 10:32:52 +02:00
#include <IRremote.h>
2020-05-06 14:51:58 +02:00
#include "config.h"
2020-05-06 17:26:39 +02:00
// Setup pins
2020-05-06 14:51:58 +02:00
const int pin_ritter = 13;
2020-05-06 17:26:39 +02:00
const int pin_pir = 14;
const int pin_tmp = 12;
2020-05-06 14:51:58 +02:00
const unsigned long ritter_group_address = 13043702;
2020-05-06 17:26:39 +02:00
// Setup classes
ESP8266WebServer server ( 80 );
2020-05-06 14:51:58 +02:00
NewRemoteTransmitter transmitter(ritter_group_address, pin_ritter);
2020-05-06 17:26:39 +02:00
DHT dht(pin_tmp, DHT11);
2020-05-06 14:51:58 +02:00
// Switchs the whole group on
void setRitterGroup(int state)
{
transmitter.sendGroup(state);
Serial.print("The state \"");
Serial.print(state);
Serial.print("\" was send to the group \"");
Serial.print(ritter_group_address);
Serial.println("\".");
}
// Switchs one plug on
void setRitterSwitch(int unit, int state)
{
transmitter.sendUnit(unit, state);
Serial.print("The state \"");
Serial.print(state);
Serial.print("\" was send to the switch \"");
Serial.print(unit);
Serial.println("\".");
}
String getJsonDht(void){
return "{\"temperature\":\""+String(dht.readTemperature())+"\",\"humidity\":\""+String(dht.readHumidity())+"\"}";
}
String getJsonPir(void){
return "{\"motion\":\""+String(digitalRead(pin_pir))+"\"";
}
String getJson(void){
2020-05-06 17:26:39 +02:00
return "{\"DHT\":"+String(getJsonDht())+",\"PIR\":"+String(getJsonPir())+"}";
2020-05-06 14:51:58 +02:00
}
2020-05-06 17:26:39 +02:00
void handleRequest(void){
if(server.arg("plug_id") && server.arg("status")){
if(server.arg("plug_id")=="group"){
setRitterGroup(server.arg("status").toInt());
}else{
setRitterSwitch(server.arg("plug_id").toInt(),server.arg("status").toInt());
}
}
if(server.arg("format")=="json"){
server.send ( 200, "text/html", getJson());
}else{
server.send ( 200, "text/html", homepage);
}
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-06 14:51:58 +02:00
Serial.begin(115200);
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.");
delay(1000);
}
void loop()
{
2020-05-06 17:26:39 +02:00
server.handleClient();
2020-05-06 14:51:58 +02:00
}