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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. //Eigene zu trackende Entfernung festlegen
  4. #define DISTANCE 15
  5. const char* SSID = "smartroom";
  6. const char* PSK = "smarthome";
  7. const char* MQTT_BROKER = "192.168.4.1";
  8. WiFiClient espClient;
  9. PubSubClient client(espClient);
  10. // defines pins numbers
  11. const int trigPin = 2; //D4
  12. const int echoPin = 0; //D3
  13. long duration;
  14. int distance;
  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(echoPin, 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. duration = pulseIn(echoPin, HIGH);
  67. // Calculating the distance
  68. distance = duration * 0.034 / 2;
  69. // Prints the distance on the Serial Monitor
  70. /* if ((distance != (distance_alt - 1)) && (distance != (distance_alt)) && (distance != (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. if (bool1 == 0) {
  78. //alternativ: if ((distance != (DISTANCE - 1)) && (distance != (DISTANCE)) && (distance != (DISTANCE + 1))) { //+-1
  79. if (((DISTANCE + 2) < distance) || ((DISTANCE - 2) > distance)) { //darf +- 2 um festgelegte entfernung schwanken, um störungen herauszufiltern
  80. //Meldung an PI, dass die Distanz gestört ist
  81. snprintf (msg, 50, "%d", 1);
  82. client.publish("/home/data", msg);
  83. //Serieller Monitor
  84. Serial.print("Motion detected! Distance: ");
  85. Serial.println(msg);
  86. //Flag auf 1
  87. bool1 = 1;
  88. }
  89. }
  90. else if (bool1 == 1) {
  91. if (((DISTANCE + 2) < distance) || ((DISTANCE - 2) > distance)) { //darf +- 2 um festgelegte entfernung schwanken, um störungen herauszufiltern
  92. Serial.print("Still motion detected! Distance: ");
  93. Serial.println(distance);
  94. }
  95. else {
  96. //Meldung an PI, dass die Ausgangsdistanz wieder gemessen wird
  97. snprintf (msg, 50, "%d", 0);
  98. client.publish("/home/data", msg);
  99. //Flag wieder auf 0
  100. bool1 = 0;
  101. }
  102. }
  103. delay(100);
  104. }