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

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