Azure Raspberry Pi 3运行Windows 10 Core和自定义程序,显示略过的x

Azure Raspberry Pi 3运行Windows 10 Core和自定义程序,显示略过的x,azure,uwp,raspberry-pi3,azure-iot-hub,windows-iot-core-10,Azure,Uwp,Raspberry Pi3,Azure Iot Hub,Windows Iot Core 10,我有一个非常简单的项目,其中我的目标是能够通过直接方法调用(通过Azure IoT hub)在运行Windows 10 IoT Core的Raspberry Pi 3上切换LED 在它的当前状态下,我编写的程序只显示一个带有x的白色框,与邮件信封类似,后端灯光控制代码不响应直接方法调用。然而,当连接到Visual Studio 2017远程调试器时,UI背后的代码似乎运行得非常好(或者我是这么认为的),因此我假设直接方法调用的失败是由于我的internet连接(正如我在中所问的) 然而,事实证明

我有一个非常简单的项目,其中我的目标是能够通过直接方法调用(通过Azure IoT hub)在运行Windows 10 IoT Core的Raspberry Pi 3上切换LED

在它的当前状态下,我编写的程序只显示一个带有x的白色框,与邮件信封类似,后端灯光控制代码不响应直接方法调用。然而,当连接到Visual Studio 2017远程调试器时,UI背后的代码似乎运行得非常好(或者我是这么认为的),因此我假设直接方法调用的失败是由于我的internet连接(正如我在中所问的)

然而,事实证明,UI背后的代码请求将一个小程序执行更新打印到文本框中,而不存在的UI从未响应,从而阻止程序继续打开Azure Iot Hub设备客户端

我不想完全抛弃UI,而是想弄清楚是什么阻止了我的UI做任何事情。奇怪的是,当我第一次尝试我的程序(不包括物联网代码)时,UI工作得很好。 坦白地说,我一点也不知道什么会突然出问题(如果你想知道的话,Pi处于head模式),所以我想我最好的做法是发布我的代码,看看其他人是否可以复制这些问题

而不是把我所有的代码都放在这篇文章里,有人会把它重建成一个项目,然后可能会遗漏一些东西,等等。。。我只是简单地将其全部推到Git回购中,可以在这里找到:

我还有几个旁白要问。。。首先,我在回购协议中忽略了呼出设备密钥和我的树莓Pi的ID。这可能是一个(半途而废的,或至少可能的)安全问题吗

而且,我的VS错误列表中有大量版本不匹配警告。在其他项目中,我只需在Web.xml或App.xml文件中启动一些版本重定向,但是(除了“运行时指令”xml文件)这些在这个项目中不存在。我能做些什么来解决这些问题

谢谢


Lucas Niewohner

需要增强应用程序的功能。因为应用程序现在需要将双向信息从它发送到azure。它需要在应用程序清单中同时检查客户端和服务器


除了Stuart Smith的回复之外,由于您的代码在GitHub中提供,这将导致死锁,因为当所有操作完成后,任务方法不会签署为已完成。您可以参考主题,也许您会理解
任务。Wait
Wait
之间的区别。我修改了下面的代码,效果很好

public sealed partial class MainPage : Page
{
    const string DeviceId = "<deviceId>";
    const string DeviceKey = "<device primary key>";
    const string HubEndpoint = "<azure iothub host name>";
    const int LEDPinNumber = 5;
    GpioPin LEDPin;
    bool LEDPinState;
    Brush StatusNormalBrush;
    DeviceClient deviceClient;

    public MainPage()
    {
        this.InitializeComponent();
        StatusNormalBrush = StatusIndicator.Fill;
        if (!TryInitGPIO().Result)
        {
            WriteMessage("GPIO initialization failed"); 
        }
        deviceClient = DeviceClient.Create(HubEndpoint,
            AuthenticationMethodFactory.CreateAuthenticationWithRegistrySymmetricKey(DeviceId, DeviceKey), TransportType.Mqtt_WebSocket_Only);
        deviceClient.SetMethodHandlerAsync("ToggleLED", new MethodCallback(ToggleLEDMethod), null);
    }

    private Task<MethodResponse> ToggleLEDMethod(MethodRequest methodRequest, object userContext)
    {
        WriteMessage("Recieved Direct Request to toggle LED"); 
        LEDPinState = !LEDPinState;
        UpdateLight();
        return Task.FromResult( new MethodResponse(Encoding.UTF8.GetBytes("{\"LightIs\":\"" + (LEDPinState ? "On" : "Off") + "\"}"), 200));
    }

