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

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