Patrick Flickinger
Published © GPL3+

whyBaby

whyBaby is a baby sleep analyzer, monitor, and snoozing companion. Six sensors act in harmony to track baby's sleep and show a star-field.

AdvancedFull instructions provided3,415
whyBaby

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
Adafruit T-Cobbler Plus
×1
MCP3008 - 8-Channel 10-Bit ADC With SPI Interface
×1
SparkFun Ambient Light Sensor Breakout - TEMT6000
×1
SparkFun Sound Detector
×1
SparkFun Humidity and Temperature Sensor Breakout - HTU21D
×1
Raspberry Pi Wifi Module
×1
Can you imagine laser stars lamp
×1
Powerswitch Tail II
×1
Breadboard (generic)
Breadboard (generic)
×2
Headers
×1

Software apps and online services

Windows 10 IoT Core
Microsoft Windows 10 IoT Core
Visual Studio 2015
Microsoft Visual Studio 2015
New Relic
Microsoft Azure
Microsoft Azure

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Main sensor array.

Code

Reading class to serialize for Azure

C#
This is the object class that we populate for sending events up to Azure.
    public class Reading
    {
        public int Light { get; set; }
        public int Audio { get; set; }
        public int Envelope { get; set; }
        public int Gate { get; set; }
        public float Temp { get; set; }
        public float Humidity { get; set; }
        public string Timestamp { get; set; }
        public string Type { get; set; }
        public DateTime SensorTime { get; set; }

        public string ToJson()
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Reading));
            MemoryStream ms = new MemoryStream();
            ser.WriteObject(ms, this);
            string json = Encoding.UTF8.GetString(ms.ToArray(), 0, (int)ms.Length);

            return json;
        }
    }

EventHubHelper

C#
Helper class for sending events to Azure.
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Web.Http;

namespace BabyMonitor1
{
    public class EventHubHelper
    {
        ...
        
        Uri uri;
        private string sas;
        HttpClient httpClient = new HttpClient();
        
        public EventHubHelper()
        {
            this.InitEventHubConnection();
        }

        public void SendData(Reading reading)
        {
            reading.Timestamp = DateTime.UtcNow.ToString("o");
            reading.SensorTime = DateTime.Now;
            reading.Type = "SensorArray";
            sendMessage(reading.ToJson());
        }

        public async void sendMessage(string message)
        {
            try
            {
                HttpStringContent content = new HttpStringContent(message, Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
                HttpResponseMessage postResult = await httpClient.PostAsync(uri, content);

                if (postResult.IsSuccessStatusCode)
                {
                    Debug.WriteLine("Message Sent: {0}", content);
                }
                else
                {
                    Debug.WriteLine("Failed sending message: {0}", postResult.ReasonPhrase);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception when sending message:" + e.Message);
            }
        }

        private string SASTokenHelper()
        {
            int expiry = (int)DateTime.UtcNow.AddHours(24).Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
            string stringToSign = WebUtility.UrlEncode(this.uri.ToString()) + "\n" + expiry.ToString();
            string signature = HmacSha256(KEY, stringToSign);
            string token = String.Format("sr={0}&sig={1}&se={2}&skn={3}", 
                WebUtility.UrlEncode(this.uri.ToString()), WebUtility.UrlEncode(signature), expiry, KEYNAME);
            return token;
        }
  
        public string HmacSha256(string key, string value)
        {
            var keyStrm = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
            var valueStrm = CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8);

            var objMacProv = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256);
            var hash = objMacProv.CreateHash(keyStrm);
            hash.Append(valueStrm);

            return CryptographicBuffer.EncodeToBase64String(hash.GetValueAndReset());
        }
        
        public bool InitEventHubConnection()
        {
            try
            {
                this.uri = new Uri("https://" + SERVICE_BUS_NAMESPACE +
                              ".servicebus.windows.net/" + EVENT_HUB_NAME +
                              "/publishers/" + DISPLAY_NAME + "/messages");
                this.sas = SASTokenHelper();

                this.httpClient.DefaultRequestHeaders.Authorization 
                    = new Windows.Web.Http.Headers.HttpCredentialsHeaderValue("SharedAccessSignature", sas);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}

Credits

Patrick Flickinger

Patrick Flickinger

1 project • 4 followers

Comments