Windows Phone/Silverlight and MicroFramework

How use a windows phone device as remote control of a Fez Panda II/MicroFramework board

In this example will be used WindowsPhone/Silverlight and the MVVM Light Toolkit to create a native mobile application which communicates with a microframework board (Fez Panda II). If you want a cross-platform application; you must create a html/javascript application.


A windows phone device can be connected to a wifi network (for example a wifi router). If you have also a .Net Micro Framework board connected to the wifi network; you can send some http rest commands from the windows phone device to the .Net Micro Framework board. The Micro Framework board must support the tcp/ip protocol through a connect shield; in this way you can implement a small http web server (Silverlight version).

<UserControl x:Class="AppPhone1.Room1"
    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:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.WP7"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    DataContext="{ Binding ViewModelRoom1, Source={StaticResource locviewmodel} }"
    mc:Ignorable="d" d:DesignHeight="480" d:DesignWidth="480">

  <StackPanel>
    <TextBlock Name="textBlock1" Margin="12" Height="30"
             Text="{Binding ResponseMessage}" />
    <Button Name="button1" Content="Command-on" Height="140" Margin="12"
      cmd:ButtonBaseExtensions.Command="{Binding ButtonRoomCmd1}" />
    <Button Name="button2" Content="Command-off" Height="140" Margin="12"
      cmd:ButtonBaseExtensions.Command="{Binding ButtonRoomCmd2}" />
    <Button Name="button3" Content="Status-1" Height="140" Margin="12"
      cmd:ButtonBaseExtensions.Command="{Binding ButtonRoomCmd3}" />
  </StackPanel>
</UserControl>


using System;
using System.Net;
using System.Windows;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Collections.ObjectModel;
using System.Windows.Media.Animation;
using System.Windows.Documents;
using System.Windows.Controls;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;

namespace AppPhone1
{
    public class BaseViewModelRoom : BaseViewModel 
    {
        private ServiceData service;
        private object syncs = new object();
        private string _ResponseMessage;
        private RelayCommand _buttonRoomCmd1;
        private bool _visibleRoomCmd1;
        private RelayCommand _buttonRoomCmd2;
        private bool _visibleRoomCmd2;
        private RelayCommand _buttonRoomCmd3;
        private bool _visibleRoomCmd3;
        public int port;

        public BaseViewModelRoom()
        {
            service = new ServiceData();
            service.GetDataCompleted += LoadedData;
            _visibleRoomCmd1 = true;
            _buttonRoomCmd1 = new RelayCommand(
                () => CommandRoomCmd1(), () => VisibleRoomCmd1
            );
            _visibleRoomCmd2 = true;
            _buttonRoomCmd2 = new RelayCommand(
                () => CommandRoomCmd2(), () => VisibleRoomCmd2
            );
            _visibleRoomCmd3 = true;
            _buttonRoomCmd3 = new RelayCommand(
                () => CommandRoomCmd3(), () => VisibleRoomCmd3
            );
            _ResponseMessage = "";
        }

        public string ResponseMessage
        {
            get { return _ResponseMessage; }
            set
            {
                _ResponseMessage = value;
                RaisePropertyChanged("ResponseMessage");
            }
        }

        public bool VisibleRoomCmd1
        {
            get { return _visibleRoomCmd1; }
            set { _visibleRoomCmd1 = value; }
        }
        public RelayCommand ButtonRoomCmd1
        {
            get { return _buttonRoomCmd1; }
        }
        protected virtual void CommandRoomCmd1()
        {
            SendCommand("cmd1");
        }

        public bool VisibleRoomCmd2
        {
            get { return _visibleRoomCmd2; }
            set { _visibleRoomCmd2 = value; }
        }
        public RelayCommand ButtonRoomCmd2
        {
            get { return _buttonRoomCmd2; }
        }
        protected virtual void CommandRoomCmd2()
        {
            SendCommand("cmd2");
        }

        public bool VisibleRoomCmd3
        {
            get { return _visibleRoomCmd3; }
            set { _visibleRoomCmd3 = value; }
        }
        public RelayCommand ButtonRoomCmd3
        {
            get { return _buttonRoomCmd3; }
        }
        protected virtual void CommandRoomCmd3()
        {
            SendCommand("cmd3");
        }

        private void SendCommand(string cmd)
        {
            service.SendCommand(cmd, port);
        }

        public void LoadedData(object sender, EventResultArgs args)
        {
            ResponseMessage = (string)args.Data;
        }
    }
}


using System;
using System.Net;
using System.Windows;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Collections.ObjectModel;
using System.Windows.Documents;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.IO;

namespace AppPhone1
{
    public class ServiceData
    {
        //private string uribase = "http://127.0.0.1:13000/";
        private string uribase = "http://192.168.1.200:13000/";
        public event EventHandler<EventResultArgs> GetDataCompleted;

        public ServiceData()
        {
           
        }

        // ---------------------------------------------------------

        public void SendCommand(string cmd, int port)
        {
            string url = "";
            string guid = Guid.NewGuid().ToString();
            if (cmd == "cmd1")
                url = "turnOn?number={0}";
            if (cmd == "cmd2")
                url = "turnOff?number={0}";
            if (cmd == "cmd3")
                url = "status?number={0}";
            url = string.Format(url, port).Trim();
            string cmd01 = uribase + url + "&" + guid;
            Uri serviceUri = new Uri(cmd01);
            WebClient service = new WebClient();
            service.OpenReadCompleted += OpenReadCompleted;
            service.OpenReadAsync(serviceUri, cmd);
        }

        private void OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            string token = (string)e.UserState;
            if (e.Cancelled == true || e.Error != null)
            {
                MessageBox.Show(e.Error.Message);
                return;
            }
            string buffer = GetBuffer(e.Result);
            EventResultArgs args = new EventResultArgs(token, buffer);
            GetDataCompleted(this, args);
            e.Result.Close();
        }

        public string GetBuffer(Stream st)
        {
            string buffer = "";
            StreamReader sr = new StreamReader(st);
            buffer = sr.ReadToEnd();
            sr.Close();
            return buffer;
        }
    }
}


I have used the FEZ Ultimate Kit. This kit provides internet connectivity to FEZ Panda II. It also include eblocks for simple plug-and-play capabilities, giving you access to input/output and sensor data. Alternatively if you have not logic on the micro framework board; if you just want to control some relays; you can use a dedicated ethernet rele board. In this way; the ethernet rele board internally implement a small webserver so you only send the command to set on/off the rele.

Windows Phone/Silverlight and MicroFramework (How create a remote control)
Windows Phone/PhoneGap and MicroFramework (How create a remote control)
MicroFramework server side (How create a remote control)
Windows Phone + Micro Framework (using tile push notification)


Last edited Sep 1, 2012 at 11:18 PM by mastefano64, version 25

Comments

No comments yet.