    public Task<bool> TryInitGPIO()
    {
        GpioController gpioController = GpioController.GetDefault();
        if (gpioController == null)
        {
            WriteMessage("This Device is not IoT friendly!  (No GPIO Controller found)", true);
            return Task.FromResult(false);
        }
        if (gpioController.TryOpenPin(LEDPinNumber, GpioSharingMode.Exclusive, out LEDPin, out GpioOpenStatus openStatus))
        {
            WriteMessage($"Output Pin ({LEDPinNumber}) Opened Successfully!!");
        }
        else
        {
            WriteMessage($"Output Pin ({LEDPinNumber}) Failed to Open", true);
            return Task.FromResult(false);
        }

        LEDPin.SetDriveMode(GpioPinDriveMode.Output);
        LEDPin.Write(GpioPinValue.High);
        LEDPinState = true;
        UpdateLight();
        WriteMessage("Output Pin initialized and on");
        return Task.FromResult(true);
    }

    private async void WriteMessage(string message, bool isError = false)
    {
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
          {
              StringBuilder sb = new StringBuilder(OutputBox.Text);
              if (isError)
              {
                  sb.AppendLine();
                  sb.AppendLine("*************ERROR**************");
              }
              sb.AppendLine(message);
              if (isError)
              {
                  sb.AppendLine("*************END ERROR**************");
                  sb.AppendLine();
              }
              OutputBox.Text = sb.ToString();
          });

    }

    private void ManualToggle_Click(object sender, RoutedEventArgs e)
    {
        WriteMessage("Recieved Manual Toggle");
        LEDPinState = !LEDPinState;
        UpdateLight();
    }

    private async void UpdateLight()
    {
        LEDPin.Write(LEDPinState ? GpioPinValue.High : GpioPinValue.Low);
        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
         {
             StatusIndicator.Fill = LEDPinState ? new SolidColorBrush(Colors.Red) : StatusNormalBrush;
         });
    }
}
公共密封部分类主页面:第页
{
常量字符串设备ID=“”;
const string DeviceKey=“”;
常量字符串“=”;
常数int-LEDPinNumber=5;
GpioPin-LEDPin;
布尔莱德平州;
刷子状态正常刷子;
设备客户端设备客户端;
公共主页()
{
this.InitializeComponent();
StatusNormalBrush=StatusIndicator.Fill;
如果(!TryInitGPIO().Result)
{
WriteMessage(“GPIO初始化失败”);
}
deviceClient=deviceClient.Create(HubEndpoint,
AuthenticationMethodFactory.CreateAuthenticationWithRegistrySymmetricKey(DeviceId,DeviceKey),TransportType.Mqtt\u仅限WebSocket\u);
deviceClient.SetMethodHandlerAsync(“Toggled”,新方法回调(ToggledMethod),null);
}
私有任务ToggledMethod(MethodRequest MethodRequest,对象userContext)
{
WriteMessage(“收到切换LED的直接请求”);
LEDPinState=!LEDPinState;
UpdateLight();
返回Task.FromResult(newMethodResponse(Encoding.UTF8.GetBytes(“{\“LightIs\”:\”+(LEDPinState?“开”):“关”)+“\”),200);
}
公共任务TryInitGPIO()
{
GpioController-GpioController=GpioController.GetDefault();
if(gpioController==null)
{
WriteMessage(“此设备不支持物联网!(未找到GPIO控制器)”,true);
返回Task.FromResult(false);
}
if(gpioController.TryOpenPin(LEDPinNumber,GpioSharingMode.Exclusive,out-LEDPin,out-GpioOpenStatus-openStatus))
{
WriteMessage($“输出引脚({LEDPinNumber})已成功打开!!”;
}
其他的
{
WriteMessage($“输出引脚({LEDPinNumber})打开失败”,true);
返回Task.FromResult(false);
}
设置驱动模式(GpioPinDriveMode.Output);
LEDPin.Write(GPIOPVALUE.High);
LEDPinState=true;
UpdateLight();
WriteMessage(“输出引脚已初始化并打开”);
返回Task.FromResult(true);
}
专用异步void WriteMessage(字符串消息,bool-isError=false)
{
wait Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,()=>
{
StringBuilder sb=新的StringBuilder(OutputBox.Text);
如果(isError)
{
(某人);
sb.附录行(“******************错误****************”);
}
某人在电话(留言)上留言;
如果(isError)
{
sb.附录行(“************************结束错误****************”);
(某人);
}
Text=sb.ToString();
});
}
私有无效手动切换单击(对象发送器,路由目标)
{
WriteMessage(“接收到的手动切换”);
LEDPinState=!LEDPinState;
UpdateLight();
}
私有异步void UpdateLight()
{
LEDPin.Write(LEDPinState?GpioPinValue.High:GpioPinValue.Low);
等待Dispatcher.RunAsync(Windows.UI.Core.CoreD