element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • 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
Enchanted Objects
  • Challenges & Projects
  • Design Challenges
  • Enchanted Objects
  • More
  • Cancel
Enchanted Objects
Blog MagicHat - 13 - Web interface
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: amgalbu
  • Date Created: 15 May 2015 3:48 PM Date Created
  • Views 527 views
  • Likes 2 likes
  • Comments 1 comment
  • enchanted_objects
  • javascript
  • websocket
  • magic_doctor_hat
  • arduino
Related
Recommended

MagicHat - 13 - Web interface

amgalbu
amgalbu
15 May 2015

In this post we will have a look  at how the client side of the web application is implemented

For the web client I used the Angular JS framework and the graphical components made by Telerik. This components are not free (I have a licence since I used those components at work, but they can be easily replaced with free and/or open source components)

 

The most interesting part is how the web socket is created and how data is processed to update the web page.

The web socket is created by passing the url of thehost we want to connect to (in this case I am using the default IP address of Arduino Yun

 

                $scope.dataService = new WebSocket('ws://192.168.240.1:3000');

 

Then, we can add the handlers for the events we are interested in. For example, I am interested to handle the "data received event"

 

                $scope.dataService.onmessage = function(event){ 
                       console.log('data ' + event.data);
                       $scope.updateData(JSON.parse(event.data));
                };

 

Eventually, you could also get notification whenever the connection is open or when an error occurs

 

$scope.dataService.onopen = function(){ 
                       console.log('Hello');
                };                                            
 $scope.dataService.onerror = function(){ 
                       console.log('Error');
                };

 

The $scope.updateData(data) is a function that takes care of updating the widgets with the data received on the web socket. The JSON.parse(event.data) function creates a Javascript object from the string received. The object structure will look like this

 

{

temperature: 37,

pressure: {min: 0, max: 120},

bpm: 62,

pulse: [0,0,0,...,0,0];

}

 

This object is passed to the   $scope.updateData function

 

$scope.updateData = function(data)
 {
 if (data.temperature)
 $("#gauge_temp").data("kendoLinearGauge").value(data.temperature);

 if (data.pressure)
 {
 $("#bpmin-value").val(data.pressure.min);
 $("#bpmax-value").val(data.pressure.max);
 $("#gauge_bp").data("kendoRadialGauge").value(data.pressure.max);
 }

 if (data.bpm)
 { 
 $("#read_bpm").each(function() {
        $(this).industrial(data.bpm);
 });
 }

 if (data.pulse)
 { 
      if ($scope.pulse.length + (data.pulse.length/10) > 50)
      {
        var howMany = ($scope.pulse.length + (data.pulse.length/10)) - 50;
        $scope.pulse.splice(0, howMany);
 }                    
           
 console.log('Adding values..' + data.pulse.length);
 for (var i=0; i<data.pulse.length; i+=10)
 {
        $scope.pulse.push({ value: data.pulse[i] });
 }
 }
 };

 

The final result is shown in the following video

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

  • Sign in to reply
  • Workshopshed
    Workshopshed over 10 years ago

    There are some great libraries now for coding in Javascript now and that should allow you to spend your time on the hat aspect rather than the UI but still have a great looking dashboard at the end of it.

    We also use lots of Telerik stuff at work, my team are mostly using Knockout rather than Angular but the principles are very similar.

    • Cancel
    • Vote Up 0 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