简体   繁体   中英

Register raspberry pi to azure via c#

I am developing a simple app that need to send and receive some data from azure. First of all, I have worked with a simulated device(and console app). I have configured my azure portal to work with the this data and everything works well.

At the same time, I checked my sensor with unit-tests and it works fine too. Now, I want to send some data from my sensors to azure(with Universal App). I tried to work with this link: https://blogs.windows.com/buildingapps/2016/03/03/connect-your-windows-app-to-azure-iot-hub-with-visual-studio/#BgxLrRq1bXolCitM.97

I choose the device that I worked with in the simulated device and got an error of "unknown host" for the client connect. Do I need to register my raspberry pi as a device before? How I can send a simple string from known universal app (ie: https://developer.microsoft.com/en-us/windows/iot/samples/helloblinky ) to azure?

I am working with Windows 10 IOT, c#

Thanks!

Update: I tried to do all what you suggested without success. Relavant code:

    public static async Task SendDeviceToCloudMessageAsync()
    {
        CreateClient();

        var currentTemperature = 20 /*getCurrentTemperature()*/;
        var currentHumidity = 20/*getCurrentHumidity()*/;

        var telemetryDataPoint = new
        {
            deviceId = DeviceId,
            plantID = 7,
            temperature = currentTemperature,
            humidity = currentHumidity,
            userId = 1
        };
        var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
        var message = new Message(Encoding.ASCII.GetBytes(messageString));
        message.Properties.Add("temperatureAlert", (currentTemperature > 30) ? "true" : "false");

        Debug.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);

        await deviceClient.SendEventAsync(message);
    }

I don't know how to validate the "sharedAccessKey" on connectionString var

Error( on await deviceClient.SendEventAsync(message) line ): Exception thrown: 'System.Exception' in System.Private.CoreLib.ni.dll No such host is known. (Exception from HRESULT: 0x80072AF9)

You will need to do two things here.

First up, connect your device. Details here - https://developer.microsoft.com/en-us/windows/iot/docs/ConnectDeviceToCloud

Second, connect your app. - https://developer.microsoft.com/en-us/windows/iot/docs/ConnectAppToCloud

If you run into issues when doing the above two, then, you will be able to ask more specific questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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