static-config.ino 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * This is an example showing a simple environment sensor based on a BME280 attached via I2C.
  3. * It shows, how the library can used to statically configure a device without a webserver for config.
  4. * This sketch was tested on a WeMos D1 mini
  5. */
  6. #include <Adafruit_BME280.h>
  7. #include <esp-knx-ip.h>
  8. // WiFi config here
  9. const char* ssid = "myssid";
  10. const char* pass = "mypassword";
  11. #define LED_PIN D4
  12. #define UPDATE_INTERVAL 10000
  13. unsigned long next_change = 0;
  14. float last_temp = 0.0;
  15. float last_hum = 0.0;
  16. float last_pres = 0.0;
  17. Adafruit_BME280 bme;
  18. // Group addresses to send to (1/1/1, 1/1/2 and 1/1/3)
  19. address_t temp_ga = knx.GA_to_address(1, 1, 1);
  20. address_t hum_ga = knx.GA_to_address(1, 1, 2);
  21. address_t pres_ga = knx.GA_to_address(1, 1, 3);
  22. void setup() {
  23. pinMode(LED_PIN, OUTPUT);
  24. Serial.begin(115200);
  25. callback_id_t temp_cb = knx.callback_register("Read Temperature", temp_cb);
  26. callback_id_t hum_cb =knx.callback_register("Read Humidity", hum_cb);
  27. callback_id_t pres_cb =knx.callback_register("Read Pressure", pres_cb);
  28. // Assign callbacks to group addresses (2/1/1, 2/1/2, 2/1/3)
  29. knx.callback_assign(temp_cb, knx.GA_to_address(2, 1, 1));
  30. knx.callback_assign(hum_cb, knx.GA_to_address(2, 1, 2));
  31. knx.callback_assign(pres_cb, knx.GA_to_address(2, 1, 3));
  32. // Set physical address (1.1.1)
  33. knx.physical_address_set(knx.PA_to_address(1, 1, 1));
  34. // Do not call knx.load() for static config, it will try to load config from EEPROM which we don't have here
  35. // Init sensor
  36. if (!bme.begin(0x76)) {
  37. Serial.println("Could not find a valid BME280 sensor, check wiring!");
  38. }
  39. // Init WiFi
  40. WiFi.hostname("env");
  41. WiFi.begin(ssid, pass);
  42. Serial.println("");
  43. Serial.print("[Connecting]");
  44. Serial.print(ssid);
  45. digitalWrite(LED_PIN, LOW);
  46. while (WiFi.status() != WL_CONNECTED) {
  47. digitalWrite(LED_PIN, HIGH);
  48. delay(250);
  49. Serial.print(".");
  50. digitalWrite(LED_PIN, LOW);
  51. delay(250);
  52. }
  53. digitalWrite(LED_PIN, HIGH);
  54. // Start knx, disable webserver by passing nullptr
  55. knx.start(nullptr);
  56. Serial.println();
  57. Serial.println("Connected to wifi");
  58. Serial.println(WiFi.localIP());
  59. }
  60. void loop() {
  61. knx.loop();
  62. unsigned long now = millis();
  63. if (next_change < now)
  64. {
  65. next_change = now + UPDATE_INTERVAL;
  66. last_temp = bme.readTemperature();
  67. last_hum = bme.readHumidity();
  68. last_pres = bme.readPressure()/100.0f;
  69. Serial.print("T: ");
  70. Serial.print(last_temp);
  71. Serial.print("°C H: ");
  72. Serial.print(last_hum);
  73. Serial.print("% P: ");
  74. Serial.print(last_pres);
  75. Serial.println("hPa");
  76. knx.write_2byte_float(temp_ga, last_temp);
  77. knx.write_2byte_float(hum_ga, last_hum);
  78. knx.write_2byte_float(pres_ga, last_pres);
  79. }
  80. delay(50);
  81. }
  82. void temp_cb(message_t const &msg, void *arg)
  83. {
  84. switch (msg.ct)
  85. {
  86. case KNX_CT_READ:
  87. {
  88. knx.answer_2byte_float(msg.received_on, last_temp);
  89. break;
  90. }
  91. }
  92. }
  93. void hum_cb(message_t const &msg, void *arg)
  94. {
  95. switch (msg.ct)
  96. {
  97. case KNX_CT_READ:
  98. {
  99. knx.answer_2byte_float(msg.received_on, last_hum);
  100. break;
  101. }
  102. }
  103. }
  104. void pres_cb(message_t const &msg, void *arg)
  105. {
  106. switch (msg.ct)
  107. {
  108. case KNX_CT_READ:
  109. {
  110. knx.answer_2byte_float(msg.received_on, last_pres);
  111. break;
  112. }
  113. }
  114. }