ESP32 / ESP8266 & BME280 / SHT2x sensor with InfluxDB support
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.

relais.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * relais.cpp
  3. *
  4. * ESP8266 / ESP32 Environmental Sensor
  5. *
  6. * ----------------------------------------------------------------------------
  7. * "THE BEER-WARE LICENSE" (Revision 42):
  8. * <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  9. * you can do whatever you want with this stuff. If we meet some day, and you
  10. * think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  11. * ----------------------------------------------------------------------------
  12. */
  13. #include <Arduino.h>
  14. #include "relais.h"
  15. #include "config.h"
  16. #if defined(RELAIS_SERIAL)
  17. #define SERIAL_RELAIS_COUNT 4
  18. /*
  19. Turn OFF the first relay : A0 01 00 A1
  20. Turn ON the first relay : A0 01 01 A2
  21. Turn OFF the second relay : A0 02 00 A2
  22. Turn ON the second relay : A0 02 01 A3
  23. Turn OFF the third relay : A0 03 00 A3
  24. Turn ON the third relay : A0 03 01 A4
  25. Turn OFF the fourth relay : A0 04 00 A4
  26. Turn ON the fourth relay : A0 04 01 A5
  27. */
  28. static int states[SERIAL_RELAIS_COUNT];
  29. static String names[SERIAL_RELAIS_COUNT] = {
  30. #if defined(SENSOR_LOCATION_BATHROOM)
  31. String("light_small"),
  32. String("light_big"),
  33. String("relais_2"),
  34. String("fan")
  35. #else
  36. String("relais_0"),
  37. String("relais_1"),
  38. String("relais_2"),
  39. String("relais_3")
  40. #endif
  41. };
  42. static int initial_values[SERIAL_RELAIS_COUNT] = {
  43. #if defined(SENSOR_LOCATION_BATHROOM)
  44. 1, 0, 0, 1
  45. #else
  46. 0, 0, 0, 0
  47. #endif
  48. };
  49. void relais_init(void) {
  50. Serial.begin(115200);
  51. for (int i = 0; i < SERIAL_RELAIS_COUNT; i++) {
  52. relais_set(i, initial_values[i]);
  53. }
  54. }
  55. int relais_count(void) {
  56. return SERIAL_RELAIS_COUNT;
  57. }
  58. void relais_set(int relais, int state) {
  59. if ((relais < 0) || (relais >= SERIAL_RELAIS_COUNT)) {
  60. return;
  61. }
  62. states[relais] = state;
  63. int cmd[4];
  64. cmd[0] = 0xA0; // command
  65. cmd[1] = relais + 1; // relais id, 1-4
  66. cmd[2] = state ? 1 : 0; // relais state
  67. cmd[3] = 0; // checksum
  68. for (int i = 0; i < 3; i++) {
  69. cmd[3] += cmd[i];
  70. }
  71. for (int i = 0; i < 4; i++) {
  72. Serial.write(cmd[i]);
  73. }
  74. delay(100);
  75. }
  76. int relais_get(int relais) {
  77. if ((relais < 0) || (relais >= SERIAL_RELAIS_COUNT)) {
  78. return 0;
  79. }
  80. return states[relais];
  81. }
  82. String relais_name(int relais) {
  83. if ((relais < 0) || (relais >= SERIAL_RELAIS_COUNT)) {
  84. return String(F("Unknown"));
  85. }
  86. return names[relais];
  87. }
  88. #elif defined(RELAIS_GPIO)
  89. #define GPIO_RELAIS_COUNT 10
  90. static int gpios[GPIO_RELAIS_COUNT] = {
  91. 0, 15, 2, 4,
  92. 16, 17, 5, 18,
  93. 19, 23
  94. };
  95. static int states[GPIO_RELAIS_COUNT];
  96. static String names[GPIO_RELAIS_COUNT] = {
  97. String(F("relais_0")),
  98. String(F("relais_1")),
  99. String(F("relais_2")),
  100. String(F("relais_3")),
  101. String(F("relais_4")),
  102. String(F("relais_5")),
  103. String(F("relais_6")),
  104. String(F("relais_7")),
  105. String(F("relais_8")),
  106. String(F("relais_9"))
  107. };
  108. void relais_init(void) {
  109. for (int i = 0; i < GPIO_RELAIS_COUNT; i++) {
  110. pinMode(gpios[i], OUTPUT);
  111. relais_set(i, 0);
  112. }
  113. }
  114. int relais_count(void) {
  115. return GPIO_RELAIS_COUNT;
  116. }
  117. void relais_set(int relais, int state) {
  118. if ((relais < 0) || (relais >= GPIO_RELAIS_COUNT)) {
  119. return;
  120. }
  121. states[relais] = state;
  122. digitalWrite(gpios[relais], state ? LOW : HIGH);
  123. }
  124. int relais_get(int relais) {
  125. if ((relais < 0) || (relais >= GPIO_RELAIS_COUNT)) {
  126. return 0;
  127. }
  128. return states[relais];
  129. }
  130. String relais_name(int relais) {
  131. if ((relais < 0) || (relais >= GPIO_RELAIS_COUNT)) {
  132. return String(F("Unknown"));
  133. }
  134. return names[relais];
  135. }
  136. #else
  137. void relais_init(void) { }
  138. int relais_count(void) { return 0; }
  139. void relais_set(int relais, int state) { }
  140. int relais_get(int relais) { return 0; }
  141. #endif