Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. #include <Wire.h>
  5. #include <SPI.h>
  6.  
  7. #include <Adafruit_Sensor.h>
  8. #include <Adafruit_BME280.h>
  9. #include <LaCrosse_TX23.h>
  10.  
  11. /* Konfiguartion für Deepsleep */
  12. #define uS_TO_S_FACTOR 1000000
  13. #define TIME_TO_SLEEP 60
  14.  
  15. RTC_DATA_ATTR int bootCount = 0;
  16.  
  17. /* Konfiguartion für Sensorik */
  18. #define SEA_LEVEL_PREPRESSURE (1013.25)
  19. #define WIND_SENSOR_DATA_PIN 18
  20.  
  21. /* Konfiguration für MQTT - Kommunikation */
  22. const char* SSID = "IoTify";
  23. const char* PSK = "wBox2019";
  24. const char* MQTT_BROKER = "192.168.178.46";
  25.  
  26. // MQTT topics
  27. const char* topic_temperature = "/wetterstation/temperature";
  28. const char* topic_humidity = "/wetterstation/humidity";
  29. const char* topic_pressure = "/wetterstation/pressure";
  30. const char* topic_windSpeed = "/wetterstation/wind_speed";
  31. const char* topic_windDirection = "/wetterstation/wind_direction";
  32.  
  33. /* Definitionen bzw. Deklaration für den Windsensor */
  34. const char* directionTable[] = {"N","NNE","NE","ENE","E","ESE","SE","SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"};
  35. float speed; // Windgeschwindigkeit in m/s
  36. int direction; // Index der directionTable (Himmelsrichtung)
  37.  
  38. /* Deklaration für den Temperatursensor */
  39. bool status;
  40. float temperature;
  41. float humidity;
  42. float pressure;
  43.  
  44. WiFiClient espClient;
  45. PubSubClient client(espClient);
  46.  
  47. Adafruit_BME280 tempSensor;
  48. LaCrosse_TX23 windSensor = LaCrosse_TX23(WIND_SENSOR_DATA_PIN);
  49.  
  50. /* Verbindung zum WLAN-Netzwerk aufbauen */
  51. void setup_wifi() {
  52. delay(10);
  53. Serial.println();
  54. Serial.print("Connecting to network: ");
  55. Serial.println(SSID);
  56.  
  57. WiFi.begin(SSID, PSK);
  58.  
  59. while (WiFi.status() != WL_CONNECTED) {
  60. delay(500);
  61. Serial.print(".");
  62. }
  63.  
  64. Serial.println("\nConnected successfully to network");
  65. Serial.print("IP address: ");
  66. Serial.println(WiFi.localIP());
  67. }
  68.  
  69. /* Verbindung bei Abbruch erneut herstellen */
  70. void reconnect() {
  71. while (!client.connected()) {
  72. Serial.println("Reconnecting...");
  73. if (!client.connect("ESP32_Weatherstation")) {
  74. Serial.print("...failed, rc=");
  75. Serial.print(client.state());
  76. Serial.println(" retrying...");
  77. delay(5000);
  78. }
  79. }
  80. }
  81.  
  82. void setup() {
  83. esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  84.  
  85. Serial.begin(115200);
  86. setup_wifi();
  87. client.setServer(MQTT_BROKER, 1883);
  88.  
  89. /* Sensoren initialisieren bzw. starten */
  90. status = tempSensor.begin();
  91.  
  92. // Fehlerbehandlung falls der Sensor nicht funktioniert
  93. if(!status) {
  94. Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
  95. Serial.print("SensorID was: 0x"); Serial.println(tempSensor.sensorID(),16);
  96. Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
  97. Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
  98. Serial.print(" ID of 0x60 represents a BME 280.\n");
  99. Serial.print(" ID of 0x61 represents a BME 680.\n");
  100. }
  101.  
  102. if (!client.connected()) {
  103. reconnect();
  104. }
  105. client.loop();
  106.  
  107. // client.publish("/home/data", "Hello World"); // Benutzung: publish( MQTT-Topic, Nachricht )
  108.  
  109. /* Temperatursensor auslesen */
  110. temperature = tempSensor.readTemperature();
  111. humidity = tempSensor.readHumidity();
  112. pressure = tempSensor.readPressure() / 100.0F;
  113.  
  114. client.publish(topic_temperature, String(temperature).c_str());
  115. client.publish(topic_humidity, String(humidity).c_str());
  116. client.publish(topic_pressure, String(pressure).c_str());
  117.  
  118. /* Windsensor bzw. Anemometer auslesen */
  119. if(windSensor.read(speed, direction)) {
  120. client.publish(topic_windSpeed, String(speed, 1).c_str());
  121. client.publish(topic_windDirection, directionTable[direction]);
  122. }
  123.  
  124. delay(5000);
  125.  
  126. Serial.println("Will sleep now...");
  127. Serial.flush();
  128. esp_deep_sleep_start(); // für 60 Sekunden schlafen
  129. }
  130.  
  131. void loop() {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement