Raspberry Pi With Azure Mobile Services

In this article we will learn how to control Raspberry pi LED by turning it on or off by passing Azure data using Windows Azure Mobile Service.

This article is about your Raspberry Pi device where you can switch on/off your LED lights connected on the bread board by giving data from Azure Mobile Services.

Requirements :
  • Visual studio 2015
  • Raspberry Pi 2 Model B
  • Azure Mobile Service
  • Bread Broad
  • Connecting Wires
  • LED

Activities We do

  1. Circuit Connection
  2. Creating Azure Mobile Service 
  3. Creating a Windows10 UWP for Raspberry Pi
  4. Creating a Client App
  5. Deploying the App
Step 1: Circuit Connection
  • Using GPIO Pin 5,6,13,19,26,7,8,25,24,18,15,14,2,3,4
  • I am take GPIO Pin 5 and 3.3v power supply


Connection can be made as the Circuit below ,
Step:2 Creating Azure Mobile Services

Creating an Azure Mobile Service from Azure Portal

New Mobile Dialog Box Opens, Click on COMPUTEà MOBILE SERVICE à CREATE,



Enter Mobile service url, select database, region & backend options,
  
  • Using existing SQL database, the below dialog box will open otherwise create new database & create new server options available.
  • If you are Creating new SQL database it means:
  • Enter the Server Login Name & Password.
  • Click Ok,

After creating mobile service, add new table.

 

Enter the table name as powercontrol.
 

Step 2: Creating a Windows10 Universal App for Raspberry PI, 

Below are the steps to create an app to the Azure Mobile Service,

  • Create a new Universal Blank App,


To perform this experiment there are two samples that we need to create,
  • Right Click on Reference Tab On Project & select “Manage NuGet Package”,
  • NuGet Package page will open & search for “Microsoft Azure Mobile Service."
  • Select from the list & click on Install button.

