发送消息到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