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 - 12 - 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: 12 May 2015 8:17 AM Date Created
  • Views 722 views
  • Likes 1 like
  • Comments 4 comments
  • enchanted_objects
  • nodejs
  • magic_doctor_hat
  • arduino
  • bridge
Related
Recommended

MagicHat - 12 - Web interface

amgalbu
amgalbu
12 May 2015

While struggling to complete the blood-pressure measurement device, let's have a look about how to show measured value

 

In this project, measures will be communicated to the user in two way

  1. through a web interface that can be accessed by any device running a web browser
  2. through a Text-To-Speech engine

 

As I'm still waiting for the components required to build the Text-To-Speech engine, in this post I will focus on the web interface

The web interface will be built using node.

The first step to take is to install node and expand the memory. I followed this tutorial to accomplish this task. All steps completed with no issues (except you have to change to opkg configuration file by replacing the line

src/gz barrier_breaker http://download.linino.org/dogstick/all-in-one/latest/packages/

with

src/gz barrier_breaker http://downloads.arduino.cc/openwrtyun/1/packages

 

 

As I like experimenting new approaches to the a problem, I decided to move away from the typical solution based on periodic poll to update the values on the web interface and use web sockets instead. The data flow is shown in picture below

 

image

 

Basically, whenever the Atmel 32U4 has updated data, it sends such data (formatted as a JSON document) to the node application that is listening on port /dev/ttyATH0)

If a client is connected on a web socket, the node application sends the same document on the web socket. Upon receiving new data, web client update widgets accordingly

This is not the best approach from a software-engineering point of view (I broke the separation-of-concern principle as the Atmel32U4 should be unaware of data format used by the web client) but it's quick and easy  to implement and to understand

 

First of all, a node package needs to be installed in order to access the serial port from the node application itself

 

opkg node-serial

 

In the node application, first of all we need to create a listening web socket, that passively waits for a client connection.

 

_ws = null;
var wss = new webSocketServer({server:server});
wss.on('connection', function(ws) {
  _ws = ws;
  ws.on('message', function incoming(message) {
    console.log('received: %s', message);
  });
  ws.on('close', function(message) {
    console.log('closed');
    _ws = null;
  });

 

Then, we need to open the serial port that listens for data from Atmel 32U4.

 

_data = "";
yunPort = new serialPort.SerialPort('/dev/ttyATH0', { baudrate: 115200 });
yunPort.on('data', function(data) {
  console.log('data ' + data);
  if (_ws)
  {
    _data = _data + data;
    if (_data.indexOf("\r\n") > 0)
    {
      _ws.send(_data);
      _data = "";
    }
  }
});

 

The CR+LF bytes to detect the end of the JSON document produced by the Arduino sketch, so inside the "data received" event I need to check for the presence of the CR+LF pair. If this the case, I can send data to the web client

 

As I said, the Arduin sketch uses the serial connection (which is typically wrapped by the Bridge library) to send data. In order to have full access to the serial port, we first need to change the /etc/inittab file and remove the link between the serial port itself and the console. This operation is very simple: just open the file /etc/inittab and comment the last line as shown here

 

::sysinit:/etc/init.d/rcS S boot
::shutdown:/etc/init.d/rcS K shutdown
# removed to use Serial1 from Arduino sketch
#ttyATH0::askfirst:/bin/ash --login

 

Now, in the Arduino sketch, we can initialize the serial port

 

Serial1.begin(115200)

 

and finally write any data we want. In this case, I'm sending a JSON document ready to be sent to the web client

 

Serial1.print("{\"temperature\":");
Serial1.print(hat.getTemperature());
    Serial1.print(", \"bpm\":");
    Serial1.print(hat.getBPM());
    Serial1.print(", \"pressure\":{\"min\":0,\"max\":");
Serial1.print(hat.getPressure());
    Serial1.print("}, \"pulse\":[");
    for (i = 0; i<pulseDataLen; i++)
    {
      if (i > 0)
Serial1.print(",");
      Serial1.print(pulseData[i]);
    }
Serial1.println("]}");

 

The information included in the JSON document are

  1. temperature
  2. heartbeat per minute
  3. blood pressure
  4. heartbeat samples taken by the pulse sensor. In this way, the web client can show an interesting chart with the sampled values

 

In next post, I will talk about the client-side implementation of the web interface. See you soon!

  • Sign in to reply

Top Comments

  • amgalbu
    amgalbu over 10 years ago in reply to Workshopshed +1
    Yes, you absolutely agree with you! Probably the idea in the back of my mind was that JSON was not the most suitable notatin to use in an embedded resource-constrained environment Cheers
  • amgalbu
    amgalbu over 10 years ago in reply to Workshopshed +1
    I never thought about that and, yes, that's definetely a big backdoor, but it's required to make atmel communicate with Linino Yun designers didn't put much emphasis on easiness of use and sacrificed security…
  • amgalbu
    amgalbu over 10 years ago in reply to Workshopshed

    I never thought about that and, yes, that's definetely  a big backdoor, but it's required to make atmel communicate with Linino

    Yun designers didn't put much emphasis on easiness of use and sacrificed security (and that's ok because Arduino boards are for students, hobbiyist and prototyping, not for market products)

    However, probably a radical solution would be to remove the bootloader from Atmel or disable/remove USB port in the final product

     

    Cheers

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

    Was thinking about this today. Swapping out the "console" with your own application on the serial port should close what I see as a big back door on the Yún. If you have control over the Atmel32U4 side then you've also got root access to the Linux side. I'd not be surprised if there was a way that a "user" could flash their own code onto the Atmel32U4 side from Linux in turn granting elevated privilege on the Linux side.

    I was wondering how that /bin/ash --login command could be made to run as a lesser user but this is actually a better solution.

    I'll likely do the same and listen on a serial port but with my language of choice which is Python.

    Thanks,

    Andy

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • amgalbu
    amgalbu over 10 years ago in reply to Workshopshed

    Yes, you absolutely agree with you!

    Probably the idea in the back of my mind was that JSON was not the most suitable notatin to use in an embedded  resource-constrained environment

     

    Cheers

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

    I'm not sure you have broken the control separation principle there. The Atmel32U4 knows only of the serial port and sends data down that. If you changed your format for the web client then you'd need to also change the Node application but you would not necessarily need to change the Atmel32U4 code so the separation is still there. Equally if you change the format sent from the Atmel32U4 the you'd only need to change the next level to keep sending the old format to the WebClient.

    • 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