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.

wlan_ultraschall.ino 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. const char* SSID = "smartroom";
  4. const char* PSK = "smarthome";
  5. const char* MQTT_BROKER = "192.168.4.1";
  6. WiFiClient espClient;
  7. PubSubClient client(espClient);
  8. // defines pins numbers
  9. const int trigPin = D4; //D4
  10. const int echoPin = D3; //D3
  11. long duration;
  12. int distance;
  13. int distance_alt=0;
  14. char msg[50];
  15. int value = 0;
  16. long lastMsg = 0;
  17. void setup() {
  18. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  19. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  20. Serial.begin(115200);
  21. setup_wifi();
  22. client.setServer(MQTT_BROKER, 1883);
  23. }
  24. void setup_wifi() {
  25. delay(10);
  26. Serial.println();
  27. Serial.print("Connecting to ");
  28. Serial.println(SSID);
  29. WiFi.mode(WIFI_STA);
  30. WiFi.begin(SSID, PSK);
  31. while (WiFi.status() != WL_CONNECTED) {
  32. delay(500);
  33. Serial.print(".");
  34. }
  35. Serial.println("");
  36. Serial.println("WiFi connected");
  37. Serial.println("IP address: ");
  38. Serial.println(WiFi.localIP());
  39. }
  40. void reconnect() {
  41. while (!client.connected()) {
  42. Serial.print("Reconnecting...");
  43. if (!client.connect("ESP8266Client")) {
  44. Serial.print("failed, rc=");
  45. Serial.print(client.state());
  46. Serial.println(" retrying in 5 seconds");
  47. delay(5000);
  48. }
  49. }
  50. }
  51. void loop() {
  52. if (!client.connected()) {
  53. reconnect();
  54. }
  55. client.loop();
  56. // Clears the trigPin
  57. digitalWrite(trigPin, LOW);
  58. delayMicroseconds(2);
  59. // Sets the trigPin on HIGH state for 10 micro seconds
  60. digitalWrite(trigPin, HIGH);
  61. delayMicroseconds(10);
  62. digitalWrite(trigPin, LOW);
  63. // Reads the echoPin, returns the sound wave travel time in microseconds
  64. duration = pulseIn(echoPin, HIGH);
  65. // Calculating the distance
  66. distance = duration * 0.034 / 2;
  67. // Prints the distance on the Serial Monitor
  68. if((distance!=(distance_alt-1))&&(distance!=(distance_alt))&&(distance!=(distance_alt+1))){//+-1 um störungen herauszufiltern
  69. snprintf (msg,50,"%d", distance);
  70. Serial.print("Publish Motion: ");
  71. Serial.println(msg);
  72. client.publish("/home/data", msg);
  73. delay(200);
  74. }
  75. distance_alt=distance;
  76. }