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_an_rpi.ino 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. long lastMsg = 0;
  9. char msg[50];
  10. int value = 0;
  11. void setup() {
  12. Serial.begin(115200);
  13. setup_wifi();
  14. client.setServer(MQTT_BROKER, 1883);
  15. }
  16. void setup_wifi() {
  17. delay(10);
  18. Serial.println();
  19. Serial.print("Connecting to ");
  20. Serial.println(SSID);
  21. WiFi.mode(WIFI_STA);
  22. WiFi.begin(SSID, PSK);
  23. while (WiFi.status() != WL_CONNECTED) {
  24. delay(500);
  25. Serial.print(".");
  26. }
  27. Serial.println("");
  28. Serial.println("WiFi connected");
  29. Serial.println("IP address: ");
  30. Serial.println(WiFi.localIP());
  31. }
  32. void reconnect() {
  33. while (!client.connected()) {
  34. Serial.print("Reconnecting...");
  35. if (!client.connect("ESP8266Client")) {
  36. Serial.print("failed, rc=");
  37. Serial.print(client.state());
  38. Serial.println(" retrying in 5 seconds");
  39. delay(5000);
  40. }
  41. }
  42. }
  43. void loop() {
  44. if (!client.connected()) {
  45. reconnect();
  46. }
  47. client.loop();
  48. snprintf (msg, 50, "Alive since %ld milliseconds", millis());
  49. Serial.print("Publish message: ");
  50. Serial.println(msg);
  51. client.publish("/home/data", "Hello asdf World");
  52. delay(5000);
  53. }