<commit_before>---
layout: page-fullwidth
title: "Sending Data to the Cloud"
subheadline: "Raspberry Pi IoT Lab 3"
teaser: "In this lab you will build a Universal Windows Platform application that collects ambient light data and sends it to the Cloud."
show_meta: true
comments: true
header: no
breadcrumb: true
categories:
    - rpi2
author: "<NAME>"
permalink: /rpi2/03/
---

### Table of Contents
*  Auto generated table of contents
{:toc}

If you haven't already done so, please follow the instructions in [Lab 00: Getting Started](/rpi2/00/) section.

In this lab you will build a Universal Windows Platform application that detects ambient light and sends the data that is being collected to Azure IoT Hub. In following lab you will build a data pipeline to process the incoming data stream and output it to a visualization tool.

## Bill of Materials
What you will need:

1. [Raspberry Pi 2 - $42.00](http://www.amazon.com/Raspberry-Pi-Model-Project-Board/dp/B00T2U7R7I/)
2. [5V 2A Switching Power Supply w/ 20AWG 6' MicroUSB Cable - $7.95](https://www.adafruit.com/product/1995)
3. [Jumper wires (Male to Male) - $1.95](https://www.adafruit.com/product/1957)
4. [Jumper wires (Male to Female) - $1.95](https://www.adafruit.com/product/1954)
5. [Photoresistor - $0.95](https://www.adafruit.com/products/161)
6. [A Red LED - 25 for $8.00](http://www.adafruit.com/products/297)
7. [A 330 Ohm resistors - 100 for $3.90](http://www.amazon.com/E-Projects-Resistors-Watt-330R-Pieces/dp/B00BVOR6IS/)
8. [A 10k Ohm resistor - 100 for $4.99](http://www.amazon.com/E-Projects-10k-Resistors-Watt-Pieces/dp/B00BWYS9BA/)
9. [MCP3208 - 8-Channel 12-Bit ADC with SPI Interface](http://www.digikey.com/product-detail/en/MCP3208-CI%2FSL/MCP3208-CI%2FSL-ND/305929) or [MCP3008 - 8-Channel 10-Bit ADC with SPI Interface](https://www.adafruit.com/product/856) or [MCP3002 - 2-Channel 10-Bit ADC with SPI Interface](https://www.sparkfun.com/products/8636)
10. 8GB micro SD card - class 10 or better. Microsoft suggests [this one](http://www.amazon.com/gp/product/B00IVPU786) or [this one](http://www.amazon.com/SanDisk-Ultra-Micro-SDHC-16GB/dp/9966573445).

## Capturing Analog Data with a Voltage Divider
For this lab you will work with a few new concepts - both in the circuits connected to the Raspberry Pi 2 (RPi2) and in the Cloud. The first thing you will do is wire up the RPi2 to be able to read voltage as determined by the resistance created by a photoresistor. Wire your board according to the diagram (wire colors don't matter, but help with identification of purpose). This wiring uses an analog-to-digital-convertor (ADC) - either an MCP3208 or an MCP3002, depending on what you have - which enables you to capture analog input instead of simply digital input. When you did the lab with the LED you dealt only with a digital signal - you sent voltage to the LED to turn it on, or off (with no voltage). Many sensors, such as a _photoresistor_, are capable of analog input or output, giving them a broader range than simply a 1 or a 0. 

A _photoresistor_, also known as _light-dependent resistor (LDR)_ or a photocell, works by limiting the amount of voltage that passes through it based on the intensity of light detected. The resistance decreases as light input increases - in other words, the more light, the more voltage passes through the photoresistor.

In order to take advantage of the photoresistor you will create a _voltage divider_ - a passive linear circuit that splits the input voltage amongst two or more components (similar to a Y-splitter).

<img src="/images/lab02_schem.png"/>

To create the voltage divider needed for this lesson you will:

- Connect the voltage from the 5-volt (input voltage) pin to a circuit (using a breadboard).
- Connect the input voltage to a static resistor (10k Ohm).
- Establish a voltage divider coming out of the static resistor:
   - One route to the ADC which is connected to the RPi2.
   - One route to a variable resistor (the photoresistor).
- Completing the circuit out of the variable resistor to ground.

As the photoresistor increases its resistance (lower light intensity) more of the input voltage coming through the circuit is diverted to the ADC. That means that the less intense the light into the photoresistor the more resistance it creates, which in turn diverts more voltage to the ADC (the current has to go somewhere). Likewise, the more intense the light into the photoresistor, the less resistance it creates, which in turn means there is less voltage to divert to the ADC.

In short, the the darker it is, the more resistance the photoresistor provides and the more voltage is diverted to the ADC.

Here are the specific wiring instructions for the MCP3008 or MCP3208. For the MCP3002 use [this](/images/rpi2/rpi2_lab03_mcp3002_bb.png) diagram.

<img src="/images/rpi2/rpi2_lab03_mcp3008_bb.png"/>

__NOTE:__ _The ADC has a notch out of one side - ensure that the side with the notch is (according to the diagram) on the lower edge of the breadboard._

## Build the Universal Windows Platform Application
In this application you will read the voltage value coming into the ADC from the voltage divider - the higher the voltage, the darker it is (remember, you are reading in the residual voltage, which is diverted to the ADC when there is resistance in the photoresistor). You will use the _darkness_ value to determine if the LED should be on or off.

The ADC is connected to the RPi2 through the Serial Peripheral Interface (SPI) bus. SPI is a synchronous serial communication interface specification used for short distance communication, primarily in embedded systems. SPI devices communicate in full duplex mode using a master-slave architecture with a single master. The master device originates the frame for reading and writing. Multiple slave devices are supported through selection with individual slave select (SS) lines. SPI is a four-wire serial bus as follows:

1. SCLK - Serial Clock (output from master).
2. MOSI - Master Output, Slave Input (output from master).
3. MISO - Master Input, Slave Output (output from slave).
4. SS - Slave Select (active low, output from master).

We won't go any deeper into SPI or the pin layout of the two ADCs - suffice to say that the ADC is wired up to support the four-channel SPI bus, plus supply voltage and ground. The wire connecting the voltage divider to the ADC is the input channel you will read the residual voltage from. 

### Create a Blank Universal App
Launch Visual Studio and start a new __Blank App (Universal Windows)__ (found in the _C# -> Windows -> Universal_ node).

Name the application _IoTLigthSensor_.

### Add the Windows IoT Extensions for the UWP
Once the solution is created, click on the _Project_ menu and select _Add Reference_.

In the Reference Manager dialog, expand the _Universal Windows_ node and select _Extensions_.

In the list of extensions, check the box next to __Windows IoT Extensions for the UWP__ and click __OK__.

### Add the Microsoft.Azure.Devices.Client NuGet Package
Once the _Windows IoT Extensions for the UWP_ are added, click on the _Project_ menu and select _Manage NuGet Packages.._

Use the search field to search for __Microsoft.Azure.Devices.Client__. Click on the __Install__ button to install the package.
<commit_msg>Adding check for prerelease note<commit_after>---
layout: page-fullwidth
title: "Sending Data to the Cloud"
subheadline: "Raspberry Pi IoT Lab 3"
teaser: "In this lab you will build a Universal Windows Platform application that collects ambient light data and sends it to the Cloud."
show_meta: true
comments: true
header: no
breadcrumb: true
categories:
    - rpi2
author: "<NAME>"
permalink: /rpi2/03/
---

### Table of Contents
*  Auto generated table of contents
{:toc}

If you haven't already done so, please follow the instructions in [Lab 00: Getting Started](/rpi2/00/) section.

In this lab you will build a Universal Windows Platform application that detects ambient light and sends the data that is being collected to Azure IoT Hub. In following lab you will build a data pipeline to process the incoming data stream and output it to a visualization tool.

## Bill of Materials
What you will need:

1. [Raspberry Pi 2 - $42.00](http://www.amazon.com/Raspberry-Pi-Model-Project-Board/dp/B00T2U7R7I/)
2. [5V 2A Switching Power Supply w/ 20AWG 6' MicroUSB Cable - $7.95](https://www.adafruit.com/product/1995)
3. [Jumper wires (Male to Male) - $1.95](https://www.adafruit.com/product/1957)
4. [Jumper wires (Male to Female) - $1.95](https://www.adafruit.com/product/1954)
5. [Photoresistor - $0.95](https://www.adafruit.com/products/161)
6. [A Red LED - 25 for $8.00](http://www.adafruit.com/products/297)
7. [A 330 Ohm resistors - 100 for $3.90](http://www.amazon.com/E-Projects-Resistors-Watt-330R-Pieces/dp/B00BVOR6IS/)
8. [A 10k Ohm resistor - 100 for $4.99](http://www.amazon.com/E-Projects-10k-Resistors-Watt-Pieces/dp/B00BWYS9BA/)
9. [MCP3208 - 8-Channel 12-Bit ADC with SPI Interface](http://www.digikey.com/product-detail/en/MCP3208-CI%2FSL/MCP3208-CI%2FSL-ND/305929) or [MCP3008 - 8-Channel 10-Bit ADC with SPI Interface](https://www.adafruit.com/product/856) or [MCP3002 - 2-Channel 10-Bit ADC with SPI Interface](https://www.sparkfun.com/products/8636)
10. 8GB micro SD card - class 10 or better. Microsoft suggests [this one](http://www.amazon.com/gp/product/B00IVPU786) or [this one](http://www.amazon.com/SanDisk-Ultra-Micro-SDHC-16GB/dp/9966573445).

## Capturing Analog Data with a Voltage Divider
For this lab you will work with a few new concepts - both in the circuits connected to the Raspberry Pi 2 (RPi2) and in the Cloud. The first thing you will do is wire up the RPi2 to be able to read voltage as determined by the resistance created by a photoresistor. Wire your board according to the diagram (wire colors don't matter, but help with identification of purpose). This wiring uses an analog-to-digital-convertor (ADC) - either an MCP3208 or an MCP3002, depending on what you have - which enables you to capture analog input instead of simply digital input. When you did the lab with the LED you dealt only with a digital signal - you sent voltage to the LED to turn it on, or off (with no voltage). Many sensors, such as a _photoresistor_, are capable of analog input or output, giving them a broader range than simply a 1 or a 0. 

A _photoresistor_, also known as _light-dependent resistor (LDR)_ or a photocell, works by limiting the amount of voltage that passes through it based on the intensity of light detected. The resistance decreases as light input increases - in other words, the more light, the more voltage passes through the photoresistor.

In order to take advantage of the photoresistor you will create a _voltage divider_ - a passive linear circuit that splits the input voltage amongst two or more components (similar to a Y-splitter).

<img src="/images/lab02_schem.png"/>

To create the voltage divider needed for this lesson you will:

- Connect the voltage from the 5-volt (input voltage) pin to a circuit (using a breadboard).
- Connect the input voltage to a static resistor (10k Ohm).
- Establish a voltage divider coming out of the static resistor:
   - One route to the ADC which is connected to the RPi2.
   - One route to a variable resistor (the photoresistor).
- Completing the circuit out of the variable resistor to ground.

As the photoresistor increases its resistance (lower light intensity) more of the input voltage coming through the circuit is diverted to the ADC. That means that the less intense the light into the photoresistor the more resistance it creates, which in turn diverts more voltage to the ADC (the current has to go somewhere). Likewise, the more intense the light into the photoresistor, the less resistance it creates, which in turn means there is less voltage to divert to the ADC.

In short, the the darker it is, the more resistance the photoresistor provides and the more voltage is diverted to the ADC.

Here are the specific wiring instructions for the MCP3008 or MCP3208. For the MCP3002 use [this](/images/rpi2/rpi2_lab03_mcp3002_bb.png) diagram.

<img src="/images/rpi2/rpi2_lab03_mcp3008_bb.png"/>

__NOTE:__ _The ADC has a notch out of one side - ensure that the side with the notch is (according to the diagram) on the lower edge of the breadboard._

## Build the Universal Windows Platform Application
In this application you will read the voltage value coming into the ADC from the voltage divider - the higher the voltage, the darker it is (remember, you are reading in the residual voltage, which is diverted to the ADC when there is resistance in the photoresistor). You will use the _darkness_ value to determine if the LED should be on or off.

The ADC is connected to the RPi2 through the Serial Peripheral Interface (SPI) bus. SPI is a synchronous serial communication interface specification used for short distance communication, primarily in embedded systems. SPI devices communicate in full duplex mode using a master-slave architecture with a single master. The master device originates the frame for reading and writing. Multiple slave devices are supported through selection with individual slave select (SS) lines. SPI is a four-wire serial bus as follows:

1. SCLK - Serial Clock (output from master).
2. MOSI - Master Output, Slave Input (output from master).
3. MISO - Master Input, Slave Output (output from slave).
4. SS - Slave Select (active low, output from master).

We won't go any deeper into SPI or the pin layout of the two ADCs - suffice to say that the ADC is wired up to support the four-channel SPI bus, plus supply voltage and ground. The wire connecting the voltage divider to the ADC is the input channel you will read the residual voltage from. 

### Create a Blank Universal App
Launch Visual Studio and start a new __Blank App (Universal Windows)__ (found in the _C# -> Windows -> Universal_ node).

Name the application _IoTLigthSensor_.

### Add the Windows IoT Extensions for the UWP
Once the solution is created, click on the _Project_ menu and select _Add Reference_.

In the Reference Manager dialog, expand the _Universal Windows_ node and select _Extensions_.

In the list of extensions, check the box next to __Windows IoT Extensions for the UWP__ and click __OK__.

### Add the Microsoft.Azure.Devices.Client NuGet Package
Once the _Windows IoT Extensions for the UWP_ are added, click on the _Project_ menu and select _Manage NuGet Packages.._

Use the search field to search for __Microsoft.Azure.Devices.Client__. 

__NOTE:__ You must check the _Include prereleases_ box.

Click on the __Install__ button to install the package.

{'fraction_non_alphanumeric': 0.06722002993604571, 'fraction_numerical': 0.02925568104504014, 'mean_word_length': 4.206872121856182, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 12, 'lorem ipsum': 0, 'www.': 26, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6927654', 'n_tokens_mistral': 4455, 'n_tokens_neox': 3998, 'n_words': 2124}