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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #elif defined(SENSOR_LOCATION_LIVINGROOM_WORKSPACE)
  36. String("light_pc"),
  37. String("light_bench"),
  38. String("relais_w2"),
  39. String("relais_w3")
  40. #elif defined(SENSOR_LOCATION_LIVINGROOM_TV)
  41. String("light_amp"),
  42. String("light_box"),
  43. String("light_kitchen"),
  44. String("relais_t3")
  45. #else
  46. String("relais_0"),
  47. String("relais_1"),
  48. String("relais_2"),
  49. String("relais_3")
  50. #endif
  51. };
  52. static int initial_values[SERIAL_RELAIS_COUNT] = {
  53. #if defined(SENSOR_LOCATION_BATHROOM)
  54. 1, 0, 0, 1
  55. #else
  56. 0, 0, 0, 0
  57. #endif
  58. };
  59. void relais_init(void) {
  60. Serial.begin(115200);
  61. for (int i = 0; i < SERIAL_RELAIS_COUNT; i++) {
  62. relais_set(i, initial_values[i]);
  63. }
  64. }
  65. int relais_count(void) {
  66. return SERIAL_RELAIS_COUNT;
  67. }
  68. void relais_set(int relais, int state) {
  69. if ((relais < 0) || (relais >= SERIAL_RELAIS_COUNT)) {
  70. return;
  71. }
  72. states[relais] = state;
  73. int cmd[4];
  74. cmd[0] = 0xA0; // command
  75. cmd[1] = relais + 1; // relais id, 1-4
  76. cmd[2] = state ? 1 : 0; // relais state
  77. cmd[3] = 0; // checksum
  78. for (int i = 0; i < 3; i++) {
  79. cmd[3] += cmd[i];
  80. }
  81. for (int i = 0; i < 4; i++) {
  82. Serial.write(cmd[i]);
  83. }
  84. delay(100);
  85. }
  86. int relais_get(int relais) {
  87. if ((relais < 0) || (relais >= SERIAL_RELAIS_COUNT)) {
  88. return 0;
  89. }
  90. return states[relais];
  91. }
  92. String relais_name(int relais) {
  93. if ((relais < 0) || (relais >= SERIAL_RELAIS_COUNT)) {
  94. return String(F("Unknown"));
  95. }
  96. return names[relais];
  97. }
  98. #elif defined(RELAIS_GPIO)
  99. #define GPIO_RELAIS_COUNT 10
  100. static int gpios[GPIO_RELAIS_COUNT] = {
  101. 0, 15, 2, 4,
  102. 16, 17, 5, 18,
  103. 19, 23
  104. };
  105. static int states[GPIO_RELAIS_COUNT];
  106. static String names[GPIO_RELAIS_COUNT] = {
  107. String(F("relais_0")),
  108. String(F("relais_1")),
  109. String(F("relais_2")),
  110. String(F("relais_3")),
  111. String(F("relais_4")),
  112. String(F("relais_5")),
  113. String(F("relais_6")),
  114. String(F("relais_7")),
  115. String(F("relais_8")),
  116. String(F("relais_9"))
  117. };
  118. void relais_init(void) {
  119. for (int i = 0; i < GPIO_RELAIS_COUNT; i++) {
  120. pinMode(gpios[i], OUTPUT);
  121. relais_set(i, 0);
  122. }
  123. }
  124. int relais_count(void) {
  125. return GPIO_RELAIS_COUNT;
  126. }
  127. void relais_set(int relais, int state) {
  128. if ((relais < 0) || (relais >= GPIO_RELAIS_COUNT)) {
  129. return;
  130. }
  131. states[relais] = state;
  132. digitalWrite(gpios[relais], state ? LOW : HIGH);
  133. }
  134. int relais_get(int relais) {
  135. if ((relais < 0) || (relais >= GPIO_RELAIS_COUNT)) {
  136. return 0;
  137. }
  138. return states[relais];
  139. }
  140. String relais_name(int relais) {
  141. if ((relais < 0) || (relais >= GPIO_RELAIS_COUNT)) {
  142. return String(F("Unknown"));
  143. }
  144. return names[relais];
  145. }
  146. #else
  147. void relais_init(void) { }
  148. int relais_count(void) { return 0; }
  149. void relais_set(int relais, int state) { }
  150. int relais_get(int relais) { return 0; }
  151. #endif