2017-08-31 91 views
0

我正在开发一个简单的应用程序,需要发送和接收来自天蓝色的一些数据。首先,我使用了一个模拟设备(和控制台应用程序)。我配置了我的天蓝色门户以处理这些数据,并且一切正常。注册树莓派到天蓝色通过c#

与此同时,我用单元测试检查了我的传感器,它也能正常工作。 现在,我想从我的传感器发送一些数据到天青(使用通用应用程序)。我尝试使用此链接: https://blogs.windows.com/buildingapps/2016/03/03/connect-your-windows-app-to-azure-iot-hub-with-visual-studio/#BgxLrRq1bXolCitM.97

我选择了在模拟设备中使用的设备,并且客户端连接出现“未知主机”错误。 我需要注册我的树莓派作为一种设备吗?我如何从已知的通用应用程序(例如:https://developer.microsoft.com/en-us/windows/iot/samples/helloblinky)发送一个简单的字符串为azure?

我正在使用Windows 10物联网,c#

谢谢!

更新: 我试图做所有你的建议没有成功。 初步认识代码:

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); 
    } 

我不知道如何验证 “sharedAccessKey” 上(在等待deviceClient.SendEventAsync(消息)线)的ConnectionString VAR

错误: 抛出异常:“ System.Private.CoreLib.ni.dll中的System.Exception' 没有这样的主机是已知的。 (异常来自HRESULT:0x80072AF9)

+0

是的,则需要先注册的装置中,使用天青门户或[按代码(https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp -getstarted#create-a-device-identity) –

+0

是的,我在为我的模拟设备应用程序创建设备时使用了此工具。我需要为通用应用程序创建一个新的吗?如何使用此代码创建新设备?这是一个控制台应用程序,我不能在覆盆子pi上运行它。谢谢! –

+0

“未知主机”的错误表示您的azure iot hub主机名称未知。如果你关注[此链接](https://blogs.windows.com/buildingapps/2016/03/03/connect-your-windows-app-to-azure-iot-hub-with-visual-studio/#BgxLrRq1bXolCitM .97)它会自动生成一个AzureIoTHub.cs文件,在这个文件中有deviceConnectionString var,请确保这是一个正确的连接字符串,与您的天蓝色iot集线器相关。 –

回答