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.

267 lines
6.9 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(bool invert) {
  128. recursive = true;
  129. unsigned char bit = 1;
  130. unsigned char mask = invert ? ~EEPROM.read(0) : EEPROM.read(0);
  131. for (unsigned int i=0; i < _relays.size(); i++) {
  132. relayStatus(i, ((mask & bit) == bit));
  133. bit += bit;
  134. }
  135. if (invert) {
  136. relaySave();
  137. }
  138. recursive = false;
  139. }
  140. void relayToggle(unsigned char id) {
  141. if (id >= _relays.size()) return;
  142. relayStatus(id, !relayStatus(id));
  143. }
  144. unsigned char relayCount() {
  145. return _relays.size();
  146. }
  147. void relayMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  148. String mqttSetter = getSetting("mqttSetter", MQTT_USE_SETTER);
  149. String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
  150. bool sameSetGet = mqttGetter.compareTo(mqttSetter) == 0;
  151. if (type == MQTT_CONNECT_EVENT) {
  152. relayMQTT();
  153. char buffer[strlen(MQTT_RELAY_TOPIC) + mqttSetter.length() + 3];
  154. sprintf(buffer, "%s/+%s", MQTT_RELAY_TOPIC, mqttSetter.c_str());
  155. mqttSubscribe(buffer);
  156. }
  157. if (type == MQTT_MESSAGE_EVENT) {
  158. // Match topic
  159. String t = String(topic + mqttTopicRootLength());
  160. if (!t.startsWith(MQTT_RELAY_TOPIC)) return;
  161. if (!t.endsWith(mqttSetter)) return;
  162. // Get relay ID
  163. unsigned int relayID = topic[strlen(topic) - mqttSetter.length() - 1] - '0';
  164. if (relayID >= relayCount()) {
  165. DEBUG_MSG("[RELAY] Wrong relayID (%d)\n", relayID);
  166. return;
  167. }
  168. // Action to perform
  169. unsigned int value = (char)payload[0] - '0';
  170. if (value == 2) {
  171. relayToggle(relayID);
  172. } else {
  173. relayStatus(relayID, value > 0, !sameSetGet);
  174. }
  175. }
  176. }
  177. void relaySetup() {
  178. #ifdef SONOFF_DUAL
  179. // Two dummy relays for the dual
  180. _relays.push_back((relay_t) {0, 0});
  181. _relays.push_back((relay_t) {0, 0});
  182. #else
  183. #ifdef RELAY1_PIN
  184. _relays.push_back((relay_t) { RELAY1_PIN, RELAY1_PIN_INVERSE });
  185. #endif
  186. #ifdef RELAY2_PIN
  187. _relays.push_back((relay_t) { RELAY2_PIN, RELAY2_PIN_INVERSE });
  188. #endif
  189. #ifdef RELAY3_PIN
  190. _relays.push_back((relay_t) { RELAY3_PIN, RELAY3_PIN_INVERSE });
  191. #endif
  192. #ifdef RELAY4_PIN
  193. _relays.push_back((relay_t) { RELAY4_PIN, RELAY4_PIN_INVERSE });
  194. #endif
  195. #endif
  196. EEPROM.begin(4096);
  197. byte relayMode = getSetting("relayMode", RELAY_MODE).toInt();
  198. for (unsigned int i=0; i < _relays.size(); i++) {
  199. pinMode(_relays[i].pin, OUTPUT);
  200. if (relayMode == RELAY_MODE_OFF) relayStatus(i, false);
  201. if (relayMode == RELAY_MODE_ON) relayStatus(i, true);
  202. }
  203. if (relayMode == RELAY_MODE_SAME) relayRetrieve(false);
  204. if (relayMode == RELAY_MODE_TOOGLE) relayRetrieve(true);
  205. mqttRegister(relayMQTTCallback);
  206. DEBUG_MSG("[RELAY] Number of relays: %d\n", _relays.size());
  207. }