EDIT: I managed to get a c# version working (below)
Original Question:
I'm trying to connect to Azure's IoT Hub through the cpprestsdk and POST a json-structured obj to it. However, i cant find anything regarding authentication anywhere.
I've followed this MSDN example and tried to get a hold of all available documentation, but i'm not sure how people connect to azure iot hub through this rest sdk. Is there some extension that i'm not aware of? Because I cannot find any examples where authorization is used. But in MSDN's examples, they PUSH and GET. Is this sdk only supposed to run locally without any form of authorization enabled? I'm confused... Can someone clarify or give me an example of how to authenticate to azure iot hub and then relay the message to the service-bus? I've already setup my routing for this.
For reference: I'm using v120 and cpprestsdk 2.9.1.1.
C++ attempt
// Upload result to Azure Hub.
pplx::task<void> UploadDataToHttpServerAsync(std::wstring data)
{
//using concurrency::streams::file_stream;
//using concurrency::streams::basic_istream;
// create a stream from a text string.
auto ss = concurrency::streams::wstringstream::open_istream(data);
try
{
// HTTP request.
http_client client(L"LINK-I-GUESS");
return client.request(methods::POST, L"WHAT-LINK-SHOULD-THIS-BE?", data).then([data](pplx::task<http_response> previousTask)
{
std::wostringstream wss;
try
{
auto response = previousTask.get();
wss << L"Server returned status code " << response.status_code() << L"." << std::endl;
if (response.status_code() == status_codes::OK)
{
cout << L"data was sent";
}
else
{
cout << L"Error: " << response.status_code() << std::endl;
}
}
catch (const http_exception& e)
{
wss << e.what() << std::endl;
}
std::wcout << wss.str();
});
}
catch (const std::system_error& e)
{
std::wostringstream wss;
wss << e.what() << std::endl;
std::wcout << wss.str();
// Return an empty task.
return pplx::task_from_result();
}
}
Azure Dashboard.
Am i looking for these connections in the wrong place?
Other resources i've looked at:
(c#) https://blogs.msdn.microsoft.com/chmitch/2016/11/16/sending-events-to-iot-hub-over-http-via-rest/
https://github.com/Microsoft/cpprestsdk/wiki
https://microsoft.github.io/cpprestsdk/namespaces.html
https://www.example-code.com/cpp/azure_service_bus_send_message_to_queue.asp -irrelevant, but had to look up how other libraries did this.
https://blogs.msdn.microsoft.com/vcblog/2013/02/26/the-c-rest-sdk-casablanca/
http://aka.ms/azure-iot-hub-vs-cs-2017-c
Any help that can point me in the right direction is highly appreciated!
EDIT: c# version
using System;
using Microsoft.Azure.Devices.Client;
using Newtonsoft.Json;
using System.Text;
using System.Threading.Tasks;
class AzureIoTHubClient
{
private static DeviceClient s_deviceClient;
private readonly static string s_connectionString = "HostName={0}.azure-devices.net;DeviceId={1};SharedAccessKey={2}";
// Async method to send simulated telemetry
private static async void SendDeviceToCloudMessagesAsync(string sData)
{
// Initial telemetry values
string currentTopic = "productionevent";
// Create JSON message
var telemetryDataPoint = new
{
topic = currentTopic,
data = sData,
};
var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
var message = new Message(Encoding.ASCII.GetBytes(messageString));
// IoT hub can filter these properties without having access to the message body.
message.Properties.Add("topic", "productionevent");
// Send the telemetry message
await s_deviceClient.SendEventAsync(message);
Console.WriteLine("{0} > Sending message: {1}", DateTime.UtcNow, messageString);
await Task.Delay(500);
}
}
I managed to get it working in c# with a different library called Microsoft.Azure.Devices.Client.
I did this so that I could attempt to import this .NET project into my c++ program. However, this is not ideal:
I would rather see a solution in c++.
I could try to get this .NET-program to run in my c++-application. For that, i've looked through these resources:
Calling C# code from visual C++
How to call a C# library from Native C++ (using C++\CLI and IJW) -- this looks promising
https://www.codeproject.com/Tips/695387/Calling-Csharp-NET-methods-from-unmanaged-C-Cplusp
Any help on authenticating to Azure's IoT hub in c++ is greatly appreciated.
TL;DR:
- Authenticate to azure iot hub in c++ v120 .NET 4.0 (preferred, otherwise 4.5)
- Send json-parsed message to iot hub.
- Include "headers" that relay message to service-bus (as seen in c# example).