How to read a sensor with a date range

I need assistance with C# code to read data from a sensor with a date range. I am able to get an AccessToken but don’t know where to go from here. Here is a sample of my code, but what is missing is the urlString to collect a date specific array of sensor readings…

string urlString = “https://notehub.io/oauth2/token”;
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, urlString);
request.Content = new StringContent(“grant_type=client_credentials&client_id=” + BluesAuthorization.ClientID + “&client_secret=” + BluesAuthorization.ClientSecret);
request.Content.Headers.ContentType = new MediaTypeHeaderValue(“application/x-www-form-urlencoded”);
HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
dynamic dataObject = JObject.Parse(responseBody);
BluesAuthorization.AccessToken = dataObject.access_token;
BluesAuthorization.TokenType = dataObject.token_type;

How do I specify the device ID and date range over which I want to retrieve data? Something like this:

string urlString = “https://notehub.io/oauth2/token” + from + “&to=” + to + “&thing=” + SensorComms.device_id + “&token=” + TokenKey.Token

Hi @Mtnbiker88 and welcome to the Blues community!

First off, the /oauth2/token endpoint in your request above is only used to get an OAuth bearer token to authenticate future Notehub API requests. We recently updated our auth mechanism to allow for Personal Access Tokens, which are an easier way of authenticating Notehub API requests. You’d use your Personal Access Token as the authorization header in your API request below (e.g. Authorization: Bearer <access_token>).

Next, if you want to get events from your Notehub project based on a date range, you’ll want to use the Get Project Events API. So, your urlString variable might look something like this, where you’ll want to swap in your <projectOrProductUID>, <deviceUID>, <startDate>, and <endDate> variables (noting that the dates are formatted as UNIX epoch times):

https://api.notefile.net/v1/projects/<projectOrProductUID>/events?deviceUID=<deviceUID>&startDate=<startDate>&endDate=<endDate>

Tip: You could even use ChatGPT, Gemini, etc to generate the proper C# code for you based on the curl examples provided in the Notehub API docs.

Thanks,
Rob

Thank you for the prompt reply, Rob. I am away from the development system and so will test your suggestions in the morning and let you know how it goes. Again, thank you very much.

Hello Rob,

Where do I get the project or projectUID?

I believe I found it, under the settings of the project…

1 Like

Okay, so I tried this and it crashes with StatusCode: 400, ReasonPhrase: ‘Bad Request’,. Any idea what I am doing wrong?

 urlString = "https://api.notefile.net/v1/projects/" +   BluesAuthorization.ProductUID + "/events?deviceUID=" + SensorComms.device_id +
     "&startDate=" + dtStartEpochTimeStr + "&endDate=" + dtEndEpochTimeStr + "&dataType=captured";
 request = new HttpRequestMessage(HttpMethod.Get, urlString);
 request.Headers.Add("Authorization", "Bearer " + BluesAuthorization.AccessToken);
 response = await client.SendAsync(request);
 response.EnsureSuccessStatusCode();
 responseBody = await response.Content.ReadAsStringAsync();

Where urlString = https://api.notefile.net/v1/projects/com.juno.channfogg:starnote_test/events?deviceUID=868032061482778&startDate=1759298700000&endDate=1759951366000&dataType=captured

I added dev: in front of the deviceUID and still no success…

I changed productUID to projectUID of app:6430cb6a-e6f3-48b0-9ffe-8bd962731975 and still a bad request…I also removed the app: and still a bad request…

Looks like you have a bad querystring parameter in there. Try dateType and not dataType.

Thanks Rob. I modified that and still get the Bad Request error. I removed the dev: from the deviceUID but no luck…I removed the dateType and still no success…

Hi @Mtnbiker88,

I think I found the problem. Your UNIX epoch date is in milliseconds and it should be in seconds. If I drop the last 3 zeros that same query works for me in one of my projects. We will update our docs to make this more clear.

Thanks,
Rob

1 Like

Yes!!! Thank you for your fantastic support, Rob!!!

2 Likes