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_2bewegungssensoren.ino 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. int ledPin = D1; // LED on Pin 1 of ESP
  10. int pirPin1 = D0; // Input for HC-SR501 #1
  11. int pirPin2 = D2; // Input for HC-SR501 #2
  12. int pirValueNeu1; // Place to read PIR Value
  13. int pirValueAlt1 = 0; //Place to store read PIR Value
  14. int pirAn1;
  15. int pirAus1;
  16. int pirValueNeu2; // Place to read PIR Value
  17. int pirValueAlt2 = 0; //Place to store read PIR Value
  18. int pirAn2;
  19. int pirAus2;
  20. char msg[50];
  21. void setup() {
  22. Serial.begin(115200);
  23. setup_wifi();
  24. client.setServer(MQTT_BROKER, 1883);
  25. pinMode(ledPin, OUTPUT);
  26. pinMode(pirPin1, INPUT);
  27. pinMode(pirPin2, INPUT);
  28. digitalWrite(ledPin, LOW);
  29. }
  30. void setup_wifi() {
  31. delay(10);
  32. Serial.println();
  33. Serial.print("Connecting to ");
  34. Serial.println(SSID);
  35. WiFi.mode(WIFI_STA);
  36. WiFi.begin(SSID, PSK);
  37. while (WiFi.status() != WL_CONNECTED) {
  38. delay(500);
  39. Serial.print(".");
  40. }
  41. Serial.println("");
  42. Serial.println("WiFi connected");
  43. Serial.println("IP address: ");
  44. Serial.println(WiFi.localIP());
  45. }
  46. void reconnect() {
  47. while (!client.connected()) {
  48. Serial.print("Reconnecting...");
  49. if (!client.connect("ESP8266Client")) {
  50. Serial.print("failed, rc=");
  51. Serial.print(client.state());
  52. Serial.println(" retrying in 5 seconds");
  53. delay(5000);
  54. }
  55. }
  56. }
  57. void loop() {
  58. if((distance!=(distance_alt-1))&&(distance!=(distance_alt))&&(distance!=(distance_alt+1))){//+-1 um störungen herauszufiltern
  59. snprintf (msg,50,"%d", distance);
  60. Serial.print("Publish Motion: ");
  61. Serial.println(msg);
  62. client.publish("/home/data", msg);
  63. delay(500);
  64. }
  65. }