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.

344 lines
8.6 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 <Ticker.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. Ticker pulseTicker;
  19. // -----------------------------------------------------------------------------
  20. // RELAY
  21. // -----------------------------------------------------------------------------
  22. void relayMQTT(unsigned char id) {
  23. if (id >= _relays.size()) return;
  24. String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
  25. char buffer[strlen(MQTT_RELAY_TOPIC) + mqttGetter.length() + 3];
  26. sprintf(buffer, "%s/%d%s", MQTT_RELAY_TOPIC, id, mqttGetter.c_str());
  27. mqttSend(buffer, relayStatus(id) ? "1" : "0");
  28. }
  29. void relayMQTT() {
  30. for (unsigned int i=0; i < _relays.size(); i++) {
  31. relayMQTT(i);
  32. }
  33. }
  34. String relayString() {
  35. DynamicJsonBuffer jsonBuffer;
  36. JsonObject& root = jsonBuffer.createObject();
  37. JsonArray& relay = root.createNestedArray("relayStatus");
  38. for (unsigned char i=0; i<relayCount(); i++) {
  39. relay.add(relayStatus(i));
  40. }
  41. String output;
  42. root.printTo(output);
  43. return output;
  44. }
  45. void relayWS() {
  46. String output = relayString();
  47. wsSend(output.c_str());
  48. }
  49. bool relayStatus(unsigned char id) {
  50. #ifdef SONOFF_DUAL
  51. if (id >= 2) return false;
  52. return ((dualRelayStatus & (1 << id)) > 0);
  53. #else
  54. if (id >= _relays.size()) return false;
  55. bool status = (digitalRead(_relays[id].pin) == HIGH);
  56. return _relays[id].reverse ? !status : status;
  57. #endif
  58. }
  59. void relayPulseBack(unsigned char id) {
  60. relayToggle(id);
  61. pulseTicker.detach();
  62. }
  63. void relayPulse(unsigned char id) {
  64. byte relayPulseMode = getSetting("relayPulseMode", RELAY_PULSE_MODE).toInt();
  65. if (relayPulseMode == RELAY_PULSE_NONE) return;
  66. bool status = relayStatus(id);
  67. bool pulseStatus = (relayPulseMode == RELAY_PULSE_ON);
  68. if (pulseStatus == status) {
  69. pulseTicker.detach();
  70. return;
  71. }
  72. pulseTicker.attach(
  73. getSetting("relayPulseTime", RELAY_PULSE_TIME).toInt(),
  74. relayPulseBack,
  75. id
  76. );
  77. }
  78. unsigned int relayPulseMode() {
  79. unsigned int value = getSetting("relayPulseMode", RELAY_PULSE_MODE).toInt();
  80. return value;
  81. }
  82. void relayPulseMode(unsigned int value, bool report) {
  83. setSetting("relayPulseMode", value);
  84. /*
  85. if (report) {
  86. String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
  87. char topic[strlen(MQTT_RELAY_TOPIC) + mqttGetter.length() + 10];
  88. sprintf(topic, "%s/pulse%s", MQTT_RELAY_TOPIC, mqttGetter.c_str());
  89. char value[2];
  90. sprintf(value, "%d", value);
  91. mqttSend(topic, value);
  92. }
  93. */
  94. char message[20];
  95. sprintf(message, "{\"relayPulseMode\": %d}", value);
  96. wsSend(message);
  97. #ifdef LED_PULSE
  98. digitalWrite(LED_PULSE, relayPulseMode != RELAY_PULSE_NONE);
  99. #endif
  100. }
  101. void relayPulseMode(unsigned int value) {
  102. relayPulseMode(value, true);
  103. }
  104. void relayPulseToggle() {
  105. unsigned int value = relayPulseMode();
  106. value = (value == RELAY_PULSE_NONE) ? RELAY_PULSE_OFF : RELAY_PULSE_NONE;
  107. relayPulseMode(value);
  108. }
  109. bool relayStatus(unsigned char id, bool status, bool report) {
  110. if (id >= _relays.size()) return false;
  111. bool changed = false;
  112. if (relayStatus(id) != status) {
  113. DEBUG_MSG("[RELAY] %d => %s\n", id, status ? "ON" : "OFF");
  114. changed = true;
  115. #ifdef SONOFF_DUAL
  116. dualRelayStatus ^= (1 << id);
  117. Serial.flush();
  118. Serial.write(0xA0);
  119. Serial.write(0x04);
  120. Serial.write(dualRelayStatus);
  121. Serial.write(0xA1);
  122. Serial.flush();
  123. #else
  124. digitalWrite(_relays[id].pin, _relays[id].reverse ? !status : status);
  125. #endif
  126. if (!recursive) {
  127. relayPulse(id);
  128. relaySync(id);
  129. relaySave();
  130. }
  131. #ifdef ENABLE_DOMOTICZ
  132. domoticzSend(id);
  133. #endif
  134. }
  135. if (report) relayMQTT(id);
  136. if (!recursive) relayWS();
  137. return changed;
  138. }
  139. bool relayStatus(unsigned char id, bool status) {
  140. return relayStatus(id, status, true);
  141. }
  142. void relaySync(unsigned char id) {
  143. if (_relays.size() > 1) {
  144. recursive = true;
  145. byte relaySync = getSetting("relaySync", RELAY_SYNC).toInt();
  146. bool status = relayStatus(id);
  147. // If RELAY_SYNC_SAME all relays should have the same state
  148. if (relaySync == RELAY_SYNC_SAME) {
  149. for (unsigned short i=0; i<_relays.size(); i++) {
  150. if (i != id) relayStatus(i, status);
  151. }
  152. // If NONE_OR_ONE or ONE and setting ON we should set OFF all the others
  153. } else if (status) {
  154. if (relaySync != RELAY_SYNC_ANY) {
  155. for (unsigned short i=0; i<_relays.size(); i++) {
  156. if (i != id) relayStatus(i, false);
  157. }
  158. }
  159. // If ONLY_ONE and setting OFF we should set ON the other one
  160. } else {
  161. if (relaySync == RELAY_SYNC_ONE) {
  162. unsigned char i = (id + 1) % _relays.size();
  163. relayStatus(i, true);
  164. }
  165. }
  166. recursive = false;
  167. }
  168. }
  169. void relaySave() {
  170. unsigned char bit = 1;
  171. unsigned char mask = 0;
  172. for (unsigned int i=0; i < _relays.size(); i++) {
  173. if (relayStatus(i)) mask += bit;
  174. bit += bit;
  175. }
  176. EEPROM.write(0, mask);
  177. EEPROM.commit();
  178. }
  179. void relayRetrieve() {
  180. recursive = true;
  181. unsigned char bit = 1;
  182. unsigned char mask = EEPROM.read(0);
  183. for (unsigned int i=0; i < _relays.size(); i++) {
  184. relayStatus(i, ((mask & bit) == bit));
  185. bit += bit;
  186. }
  187. recursive = false;
  188. }
  189. void relayToggle(unsigned char id) {
  190. if (id >= _relays.size()) return;
  191. relayStatus(id, !relayStatus(id));
  192. }
  193. unsigned char relayCount() {
  194. return _relays.size();
  195. }
  196. void relayMQTTCallback(unsigned int type, const char * topic, const char * payload) {
  197. String mqttSetter = getSetting("mqttSetter", MQTT_USE_SETTER);
  198. String mqttGetter = getSetting("mqttGetter", MQTT_USE_GETTER);
  199. bool sameSetGet = mqttGetter.compareTo(mqttSetter) == 0;
  200. if (type == MQTT_CONNECT_EVENT) {
  201. relayMQTT();
  202. char buffer[strlen(MQTT_RELAY_TOPIC) + mqttSetter.length() + 10];
  203. sprintf(buffer, "%s/+%s", MQTT_RELAY_TOPIC, mqttSetter.c_str());
  204. mqttSubscribe(buffer);
  205. sprintf(buffer, "%s/pulse%s", MQTT_RELAY_TOPIC, mqttSetter.c_str());
  206. mqttSubscribe(buffer);
  207. }
  208. if (type == MQTT_MESSAGE_EVENT) {
  209. // Match topic
  210. String t = String(topic + mqttTopicRootLength());
  211. if (!t.startsWith(MQTT_RELAY_TOPIC)) return;
  212. if (!t.endsWith(mqttSetter)) return;
  213. // Get value
  214. unsigned int value = (char)payload[0] - '0';
  215. // Pulse topic
  216. if (t.indexOf("pulse") > 0) {
  217. relayPulseMode(value, !sameSetGet);
  218. return;
  219. }
  220. // Get relay ID
  221. unsigned int relayID = topic[strlen(topic) - mqttSetter.length() - 1] - '0';
  222. if (relayID >= relayCount()) {
  223. DEBUG_MSG("[RELAY] Wrong relayID (%d)\n", relayID);
  224. return;
  225. }
  226. // Action to perform
  227. if (value == 2) {
  228. relayToggle(relayID);
  229. } else {
  230. relayStatus(relayID, value > 0, !sameSetGet);
  231. }
  232. }
  233. }
  234. void relaySetup() {
  235. #ifdef SONOFF_DUAL
  236. // Two dummy relays for the dual
  237. _relays.push_back((relay_t) {0, 0});
  238. _relays.push_back((relay_t) {0, 0});
  239. #else
  240. #ifdef RELAY1_PIN
  241. _relays.push_back((relay_t) { RELAY1_PIN, RELAY1_PIN_INVERSE });
  242. #endif
  243. #ifdef RELAY2_PIN
  244. _relays.push_back((relay_t) { RELAY2_PIN, RELAY2_PIN_INVERSE });
  245. #endif
  246. #ifdef RELAY3_PIN
  247. _relays.push_back((relay_t) { RELAY3_PIN, RELAY3_PIN_INVERSE });
  248. #endif
  249. #ifdef RELAY4_PIN
  250. _relays.push_back((relay_t) { RELAY4_PIN, RELAY4_PIN_INVERSE });
  251. #endif
  252. #endif
  253. EEPROM.begin(4096);
  254. byte relayMode = getSetting("relayMode", RELAY_MODE).toInt();
  255. for (unsigned int i=0; i < _relays.size(); i++) {
  256. pinMode(_relays[i].pin, OUTPUT);
  257. if (relayMode == RELAY_MODE_OFF) relayStatus(i, false);
  258. if (relayMode == RELAY_MODE_ON) relayStatus(i, true);
  259. }
  260. if (relayMode == RELAY_MODE_SAME) relayRetrieve();
  261. mqttRegister(relayMQTTCallback);
  262. DEBUG_MSG("[RELAY] Number of relays: %d\n", _relays.size());
  263. }