简体   繁体   English

从Raspberry向Azure Iot Hub发送消息

[英]Sending message to Azure Iot Hub from Raspberry

I need to send a message to Azure Iot Hub ( https://azure.microsoft.com/it-it/services/iot-hub/ ) from an Universal App installed in my Raspberry. 我需要从安装在Raspberry中的通用应用程序向Azure Iot Hub( https://azure.microsoft.com/it-it/services/iot-hub/ )发送消息。 I've to use HTTP protocol because Raspberry doesn't supports AMQP. 我必须使用HTTP协议,因为Raspberry不支持AMQP。 I use the following code: 我使用以下代码:

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 gives me: SendEventAsync给我:

Exception thrown: 'Microsoft.Azure.Devices.Client.Exceptions.IotHubCommunicationException' in mscorlib.ni.dll

Message: {"An error occurred while sending the request."}

I've included in my project Microsoft.AspNet.WebApi.Client as documented here: https://github.com/Azure/azure-iot-sdks/issues/65 with no results. 我已将其包含在我的项目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": {}
}

If I try the SAME code in a Console application it works as expected. 如果我在控制台应用程序中尝试使用SAME代码,则它将按预期工作。

@danvy is right you need to generate a SAS token,here is a signature generator https://github.com/sandrinodimattia/RedDog/releases @danvy是正确的,您需要生成SAS令牌,这是签名生成器https://github.com/sandrinodimattia/RedDog/releases

There could be multiple ways to send events, you are using http, check out this sample 可能有多种发送事件的方法,您正在使用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));
}

Check out this article for more details http://fabriccontroller.net/iot-with-azure-service-bus-event-hubs-authenticating-and-sending-from-any-type-of-device-net-and-js-samples/ 请查看本文以获取更多详细信息http://fabriccontroller.net/iot-with-azure-service-bus-event-hubs-authenticating-and-send-from-any-type-of-device-net-and-js -样本/

try to set a connection string to your device (_deviceConnectionString) using this tutorial 尝试使用本教程为您的设备(_deviceConnectionString)设置连接字符串

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

You can do it by hand using the information you get from the IoT Hub directly or from the dashboard created by the IoT Suite wizard. 您可以直接使用从IoT中心或IoT Suite向导创建的仪表板中获得的信息来手动完成此操作。 It will look like this 看起来像这样

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

Did you generate the DeviceId and device key correctly using CreateDeviceIdentity tool? 您是否使用CreateDeviceIdentity工具正确生成了DeviceId和设备密钥? Here is the guide: https://blogs.windows.com/buildingapps/2015/12/09/windows-iot-core-and-azure-iot-hub-putting-the-i-in-iot/ 这是指南: https//blogs.windows.com/buildingapps/2015/12/09/windows-iot-core-and-azure-iot-hub-putting-the-i-in-iot/

After hours of searching I found an hardware problem. 经过数小时的搜索,我发现了硬件问题。 My Raspberry was trying to use the non-configured Wi-Fi dongle to send the request while for all other requests used the network cable. 我的Raspberry尝试使用未配置的Wi-Fi加密狗发送请求,而所有其他请求都使用网线。 Removing the dongle did the trick. 删除加密狗就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM