Saved development process

This commit is contained in:
2020-05-06 17:26:39 +02:00
parent 4758f80d83
commit c7c9b3f6b5
5 changed files with 79 additions and 28 deletions

44
main/config.h.dist Normal file
View File

@@ -0,0 +1,44 @@
const String titel = "Physical Interface";
// Setup for wifi connection.
const char *ssid = ""; //SSID
const char *password = ""; //Wlan-Passwort
const String homepage =
"<!DOCTYPE html>"
"<html>"
"<head>"
"<title>"+String(titel)+"</title>"
"<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css\">"
"<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js\"></script>"
"<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js\"></script>"
"</head>"
"<body>"
"<div class=\"container\">"
"<h1>"+String(titel)+"</h1>"
"<p>An REST-API Arduino microcontroller, which allows you to interact with hardware and the physical environment.</p>"
"<h2>Actors</h2>"
"<h3>Remote Plug Interface</h3>"
"<form method=\"get\" >"
"<div class=\"form-group\">"
"<label for=\"plug_id\">Plug-ID:</label>"
"<input type=\"number\" class= \"form-control\" id=\"plug_id\" name=\"plug_id\">"
"</div>"
"<div class=\"form-group\">"
"<label class=\"checkbox-inline\" for=\"on\">"
"<input type=\"radio\" id=\"on\" name=\"status\" value=\"1\">"
"on"
"</label>"
"<label class=\"checkbox-inline\" for=\"off\">"
"<input type=\"radio\" id=\"off\" name=\"status\" value=\"0\">"
"off"
"</label>"
"</div>"
"<input type=\"submit\" class=\"btn btn-secondary\">"
"</form>"
"<h2>Sensors</h2>"
"<p>The sensor datas can be reached <a href=\"?format=json\">here</a>.</p>"
"<hr />"
"<p><small>Please check out the <a href=\"https://github.com/kevinveenbirkenbach/physical-interface\">git-repository</a> to get more information about this software.</small></p>"
"</div>"
"<body>"
"<html>";

96
main/main.ino Normal file
View File

@@ -0,0 +1,96 @@
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <NewRemoteTransmitter.h>
#include <DHT.h>
#include "config.h"
// Setup pins
const int pin_ritter = 13;
const int pin_pir = 14;
const int pin_tmp = 12;
const unsigned long ritter_group_address = 13043702;
// Setup classes
ESP8266WebServer server ( 80 );
NewRemoteTransmitter transmitter(ritter_group_address, pin_ritter);
DHT dht(pin_tmp, DHT11);
// 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){
return "{\"DHT\":"+String(getJsonDht())+",\"PIR\":"+String(getJsonPir())+"}";
}
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);
}
//Arduino-Setup
void setup(void)
{
pinMode(pin_pir, INPUT);
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()
{
server.handleClient();
}