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 - 20 - Adding wisdom
  • 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: 17 Jun 2015 2:04 PM Date Created
  • Views 552 views
  • Likes 2 likes
  • Comments 1 comment
  • enchanted_objects
  • quotes
  • nodejs
  • magic_doctor_hat
  • enchanted-objects
Related
Recommended

MagicHat - 20 - Adding wisdom

amgalbu
amgalbu
17 Jun 2015

Thanks to the Text-To_Speech engine, I can easily add a nice function to the MagicHat: the capability to say wisdom phrases while it's idling


The implementation will be based on a web service that provides quotes as a JSON document

I found a lot of alternatives on www.mashape.com and I selected the quotes service provided by quotbook.com

The URL to invoke to get a new quote has the following format


https://yusufnb-quotes-v1.p.mashape.com/widget/~{search}.json


where {search} is the topic you are interested to get a quote about. To stay on the challenge topic, I will randomly search for the following topics

  • rowling
  • magician
  • harry potter

 

The quote web service will be queried by theNodeJS application

 

function getQuote(topic) {
  var http = require('http');
  options = { 
    host: 'yusufnb-quotes-v1.p.mashape.com', 
    path: '/widget/~' + topic + '.json',
    port: '1338', 
    headers: {
       'X-Mashape-Key': '<your mashape key>',
       'Accept': 'application/json"

     } }; 
 
 
  callback = function(response) { 
  r str = '';
     response.on('data', function(chunk) { 
      str += chunk; 
    }); 
    response.on('end', function() { 
      console.log(str); 
      var obj = JSON.parse(str);
 
 var mp3url='http://tts-api.com/tts.mp3?q='+encodeURIComponent(obj.quote);
      getMp3(mp3url, filename, function() {
        exec('/usr/bin/madplay -A10 ' + filename);
      });
    }); 
  }
 

  req = http.request(options, callback); 
  req.end();
 };


Since the Arduino side is aware whether the MagicHat is idling or not, the quote function will be triggered by Arduino itself by sending a new command


Serial1.println("QUOTE");


This command will be parsed by the serial port's data handler

 

// quote topics
var topics = ['rowling', 'magician', 'harry_potter'];

// generate a random int value between low and high (including limits)
function randomInt (low, high) {
    return Math.floor(Math.random() * (high - low) + low);
}

// serial port data handler

yunPort.on('data', function(data) {

  //console.log('data ' + data);
  
  _data = _data + data;
  var eol = _data.indexOf("\r\n");
  if (eol > 0)
  {
    _data = _data.substring(0, eol);
    if (_data.substring(0,5) == "PLAY ")
    {   
      console.log("Received command " + _data.substring(0,5)+","+_data.substring(5));
      var text = _data.substring(5);
      _data = "";

      text = text.replace(/ /g, '_');
      var filename = '/mnt/sda1/' + text + '.mp3';
      console.log("Checking file " + filename);
      if (fs.existsSync(filename))
      {
        console.log("Playing file " + filename);
        exec('/usr/bin/madplay -A10 ' + filename);
      }
      else
      {
        var mp3url='http://tts-api.com/tts.mp3?q='+encodeURIComponent(text);
        getMp3(mp3url, filename, function() {
          exec('/usr/bin/madplay -A10 ' + filename);
        });
      }
    }
    else if (_data.substring(0,5) == "QUOTE")
    {
      getQuote(topics[randomInt(0,topics.length-1)]);   
    }
    else
    {
      if (_ws)
        _ws.send(_data);
      
      _data = "";
    }  
  }
});

  • Sign in to reply

Top Comments

  • Workshopshed
    Workshopshed over 10 years ago +1
    This is definitely a good way to use the Yún with the "smarts" on the Linux side where there is plenty of storage and processing capability. I've seen lots of example where the Arduino side is doing way…
  • Workshopshed
    Workshopshed over 10 years ago

    This is definitely a good way to use the Yún with the "smarts" on the Linux side where there is plenty of storage and processing capability. I've seen lots of example where the Arduino side is doing way too much.

    • 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