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.

Ultrasonic_Sensor_optimiert.ino 3.8KB

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