0

CIrcuit Python and local Influxdb
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

CIrcuit Python and local Influxdb

by Jsaddiction on Sun Feb 09, 2020 9:05 pm

I have a feather M4 running circuitpython with airlift feather wing. I have a local server running influxdb and grafana. I am trying to log data into the DB via wifi connection and the only response from the server i can seem to get is

{'error': 'unable to parse \'"plant2,host=livingroom moisture=276i,temp=76i"\': invalid number'}

I can add points via curl from cmd line on windows and from python3 library running on my laptop.

Code: Select all | TOGGLE FULL SIZE
def push_data(self, moisture, temp):
        influxDB_str = b'plant2,host=livingroom moisture={0}i,temp={1}i'.format(str(moisture), str(temp))
        print(json.dumps(influxDB_str))
        #try:
        response = self.wifi.post(self.write_url, data=influxDB_str)
        print(response.json())
        response.close()
        #except:
        #    print('error in sending POST, resetting')
        #    self.wifi.reset()


Any Ideas???

Jsaddiction
 
Posts: 5
Joined: Sun Feb 09, 2020 8:55 pm

Re: CIrcuit Python and local Influxdb

by tannewt on Mon Feb 10, 2020 5:45 pm

It kind of looks like the adafruit requests library is adding quotes around the data. I think most uses use the json= param instead. The code for it is here: https://github.com/adafruit/Adafruit_Ci ... ts.py#L214

What happens when you print just `influxDB_str`? I'm confused why you are using json at all.

tannewt
 
Posts: 1490
Joined: Thu Oct 06, 2016 8:48 pm

Re: CIrcuit Python and local Influxdb

by Jsaddiction on Mon Feb 10, 2020 5:55 pm

Honestly I have tried it so many different ways I'm not even sure why I am using JSON... As I understand using the data= param for a request should just pass the string. I was using the json.dumps function as another attempt to make it work. Thanks for the link to the library, I will have to browse through it and see just how to get this string passed. What do the ` around the variable do?

If you weren't aware of how influxdb handles http requests, a curl request can be made using the --data-binary flag like

Code: Select all | TOGGLE FULL SIZE
curl -i -XPOST 'http://hostIP:8086/write?db=test' --data-binary 'plant2,host=livingroom moisture=1200i'

Jsaddiction
 
Posts: 5
Joined: Sun Feb 09, 2020 8:55 pm

Re: CIrcuit Python and local Influxdb

by tannewt on Mon Feb 10, 2020 6:02 pm

The `` are just markdown for inline code that the forum doesn't know about but I use anyway.

I wonder if the data-binary also sets a different content type. Can you get it going with Python's normal request library?

tannewt
 
Posts: 1490
Joined: Thu Oct 06, 2016 8:48 pm

Re: CIrcuit Python and local Influxdb

by Jsaddiction on Mon Feb 10, 2020 6:23 pm

works easily with python3's full library so it has to be something with the adafruit's rendition.

Jsaddiction
 
Posts: 5
Joined: Sun Feb 09, 2020 8:55 pm

Re: CIrcuit Python and local Influxdb

by Jsaddiction on Mon Feb 10, 2020 7:01 pm

Actually this works in python3

Code: Select all | TOGGLE FULL SIZE
import requests
import time

moisture = 1500
temp = 76
url = 'http://hostServer:8086/write?db=test'
influxDB_str = 'plant2,host=livingroom moisture={0}i,temp={1}i'.format(moisture, temp)

while True:
    reply = requests.post(url, data=influxDB_str)
    print(reply)
    time.sleep(3)

Jsaddiction
 
Posts: 5
Joined: Sun Feb 09, 2020 8:55 pm

Re: CIrcuit Python and local Influxdb

by Jsaddiction on Mon Feb 10, 2020 7:16 pm

Got it all figured out. Not sure what I had been doing wrong yesterday but today it decided to work as expected. Anyway for people wondering how to push data to an InfluxDB the following code works.

self.wifi refers to an instance of the adafruit_esp32spi_wifimanager

Code: Select all | TOGGLE FULL SIZE
def push_data(self, moisture, temp):
        influxDB_str = 'plant2,host=livingroom moisture={0}i,temp={1}i'.format(str(moisture), str(temp))
        response = self.wifi.post(self.write_url, data=influxDB_str)
        status_code = response.status_code
        response.close()
        return status_code

Jsaddiction
 
Posts: 5
Joined: Sun Feb 09, 2020 8:55 pm

Re: CIrcuit Python and local Influxdb

by tannewt on Tue Feb 11, 2020 2:26 pm

Awesome! I'm glad you got it going.

tannewt
 
Posts: 1490
Joined: Thu Oct 06, 2016 8:48 pm

Please be positive and constructive with your questions and comments.