Posting sensor data directly into Influxdb

Hello,
I'm looking to read data from 3 separate sensors at the same time and have that data posted to InfluxDB running on a raspberry pi 3 B, then using that data in Grafana.

I've gotten the whole TICK stack working as it should, but was looking to bypass having to use webhooks and since the Photon would be on the same network as the InfluxDB server, what would I have to do to my code to make that work?

It's pretty simple, based off of the photoresistor example that comes with the Photon, as it's using a simple resistive based pressure sensor.

I found this comment from nearly 4 years ago that goes over it, but in the event the original poster doesn't reply, I'd like to figure this out.

// This #include statement was automatically added by the Particle IDE.
#include <HttpClient.h>


#include "HttpClient/HttpClient.h"
#define INFLUXDB_HOST   "raspberrypi"
#define INFLUXDB_PORT   8086
#define INFLUXDB_DB     "telegraf"

HttpClient http;

// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {
    { "Host", INFLUXDB_HOST },
    { "Connection", "close" },
    { "Accept" , "application/json" },
    { NULL, NULL } // NOTE: Always terminate headers will NULL
};


bool sendInflux(String payload) {   
    http_request_t     request;
    http_response_t    response;
    
    request.hostname = INFLUXDB_HOST;
    request.port     = INFLUXDB_PORT;
    request.path     = "/write?db=" + String(INFLUXDB_DB);
    request.body     = payload;
   
    http.post(request, response, headers);
    
    if (response.status == 204) {
        return true;
    } else {
        return false;
    }
}

int led = D6; // This is where your LED is plugged in. The other side goes to a resistor connected to GND.

int pressure1 = A0; // Pressure Sensors
int pressure2 = A1;
int pressure3 = A2;
int analog1; // Here we are declaring the integer variable analogX, which we will use later to store the value of the pressure sensor.
int analog2;
int analog3;
int ledToggle(String command); // Forward declaration



void setup() {
	
	Serial.begin();
	pinMode(led, OUTPUT); // Our LED pin is output (lighting up the LED)
	digitalWrite(led, HIGH);

	
	Particle.variable("analog1", &analog1, INT);
	Particle.variable("analog2", &analog2, INT);
    Particle.variable("analog3", &analog3, INT);
//	Serial.println("analogvalue", &analogvalue, INT);
	Particle.function("led",ledToggle);
}

void loop() {

	// check to see what the value of the pressure sensor is and store it in the int variable analogvalue
	analog1 = analogRead(pressure1);
    analog2 = analogRead(pressure2);
    analog3 = analogRead(pressure3);

	// This prints the value to the USB debugging serial port (for optional debugging purposes)
if (analog1 >50){
	Serial.printlnf("%d", analog1);
	sendInflux("%d");
	Particle.publish("analog1", String(analog1), PRIVATE);
}
if (analog2 >50){	
	Serial.printlnf("%d", analog2);
	Particle.publish("analog2", String(analog2), PRIVATE);
}
if (analog3 >50){
	Serial.printlnf("%d", analog3);
    Particle.publish("analog3", String(analog3), PRIVATE);
}
	
	delay(1000);
}

int ledToggle(String command) {

	if (command=="on") {
		digitalWrite(led,HIGH);
		return 1;
	}
	else if (command=="off") {
		digitalWrite(led,LOW);
		return 0;
	}
	else {
		return -1;
	}
}

Thank you

What you rather need is some server on your RPi that deals with your HTTP requests - have you got something running there?
If not, why going with HTTP and not just use TCPClient with a simple TCP server running on your RPi.

One example implementation of such a server (with a sightly different intent tho') can be found here

It appears there is an issue with where the install for influxdb is placed versus what the available documentation says. It installs to /usr/local/bin/ but documentation reports /usr/bin/. I think I can get it going but will just wipe the sd and start all over due to some issues I’ve had and do it via docker. Thank you for replying.

I redid the server so that it is an instance of docker with 2 containers, one for influxdb and one for Grafana. Both are working great, but now I’m just back to my original post.

I know that I’m missing something that would throw the 3 different variables from 3 different sensors into the sendInflux, but I’m not familiar enough with InfluxDB to make it work.
Any help would be appreciated

Hi, hope this isn’t too late, but it looks like you are pointing to the docker “container” port for influxDB not the “local” port of the host of the docker.

This caught me out in a similar instance, where I initially was trying to send a POST to the docker container port for my DB (thingsboard rather than influx, but same principle) but didnt notice that the docker actually has two ports, an internal “container” port (for the virtual environment) and an external “local” port (that resides in the host of the docker container).

I just set up a temp influxdb instance on my synology to show how this looks. Your ports will be different, but shows what I mean. If you want this to be externally accessible you will also need to forward the ports on your router, but this shouldnt be needed if you are only accessing locally.

See image below:

1 Like

I eventually got it to work. I went a different direction, but things started making sense. Thank you for responding.

1 Like

Would you mind sharing how? I am still using webhooks (really quite good once I got the hang of it) but would be interested to know the alternative.