Höhe 3,0m | |||||
Breite 5,9m | |||||
Länge 8,9m |
void setup() { | void setup() { | ||||
Serial.begin(115200); | Serial.begin(115200); | ||||
setup_wifi(); | setup_wifi(); | ||||
client.setServer(MQTT_BROKER, 1883); | |||||
client.setServer(MQTT_BROKER, 1883); | |||||
pinMode(ledPin, OUTPUT); | pinMode(ledPin, OUTPUT); | ||||
pinMode(pirPin1, INPUT); | pinMode(pirPin1, INPUT); | ||||
pinMode(pirPin2, INPUT); | pinMode(pirPin2, INPUT); | ||||
} | } | ||||
void reconnect() { | void reconnect() { | ||||
while (!client.connected()) { | |||||
Serial.print("Reconnecting..."); | |||||
if (!client.connect("ESP8266Client")) { | |||||
Serial.print("failed, rc="); | |||||
Serial.print(client.state()); | |||||
Serial.println(" retrying in 5 seconds"); | |||||
delay(5000); | |||||
} | |||||
while (!client.connected()) { | |||||
Serial.print("Reconnecting..."); | |||||
if (!client.connect("ESP8266Client")) { | |||||
Serial.print("failed, rc="); | |||||
Serial.print(client.state()); | |||||
Serial.println(" retrying in 5 seconds"); | |||||
delay(5000); | |||||
} | } | ||||
} | |||||
} | } | ||||
void loop() { | void loop() { | ||||
if (!client.connected()) { | |||||
reconnect(); | |||||
} | |||||
client.loop(); | |||||
if (!client.connected()) { | |||||
reconnect(); | |||||
} | |||||
client.loop(); | |||||
pirValueNeu1 = digitalRead(pirPin1); | pirValueNeu1 = digitalRead(pirPin1); | ||||
pirValueNeu2 = digitalRead(pirPin2); | pirValueNeu2 = digitalRead(pirPin2); | ||||
digitalWrite(ledPin,(pirValueNeu1|pirValueNeu2)); | |||||
pirAn1= ((pirValueNeu1==1)&&(pirValueAlt1==0)); | |||||
pirAn2= ((pirValueNeu2==1)&&(pirValueAlt2==0)); | |||||
pirAus1= ((pirValueNeu1==0)&&(pirValueAlt1==1)); | |||||
pirAus2= ((pirValueNeu2==0)&&(pirValueAlt2==1)); | |||||
if(pirAn1){ | |||||
Serial.println("Steigende Flanke von PIR1"); | |||||
snprintf (msg,50,"%c%c",49,48); //Codierung PIR1 steigende Flanke: 10 | |||||
digitalWrite(ledPin, (pirValueNeu1 | pirValueNeu2)); | |||||
pirAn1 = ((pirValueNeu1 == 1) && (pirValueAlt1 == 0)); | |||||
pirAn2 = ((pirValueNeu2 == 1) && (pirValueAlt2 == 0)); | |||||
pirAus1 = ((pirValueNeu1 == 0) && (pirValueAlt1 == 1)); | |||||
pirAus2 = ((pirValueNeu2 == 0) && (pirValueAlt2 == 1)); | |||||
if (pirAn1) { | |||||
Serial.println("Steigende Flanke von PIR1"); | |||||
snprintf (msg, 50, "%c%c", 49, 48); //Codierung PIR1 steigende Flanke: 10 | |||||
Serial.print("Publish Motion: "); | Serial.print("Publish Motion: "); | ||||
Serial.println(msg); | Serial.println(msg); | ||||
client.publish("/home/data", msg); | |||||
client.publish("/gso/bb/104/Pir/1", msg); | |||||
} | } | ||||
if(pirAn2){ | |||||
Serial.println("Steigende Flanke von PIR2"); | |||||
snprintf (msg,50,"%c%c",49,49); //Codierung PIR2 steigende Flanke: 11 | |||||
if (pirAn2) { | |||||
Serial.println("Steigende Flanke von PIR2"); | |||||
snprintf (msg, 50, "%c%c", 49, 49); //Codierung PIR2 steigende Flanke: 11 | |||||
Serial.print("Publish Motion: "); | Serial.print("Publish Motion: "); | ||||
Serial.println(msg); | Serial.println(msg); | ||||
client.publish("/home/data", msg); | |||||
client.publish("/gso/bb/104/Pir/2", msg); | |||||
} | } | ||||
if(pirAus1){ | |||||
Serial.println("Fallende Flanke von PIR1"); | |||||
snprintf (msg,50,"%c%c",48,48); //Codierung PIR1 fallende Flanke: 00 | |||||
if (pirAus1) { | |||||
Serial.println("Fallende Flanke von PIR1"); | |||||
snprintf (msg, 50, "%c%c", 48, 48); //Codierung PIR1 fallende Flanke: 00 | |||||
Serial.print("Publish Motion: "); | Serial.print("Publish Motion: "); | ||||
Serial.println(msg); | Serial.println(msg); | ||||
client.publish("/home/data", msg); | |||||
client.publish("/gso/bb/104/Pir/1", msg); | |||||
} | } | ||||
if(pirAus2){ | |||||
Serial.println("Fallende Flanke von PIR2"); | |||||
snprintf (msg,50,"%c%c",48,49); //Codierung PIR2 fallende Flanke: 01 | |||||
if (pirAus2) { | |||||
Serial.println("Fallende Flanke von PIR2"); | |||||
snprintf (msg, 50, "%c%c", 48, 49); //Codierung PIR2 fallende Flanke: 01 | |||||
Serial.print("Publish Motion: "); | Serial.print("Publish Motion: "); | ||||
Serial.println(msg); | Serial.println(msg); | ||||
client.publish("/home/data", msg); | |||||
client.publish("/gso/bb/104/Pir/2", msg); | |||||
} | } | ||||
pirValueAlt1=pirValueNeu1; | |||||
pirValueAlt2=pirValueNeu2; | |||||
} | |||||
pirValueAlt1 = pirValueNeu1; | |||||
pirValueAlt2 = pirValueNeu2; | |||||
} |
#include <PubSubClient.h> | #include <PubSubClient.h> | ||||
//Eigene zu trackende Entfernung festlegen | //Eigene zu trackende Entfernung festlegen | ||||
#define DISTANCE 15 | |||||
#define DISTANCE 155 | |||||
const char* SSID = "smartroom"; | const char* SSID = "smartroom"; | ||||
const char* PSK = "smarthome"; | const char* PSK = "smarthome"; | ||||
const char* MQTT_BROKER = "192.168.4.1"; | |||||
const char* MQTT_BROKER = "192.168.252.1"; | |||||
WiFiClient espClient; | WiFiClient espClient; | ||||
PubSubClient client(espClient); | PubSubClient client(espClient); | ||||
// Calculating the distance | // Calculating the distance | ||||
distance = duration * 0.034 / 2; | distance = duration * 0.034 / 2; | ||||
// Prints the distance on the Serial Monitor | // Prints the distance on the Serial Monitor | ||||
/* if ((distance != (distance_alt - 1)) && (distance != (distance_alt)) && (distance != (distance_alt + 1))) { //+-1 um störungen herauszufiltern | |||||
snprintf (msg, 50, "%d", distance); | |||||
Serial.print("Publish Motion: "); | |||||
Serial.println(msg); | |||||
client.publish("/home/data", msg); | |||||
delay(200); | |||||
} */ | |||||
/* if ((distance != (distance_alt - 1)) && (distance != (distance_alt)) && (distance != (distance_alt + 1))) { //+-1 um störungen herauszufiltern | |||||
snprintf (msg, 50, "%d", distance); | |||||
Serial.print("Publish Motion: "); | |||||
Serial.println(msg); | |||||
client.publish("/home/data", msg); | |||||
delay(200); | |||||
} */ | |||||
if (bool1 == 0) { | if (bool1 == 0) { | ||||
//alternativ: if ((distance != (DISTANCE - 1)) && (distance != (DISTANCE)) && (distance != (DISTANCE + 1))) { //+-1 | //alternativ: if ((distance != (DISTANCE - 1)) && (distance != (DISTANCE)) && (distance != (DISTANCE + 1))) { //+-1 | ||||
if (((DISTANCE + 2) < distance) || ((DISTANCE - 2) > distance)) { //darf +- 2 um festgelegte entfernung schwanken, um störungen herauszufiltern | if (((DISTANCE + 2) < distance) || ((DISTANCE - 2) > distance)) { //darf +- 2 um festgelegte entfernung schwanken, um störungen herauszufiltern | ||||
//Meldung an PI, dass die Distanz gestört ist | //Meldung an PI, dass die Distanz gestört ist | ||||
snprintf (msg, 50, "%d", 1); | snprintf (msg, 50, "%d", 1); | ||||
client.publish("/home/data", msg); | |||||
client.publish("/gso/bb/104/ultraschall/1", msg); | |||||
//Serieller Monitor | //Serieller Monitor | ||||
Serial.print("Motion detected! Distance: "); | Serial.print("Motion detected! Distance: "); | ||||
Serial.println(msg); | |||||
Serial.println(distance); | |||||
//Flag auf 1 | //Flag auf 1 | ||||
bool1 = 1; | bool1 = 1; | ||||
} | |||||
else { | |||||
snprintf (msg, 50, "%d", 0); | |||||
client.publish("/gso/bb/104/ultraschall/1", msg); | |||||
} | } | ||||
} | } | ||||
else if (bool1 == 1) { | else if (bool1 == 1) { | ||||
if (((DISTANCE + 2) < distance) || ((DISTANCE - 2) > distance)) { //darf +- 2 um festgelegte entfernung schwanken, um störungen herauszufiltern | if (((DISTANCE + 2) < distance) || ((DISTANCE - 2) > distance)) { //darf +- 2 um festgelegte entfernung schwanken, um störungen herauszufiltern | ||||
Serial.print("Still motion detected! Distance: "); | Serial.print("Still motion detected! Distance: "); | ||||
Serial.println(distance); | Serial.println(distance); | ||||
//Meldung an PI, dass die Ausgangsdistanz wieder gemessen wird | |||||
snprintf (msg, 50, "%d", 1); | |||||
client.publish("/gso/bb/104/ultraschall/1", msg); | |||||
} | } | ||||
else { | else { | ||||
//Meldung an PI, dass die Ausgangsdistanz wieder gemessen wird | //Meldung an PI, dass die Ausgangsdistanz wieder gemessen wird | ||||
snprintf (msg, 50, "%d", 0); | snprintf (msg, 50, "%d", 0); | ||||
client.publish("/home/data", msg); | |||||
client.publish("/gso/bb/104/ultraschall/1", msg); | |||||
//Flag wieder auf 0 | //Flag wieder auf 0 | ||||
bool1 = 0; | bool1 = 0; | ||||