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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.252.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 (!client.connected()) {
  59. reconnect();
  60. }
  61. client.loop();
  62. pirValueNeu1 = digitalRead(pirPin1);
  63. pirValueNeu2 = digitalRead(pirPin2);
  64. digitalWrite(ledPin,(pirValueNeu1|pirValueNeu2));
  65. pirAn1= ((pirValueNeu1==1)&&(pirValueAlt1==0));
  66. pirAn2= ((pirValueNeu2==1)&&(pirValueAlt2==0));
  67. pirAus1= ((pirValueNeu1==0)&&(pirValueAlt1==1));
  68. pirAus2= ((pirValueNeu2==0)&&(pirValueAlt2==1));
  69. if(pirAn1){
  70. Serial.println("Steigende Flanke von PIR1");
  71. snprintf (msg,50,"%c%c",49,48); //Codierung PIR1 steigende Flanke: 10
  72. Serial.print("Publish Motion: ");
  73. Serial.println(msg);
  74. client.publish("/home/data", msg);
  75. }
  76. if(pirAn2){
  77. Serial.println("Steigende Flanke von PIR2");
  78. snprintf (msg,50,"%c%c",49,49); //Codierung PIR2 steigende Flanke: 11
  79. Serial.print("Publish Motion: ");
  80. Serial.println(msg);
  81. client.publish("/home/data", msg);
  82. }
  83. if(pirAus1){
  84. Serial.println("Fallende Flanke von PIR1");
  85. snprintf (msg,50,"%c%c",48,48); //Codierung PIR1 fallende Flanke: 00
  86. Serial.print("Publish Motion: ");
  87. Serial.println(msg);
  88. client.publish("/home/data", msg);
  89. }
  90. if(pirAus2){
  91. Serial.println("Fallende Flanke von PIR2");
  92. snprintf (msg,50,"%c%c",48,49); //Codierung PIR2 fallende Flanke: 01
  93. Serial.print("Publish Motion: ");
  94. Serial.println(msg);
  95. client.publish("/home/data", msg);
  96. }
  97. pirValueAlt1=pirValueNeu1;
  98. pirValueAlt2=pirValueNeu2;
  99. }