Skip to content

Latest commit

 

History

History
100 lines (73 loc) · 8.98 KB

README.md

File metadata and controls

100 lines (73 loc) · 8.98 KB

SmartCoffee

Brew a perfect cup of coffee from iOS and Android apps using Xamarin, Azure IoT Hub, and Microsoft Cognitive Services.

Architecture Overview

Mobile Apps

SmartCoffee brings control over your coffee maker directly to you, wherever you are. To accomplish this, we built mobile apps for iOS and Android using Xamarin, which allows us to build native mobile apps for iOS, Android, and Windows using C#. Xamarin allows us to share significant amounts of our code across multiple platforms, while still maintaining 100% access to the underlying platform APIs and building native user interfaces with iOS and Android designers. SmartCoffee utilizes Xamarin.Forms to take this a step further, allowing us to build native user interfaces for iOS, Android, and Windows from a single, shared C# or XAML codebase.

The mobile application has two main functions:

  • Remotely start and stop the brewing process.
  • Control the coffee maker remotely using spoken commands.

To accomplish these functions, SmartCoffee utilizes Azure IoT Hub and Microsoft Cognitive Services, which are described in greater detail below.

Raspberry Pi

The Rapsberry Pi was invented in 2006 to revolutionize computing education. Ten years later, it has become the defacto standard for building Internet of Things applications.

What is the Raspberry Pi?

The Raspberry Pi is a low cost, credit-card sized computer that plugs into a computer monitor or TV, and uses a standard keyboard and mouse. It is a capable little device that enables people of all ages to explore computing, and to learn how to program in languages like Scratch and Python. It’s capable of doing everything you’d expect a desktop computer to do, from browsing the internet and playing high-definition video, to making spreadsheets, word-processing, and playing games. Source: https://www.raspberrypi.org/help/what-is-a-raspberry-pi/

Windows IoT Core is a compact, portable version of the Windows 10 operating system that is optimized for devices such as the Raspberry Pi. As a developer, one of the awesome things about Windows 10 IoT Core is that it allows me to build applications in C# using the Universal Windows Platform (UWP) API. This brings C# not only to the desktop, web, game consoles, and mobile phones, but now to devices such as the Raspberry Pi as well.

SmartCoffee uses the Raspberry Pi as a "field gateway" (described later) that acts as a communication medium between Azure IoT Hub and our coffee maker. When the SmartCoffee app running on the Raspberry Pi receives a message, such as "turn on", the Raspberry Pi acts on that message supplies power to our coffee maker to begin the brewing process.

Azure IoT Hub

Azure IoT Hub allows us to connect, monitor, and control our smart devices from anywhere. SmartCoffee uses Azure IoT Hub to make our coffee maker accessible from anywhere in the world with an internet connection, regardless of physical locations. In addition, Azure IoT Hub allows us to configure identities for each of our smart devices and provides per-device security mechanisms, so we can ensure that our messages are confidential and tamperproof. We can also collect device telemetry, giving us valuable insight the data our IoT solutions may be collecting.

The SmartCoffee app sends cloud-to-device messages to IoT Hub. Just like any message, we must specify a recipient (our unique identifier for our smart coffee maker) and a body (the action for the device to take). Azure IoT Hub receives that message and forwards it to correct device, based on unique identifier. SmartCoffee utilizes the concept of a "field gateway" (in this case a Raspberry Pi 3) which acts as a communication broker between Azure IoT Hub and our coffee maker.

public async Task SendMessageToDevice(string messageToDevice)
{
   var sender = new SenderLink(session, "sender-link", "/messages/devicebound");
   
   var message = new Message(System.Text.Encoding.UTF8.GetBytes(messageToDevice));
   message.Properties = new Properties();
   message.Properties.To = BuzzerDeviceTo;
   message.Properties.MessageId = Guid.NewGuid().ToString();
   message.ApplicationProperties = new ApplicationProperties();
   message.ApplicationProperties["iothub-ack"] = "full";

   await sender.SendAsync(message);
   await sender.CloseAsync();
}

When the Raspberry Pi receives a cloud-to-device message from Azure IoT Hub, it checks the body of the message to see what action should be taken. For example, if the message contains "turn coffee on", the Raspberry Pi supplies power to the correct GPIO pins to power the coffee maker and begin the brewing process. If the message contains "turn coffee off", the Raspberry Pi reduces the power to the coffe maker to halt the brewing process. The Raspberry Pi can also send device-to-cloud messages to report back device telemetry, such as the current state of the coffee maker.

var receivedMessage = await deviceClient.ReceiveAsync();
if (receivedMessage != null)
{
     var message = Encoding.UTF8.GetString(receivedMessage.GetBytes());
     if (message.ToLower().Contains("turn coffee maker on")) {
       ToggleCoffeeMakerRelay();
     }
     else if (message.ToLower().Contains("turn coffee maker off")) {
       ToggleCoffeeMakerRelay();
     }
     await deviceClient.CompleteAsync(receivedMessage);
}

We recently utilized Azure and Xamarin to build MyDriving, an open-source mobile application for iOS, Android, and Windows that collects car telemetry data from an OBD-II reader in the car, and reports that data to the cloud to gain valuable insights on routes and driving patterns.

Microsoft Cognitive Services

Microsoft Cognitive Services provides a set of APIs that makes difficult things, like speech and emotion recognition, very easy to accomplish in just a few lines of code. These APIs work across all platforms, and because they are based on machine learning, continue to improve over time.

SmartCoffee the Bing Speech API, one of the 20+ APIs exposed via Microsoft Cognitive Services to recognize speech input from an end user and convert it into an actionable text sequence. For example, when a user says "start making coffee", SmartCoffee sends this text to Microsoft Cognitive Services which converts the speech to text and sends back a JSON response containing the text spoken, as well as a confidence score. If the user spoke an actionable text sequence, SmartCoffee sends this message to Azure IoT Hub for processing by the coffee maker.

var speechResult = await BingSpeechApi.SpeechToTextAsync();

Console.WriteLine($"Name: {speechResult.Name}");
Console.WriteLine($"Name ToLowerInvariant: {speechResult.Name.ToLowerInvariant()}");
Console.WriteLine($"Confidence: {speechResult.Confidence}");

Terms of Usage

Data submitted to any of the smarter-apps samples may be retained by Microsoft for service improvement purposes. For more information, see the Microsoft Cognitive Services Preview Terms and the Developer Code of Conduct for Microsoft Cognitive Services.

License

Copyright (c) 2016 Pierce Boggan

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.