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.

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