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.

132 lines
3.8 KiB

  1. /*
  2. THINGSPEAK MODULE
  3. Copyright (C) 2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if THINGSPEAK_SUPPORT
  6. #include <ESP8266WiFi.h>
  7. bool _tspk_enabled = false;
  8. WiFiClientSecure _tspk_client;
  9. char * _tspk_queue[0];
  10. // -----------------------------------------------------------------------------
  11. void _tspkWebSocketOnSend(JsonObject& root) {
  12. root["tspkVisible"] = 1;
  13. root["tspkEnabled"] = getSetting("idbEnabled", THINGSPEAK_ENABLED).toInt() == 1;
  14. JsonArray& relays = root.createNestedArray("tspkRelays");
  15. for (byte i=0; i<relayCount(); i++) {
  16. relays.add(getSetting("tspkRelay", i, 0).toInt());
  17. }
  18. #if SENSOR_SUPPORT
  19. JsonArray& list = root.createNestedArray("tspkMagnitudes");
  20. for (byte i=0; i<magnitudeCount(); i++) {
  21. JsonObject& element = list.createNestedObject();
  22. element["name"] = magnitudeName(i);
  23. element["type"] = magnitudeType(i);
  24. element["index"] = magnitudeIndex(i);
  25. element["idx"] = getSetting("tspkMagnitude", i, 0).toInt();
  26. }
  27. #endif
  28. }
  29. void _tspkConfigure() {
  30. _tspk_enabled = getSetting("tspkEnabled", THINGSPEAK_ENABLED).toInt() == 1;
  31. if (_tspk_enabled && (getSetting("tspkKey").length() == 0)) {
  32. _tspk_enabled = false;
  33. setSetting("tspkEnabled", 0);
  34. }
  35. }
  36. bool _tspkPost(String data) {
  37. if (_tspk_client.connect(THINGSPEAK_HOST, 443)) {
  38. _tspk_client.println("POST " + String(THINGSPEAK_URL) + " HTTP/1.1");
  39. _tspk_client.println("Host: " + String(THINGSPEAK_HOST));
  40. //_tspk_client.println("User-Agent: ESPurna");
  41. _tspk_client.println("Connection: close");
  42. _tspk_client.println("Content-Type: application/x-www-form-urlencoded;");
  43. _tspk_client.print("Content-Length: ");
  44. _tspk_client.println(data.length());
  45. _tspk_client.println();
  46. _tspk_client.println(data);
  47. delay(10);
  48. String response = _tspk_client.readString();
  49. int bodypos = response.indexOf("\r\n\r\n") + 4;
  50. Serial.println(response.substring(bodypos));
  51. return true;
  52. }
  53. return false;
  54. }
  55. // -----------------------------------------------------------------------------
  56. bool tspkSendRelay(unsigned char index, unsigned char status, bool enqueue) {
  57. if (!_tspk_enabled) return true;
  58. unsigned char id = getSetting("tspkRelay", index, 0).toInt();
  59. if (id > 0) {
  60. --id;
  61. char payload[3];
  62. itoa(status ? 1 : 0, payload, 10);
  63. if (_tspk_queue[id]) free(_tspk_queue[id]);
  64. _tspk_queue[id] = strdup(payload);
  65. if (!enqueue) tspkFlush();
  66. }
  67. }
  68. bool tspkSendMeasurement(unsigned char index, char * payload, bool enqueue) {
  69. if (!_tspk_enabled) return true;
  70. unsigned char id = getSetting("tspkMagnitude", index, 0).toInt();
  71. if (id > 0) {
  72. --id;
  73. if (_tspk_queue[id]) free(_tspk_queue[id]);
  74. _tspk_queue[id] = strdup(payload);
  75. if (!enqueue) tspkFlush();
  76. }
  77. }
  78. bool tspkFlush() {
  79. if (!_tspk_enabled) return true;
  80. if (!wifiConnected() || (WiFi.getMode() != WIFI_STA)) return true;
  81. String data;
  82. // Walk the fields
  83. for (unsigned char id=0; id<8; id++) {
  84. if (_tspk_queue[id]) {
  85. if (data.length() > 0) data = data + String("&");
  86. data = data + String("field") + String(id+1) + String("=") + String(_tspk_queue[id]);
  87. free(_tspk_queue[id]);
  88. }
  89. }
  90. // POST data if any
  91. if (data.length() > 0) {
  92. data = data + String("&api_key=") + getSetting("tspkKey");
  93. _tspkPost(data);
  94. }
  95. }
  96. bool tspkEnabled() {
  97. return _tspk_enabled;
  98. }
  99. void tspkSetup() {
  100. _tspkConfigure();
  101. #if WEB_SUPPORT
  102. wsOnSendRegister(_tspkWebSocketOnSend);
  103. wsOnAfterParseRegister(_tspkConfigure);
  104. #endif
  105. }
  106. #endif