Things used in this project

Hardware components:
Pi 3 02
Raspberry Pi 3 Model B
×1
09590 01
LED (generic)
×1
12002 04
Breadboard (generic)
×1
11026 02
Jumper wires (generic)
×1
Software apps and online services:
10
Microsoft Windows 10 IoT Core
Vs2015logo
Microsoft Visual Studio 2015
W9gt7hzo
Microsoft Azure

Schematics

azurelight-fritzing.png
Azurelight fritzing

Code

Raspberry Pi UWP UIXML
XAML
<Page
    x:Class="AzureRemoteLight.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AzureRemoteLight"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    DataContext="{Binding Source={StaticResource Locator}, Path=Main}"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Padding="12">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        
        <Image Source="Assets/cloud-hero.png" Grid.Row="0" Height="80" HorizontalAlignment="Right" VerticalAlignment="Top" />
        
        <StackPanel Grid.Row="0">
            <TextBlock Text="Windows 10 IoT + Microsoft Azure" Style="{StaticResource SubtitleTextBlockStyle}" />
            <TextBlock Text="Remote Light Control" Style="{StaticResource SubheaderTextBlockStyle}" />
        </StackPanel>

        <Grid Grid.Row="1" Margin="0,20,0,0">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            
            <Image Grid.Row="0" Grid.RowSpan="2" Source="Assets/Windows_Insiders_Flag.png" HorizontalAlignment="Right" Height="300" VerticalAlignment="Bottom" />
            
            <StackPanel Orientation="Horizontal" Grid.Row="0">
                <TextBlock Text="Azure IoT Hub Connection: " />
                <TextBlock Text="{Binding IsAzureConnected}" />
            </StackPanel>
            
            <Border Grid.Row="1" BorderBrush="#CCC" BorderThickness="1" Margin="0,10,0,0" Padding="10">
                <TextBlock TextWrapping="Wrap" Text="{Binding CloudToDeviceLog}" Foreground="#CCC" FontFamily="Consolas" />
            </Border>
        </Grid>
    </Grid>
</Page>
Controller UIXML
XAML
<Window x:Class="LightController.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:LightController"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
            <Button x:Name="BtnTurnOn" Content="Turn Light On" HorizontalAlignment="Center" Click="BtnTurnOn_OnClick" />
            <Button x:Name="BtnTurnOff" Content="Turn Light Off" Margin="0,10,0,0" Click="BtnTurnOff_OnClick" />
        </StackPanel>
    </Grid>
</Window>
Controller LogicC#
public partial class MainWindow : Window
{
    static ServiceClient serviceClient;
    static string connectionString = "YOUR IOT HUB CONNECTION STRING";

    public MainWindow()
    {
        InitializeComponent();

        serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
    }

    private async Task TurnLight(bool isOn)
    {
        await SendCloudToDeviceMessageAsync(isOn);
    }

    private static async Task SendCloudToDeviceMessageAsync(bool isOn)
    {
        var commandMessage = new Message(Encoding.ASCII.GetBytes(isOn ? "on" : "off"));
        await serviceClient.SendAsync("YOUR DEVICE NAME", commandMessage);
    }

    private async void BtnTurnOn_OnClick(object sender, RoutedEventArgs e)
    {
        await TurnLight(true);
    }

    private async void BtnTurnOff_OnClick(object sender, RoutedEventArgs e)
    {
        await TurnLight(false);
    }
}

Credits

20170310 041553193 ios (1) mka4nfm6gi
Edi Wang
5 projects • 17 followers
ASP.NET and Windows 10 Developer
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaBlog