Поделиться через


Consuming data/Azure from Raspberry PI and Windows 10 IoT

Windows 10 IoT is relatively new, and it is based on the Windows Universal platform. That said, it makes normal developer things we do slightly “different” as this uses different APIs and libraries than we are used to. For example, on a normal .net system we can use the QueueClient in azure; on a Universal app we can use Queue but that does not work on Win10 IoT.

So how do we get data to the Raspberry PI (RPI)? Enter WebAPI! One of my favorite technologies!

At the end of the day, you probably need more data/server site functionality anyway.

Currently I am using a timer to call my Azure hosted WebAPI to send data down to my RPI.

            m_dtTimer                                                       = new DispatcherTimer();
            m_dtTimer.Interval                                          = TimeSpan.FromMilliseconds(5000);
            m_dtTimer.Tick                                               += TimerEvent;
            m_dtTimer.Start(); 

 

Then in the Timer call use the HttpClient

private void TimerEvent(object o, object e)
{

     MakeWebRequest();

}

public async void MakeWebRequest()
{
            HttpClient http                                                 = new System.Net.Http.HttpClient();
            HttpResponseMessage hrResponse              = await http.GetAsync("https://yoursite.azurewebsites.net/api/yourapi");
            string strProgram                                            = await hrResponse.Content.ReadAsStringAsync();

            //Do coo stuff

 

On the WebAPI side, for this example I am just returning some data from the Azure Message Bus (Queue) on the Get() method. Here is that code:

public string Get()
{
            string strReturn                                             = string.Empty;
            string strConnectionString                            = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");           
            QueueClient qcClient                                   = QueueClient.CreateFromConnectionString(strConnectionString, "Pump"); 
            BrokeredMessage bm                                  = qcClient.Peek();
           
            if(bm != null)
            {
                bm                                                            = qcClient.Receive();
                strReturn                                                  = bm.Properties["Action"].ToString();
                bm.Complete();
            }

            return strReturn;
}

/*
Microsoft provides programming examples for illustration only, without
warranty either expressed or implied, including, but not limited to, the
implied warranties of merchantability and/or fitness for a particular
purpose. 

This sample assumes that you are familiar with the programming language
being demonstrated and the tools used to create and debug procedures.
Microsoft support professionals can help explain the functionality of a
particular procedure, but they will not modify these examples to provide
added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a
Microsoft Certified Partner or the Microsoft fee-based consulting line
at (800) 936-5200.
*/

Comments

  • Anonymous
    February 23, 2016
    Hi just wondering if you've done anything with azure IoT I was hoping to have an IFTTT push a message to azure and send it to a win10 iot pi or netduino.. Information on a setup like this is pretty light on