element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Andy Clark's Blog Azure Sphere Secure IOT - Connecting to Azure Functions
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Workshopshed
  • Date Created: 13 Nov 2018 10:34 PM Date Created
  • Views 1659 views
  • Likes 5 likes
  • Comments 6 comments
  • azure sphere
  • iot
  • microsoft azure
Related
Recommended

Azure Sphere Secure IOT - Connecting to Azure Functions

Workshopshed
Workshopshed
13 Nov 2018

For my IOTCompass I need to be able to determined the direction to turn in. This can be done by passing the current and target positions to a function which can calculate a bearing. The calculation for this is approximated by the following formula as it does not take into account the wobbly shape of the earth:

 

Formula:θ = atan2( sin Δλ ⋅ cos φ2 , cos φ1 ⋅ sin φ2 − sin φ1 ⋅ cos φ2 ⋅ cos Δλ )
whereφ1,λ1 is the start point, φ2,λ2 the end point (Δλ is the difference in longitude)

 

From: https://www.movable-type.co.uk/scripts/latlong.html

 

This kind of maths is well within the capability of the Sphere's A7 core but to make the project a little more realistic I've decided to provide this calculation via an Azure Function

 

Getting going with Azure Functions

To be able to develop a function for Azure I needed to install the "Azure development workload" for Visual Studio.

I then created a new project, selected Azure Function App, selected .Net Standard 2 and a Http Trigger. A simple "Hello {name}" example was generated so I set the local.settings.json to have a valid port and I tested the function locally.

{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "",
        "AzureWebJobsDashboard": ""
    },
    "Host": {
        "LocalHttpPort": 7071,
        "CORS": "*"
    }
}

image

There's quite a lot of information generated in the console for this simple operation, and if you are not sure of the url for your function it's shown in green.

image

I published the test function to the portal and checked it was published and working. From the function you can get the function URL which enables you to test the function in the cloud. So I checked that one too.

image

image

Function to take location and calc a direction

I translated the function across to C# and wrapped that with an Azure Function and tested using the local debug option.

 

public static double calcDirection(Coords source, Coords dest) { 
            // From https://www.movable-type.co.uk/scripts/latlong.html
            var y = Math.Sin(dest.lon - source.lon) * Math.Cos(dest.lat);
            var x = Math.Cos(source.lat) * Math.Sin(dest.lat) - Math.Sin(source.lat) * Math.Cos(source.lat) * Math.Cos(dest.lon - source.lon);
            var brng = Math.Atan2(x, y);

            //Convert to degrees and normalise
            return 180 + (brng * (180 / Math.PI));
        }

 

      [FunctionName("LocationToDirection")]
        public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req, TraceWriter log) {

            log.Info($"LocationToDirection called");

            Coords source = new Coords();
            Coords dest = new Coords();

            var parseResult = parseParameters(req, out source, out dest);
            if (parseResult != null) return parseResult;

            double direction = calcDirection(source,dest);

            var returnObject = ResultsToJObject(source, dest, direction);

            return new JsonResult(returnObject);
        }

 

Calling an Azure Function from the Azure Sphere

For my initial version I'm just going to read the text result from the function, there are some CURL library examples provided with the Azure Sphere so that angle seems the best approach.

 

There's a couple of configuration items to set up. The app_manifest.json needs to be updated with the "AllowedConnections" to allow connection to the Azure function.  The connection string for the function also needs to be slotted into the curl_easy_setopt(curlHandle, CURLOPT_URL, call.

 

I tested this with the "Hello" function.

image

 

https://docs.microsoft.com/en-us/azure-sphere/app-development/curl

https://github.com/Azure/azure-sphere-samples/tree/master/Samples/CurlEasyHttps/CurlEasyHttps

 

Next

There's a few challenging tasks still to complete.

 

Talking to the GPS dongle over the UART port

Controlling a stepper motor using GPIO https://docs.microsoft.com/en-us/azure-sphere/reference/applibs-reference/function-gpio-getvalue

Parsing the JSON, there seems to be a good library for this https://zserge.com/jsmn.html

Using a device twin to pass the target coordinates, function URL and code to the Azure Sphere https://docs.microsoft.com/en-us/azure-sphere/app-development/azure-iot-sample

  • Sign in to reply

Top Comments

  • Fred27
    Fred27 over 6 years ago +1
    I'm following your investigation of Azure Sphere with interest. A couple of comments on the project in general though. How are you going to zero the stepper motor when it's powered on? When I did a similar…
  • Workshopshed
    Workshopshed over 6 years ago in reply to Fred27 +1
    1) I've a couple of options here. I could use a slotted opto sensor same as I did with my TopsyTurvy clock, that "homed" when started up so it knew where the hands were. Alternatively I could use the buttons…
  • Workshopshed
    Workshopshed over 6 years ago in reply to DAB +1
    It's not that tricky but as with all cloud platforms, you need to take care to ensure you don't spend lots of money by mistake
  • Workshopshed
    Workshopshed over 6 years ago in reply to shabaz

    Unless I make a major error I should keep only spend a few pence.

     

    https://azure.microsoft.com/en-gb/pricing/details/functions/

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • shabaz
    shabaz over 6 years ago in reply to Workshopshed

    One of the benefits of some PaaS, more flexible pricing : ) I once accidentally spent $200 in a single month with IaaS, forgot to turn the (massive) virtual machine off..

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Workshopshed
    Workshopshed over 6 years ago in reply to DAB

    It's not that tricky but as with all cloud platforms, you need to take care to ensure you don't spend lots of money by mistake

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 6 years ago

    Interesting capability.

    I am not sure if I am ready to start poking Azure with a stick yet.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Workshopshed
    Workshopshed over 6 years ago in reply to Fred27

    1) I've a couple of options here. I could use a slotted opto sensor same as I did with my TopsyTurvy clock, that "homed" when started up so it knew where the hands were. Alternatively I could use the buttons so that a user would manually complete the process.

    2) I want to keep the "UI" very simple so don't really want any kind of display on the device. However I did spot that the GPS module has a "GeoFence" option so I was thinking about spin the dial when you are really close to the destination ( a distance function would also work).

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube