No Description
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 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * relais.cpp
  3. *
  4. * ESP8266 / ESP32 Relais Actor
  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 "config.h"
  15. #include "relais.h"
  16. #if defined(ARDUINO_ARCH_ESP8266)
  17. /*
  18. Turn OFF the first relay : A0 01 00 A1
  19. Turn ON the first relay : A0 01 01 A2
  20. Turn OFF the second relay : A0 02 00 A2
  21. Turn ON the second relay : A0 02 01 A3
  22. Turn OFF the third relay : A0 03 00 A3
  23. Turn ON the third relay : A0 03 01 A4
  24. Turn OFF the fourth relay : A0 04 00 A4
  25. Turn ON the fourth relay : A0 04 01 A5
  26. */
  27. void relais_set(int relais, int state) {
  28. if ((relais < 0) || (relais >= RELAIS_COUNT)) {
  29. return;
  30. }
  31. int cmd[4];
  32. cmd[0] = 0xA0; // command
  33. cmd[1] = relais + 1; // relais id, 1-4
  34. cmd[2] = state ? 1 : 0; // relais state
  35. cmd[3] = 0; // checksum
  36. for (int i = 0; i < 3; i++) {
  37. cmd[3] += cmd[i];
  38. }
  39. for (int i = 0; i < 4; i++) {
  40. Serial.write(cmd[i]);
  41. }
  42. delay(100);
  43. }
  44. void relais_init(void) {
  45. #if RELAIS_COUNT > 0
  46. Serial.begin(115200);
  47. #endif
  48. for (int i = 0; i < RELAIS_COUNT; i++) {
  49. relais_set(i, 0);
  50. }
  51. }
  52. #elif defined(ARDUINO_ARCH_ESP32)
  53. static int gpios[RELAIS_COUNT] = {
  54. 0, 15, 2, 4,
  55. 16, 17, 5, 18,
  56. 19, 23
  57. };
  58. void relais_set(int relais, int state) {
  59. if ((relais < 0) || (relais >= RELAIS_COUNT)) {
  60. return;
  61. }
  62. digitalWrite(gpios[relais], state ? LOW : HIGH);
  63. }
  64. void relais_init(void) {
  65. for (int i = 0; i < RELAIS_COUNT; i++) {
  66. pinMode(gpios[i], OUTPUT);
  67. relais_set(i, 0);
  68. }
  69. }
  70. #endif
  71. int relais_count(void) {
  72. return RELAIS_COUNT;
  73. }