How to write data in Influxdb using c#

0

I want to send data to Influxdb rest API v2. However, I get a 400 error and cannot send data. Because unity is being used, the corresponding influxDB.client library conflicts with assembly and cannot be used. How do I send data to influxDb 2.0 and write it?

this is my code

const string url = "http://localhost:8086/api/v2/write org=DT_navigator&bucket=test_bk&precision=s";

     async System.Threading.Tasks.Task WriteDataAsync()
    {
      var json = JsonConvert.SerializeObject("mem,host=host1 used_percent=23");
      var data = new StringContent(json, Encoding.UTF8, "application/json");

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Add("Authorization", "Token " + token);

    var response = await client.PostAsync(url, data);

    string result = response.Content.ReadAsStringAsync().Result;
    Debug.Log(result);
}

and error message {β€œcode”:β€œinvalid”,β€œmessage”:β€œunable to parse β€˜β€œmem,host=host1 used_percent=23”’: invalid number”}

Since it looks like your data is already in Influx Line Protocol, you shouldn’t need to convert it to JSON before sending it to the write API

1 Like

Hi there,

I am trying to do a similar thing. I am trying to send data remotely from C# code to influx DB 2.0. The health status on connection gives success but the data is not getting inserted into influxDB when I check on localhost:8086

Here is my code:

usingIoTHubTrigger=Microsoft.Azure.WebJobs.EventHubTriggerAttribute;

usingMicrosoft.Azure.WebJobs;

usingMicrosoft.Azure.WebJobs.Host;

usingMicrosoft.Azure.EventHubs;

usingSystem.Text;

usingSystem.Net.Http;

usingMicrosoft.Extensions.Logging;

usingSystem;

usingSystem.Text.Json;

usingSystem.Collections.Generic;

usingNewtonsoft.Json;

usingInfluxDB.Client;

usingInfluxDB.Client.Api.Domain;

usingInfluxDB.Client.Core;

usingInfluxDB.Client.Writes;

usingSystem.Threading.Tasks;

namespaceWatlow.Function

{​​​​​​​​

publicclassObject{​​​​​​​​

publicstringserial_number{​​​​​​​​get;set;}​​​​​​​​

publicfloatuniversal_input_1_mod_5{​​​​​​​​get;set;}​​​​​​​​

publicfloatuniversal_input_2_mod_5{​​​​​​​​get;set;}​​​​​​​​

publicfloatuniversal_input_3_mod_5{​​​​​​​​get;set;}​​​​​​​​

publicfloatuniversal_input_4_mod_5{​​​​​​​​get;set;}​​​​​​​​

publicstringdisplay_units{​​​​​​​​get;set;}​​​​​​​​

publicDateTimets{​​​​​​​​get;set;}​​​​​​​​

}​​​​​​​​

publicstaticclassIotHubDataProcessing

{​​​​​​​​

privatestaticHttpClientclient=newHttpClient();

[FunctionName(β€œIotHubDataProcessing”)]

publicstaticvoidRun([IoTHubTrigger(β€œmessages/events”,Connection=β€œConnectionString”)]EventDatamessage,ILoggerlog)

{​​​​​​​​

log.LogInformation($β€œC# IoT Hub trigger function processed a message: {​​​​​​​​Encoding.UTF8.GetString(message.Body.Array)}​​​​​​​​”);

Objectobj=JsonConvert.DeserializeObject(Encoding.UTF8.GetString(message.Body.Array));

WriteInflux(obj);

}​​​​​​​​

publicstaticasyncvoidWriteInflux(Objectobj)

{​​​​​​​​

conststringtoken=β€œpcSpeRibWx0Xak–KpLI23kk52LZDbPcRjKKpmtPqZ8l47_mlUMsrgG69XeSmchQ_apOWwmgh8CpN1QQ2oQTuQ==”;

conststringbucket=β€œtimeseriesDB”;

conststringorg=β€œxxxx”;

conststringpassword=β€œxxxxx”;

varclient=InfluxDBClientFactory.Create(β€œhttp://20.83.176.xxx:8086”,β€œxxxx”,password.ToCharArray());

varhealth=awaitclient.HealthAsync();

if(health.Status.ToString()==β€œPass”){​​​​​​​​

Console.WriteLine(β€œConnection success.”);

}​​​​​​​​

else{​​​​​​​​

Console.WriteLine($"{​​​​​​​​health.Status}​​​​​​​​");

Console.WriteLine($β€œConnection failure: {​​​​​​​​health.Message}​​​​​​​​!”);

}​​​​​​​​

varpoint=PointData

.Measurement(β€œtemperature”)

.Tag(β€œserialNumber”,obj.serial_number)

.Field(β€œtc1”,obj.universal_input_1_mod_5)

.Field(β€œtc2”,obj.universal_input_2_mod_5)

.Field(β€œtc3”,obj.universal_input_3_mod_5)

.Field(β€œtc4”,obj.universal_input_4_mod_5)

.Timestamp(DateTime.UtcNow,WritePrecision.Ns);

using(varwriteApi=client.GetWriteApi())

{​​​​​​​​

writeApi.WritePoint(bucket,org,point);

}​​​​​​​​

}​​​​​​​​

}​​​​​​​​

}​​​​​​​​

​