Using http post *without* curl to send data to InfluxDB

I have been successful sending data the Influx DB using the ESP32 Influx library. I need though to develop a lighter and more flexible means of sending data to Influx using just http post, without curl, but I have not found any examples that I can get working on ESP32. I connect to the URL but no data appears in the bucket. I think I may be failing at either the authentication via the authorization token and/or in the format of the data I am trying to send. Perhaps I also need to add the Org to the input??? Your inputs are appreciated. The core of the code is below. (diagnostic output also posted below).

#define IF_BUCKET "TESTBucket"
#define IF_DEVICE "TESTDevice"
#define IF_LOCATION "Lab"
#define IF_SENSOR "Channel_1"
#define IF_URL "influx.local:8086"
#define IF_TOKEN "OneLongToken==="

    WiFiClient client;
    HTTPClient http;

    String HOST_LINE = (String)IF_URL + "/?bucket=" + IF_BUCKET;
    ESP_LOGD(TAG, "HOST_LINE = %s", HOST_LINE.c_str());
    http.begin(client, HOST_LINE);

    http.addHeader("Authorization", (String)"Bearer " + IF_TOKEN);

    // referred to
    String DATA_LINE = (String) "Energy,Device=" + IF_DEVICE + "," + "Location=" + IF_LOCATION + "," + "Sensor=" + IF_SENSOR + " " + "value=" "42"; // excluding timestamp
    ESP_LOGD(TAG, "DATA_LINE = %s", DATA_LINE.c_str());
    int httpResponseCode = http.POST(DATA_LINE);

    ESP_LOGI(TAG, "HTTP Response code: %d", httpResponseCode);

    // Free resources


[  1826][D][main.cpp:84] setup(): [setup] HOST_LINE =
[  1835][V][HTTPClient.cpp:252] beginInternal(): url:
[  1844][D][HTTPClient.cpp:303] beginInternal(): protocol: http, host: port: 8086 url: /?bucket=TESTBucket
[  1855][D][main.cpp:94] setup(): [setup] DATA_LINE = Energy,Device=TESTDevice,Location=Lab,Sensor=Channel_1 value=42
[  1865][D][HTTPClient.cpp:598] sendRequest(): request type: 'POST' redirCount: 0

[  1901][D][HTTPClient.cpp:1156] connect():  connected to
[  1964][V][HTTPClient.cpp:1250] handleHeaderResponse(): RX: 'HTTP/1.1 200 OK'
[  1964][V][HTTPClient.cpp:1250] handleHeaderResponse(): RX: 'Accept-Ranges: bytes'
[  1967][V][HTTPClient.cpp:1250] handleHeaderResponse(): RX: 'Cache-Control: public, max-age=3600'
[  1976][V][HTTPClient.cpp:1250] handleHeaderResponse(): RX: 'Content-Length: 515'
[  1983][V][HTTPClient.cpp:1250] handleHeaderResponse(): RX: 'Content-Type: text/html; charset=utf-8'
[  1992][V][HTTPClient.cpp:1250] handleHeaderResponse(): RX: 'Etag: "5152816413"'
[  1999][V][HTTPClient.cpp:1250] handleHeaderResponse(): RX: 'Last-Modified: Wed, 28 Dec 2022 21:04:13 GMT'
[  2009][V][HTTPClient.cpp:1250] handleHeaderResponse(): RX: 'X-Influxdb-Build: OSS'
[  2016][V][HTTPClient.cpp:1250] handleHeaderResponse(): RX: 'X-Influxdb-Version: v2.6.1'
[  2024][V][HTTPClient.cpp:1250] handleHeaderResponse(): RX: 'Date: Wed, 12 Apr 2023 22:29:29 GMT'
[  2033][V][HTTPClient.cpp:1250] handleHeaderResponse(): RX: ''
[  2038][D][HTTPClient.cpp:1307] handleHeaderResponse(): code: 200
[  2044][D][HTTPClient.cpp:1310] handleHeaderResponse(): size: 515
[  2050][D][HTTPClient.cpp:628] sendRequest(): sendRequest code=200

[  2056][I][main.cpp:97] setup(): [setup] HTTP Response code: 200
[  2062][D][HTTPClient.cpp:388] disconnect(): still data in buffer (515), clean up.

[  2070][E][WiFiClient.cpp:517] flush(): fail on fd 48, errno: 11, "No more processes"
[  2078][D][HTTPClient.cpp:393] disconnect(): tcp keep open for reuse

Anybody have an input here? Anybody???

well if anyone is interested… here is how you do it… with some notes to help you out.

#include <HTTPClient.h>

#define IF_ORG "Company%20Inc" // space requires %20 (will not work without this if there is a space)
#define IF_BUCKET "TESTBucket"
#define IF_DEVICE "TESTDevice"
#define IF_LOCATION "Lab"
#define IF_SENSOR "Channel_1"
#define IF_URL "  // fake IP address, port is important
#define IF_TOKEN "YourLongTokenGoesHere0*)*)&*-!!"

    // make up some data
    float myData = random(1,100) * PI;

    WiFiClient client;
    HTTPClient http;

    String hostLine = (String)IF_URL + "/api/v2/write?org=" + IF_ORG + "&bucket=" + IF_BUCKET;
    http.begin(client, hostLine);

    http.setReuse(false);  // so connection closes instead of using keep-alive

    http.addHeader("Authorization", (String) "Token " + IF_TOKEN); // case sensitive
    http.addHeader("Content-Type", "text/plain");  // case sensitive

    String dataLine = (String) "Energy,device=" + IF_DEVICE + "," + "location=" + IF_LOCATION + "," + "sensor=" + IF_SENSOR + " " + "value=" + myData; // excluding timestamp
    int httpResponseCode = http.POST(dataLine);

    ESP_LOGI("finishing up", "HTTP Response code: %d", httpResponseCode);

    // Free resources
1 Like

I rather wonder what flexibility you miss from the InfluxDB Arduino Client?

The ESP32 Influxdb client only supports wifi and Ethernet. It does not support other transport methods. For at least three years, the lack of support of GSM, such as being compatible with Tiny GSM, has been in the issues list of the driver on GitHub. Now, for my case, I need to support GSM, Swarm, Lora, and Ethernet in addition to wifi and Ethernet. This means I need to send the right http messages via these other transport streams.

Thanks for the feedback. Sure, supporting TinyGSM is a nice feature. But the use case is sending data to a publicly available server, not a local network, and thus needs TLS, and it is supported by only a few GSM modules (SIM800).
Actually, work on supporting other communication layers (TinyGSM, WiFiNINA) is nearly completed in the library. I wrote it in December 2022 but hadn’t had time to finish it yet.
I’m using LoRa as well. LoRa is definitely not suitable for sending such bulky data as the line protocol is. LoRa gateway works here nicely.
I don’t know Swarm, but if its client supports the Client interface, it should work in the next version.

Sounds great. I’ll be sure to check it out. When do you expect to release it?

BTW, I did manage to send my data to my InfluxDB server via GSM using SIM800. Pretty simple in the end now that I know the grammar influx is looking for.

How is your progress? Is there a link to the new release yet?

Any news yet on your release supporting GSM?