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