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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. int relais_count(void) {
  17. return 0;
  18. }
  19. int relais_enable(int relais, unsigned long time) {
  20. return 0;
  21. }
  22. void relais_init(void) { }
  23. void relais_run(void) { }
  24. #elif defined(ARDUINO_ARCH_ESP32)
  25. #define RELAIS_COUNT 10
  26. struct relais_state {
  27. unsigned long turn_off;
  28. };
  29. static struct relais_state state[RELAIS_COUNT];
  30. static int gpios[RELAIS_COUNT] = {
  31. 0, 15, 2, 4,
  32. 16, 17, 5, 18,
  33. 19, 23
  34. };
  35. static void relais_set(int relais, int state) {
  36. digitalWrite(gpios[relais], state ? LOW : HIGH);
  37. }
  38. int relais_count(void) {
  39. return RELAIS_COUNT;
  40. }
  41. int relais_enable(int relais, unsigned long time) {
  42. if ((relais < 0) || (relais >= RELAIS_COUNT)) {
  43. return -1;
  44. }
  45. relais_set(relais, 1);
  46. state[relais].turn_off = millis() + time;
  47. return 0;
  48. }
  49. void relais_init(void) {
  50. for (int i = 0; i < RELAIS_COUNT; i++) {
  51. pinMode(gpios[i], OUTPUT);
  52. relais_set(i, 0);
  53. state[i].turn_off = 0;
  54. }
  55. }
  56. void relais_run(void) {
  57. for (int i = 0; i < RELAIS_COUNT; i++) {
  58. if (state[i].turn_off > 0) {
  59. if (millis() >= state[i].turn_off) {
  60. relais_set(i, 0);
  61. }
  62. }
  63. }
  64. }
  65. #endif