Create your first object connected to the Cloud

adminIoT1 Comment

The Cloud

Add a new “Web site” project to your solution (Right-click on the solution node, select “Add”, then “New web site”)

Select the project template named “ASP.NET Web Site”.

Select the “Web API” template and check the “MVC”, “Web API” and “Host in the Cloud” options.

Sign in with the Microsoft Account associated with your Microsoft Azure subscription.

If you do not own a Microsoft Azure subscription, check your MSDN subscription or BizSpark membership. You might have some unused benefits.

Type a unique domain prefix and select the closest region. “West Europe” for France. We don’t need a database.

Your web site project is now setup 🙂

We’re going to create a model object to keep our LED state.
Select the “Models” folder in the web site project, right-click and add a “Class”.

Name it “DeviceModel”.

This is the code for the model.
We use a singleton pattern to ensure than only one instance of this class is created, the same state will be shared in the whole app.
Only one property is needed. “LightOn” will be true when the LED is on, and false when the LED is off.

In an ASP.NET MVC app, a controller groups the APIs. We need one for our APIs
Right-click the “Controllers” folder, select “Add”, “Controller”.

Select the “Web API 2 Controller with read/write actions”.

Name it “DeviceController”

Wee will add 2 methods to our controller:
Get() to get the LED current state
Put() to change the LED state.

In the same folder, open the file “HomeController.cs”. This file is the code behind the web site’s home page
Add a “Switch” method to invert the LED state.

Open the “Index.cshtml” file in “Views\Home” folder. This is the home page.
Add a button to call the “Switch” mathod we just created.

The Cloud part is now done 🙂 It’s time to publish our web site to Microsoft Azure.
Right-click the web site project node, select “Publish”.

All the required information should be already filled.
Click “Publish”.

Just after a successful deployment, your web site will be displayed in a web browser.

Your web site is now fully functional. You can add “/help” to the URL to test your APIs.

Your project should look like this.

Related posts

  • Hello, it’s the first time I see something like:
    LightOn = s == “true”;
    What does that do exactly? Does this coding trick has a name? (in order to search for it on Google…)?
    Thank you