environment-sensor.ino 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * This is an example showing a simple environment sensor based on a BME280 attached via I2C.
  3. * This sketch was tested on a WeMos D1 mini
  4. */
  5. #include <Adafruit_BME280.h>
  6. #include <esp-knx-ip.h>
  7. // WiFi config here
  8. const char* ssid = "myssid";
  9. const char* pass = "mypassword";
  10. #define LED_PIN D4
  11. #define UPDATE_INTERVAL 10000
  12. unsigned long next_change = 0;
  13. float last_temp = 0.0;
  14. float last_hum = 0.0;
  15. float last_pres = 0.0;
  16. config_id_t temp_ga, hum_ga, pres_ga;
  17. config_id_t hostname_id;
  18. config_id_t update_rate_id, send_rate_id;
  19. config_id_t enable_sending_id;
  20. config_id_t enable_reading_id;
  21. Adafruit_BME280 bme;
  22. void setup() {
  23. pinMode(LED_PIN, OUTPUT);
  24. Serial.begin(115200);
  25. hostname_id = knx.config_register_string("Hostname", 20, String("env"));
  26. enable_sending_id = knx.config_register_bool("Send on update", true);
  27. update_rate_id = knx.config_register_int("Update rate (ms)", UPDATE_INTERVAL);
  28. temp_ga = knx.config_register_ga("Temperature", show_periodic_options);
  29. hum_ga = knx.config_register_ga("Humidity", show_periodic_options);
  30. pres_ga = knx.config_register_ga("Pressure", show_periodic_options);
  31. knx.callback_register("Read Temperature", temp_cb);
  32. knx.callback_register("Read Humidity", hum_cb);
  33. knx.callback_register("Read Pressure", pres_cb);
  34. knx.feedback_register_float("Temperature (°C)", &last_temp);
  35. knx.feedback_register_float("Humidity (%)", &last_hum);
  36. knx.feedback_register_float("Pressure (hPa)", &last_pres, 0);
  37. // Load previous config from EEPROM
  38. knx.load();
  39. // Init sensor
  40. if (!bme.begin(0x76)) {
  41. Serial.println("Could not find a valid BME280 sensor, check wiring!");
  42. }
  43. // Init WiFi
  44. WiFi.hostname(knx.config_get_string(hostname_id));
  45. WiFi.begin(ssid, pass);
  46. Serial.println("");
  47. Serial.print("[Connecting]");
  48. Serial.print(ssid);
  49. digitalWrite(LED_PIN, LOW);
  50. while (WiFi.status() != WL_CONNECTED) {
  51. digitalWrite(LED_PIN, HIGH);
  52. delay(250);
  53. Serial.print(".");
  54. digitalWrite(LED_PIN, LOW);
  55. delay(250);
  56. }
  57. digitalWrite(LED_PIN, HIGH);
  58. // Start knx
  59. knx.start();
  60. Serial.println();
  61. Serial.println("Connected to wifi");
  62. Serial.println(WiFi.localIP());
  63. }
  64. void loop() {
  65. knx.loop();
  66. unsigned long now = millis();
  67. if (next_change < now)
  68. {
  69. next_change = now + knx.config_get_int(update_rate_id);
  70. last_temp = bme.readTemperature();
  71. last_hum = bme.readHumidity();
  72. last_pres = bme.readPressure()/100.0f;
  73. Serial.print("T: ");
  74. Serial.print(last_temp);
  75. Serial.print("°C H: ");
  76. Serial.print(last_hum);
  77. Serial.print("% P: ");
  78. Serial.print(last_pres);
  79. Serial.println("hPa");
  80. if (knx.config_get_bool(enable_sending_id))
  81. {
  82. knx.write_2byte_float(knx.config_get_ga(temp_ga), last_temp);
  83. knx.write_2byte_float(knx.config_get_ga(hum_ga), last_hum);
  84. knx.write_2byte_float(knx.config_get_ga(pres_ga), last_pres);
  85. }
  86. }
  87. delay(50);
  88. }
  89. bool show_periodic_options()
  90. {
  91. return knx.config_get_bool(enable_sending_id);
  92. }
  93. bool enable_reading_callback()
  94. {
  95. return knx.config_get_bool(enable_reading_id);
  96. }
  97. void temp_cb(message_t const &msg, void *arg)
  98. {
  99. switch (msg.ct)
  100. {
  101. case KNX_CT_READ:
  102. {
  103. knx.answer_2byte_float(msg.received_on, last_temp);
  104. break;
  105. }
  106. }
  107. }
  108. void hum_cb(message_t const &msg, void *arg)
  109. {
  110. switch (msg.ct)
  111. {
  112. case KNX_CT_READ:
  113. {
  114. knx.answer_2byte_float(msg.received_on, last_hum);
  115. break;
  116. }
  117. }
  118. }
  119. void pres_cb(message_t const &msg, void *arg)
  120. {
  121. switch (msg.ct)
  122. {
  123. case KNX_CT_READ:
  124. {
  125. knx.answer_2byte_float(msg.received_on, last_pres);
  126. break;
  127. }
  128. }
  129. }