Raspberry Pi With Azure Mobile Services

Introduction

 
This article is about your Raspberry Pi device where you can switch on/off your LED lights connected on the breadboard 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 take GPIO Pin 5 and 3.3v power supply
 
Connection can be made as to 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,
 
5.png
  • 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 a 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"   
  • Create one class (right-click on project name & add a new class item from the list).
  • Note: table name on Azure & class name in the project must be the same name.
    1. namespace RaspberryCloud    
    2. {    
    3.  public class powercontrol    
    4.  {    
    5.  public string id { get; set; }    
    6.     
    7.  public string status { get; set; }    
    8.  }    
    9. }  
  • Open Mainpage.xaml.
  • Add the Code.
    1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
    2.    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">  
    3.       <Ellipse x:Name="LED" Fill="LightGray" Stroke="White" Width="100" Height="100" Margin="10"/>  
    4.       <TextBlock x:Name="GpioStatus" Text="Waiting to initialize GPIO..." Margin="10,50,10,10" TextAlignment="Center" FontSize="26.667" />  
    5.       <StackPanel Orientation="Horizontal" Width="500">  
    6.          <TextBlock x:Name="DelayText" Text="Current Status" Margin="10" FontSize="26.667" />  
    7.          <ListBox Name="l1" HorizontalAlignment="Center">  
    8.             <ListBox.ItemTemplate>  
    9.                <DataTemplate>  
    10.                   <StackPanel>  
    11.                      <TextBlock FontSize="30" Foreground="Red" Text="{Binding status}"/>  
    12.                   </StackPanel>  
    13.                </DataTemplate>  
    14.             </ListBox.ItemTemplate>  
    15.          </ListBox>  
    16.       </StackPanel>  
    17.    </StackPanel>  
    18. </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 the 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.    <Button x:Name="btnOn" Content="On" Click="btnOn_Click" HorizontalAlignment="Stretch"/>    
    3.    <Button x:Name="btnOff" Content="Off" Click="btnOff_Click" HorizontalAlignment="Stretch" Margin="0,50,0,0"/>    
    4.    <Button x:Name="btnInsert" Content="Reset" Click="btnInsert_Click" HorizontalAlignment="Stretch" Margin="0,50,0,0"/>    
    5. </StackPanel> 
  • Open the Mainpage.xaml.cs
  • Add the below code.
    1. Namespace RaspberryClientApp   
    2. {  
    3.  public sealed partial class MainPage: Page   
    4.  {  
    5.   IMobileServiceTable < powercontrol > userTable = App.MobileService.GetTable < powercontrol > ();  
    6.   public MainPage()   
    7.   {  
    8.    this.InitializeComponent();  
    9.   }  
    10.   private async void btnOn_Click(object sender, RoutedEventArgs e)   
    11.   {  
    12.    List < powercontrol > datalist = await App.MobileService.GetTable < powercontrol > ().ToListAsync();  
    13.    powercontrol obj = new powercontrol();  
    14.    foreach(var x in datalist) {  
    15.     obj.id = x.id;  
    16.     obj.status = "on";  
    17.    }  
    18.    await userTable.UpdateAsync(obj);  
    19.   }  
    20.   private async void btnOff_Click(object sender, RoutedEventArgs e)   
    21.   {  
    22.    List < powercontrol > datalist = await App.MobileService.GetTable < powercontrol > ().ToListAsync();  
    23.    powercontrol obj = new powercontrol();  
    24.    foreach(var x in datalist)   
    25.    {  
    26.     obj.id = x.id;  
    27.     obj.status = "off";  
    28.    }  
    29.    await userTable.UpdateAsync(obj);  
    30.   }  
    31.   private async void btnInsert_Click(object sender, RoutedEventArgs e)   
    32.   {  
    33.    powercontrol obj1 = new powercontrol();  
    34.    obj1.status = "on";  
    35.    await userTable.InsertAsync(obj1);  
    36.   }  
    37.  }  

  • 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: