Sending data to influx db using http

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
Groovy3286
Posts: 3
Joined: Mon Jul 04, 2022 9:22 am

Sending data to influx db using http

Post by Groovy3286 » Mon Jul 04, 2022 9:27 am

Hey I have an ESP32 with micropyton firmware, and a locally hosted influx database. I want to send just plain text from the ESP32 to the influx db (have a dht22 sensor hooked up to the ESP32). I am super confused as to how I would go about doing this, looked around and seen POST but not entirely sure how to use that properly.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Sending data to influx db using http

Post by jimmo » Mon Jul 04, 2022 12:23 pm

Groovy3286 wrote:
Mon Jul 04, 2022 9:27 am
Hey I have an ESP32 with micropyton firmware, and a locally hosted influx database. I want to send just plain text from the ESP32 to the influx db (have a dht22 sensor hooked up to the ESP32). I am super confused as to how I would go about doing this, looked around and seen POST but not entirely sure how to use that properly.
Yes, you can use HTTP POST requests to send data -- the docs are here: https://docs.influxdata.com/influxdb/v1 ... fluxdb-api

On MicroPython you can use the urequests module to send a POST request. Depending on which board you're using it may already be included, otherwise you can get it from here https://github.com/micropython/micropyt ... equests.py

Groovy3286
Posts: 3
Joined: Mon Jul 04, 2022 9:22 am

Re: Sending data to influx db using http

Post by Groovy3286 » Mon Jul 04, 2022 2:19 pm

This looks like a lot more code than I've seen either on this forum, or just other websites in general.
url = ''
headers = {'Temperature': temp, 'Humidity' : hum}
data = hum, temp
r = requests.post(url, data=data, headers=headers)
results = r.json()
print(results)
This is what i've got right now, what I am super confused on is how to send directly to an IP address. Is is just IP:Port/file path.
Also confused on how I would input the data directly from the sensor to the DB, and how I would format data and headers. Sorry if im not making sense just been confused on this for super long and have looked at every single page regarding this lol

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Sending data to influx db using http

Post by jimmo » Mon Jul 04, 2022 2:40 pm

Groovy3286 wrote:
Mon Jul 04, 2022 2:19 pm
This looks like a lot more code than I've seen either on this forum, or just other websites in general.
Not quite sure what you mean by this? Can you give an example.
Groovy3286 wrote:
Mon Jul 04, 2022 2:19 pm
r = requests.post(url, data=data, headers=headers)
The "url" here is where you put the ip and port (and path)... e.g. "http://192.168.1.100:12345/write?db=mydb"

So to translate that first example:

Code: Select all

import urequests
url = "http://192.168.1.100:12345/write?db=mydb"
data = "'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'"
r = requests.post(url, data=data)
print(r.json())
FYI urequests on the MicroPython device should work exactly the same as "requests" in regular Python, so definitely easier to get this working on your PC first.
Groovy3286 wrote:
Mon Jul 04, 2022 2:19 pm
how I would format data
I don't know much about influxdb, but that link gives a pretty good example of how to combine measurement, tag keys, etc into the string above.
Groovy3286 wrote:
Mon Jul 04, 2022 2:19 pm
and headers
Based on that example, I don't think any headers are required.
Groovy3286 wrote:
Mon Jul 04, 2022 2:19 pm
Sorry if im not making sense just been confused on this for super long and have looked at every single page regarding this lol
There's a lot of different concepts here!!! It's definitely worth trying to understand one thing at a time -- try getting this working in regular Python then move it to MicroPython.

The key thing to understand is the HTTP protocol, how posting data works etc. Influx has a particularly simple use of post (there's no complicated encoding or anything, I suspect a lot of other guides about HTTP are making this sound more complicated).

Groovy3286
Posts: 3
Joined: Mon Jul 04, 2022 9:22 am

Re: Sending data to influx db using http

Post by Groovy3286 » Mon Jul 04, 2022 2:58 pm

viewtopic.php?t=5496 For example, on here. It shows a relatively short piece of code to send data to a URL.

Ok that makes sense thank you, was looking around and saw you had to use a file directory as well as the IP and port just wanted confirmation.

I saw that it was pretty similar to normal python when I was researching it, but the main thing I'm confused on is just getting it to send the raw output of the DHT22.
Definitely making it sound more complicated lol, i've seen tons of other ways of doing this and they are all slightly similar, but vastly different. It's what was mainly making me confused earlier.

Post Reply