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.

264 lines
6.7 KiB

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