tobiasschuerg / InfluxDB-Client-for-Arduino

Simple library for sending measurements to an InfluxDB with a single network request. Supports ESP8266 and ESP32.
MIT License
379 stars 94 forks source link

Ethernet Shield Compatibility? #127

Open faultfactory opened 3 years ago

faultfactory commented 3 years ago

Is there anything in this library that would not work with an arduino ethernet shield vs one of the wifi modules? If i wanted to modify this code to work in that manner are there some pointers you can provide?

vlastahajek commented 3 years ago

@faultfactory, this library heavily relies on HTTPClient and WifiClient classes from ESP SDKs. You would have to get sth similar for arduino devices or update all related parts for different HTTPClient. This is, in fact, sth which makes implementation for WiFiNINA compatible devices difficult.

faultfactory commented 3 years ago

Understood.

I'll explore the topic a bit more. Would be great to add a fork or a pre processor directive to make this

faultfactory commented 3 years ago

@vlastahajek

I've dug into this a bit and I believe I understand the problem more. There's a base arduino library called HttpClient, separate from the ESP ones and it looks to have similar functionality with slightly different names. For example, addHeader() in the ESP vs sendHeader() in the base arduino library. The same exists for connectionReuse.

I am going to attempt a fork to see if this works but could use your input to help limit my focus to where it matters. Assuming for the moment I am only interested in writing to a (local network, not cloud) database, and not sending queries, it looks like the only files i would need to modify are InfluxDBClient.h and InfluxDBClient.cpp. The other files appear to be wrappers and abstractions. Am I missing anything?

faultfactory commented 3 years ago

This is an extremely hard nut to crack as there are so many library functions in the ESP packages that need to be recreated and generally the existing library relies on them so heavily, that the existing debug functionality is null and void.

@tobiasschuerg @vlastahajek Might you know of a minimum viable product sketch that is able to write to InfluxDB from an ethernet shield? I have tried numerous combinations of influx ENV variable settings and sample sketches. Nothing appears to work, however i know the system is operable because i have code running on a raspberry pi that easily writes to the database.

vlastahajek commented 3 years ago

@faultfactory, basicaly, for writing data to local server you just need to send a single HTTP POST request. See https://docs.influxdata.com/influxdb/v1.8/guides/write_data/ for more details You will probably find many examples how to construct HTTP POST request without a HTTP client library for Ethernet Shield.

faultfactory commented 3 years ago

@vlastahajek I grinded on it another day and got it to work by watching the logs for the influxdb container from the portainer logs. My db is named ENV_STATUS I'm using what I assume to be the v1 authentication in the post header (i think?) like this: client.println("POST /write?db=ENV_STATUS&p=PW&u=USER HTTP/1.1");

If I can wrap up something that looks library-like, I'll see if it fits into the context of the existing api. Thanks for fielding the questions.

bananograms commented 3 years ago

I'm still having trouble with the ethernet shield in writing to the influxdb client with HTTP post alone, is it because Influx2.0 needs to have the bucket, enviroment, and token somewhere to be referenced in order to write data? Sorry for the question, but I'm trying to figure out if it would still be possible to take the existing influxdb library or if it was determined HTTP post was the way to go for the ethernet shield

faultfactory commented 3 years ago

I ended up having to go with the HTTP post directly, but I am using Influx 1.8 as the 2.0 docker wasn't available when i was working on this

void sendHttpPostRequest()
{
  client.println("POST /write?db=DATABASENAME&p=PASSWORD&u=USERNAME HTTP/1.1");
  client.println("User-Agent: arduino-ethernet");
  client.println("Host: NUMERIC_IP:8086");
  client.println("Accept: */*");
  client.println("Content-Length: " + String(strlen(buff)));
  client.println("Content-Type: application/x-www-form-urlencoded");
  client.println("Connection: close");
  client.println("");
}

I'm logging hvac status information. One of my lines is the following.

    sprintf(buff, 
        "FurnaceSignalState Cooling=%ii,Furnace\\ Fan=%ii,Heating=%ii,Humidifier=%ii\nFanStatus CommandPWM=%ii,FanSpeed=%s", 
        stateMap[COOL],
        stateMap[FURNACE_FAN],
        stateMap[HEAT],
        stateMap[HUMIDIFY],
        fanCommandPWM,
        spdStr);

I'll run

    sendHttpPostRequest();
    client.println(buff);

and it sends the data to influx

vlastahajek commented 3 years ago

@bananograms, try HTTP post with curl first to understand URL syntax and necessary headers. Look at InfluxDB 2 Write doc.

aldomeza-dev commented 2 years ago

Hi guys, Sooo, I'm also having trouble using the library with my ethernet shield, I'm getting stuck in the timeSync function before even connecting to my InfluxDB, if i get the timeSync to work will i be able to use the library the same? or do i need to go for the HTTP POST method? Has anyone solve the timeSync step using ethernet?

Right now i have working code to write to my DB some sensor measurements but it only works with WiFI (with ethernet is gets stuck on the timeSync)...

EDIT#1 If someone could give me an idea of how to structure the http post for my ESP32 Ethernet shield i would be so grateful

vlastahajek commented 2 years ago

@aldomeza-dev, what is the ethernet chip on your shield? W5500? LAN8720?

rjjrbatarao commented 2 years ago

any libraries used by WiFi class works in esp32 if you use LAN8720 so no need to modify anything on this library, incase of w5500 its more complicated but I managed to make it work just like lan8720, I extracted the example from esp32 sdk and build my own header file now its working perfectly I can freely switch to wifi and lan without changes to every libraries I used, as for esp8266 use the w5500lwip library, hope this helps

vlastahajek commented 2 years ago

@rjjrbatarao, exactly. A driver for LAN8720 is part of the ESP-IDF and uses the lwIP layer. So it can be transparently used by WiFi Client. There is an example for getting network information part of the ESP32 examples

Great job making it work with W5500. Do you have a driver for it available on GitHub?

faultfactory commented 2 years ago

@rjjrbatarao yes please integrate this to the library, that'd save us all so much pain.

aldomeza-dev commented 1 year ago

@aldomeza-dev, what is the ethernet chip on your shield? W5500? LAN8720?

It's the WIZ5500, to be specific this one Adafruit Ethernet FeatherWing