Connect your app to Azure

  • Again go to the Azure Management portal.
  • Select mobile service first tab & select windows platform.
  • Start Visual Studio & Open App.mainpage.xaml.cs,
  • Copy App Connection String From the Azure Portal.
  1. sealed partial class App : Application  
  2.     {  
  3.         /// <summary>  
  4.         /// Initializes the singleton application object.  This is the first line of authored code  
  5.         /// executed, and as such is the logical equivalent of main() or WinMain().  
  6.         /// </summary>  
  7.         public App()  
  8.         {  
  9.             this.InitializeComponent();  
  10.             this.Suspending += OnSuspending;  
  11.         }  
  12.    
  13.         public static MobileServiceClient MobileService = new MobileServiceClient(  
  14.             "https://mobileservice.azure-mobile.net/",  
  15.             "jwernSrEUNmPcpXPfMWatoCcCahglki89"  
  16.      
  •        Create one class (right click on project name & add new class item from list).

  • Note : table name on Azure & class name in project must be the same name.
  1. namespace RaspberryCloud  
  2. {  
  3.  public class powercontrol  
  4.  {  
  5.  public string id { getset; }  
  6.   
  7.  public string status { getset; }  
  8.  }  
  9. }  
  • Open Mainpage.xaml.
  • Add the Code.
    1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
    2.   
    3.         <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">  
    4.   
    5.             <Ellipse x:Name="LED" Fill="LightGray" Stroke="White" Width="100" Height="100" Margin="10"/>  
    6.   
    7.             <TextBlock x:Name="GpioStatus" Text="Waiting to initialize GPIO..." Margin="10,50,10,10" TextAlignment="Center" FontSize="26.667" />  
    8.   
    9.             <StackPanel Orientation="Horizontal" Width="500">  
    10.   
    11.                 <TextBlock x:Name="DelayText" Text="Current Status" Margin="10" FontSize="26.667" />  
    12.   
    13.                 <ListBox Name="l1" HorizontalAlignment="Center">  
    14.   
    15.                     <ListBox.ItemTemplate>  
    16.   
    17.                         <DataTemplate>  
    18.   
    19.                             <StackPanel>  
    20.   
    21.                                 <TextBlock FontSize="30" Foreground="Red" Text="{Binding status}"/>  
    22.   
    23.                             </StackPanel>  
    24.   
    25.                         </DataTemplate>  
    26.   
    27.                     </ListBox.ItemTemplate>  
    28.   
    29.                 </ListBox>  
    30.   
    31.             </StackPanel>  
    32.   
    33.         </StackPanel>  
    34.   
    35.     </Grid>  
  • Open MainPage.xaml.cs.
  • Add Reference for GPIO.
  • Add  Namespace using Windows.Devices.Gpio in Mainpage.xaml.cs.
  • Add Windows IoT Extensions for the UWP from Reference Library tab.
  • Add Below Code in Mainpage.xaml.cs,
  1. namespace Raspberrycloud  
  2. {  
  3.     /// <summary>  
  4.     /// An empty page that can be used on its own or navigated to within a Frame.  
  5.     /// </summary>  
  6.     public sealed partial class MainPage : Page  
  7.     {  
  8.         private const int LED_PIN = 5;  
  9.         private GpioPin pin;  
  10.         private GpioPinValue pinValue;  
  11.         private DispatcherTimer timer;  
  12.         private SolidColorBrush redBrush = new SolidColorBrush(Windows.UI.Colors.Red);  
  13.         private SolidColorBrush grayBrush = new SolidColorBrush(Windows.UI.Colors.LightGray);  
  14.   
  15.         public MainPage()  
  16.         {  
  17.             this.InitializeComponent();  
  18.             timer = new DispatcherTimer();  
  19.             timer.Interval = TimeSpan.FromSeconds(5);  
  20.             timer.Tick += Timer_Tick;  
  21.             InitGPIO();  
  22.             if (pin != null)  
  23.             {  
  24.                 timer.Start();  
  25.             }  
  26.         }  
  27.   
  28.         private void InitGPIO()  
  29.         {  
  30.             var gpio = GpioController.GetDefault();  
  31.   
  32.             // Show an error if there is no GPIO controller  
  33.             if (gpio == null)  
  34.             {  
  35.                 pin = null;  
  36.                 GpioStatus.Text = "There is no GPIO controller on this device.";  
  37.                 return;  
  38.             }  
  39.   
  40.             pin = gpio.OpenPin(LED_PIN);  
  41.             pinValue = GpioPinValue.High;  
  42.             pin.Write(pinValue);  
  43.             pin.SetDriveMode(GpioPinDriveMode.Output);  
  44.   
  45.             GpioStatus.Text = "GPIO pin initialized correctly.";  
  46.         }  
  47.   
  48.         public string value;  
  49.   
  50.         private async void Timer_Tick(object sender, object e)  
  51.         {  
  52.             List<powercontrol> datalist = await App.MobileService.GetTable<powercontrol>().ToListAsync();  
  53.             l1.ItemsSource = datalist;  
  54.   
  55.             foreach (var x in datalist)  
  56.             {  
  57.                 value = x.status.ToString();  
  58.             }  
  59.   
  60.             if (value == "on")  
  61.             {  
  62.                 pinValue = GpioPinValue.Low;  
  63.                 pin.Write(pinValue);  
  64.                 LED.Fill = redBrush;  
  65.             }  
  66.             else  
  67.             {  
  68.                 pinValue = GpioPinValue.High;  
  69.                 pin.Write(pinValue);  
  70.                 LED.Fill = grayBrush;  
  71.             }  
  72.   
  73.         }  
  74.     } 
To Deploy this App to Raspberry pi -- you need to change Architecture to ARM,

 
  • To Run this App, use Remote Machine to deploy.
  • Change Target device to Remote Machine.
  • Enter the Remote Machine IP address or DeviceName of Raspberry Pi.
  • Authentication mode : Universal (Unencrypted Protocol).
  • Using WINDOWS IOT CORE WATCHER to track IOT Devices easily.
  • Click on “Remote Machine” to run the app 

Step 4: Creating a Client App

Create a new Universal Blank App.



To perform this experiment there are two samples that we need to create,
  • Right Click on Reference Tab On Project & select “Manage NuGet Package”.
  • Add Azure Mobile Service Reference to the project.
  • Open Azure Management portal & Copy the Connection String and Paste the string in App.xaml.cs.

Create one class (right click on project name & add new class item from list).

Note: Table name on Azure & class name in project must be same name,
  1. namespace RaspberryCloud  
  2. {  
  3.     public class powercontrol  
  4.     {  
  5.         public string id { getset; }  
  6.    
  7.         public string status { getset; }  
  8.     }  
  9. }  
  • Open Mainpage.xaml
  • Add the below code.
  1. <StackPanel Margin="0,100,0,0" Width="200">  
  2.   
  3.             <Button x:Name="btnOn" Content="On" Click="btnOn_Click" HorizontalAlignment="Stretch"/>  
  4.   
  5.             <Button x:Name="btnOff" Content="Off" Click="btnOff_Click" HorizontalAlignment="Stretch" Margin="0,50,0,0"/>  
  6.   
  7.             <Button x:Name="btnInsert" Content="Reset" Click="btnInsert_Click" HorizontalAlignment="Stretch" Margin="0,50,0,0"/>  
  8.   
  9.         </StackPanel>  
  • Open the Mainpage.xaml.cs
  • Add the below code.
  1. Namespace RaspberryClientApp  
  2.   
  3. {  
  4.   
  5.     /// <summary>  
  6.   
  7.     /// An empty page that can be used on its own or navigated to within a Frame.  
  8.   
  9.     /// </summary>  
  10.   
  11.     public sealed partial class MainPage : Page  
  12.   
  13.     {  
  14.   
  15.         IMobileServiceTable<powercontrol> userTable = App.MobileService.GetTable<powercontrol>();  
  16.   
  17.    
  18.   
  19.         public MainPage()  
  20.   
  21.         {  
  22.   
  23.             this.InitializeComponent();  
  24.   
  25.         }  
  26.   
  27.    
  28.   
  29.         private async void btnOn_Click(object sender, RoutedEventArgs e)  
  30.   
  31.         {  
  32.   
  33.             List<powercontrol> datalist = await App.MobileService.GetTable<powercontrol>().ToListAsync();  
  34.   
  35.             powercontrol obj = new powercontrol();  
  36.   
  37.             foreach (var x in datalist)  
  38.   
  39.             {  
  40.   
  41.                 obj.id = x.id;  
  42.   
  43.                 obj.status = "on";  
  44.   
  45.             }  
  46.   
  47.             await userTable.UpdateAsync(obj);  
  48.   
  49.    
  50.   
  51.         }  
  52.   
  53.    
  54.   
  55.         private async void btnOff_Click(object sender, RoutedEventArgs e)  
  56.   
  57.         {  
  58.   
  59.             List<powercontrol> datalist = await App.MobileService.GetTable<powercontrol>().ToListAsync();  
  60.   
  61.             powercontrol obj = new powercontrol();  
  62.   
  63.             foreach (var x in datalist)  
  64.   
  65.             {  
  66.   
  67.                 obj.id = x.id;  
  68.   
  69.                 obj.status = "off";  
  70.   
  71.             }  
  72.   
  73.             await userTable.UpdateAsync(obj);  
  74.   
  75.         }  
  76.   
  77.    
  78.   
  79.         private async void btnInsert_Click(object sender, RoutedEventArgs e)  
  80.   
  81.         {  
  82.   
  83.             powercontrol obj1 = new powercontrol();  
  84.   
  85.             obj1.status = "on";  
  86.   
  87.    
  88.   
  89.             await userTable.InsertAsync(obj1);  
  90.   
  91.         }  
  92.   
  93.     }  
  94.   
  95. }  Run ClientApp in All Windows 10 devices 
  • Now run the app on Local Machine and execute On/Off operations from your Azure Mobile Services 
Conclusion:

We find out that it's easy to Connect your Raspberry pi devices to Azure Mobile Services for passing and sending data to azure services by managing IoT devices.
 
Read more articles on Raspberry Pi: