ESP32 / ESP8266 & BME280 / SHT2x sensor with InfluxDB support
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.

SimpleInflux.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * SimpleInflux.cpp
  3. *
  4. * ESP8266 / ESP32 Environmental Sensor
  5. *
  6. * ----------------------------------------------------------------------------
  7. * "THE BEER-WARE LICENSE" (Revision 42):
  8. * <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  9. * you can do whatever you want with this stuff. If we meet some day, and you
  10. * think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  11. * ----------------------------------------------------------------------------
  12. */
  13. #include <Arduino.h>
  14. #include "DebugLog.h"
  15. #include "SimpleInflux.h"
  16. #if defined(ARDUINO_ARCH_AVR)
  17. #include <WiFiLink.h>
  18. WiFiClient client;
  19. #endif
  20. void InfluxData::addTag(const char *name, const char *value) {
  21. if (tag_count < SIMPLE_INFLUX_MAX_ELEMENTS) {
  22. tag_name[tag_count] = name;
  23. tag_value[tag_count] = value;
  24. tag_count++;
  25. }
  26. }
  27. void InfluxData::addValue(const char *name, double value) {
  28. if (value_count < SIMPLE_INFLUX_MAX_ELEMENTS) {
  29. value_name[value_count] = name;
  30. value_value[value_count] = value;
  31. value_count++;
  32. }
  33. }
  34. // https://docs.influxdata.com/influxdb/v1.8/guides/write_data/
  35. boolean Influxdb::write(InfluxData &data) {
  36. #if defined(ARDUINO_ARCH_AVR)
  37. debug.print(F("Writing "));
  38. debug.println(data.dataName());
  39. client.stop();
  40. if (client.connect(db_host, db_port)) {
  41. client.print(F("POST /write?db="));
  42. client.print(db_name);
  43. client.println(F(" HTTP/1.1"));
  44. client.print(F("Host: "));
  45. client.print(db_host);
  46. client.print(F(":"));
  47. client.println(String(db_port));
  48. client.println(F("Connection: close"));
  49. int len = strlen(data.dataName()) + 1;
  50. for (int i = 0; i < data.tagCount(); i++) {
  51. len += strlen(data.tagName(i)) + strlen(data.tagValue(i)) + 2;
  52. }
  53. for (int i = 0; i < data.valueCount(); i++) {
  54. len += strlen(data.valueName(i)) + String(data.valueValue(i)).length() + 1;
  55. if (i > 0) {
  56. len += 1;
  57. }
  58. }
  59. client.print(F("Content-Length: "));
  60. client.println(String(len));
  61. client.println();
  62. client.print(data.dataName());
  63. for (int i = 0; i < data.tagCount(); i++) {
  64. client.print(F(","));
  65. client.print(data.tagName(i));
  66. client.print(F("="));
  67. //client.print(F("\""));
  68. client.print(data.tagValue(i));
  69. //client.print(F("\""));
  70. }
  71. client.print(F(" "));
  72. for (int i = 0; i < data.valueCount(); i++) {
  73. if (i > 0) {
  74. client.print(F(","));
  75. }
  76. client.print(data.valueName(i));
  77. client.print(F("="));
  78. client.print(data.valueValue(i));
  79. }
  80. // we're leaving out the timestamp, it's optional
  81. boolean currentLineIsBlank = true, contains_error = false;
  82. int compare_off = 0;
  83. String compare_to(F("X-Influxdb-Error"));
  84. while (client.connected()) {
  85. if (client.available()) {
  86. char c = client.read();
  87. if (c != '\r') {
  88. debug.write(c);
  89. }
  90. if (compare_off == compare_to.length()) {
  91. contains_error = true;
  92. } else if ((compare_off < compare_to.length()) && (c == compare_to[compare_off])) {
  93. if ((compare_off > 0) || currentLineIsBlank) {
  94. compare_off++;
  95. }
  96. } else {
  97. compare_off = 0;
  98. }
  99. if ((c == '\n') && currentLineIsBlank) {
  100. // http headers ended
  101. break;
  102. }
  103. if (c == '\n') {
  104. // you're starting a new line
  105. currentLineIsBlank = true;
  106. } else if (c != '\r') {
  107. // you've gotten a character on the current line
  108. currentLineIsBlank = false;
  109. }
  110. }
  111. }
  112. client.stop();
  113. debug.println(contains_error ? F("Request failed") : F("Request Done"));
  114. return !contains_error;
  115. } else {
  116. debug.println(F("Error connecting"));
  117. return false; // failed
  118. }
  119. #else
  120. return true; // success
  121. #endif
  122. }