Fork of the espurna firmware for `mhsw` switches
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.

263 lines
6.7 KiB

  1. /*
  2. RELAY MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include <EEPROM.h>
  6. #include <ArduinoJson.h>
  7. #include <vector>
  8. typedef struct {
  9. unsigned char pin;
  10. bool reverse;
  11. } relay_t;
  12. std::vector<relay_t> _relays;
  13. bool recursive = false;
  14. #ifdef SONOFF_DUAL
  15. unsigned char dualRelayStatus = 0;
  16. #endif
  17. // -----------------------------------------------------------------------------
  18. // RELAY
  19. // -----------------------------------------------------------------------------
  20. void relayMQTT(unsigned char id) {
  21. if (id >= _relays.size()) return;
  22. String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
  23. char buffer[strlen(MQTT_RELAY_TOPIC) + mqttGetter.length() + 3];
  24. sprintf(buffer, "%s/%d%s", MQTT_RELAY_TOPIC, id, mqttGetter.c_str());
  25. mqttSend(buffer, relayStatus(id) ? "1" : "0");
  26. }
  27. void relayMQTT() {
  28. for (unsigned int i=0; i < _relays.size(); i++) {
  29. relayMQTT(i);
  30. }
  31. }
  32. String relayString() {
  33. DynamicJsonBuffer jsonBuffer;
  34. JsonObject& root = jsonBuffer.createObject();
  35. JsonArray& relay = root.createNestedArray("relayStatus");
  36. for (unsigned char i=0; i<relayCount(); i++) {
  37. relay.add(relayStatus(i));
  38. }
  39. String output;
  40. root.printTo(output);
  41. return output;
  42. }
  43. void relayWS() {
  44. String output = relayString();
  45. wsSend(output.c_str());
  46. }
  47. bool relayStatus(unsigned char id) {
  48. #ifdef SONOFF_DUAL
  49. if (id >= 2) return false;
  50. return ((dualRelayStatus & (1 << id)) > 0);
  51. #else
  52. if (id >= _relays.size()) return false;
  53. bool status = (digitalRead(_relays[id].pin) == HIGH);
  54. return _relays[id].reverse ? !status : status;
  55. #endif
  56. }
  57. bool relayStatus(unsigned char id, bool status, bool report) {
  58. if (id >= _relays.size()) return false;
  59. bool changed = false;
  60. if (relayStatus(id) != status) {
  61. DEBUG_MSG("[RELAY] %d => %s\n", id, status ? "ON" : "OFF");
  62. changed = true;
  63. #ifdef SONOFF_DUAL
  64. dualRelayStatus ^= (1 << id);
  65. Serial.flush();
  66. Serial.write(0xA0);
  67. Serial.write(0x04);
  68. Serial.write(dualRelayStatus);
  69. Serial.write(0xA1);
  70. Serial.flush();
  71. #else
  72. digitalWrite(_relays[id].pin, _relays[id].reverse ? !status : status);
  73. #endif
  74. if (!recursive) {
  75. relaySync(id);
  76. relaySave();
  77. }
  78. #ifdef ENABLE_DOMOTICZ
  79. domoticzSend(id);
  80. #endif
  81. }
  82. if (report) relayMQTT(id);
  83. if (!recursive) relayWS();
  84. return changed;
  85. }
  86. bool relayStatus(unsigned char id, bool status) {
  87. if (id >= _relays.size()) return false;
  88. return relayStatus(id, status, true);
  89. }
  90. void relaySync(unsigned char id) {
  91. if (_relays.size() > 1) {
  92. recursive = true;
  93. byte relaySync = getSetting("relaySync", RELAY_SYNC).toInt();
  94. bool status = relayStatus(id);
  95. // If RELAY_SYNC_SAME all relays should have the same state
  96. if (relaySync == RELAY_SYNC_SAME) {
  97. for (unsigned short i=0; i<_relays.size(); i++) {
  98. if (i != id) relayStatus(i, status);
  99. }
  100. // If NONE_OR_ONE or ONE and setting ON we should set OFF all the others
  101. } else if (status) {
  102. if (relaySync != RELAY_SYNC_ANY) {
  103. for (unsigned short i=0; i<_relays.size(); i++) {
  104. if (i != id) relayStatus(i, false);
  105. }
  106. }
  107. // If ONLY_ONE and setting OFF we should set ON the other one
  108. } else {
  109. if (relaySync == RELAY_SYNC_ONE) {
  110. unsigned char i = (id + 1) % _relays.size();
  111. relayStatus(i, true);
  112. }
  113. }
  114. recursive = false;
  115. }
  116. }
  117. void relaySave() {
  118. unsigned char bit = 1;
  119. unsigned char mask = 0;
  120. for (unsigned int i=0; i < _relays.size(); i++) {
  121. if (relayStatus(i)) mask += bit;
  122. bit += bit;
  123. }
  124. EEPROM.write(0, mask);
  125. EEPROM.commit();
  126. }
  127. void relayRetrieve() {
  128. recursive = true;
  129. unsigned char bit = 1;
  130. unsigned char mask = EEPROM.read(0);
  131. for (unsigned int i=0; i < _relays.size(); i++) {
  132. relayStatus(i, ((mask & bit) == bit));
  133. bit += bit;
  134. }
  135. recursive = false;
  136. }
  137. void relayToggle(unsigned char id) {
  138. if (id >= _relays.size()) return;
  139. relayStatus(id, !relayStatus(id));
  140. }
  141. unsigned char relayCount() {
  142. return _relays.size();
  143. }
  144. void relayMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  145. String mqttSetter = getSetting("mqttSetter", MQTT_USE_SETTER);
  146. String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
  147. bool sameSetGet = mqttGetter.compareTo(mqttSetter) == 0;
  148. if (type == MQTT_CONNECT_EVENT) {
  149. relayMQTT();
  150. char buffer[strlen(MQTT_RELAY_TOPIC) + mqttSetter.length() + 3];
  151. sprintf(buffer, "%s/+%s", MQTT_RELAY_TOPIC, mqttSetter.c_str());
  152. mqttSubscribe(buffer);
  153. }
  154. if (type == MQTT_MESSAGE_EVENT) {
  155. // Match topic
  156. String t = String(topic + mqttTopicRootLength());
  157. if (!t.startsWith(MQTT_RELAY_TOPIC)) return;
  158. if (!t.endsWith(mqttSetter)) return;
  159. // Get relay ID
  160. unsigned int relayID = topic[strlen(topic) - mqttSetter.length() - 1] - '0';
  161. if (relayID >= relayCount()) {
  162. DEBUG_MSG("[RELAY] Wrong relayID (%d)\n", relayID);
  163. return;
  164. }
  165. // Action to perform
  166. unsigned int value = (char)payload[0] - '0';
  167. if (value == 2) {
  168. relayToggle(relayID);
  169. } else {
  170. relayStatus(relayID, value > 0, !sameSetGet);
  171. }
  172. }
  173. }
  174. void relaySetup() {
  175. #ifdef SONOFF_DUAL
  176. // Two dummy relays for the dual
  177. _relays.push_back((relay_t) {0, 0});
  178. _relays.push_back((relay_t) {0, 0});
  179. #else
  180. #ifdef RELAY1_PIN
  181. _relays.push_back((relay_t) { RELAY1_PIN, RELAY1_PIN_INVERSE });
  182. #endif
  183. #ifdef RELAY2_PIN
  184. _relays.push_back((relay_t) { RELAY2_PIN, RELAY2_PIN_INVERSE });
  185. #endif
  186. #ifdef RELAY3_PIN
  187. _relays.push_back((relay_t) { RELAY3_PIN, RELAY3_PIN_INVERSE });
  188. #endif
  189. #ifdef RELAY4_PIN
  190. _relays.push_back((relay_t) { RELAY4_PIN, RELAY4_PIN_INVERSE });
  191. #endif
  192. #endif
  193. EEPROM.begin(4096);
  194. byte relayMode = getSetting("relayMode", RELAY_MODE).toInt();
  195. for (unsigned int i=0; i < _relays.size(); i++) {
  196. pinMode(_relays[i].pin, OUTPUT);
  197. if (relayMode == RELAY_MODE_OFF) relayStatus(i, false);
  198. if (relayMode == RELAY_MODE_ON) relayStatus(i, true);
  199. }
  200. if (relayMode == RELAY_MODE_SAME) relayRetrieve();
  201. mqttRegister(relayMQTTCallback);
  202. DEBUG_MSG("[RELAY] Number of relays: %d\n", _relays.size());
  203. }