Adding an IoT Module to Sitecore. Part 1

Recently our team has participated in the Sitecore Hackathon - for the third year in a row. But long before the Hackathon day, we’ve been thinking about new ideas, projects and modules. I have been interested in hardware and building quadcopters for already a couple of years; and by chance I have several Arduino boards and a couple of Raspberry Pi microcomputers. So, when the #SCHackathon time has come, me and my colleagues thought it’d be cool to use some of this stuff and build a prototype to extend Sitecore xDB onto offline world via ‘Internet of Things’ concept.

hardware for Sitecore iot

Here’s the idea: let’s imagine, there is a big retailer with a network of showrooms in a country and a Sitecore based website with a catalogue of all the products. It would be helpful to track offline interactions with sample products in showrooms to adjust products positioning or advertising as well as having statistics of offline interest in marketing reports.

Typically, sample products stay on display tables and are attached to power supply and security sensors. Therefore it would be pretty easy to add a system which will recognize the fact that a ‘product with specific ID was taken from the table in specific location’ and send this event somewhere ( https://en.wikipedia.org/wiki/Internet_of_things). Ideally, by ‘somewhere’ we mean the Azure events hub (https://azure.microsoft.com/en-us/services/event-hubs/) which can maintain a lot of incoming requests and provide these events to the Sitecore when required.

In Sitecore site we will have a module which will be able to get offline interactions data from the Azure, resolve Sitecore item from the event data and perform useful operations with it. Meaning of ‘useful’ and ways to define product item in Sitecore by IoT event can vary a lot so we’ve decided to have a rule-based setting for possible actions and custom pipeline for IoT event parsing to allow easily extend and replace this logic. Raspberry Pi + Windows 10 IoT Core is the fastest way for a .NET developer to create prototype of an IoT item because it supports C# and does not require deep hardware knowledge. Also, there’re a lot of samples for this implementation: https://developer.microsoft.com/en-us/windows/iot/samples, https://github.com/ms-iot/samples/. You just write code in Visual Studio and then run it on the device available in your network. Thus, we took a spare Raspberry Pi 3, three buttons, several wires and connected these buttons to three I/O pins to simulate three different signals from a hypothetic sensor.

Sitecore iot prototype

Setting up a prototype like this requires just a few lines of code to read ‘button pressed’ event and send it to the cloud (simplified code):

 public void Init()
	{
		var gpio = GpioController.GetDefault();

		GpioPin button1Pin = gpio.OpenPin(5);
		if (button1Pin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
		{
			button1Pin.SetDriveMode(GpioPinDriveMode.InputPullUp);
		}
		else
		{
			button1Pin.SetDriveMode(GpioPinDriveMode.Input);
		}

		button1Pin.ValueChanged += buttonPin_ValueChanged;
	}
private void buttonPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
	{
		if (e.Edge == GpioPinEdge.FallingEdge)
		{
			SendDeviceToCloudMessagesAsync(CreateMessageByPinNumber(sender.PinNumber));
		}
	}
	

Press ‘Run’ in VS, wait a minute while app is deploying, press buttons and you can see that your messages received by IoT Hub in your Azure account. Now we can use them into useful information datasets,- for example register offline interactions in Sitecore xDB.

Coming soon: part 2 will describe Sitecore part