树莓

2015-12-10 35 views
0

发送消息到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代码,它按预期工作。

+0

没有你的项目有什么样的能力? – danvy

+0

只有'互联网(客户端)' – danyolgiax

+0

您可以尝试使用以下代码手动发送事件: https://github.com/danvy/sigfox/blob/master/src/SigfoxEventSimulator/Program.cs 您可以创建您的使用SAS令牌这个工具: http://danvy.tv/sas-token-generator.html – danvy

回答

0

搜索后几个小时,我发现一个硬件问题。我的Raspberry试图使用未配置的Wi-Fi适配器发送请求,而对于使用网络电缆的所有其他请求。去掉加密狗的确有窍门。

+1

在这个笔记上,我发现如果有多个网络连接可供选择,我会感到困惑。 –

+0

我现在面对与我的Raspberry Pi 3相同的问题,但问题是我没有使用任何WiFi加密狗,但也许内部的WiFi组件导致类似的问题? – ffffff01

+0

如果内部树莓datime未设置为现在,您可能会遇到同样的问题!我的过去是2小时!看看这里http://stackoverflow.com/questions/32828332/how-to-use-powershell-to-set-the-time-on-a-remote-device – danyolgiax

0

@danvy是正确的,你需要生成一个SAS令牌,这里是一个签名生成https://github.com/sandrinodimattia/RedDog/releases

可能有多种方式发送事件,您正在使用HTTP,看看这个样本

// Generate a SAS key with the Signature Generator.: 
var sas = "SharedAccessSignature sr=https%3a%2f%2freddogeventhub.servicebus.windows.net%2ftemperature%2fpublishers%2flivingroom%2fmessages&sig=I7n%2bqlIExBRs23V4mcYYfYVYhc6adOlMAeTY9VM9kNg%3d&se=1405562228&skn=SenderDevice"; 

// Namespace info. 
var serviceNamespace = "myeventhub"; 
var hubName = "temperature"; 
var deviceName = "livingroom"; 

// Create client. 
var httpClient = new HttpClient(); 
httpClient.BaseAddress = 
      new Uri(String.Format("https://{0}.servicebus.windows.net/", serviceNamespace)); 
httpClient.DefaultRequestHeaders 
      .TryAddWithoutValidation("Authorization", sas); 

Console.WriteLine("Starting device: {0}", deviceName); 

// Keep sending. 
while (true) 
{ 
    var eventData = new 
    { 
     Temperature = new Random().Next(20, 50) 
    }; 

    var postResult = httpClient.PostAsJsonAsync(
      String.Format("{0}/publishers/{1}/messages", hubName, deviceName), eventData).Result; 

    Console.WriteLine("Sent temperature using HttpClient: {0}", 
      eventData.Temperature); 
    Console.WriteLine(" > Response: {0}", 
      postResult.StatusCode); 
    Console.WriteLine(" > Response Content: {0}", 
      postResult.Content.ReadAsStringAsync().Result); 

    Thread.Sleep(new Random().Next(1000, 5000)); 
} 

看看这篇文章了解更多详情http://fabriccontroller.net/iot-with-azure-service-bus-event-hubs-authenticating-and-sending-from-any-type-of-device-net-and-js-samples/

+0

我使用的是新Azure的物联网中心(https://azure.microsoft.com/it-it/services/iot -hub /),我跟着微软的入门指南这里:https://azure.microsoft.com/it-it/develop/iot/get-started/ – danyolgiax

0

尝试连接字符串设定为您的设备(_deviceConnectionString)使用本教程

https://github.com/Azure/azure-iot-sdks/blob/master/tools/DeviceExplorer/doc/how_to_use_device_explorer.md

你可以手工使用从物联网中心获得直接或通过物联网套房向导创建的仪表盘的信息做到这一点。它看起来像这样

_deviceConnectionString =“HostName = YourIoTHubName.azure-devices.net; DeviceId = YourDeviceId; SharedAccessKey = YourDeviceSharedAccessKey”;

+0

连接字符串工作正常使用一个控制台应用程序相同的代码。 – danyolgiax

+0

您的应用程序是否在您的电脑上运行? – danvy

+0

我已经添加了解决方案。这是一个硬件问题。 – danyolgiax

0

你生成使用CreateDeviceIdentity工具,设备ID和设备密钥是否正确? 下面是指南:https://blogs.windows.com/buildingapps/2015/12/09/windows-iot-core-and-azure-iot-hub-putting-the-i-in-iot/

+0

后续文章一步一步地复制粘贴代码和相同的错误! '发送请求时发生错误。'内部异常是:'{“异常来自HRESULT:0x80072F05”}' – danyolgiax

+0

你可以在GitHub或其他地方与我分享代码吗? –

+0

我添加了解决方案,这是一个硬件问题。谢谢 – danyolgiax