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.

252 lines
6.2 KiB

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