Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2Ultrasonic_Sensor_an_rpi.ino 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. //Eigene zu trackende Entfernung festlegen
  4. #define DISTANCE 155
  5. const char* SSID = "smartroom";
  6. const char* PSK = "smarthome";
  7. const char* MQTT_BROKER = "192.168.252.1";
  8. WiFiClient espClient;
  9. PubSubClient client(espClient);
  10. // defines pins numbers
  11. const int trigPin = D3; //D4
  12. const int echoPin1 = D4; //D3
  13. const int echoPin2 = D2; //D2
  14. long duration1;
  15. long duration2;
  16. int distance1;
  17. int distance2;
  18. int bool1 = 0;
  19. int bool2 = 0;
  20. char msg[50];
  21. int value = 0;
  22. long lastMsg = 0;
  23. void setup() {
  24. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  25. pinMode(echoPin1, INPUT); // Sets the echoPin as an Input
  26. pinMode(echoPin2, INPUT); // Sets the echoPin as an Input
  27. Serial.begin(115200);
  28. setup_wifi();
  29. client.setServer(MQTT_BROKER, 1883);
  30. }
  31. void setup_wifi() {
  32. delay(10);
  33. Serial.println();
  34. Serial.print("Connecting to ");
  35. Serial.println(SSID);
  36. WiFi.mode(WIFI_STA);
  37. WiFi.begin(SSID, PSK);
  38. while (WiFi.status() != WL_CONNECTED) {
  39. delay(500);
  40. Serial.print(".");
  41. }
  42. Serial.println("");
  43. Serial.println("WiFi connected");
  44. Serial.println("IP address: ");
  45. Serial.println(WiFi.localIP());
  46. }
  47. void reconnect() {
  48. while (!client.connected()) {
  49. Serial.print("Reconnecting...");
  50. if (!client.connect("ESP8266Client")) {
  51. Serial.print("failed, rc=");
  52. Serial.print(client.state());
  53. Serial.println(" retrying in 5 seconds");
  54. delay(5000);
  55. }
  56. }
  57. }
  58. void loop() {
  59. if (!client.connected()) {
  60. reconnect();
  61. }
  62. client.loop();
  63. // Clears the trigPin
  64. digitalWrite(trigPin, LOW);
  65. delayMicroseconds(2);
  66. // Sets the trigPin on HIGH state for 10 micro seconds
  67. digitalWrite(trigPin, HIGH);
  68. delayMicroseconds(10);
  69. digitalWrite(trigPin, LOW);
  70. // Reads the echoPin, returns the sound wave travel time in microseconds
  71. duration1 = pulseIn(echoPin1, HIGH);
  72. duration2 = pulseIn(echoPin2, HIGH);
  73. // Calculating the distance
  74. distance1 = getDistance(duration1);
  75. distance2 = getDistance(duration2);
  76. // Prints the distance1 on the Serial Monitor
  77. /* if ((distance1!= (distance_alt - 1)) && (distance1!= (distance_alt)) && (distance1!= (distance_alt + 1))) { //+-1 um störungen herauszufiltern
  78. snprintf (msg, 50, "%d", distance);
  79. Serial.print("Publish Motion: ");
  80. Serial.println(msg);
  81. client.publish("/home/data", msg);
  82. delay(200);
  83. } */
  84. bool1 = presenceDetection(bool1, distance1);
  85. bool2 = presenceDetection(bool2, distance2);
  86. delay(80);
  87. }
  88. int presenceDetection(int bool1, int distance1){
  89. if (bool1 == 0) {
  90. //alternativ: if ((distance1!= (DISTANCE - 1)) && (distance1!= (DISTANCE)) && (distance1!= (DISTANCE + 1))) { //+-1
  91. if (((distance1 > (DISTANCE + 2)) || (distance1 < (DISTANCE - 2))) && (distance1 < (DISTANCE + 2))) { //darf +- 2 um festgelegte entfernung schwanken, um störungen herauszufiltern
  92. //Meldung an PI, dass die Distanz gestört ist
  93. snprintf (msg, 50, "%d", 1);
  94. client.publish("/gso/bb/104/ultraschall/1", msg);
  95. //Serieller Monitor
  96. Serial.print("Motion detected! Distance: ");
  97. Serial.println(distance1);
  98. //Flag auf 1
  99. bool1 = 1;
  100. return bool1;
  101. }
  102. else {
  103. snprintf (msg, 50, "%d", 0);
  104. client.publish("/gso/bb/104/ultraschall/1", msg);
  105. return bool1;
  106. }
  107. }
  108. else if (bool1 == 1) {
  109. if (((distance1> (DISTANCE + 2)) || (distance1<(DISTANCE - 2)))&&(distance1<(DISTANCE + 2))) { //darf +- 2 um festgelegte entfernung schwanken, um störungen herauszufiltern
  110. Serial.print("Still motion detected! Distance: ");
  111. Serial.println(distance1);
  112. //Meldung an PI, dass die Ausgangsdistanz wieder gemessen wird
  113. snprintf (msg, 50, "%d", 1);
  114. client.publish("/gso/bb/104/ultraschall/1", msg);
  115. return bool1;
  116. }
  117. else {
  118. //Meldung an PI, dass die Ausgangsdistanz wieder gemessen wird
  119. snprintf (msg, 50, "%d", 0);
  120. client.publish("/gso/bb/104/ultraschall/1", msg);
  121. //Flag wieder auf 0
  122. bool1 = 0;
  123. return bool1;
  124. }
  125. }
  126. }
  127. int getDistance(int duration){
  128. int distance = duration * 0.034 / 2;
  129. return distance;
  130. }