Ok, full solution below, this has taken me a while to get through and to the devs… THE PARTICLE TO INFLUX TUTORIAL NEEDS TO BE UPDATED.
Step 1) Update your telegraf.conf
sudo nano /etc/telegraf/telegraf.conf
you need to change a few things here, they are:
Update the influxDB credentials. These weren’t current in my .conf.
(note 1, where <> brackets are used, you need to delete the and enter your own data)
(note 2, you need to un-comment the stuff you want to take effect by deleting the ‘#’)
Then, update the Particle Webhooks section (way down the bottom, or use ^W to search for “particle”:
Now, save ^X, then ‘Y’, then save as ‘telegraf.conf’
then restart the telegraf server:
sudo systemctl restart telegraf.service
Step 2) create the webhook:
copypasta:
{
"event": "{{{PARTICLE_EVENT_NAME}}}",
"data": {{{PARTICLE_EVENT_VALUE}}},
"coreid": "{{{PARTICLE_DEVICE_ID}}}",
"published_at": "{{{PARTICLE_PUBLISHED_AT}}}",
"userid": "{{{PRODUCT_USER_ID}}}",
"fw_version": "{{{PRODUCT VERSION}}}",
"public": "{{{PARTICLE_EVENT_PUBLIC}}}"
}
- no username and password needed
- no SSL
Now, SAVE.
DO NOT use the “Test” it’s a waste of time and will throw an error, even if everything is fine. Also, yes, I had to omit the double quotes on data. It works this way, and smarter people than me can explain why.
Note, the “publishName” is the name of the Particle.publish you will need to use in your microcontroller code in the next step.
Note 2: “event” is the name that will be used for the ‘Measurement’ in InfluxDB. In the above it will use the same name as your particle publish function (in this case “publishName”). If you want to override this, replace the “{{{PARTICLE_EVENT_NAME}}}” with “your_Measurement_name”.
Note 3: the term “measurement” appears to be completely redundant.
Note 4: Dont put a comma , after the last item in the JSON or it will fail without any reasoning (I lost half a day on this!)
Step 3) Particle code (almost there!)
This bit took the life out of me, but I now have it working. You’ll want some tags, add as many or as little as you like, just keep following the format below (you can move the variables outside the loop() if you like).
void setup()
{
}
void loop()
{
int value2Integer = 10;
float value3Float = 1.234;
String data = String::format("{ \"tags\" : {\"influx_db\": \"<databaseName>\", \"<tag1Name>\": \"<tag1Value>\", \"<tag2Name>\": \"<tag2Value>\"}, \"values\": {\"value1Name\": \"%s\", \"value2Name\": %d, \"value3Name\": %f}}", "value1String", value2Integer, value3Float);
Particle.publish("publishName", data, PRIVATE);
delay(1000);
}
note: you should include one of the tags as ‘influx_db’ as this is where Telegraf learns the name of the alternative InfluxDB to use. Otherwise it will use the default ‘telegraf’.
Also, you could make the ‘data’ payload a little neater to read in code by adding each section to a new line and appending it with ‘+=’ also… I’ll do that later and see how it goes.
edit: I have this working in the below code:
/////// CONSTANTS ///////
String IDB_Name = "my_database_name";
String IDB_tag1Name = "tag1Name";
String IDB_tag2Name = "tag2Name";
String IDB_tag3Name = "tag3Name";
String IDB_tag1Value = "tag1Value";
String IDB_tag2Value = "tag1Value";
String IDB_tag3Value = "tag1Value";
/////// VARIABLES ///////
String value1Value = String(10.5); // There is probably an easier way to do this?
String value2Value = String(11.5); // In your loop you can set these to your various data.
String value3Value = String(12.5);
void setup()
{
}
void loop()
{
String payload = "{ \"tags\" : {\"influx_db\": \"";
payload += IDB_Name;
payload += "\", \"";
payload += IDB_tag1Name;
payload += "\": \"";
payload += IDB_tag1Value;
payload += "\", \"";
payload += IDB_tag2Name;
payload += "\": \"";
payload += IDB_tag2Value;
payload += "\", \"";
payload += IDB_tag3Name;
payload += "\": \"";
payload += IDB_tag3Value;
payload += "\"}, \"values\": {\"";
payload += "value1Name\": ";
payload += value1Value;
payload += ", \"value2Name\": ";
payload += value2Value;
payload += ", \"value3Name\": ";
payload += value3Value;
payload += "}}";
Particle.publish("publishName", payload, PRIVATE);
delay(1000);
}
DONE!