Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Štěpán Bechynský
Published © Apache-2.0

FEZ HAT, Windows 10 IoT Core and Azure IoT Hub

It is very basic application how to use sensors on FEZ HAT for Raspberry Pi and Azure IoT Hub. You can use this concept for IoT projects.

IntermediateFull instructions provided5,435
FEZ HAT, Windows 10 IoT Core and Azure IoT Hub

Things used in this project

Story

Read more

Code

MainPage.xaml.cs

C#
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

using Microsoft.Azure.Devices.Client;
using GHIElectronics.UWP.Shields;
using System.Text;

namespace IoTHUB101
{
    public sealed partial class MainPage : Page
    {
        // device connection string
        private const string _deviceConnectionString = "HostName=stepanb.azure-devices.net;DeviceId=stepanb-rpi-01;SharedAccessKey=xxxxxxx=";
        // object for device to 
        private DeviceClient _deviceClient = null;
        // FEZ HAT
        private FEZHAT _hat = null;
        // Timer for data reading
        private DispatcherTimer _timer = null;

        public MainPage()
        {
            this.InitializeComponent();

            _deviceClient = DeviceClient.CreateFromConnectionString(_deviceConnectionString, TransportType.Http1);
            SetupHAT();

            this._timer = new DispatcherTimer();
            // check how meny messages you can send in your pricing tier
            // every 5 minutes is 288 message per day so it will work for 10 devices in Free tier
            this._timer.Interval = TimeSpan.FromMinutes(5);
            this._timer.Tick += _timer_Tick;
            this._timer.Start();
        }
        private async void _timer_Tick(object sender, object e)
        {
            if (_hat == null)
            {
                return;
            }

            // message, check filed names and Stream Analytics Output configuration
            string msg = "{deviceId: 'stepanb-rpi-01',timestamp: " 
                + DateTime.Now.Ticks 
                + ",temperature: " 
                + _hat.GetTemperature().ToString() + "}";

            // prepare message
            Message eventMessage = new Message(Encoding.UTF8.GetBytes(msg));
            // send message
            await _deviceClient.SendEventAsync(eventMessage);
        }

        private async void SetupHAT()
        {
            // connect to FEZ HAT
            _hat = await FEZHAT.CreateAsync();
        }
    }
}

Credits

Štěpán Bechynský
8 projects • 30 followers
Internet of Things, Arduino, Microsoft, ex-Microsoft MVP

Comments