發送消息到Azure的IOT中心我需要從安裝在我的覆盆子一個通用的應用程序將消息發送到Azure的IOT中心(https://azure.microsoft.com/it-it/services/iot-hub/)。我必須使用HTTP協議,因爲樹莓不支持AMQP。 我使用下面的代碼:樹莓
public sealed partial class MainPage : Page
{
private DispatcherTimer _timer = null;
private DeviceClient _deviceClient = null;
private const string _deviceConnectionString = "<myConnectionString>";
public MainPage()
{
InitializeComponent();
_deviceClient = DeviceClient.CreateFromConnectionString(_deviceConnectionString, TransportType.Http1);
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromSeconds(5);
_timer.Tick += _timer_Tick;
_timer.Start();
}
private async void _timer_Tick(object sender, object e)
{
string msg = "{deviceId: 'myFirstDevice', timestamp: " + DateTime.Now.Ticks + " }";
Message eventMessage = new Message(Encoding.UTF8.GetBytes(msg));
await _deviceClient.SendEventAsync(eventMessage);
}
}
SendEventAsync
給我:
Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubCommunicationException' in mscorlib.ni.dll
Message: {"An error occurred while sending the request."}
我已經包括在我的項目Microsoft.AspNet.WebApi.Client
如下記載:https://github.com/Azure/azure-iot-sdks/issues/65沒有結果。
"dependencies": {
"Microsoft.AspNet.WebApi.Client": "5.2.3",
"Microsoft.Azure.Devices.Client": "1.0.0-preview-007",
"Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0"
},
"frameworks": {
"uap10.0": {}
}
如果我在控制檯應用程序中嘗試SAME代碼,它按預期工作。
沒有你的項目有什麼樣的能力? – danvy
只有'互聯網(客戶端)' – danyolgiax
您可以嘗試使用以下代碼手動發送事件: https://github.com/danvy/sigfox/blob/master/src/SigfoxEventSimulator/Program.cs 您可以創建您的使用SAS令牌這個工具: http://danvy.tv/sas-token-generator.html – danvy