12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- /*
- * relais.cpp
- *
- * ESP8266 / ESP32 Relais Actor
- *
- * ----------------------------------------------------------------------------
- * "THE BEER-WARE LICENSE" (Revision 42):
- * <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
- * you can do whatever you want with this stuff. If we meet some day, and you
- * think this stuff is worth it, you can buy me a beer in return. Thomas Buck
- * ----------------------------------------------------------------------------
- */
-
- #include <Arduino.h>
- #include "config.h"
- #include "relais.h"
-
- #if defined(ARDUINO_ARCH_ESP8266)
-
- /*
- Turn OFF the first relay : A0 01 00 A1
- Turn ON the first relay : A0 01 01 A2
- Turn OFF the second relay : A0 02 00 A2
- Turn ON the second relay : A0 02 01 A3
- Turn OFF the third relay : A0 03 00 A3
- Turn ON the third relay : A0 03 01 A4
- Turn OFF the fourth relay : A0 04 00 A4
- Turn ON the fourth relay : A0 04 01 A5
- */
-
- void relais_set(int relais, int state) {
- if ((relais < 0) || (relais >= RELAIS_COUNT)) {
- return;
- }
-
- int cmd[4];
- cmd[0] = 0xA0; // command
-
- cmd[1] = relais + 1; // relais id, 1-4
- cmd[2] = state ? 1 : 0; // relais state
-
- cmd[3] = 0; // checksum
- for (int i = 0; i < 3; i++) {
- cmd[3] += cmd[i];
- }
-
- for (int i = 0; i < 4; i++) {
- Serial.write(cmd[i]);
- }
-
- delay(100);
- }
-
- void relais_init(void) {
- #if RELAIS_COUNT > 0
- Serial.begin(115200);
- #endif
-
- for (int i = 0; i < RELAIS_COUNT; i++) {
- relais_set(i, 0);
- }
- }
-
- #elif defined(ARDUINO_ARCH_ESP32)
-
- static int gpios[RELAIS_COUNT] = {
- 0, 15, 2, 4,
- 16, 17, 5, 18,
- 19, 23
- };
-
- void relais_set(int relais, int state) {
- if ((relais < 0) || (relais >= RELAIS_COUNT)) {
- return;
- }
-
- digitalWrite(gpios[relais], state ? LOW : HIGH);
- }
-
- void relais_init(void) {
- for (int i = 0; i < RELAIS_COUNT; i++) {
- pinMode(gpios[i], OUTPUT);
- relais_set(i, 0);
- }
- }
-
- #endif
-
- int relais_count(void) {
- return RELAIS_COUNT;
- }
|