Monthly Archives: January 2017

IoTuesday: New Year’s Resolution #2 – Healthier Eating

via SparkFun Electronics Blog Posts

My first few blog posts of the new year will focus on the high-tech tools that might assist in keeping some classic New Year’s resolutions. If you missed the first one on vaping and smoking cessation please check it out! Also, this article serves as part 2 to my IoT Smart Pantry project from September.

-Nick


Last year I wrote about a project that I was working on for organizing foodstuffs and tracking their nutritional value. That post was mainly about the hardware build so this time around, I want to talk more about the software. You may remember I was planning to use the Nutritionix API to search product UPCs and return their nutrition facts. Let’s hack together a little javascript to interact with the Nutritionix service and maybe we can start building a nice display in the process.

Looking into the Nutritionix docs, I found out that I could sign up for free and request up to 50 UPC searches per day. In order to do that, I had to sign up for an application ID which was pretty simple. After my first sign in, I was presented with this page:

a screenshot of the Nutritionix App Key page

I blacked out my ID stuff so you weasels don’t go maxing out my free account

This is all of the info that I need to start poking at the API. According to the UPC Scanning quick-start guide, UPC searches are performed using a GET request formatted as follows:

https://api.nutritionix.com/v1_1/item?upc=[UPC]&appId=[ID]&appKey=[KEY]

And when I do that, I should get a response that looks something like this:

{
   "item_id": "51c3d78797c3e6d8d3b546cf",
   "item_name": "Cola, Cherry",
   "brand_id": "51db3801176fe9790a89ae0b",
   "brand_name": "Coke",
   "item_description": "Cherry",
   "updated_at": "2013-07-09T00:00:46.000Z",
   "nf_ingredient_statement": "Carbonated Water, High Fructose Corn Syrup and/or Sucrose, Caramel Color, Phosphoric Acid, Natural Flavors, Caffeine.",
   "nf_calories": 100,
   "nf_calories_from_fat": 0,
   "nf_total_fat": 0,
   "nf_saturated_fat": null,
   "nf_cholesterol": null,
   "nf_sodium": 25,
   "nf_total_carbohydrate": 28,
   "nf_dietary_fiber": null,
   "nf_sugars": 28,
   "nf_protein": 0,
   "nf_vitamin_a_dv": 0,
   "nf_vitamin_c_dv": 0,
   "nf_calcium_dv": 0,
   "nf_iron_dv": 0,
   "nf_servings_per_container": 6,
   "nf_serving_size_qty": 8,
   "nf_serving_size_unit": "fl oz",
}

So, make a GET request and get a JSON response with all of the product information neatly organized – perfect! This is going to be easy to handle with Javascript! Let’s start by putting a few basic inputs on a web page and use them to make the GET request to Nutritionix. Ideally, I’d like to be able to enter the UPC of a product into a text box and view the JSON report that comes back; that means I’ll at least need a text input, a button and a text field. The HTML looks something like this:

<input name="upc"/>
<input type="button" name="search" value="Food Me"/><br/>
<textarea name="results" style="width: 222px; height: 350px;"></textarea>

I couldn’t help but style things a little bit, but the basics are all there: a few elements with unique names so we can script them, and a clearly labeled button. Now that we have our page built, let’s add script to do the work. At the top of my Javascript, I’m going to declare two constants: appKEY and appID. This way I can reference them throughout this post without giving away the actual keys. The next order of business is to actually construct our GET request.

In Javascript, it’s best practice to write asynchronous code so that if a function takes longer than expected to execute, it doesn’t disrupt the user experience or produce unexpected results. In our case, that means we want our script to send the request and then go on managing other things until the response comes back from Nutritionix. The way we achieve this is by using callbacks. Callbacks, or callback functions, are essentially just a way of telling one function to hand off to another function when the first is finished. You can set a callback in Javascript by passing the name of your callback function as the “callback” parameter of another function. I know it can sound confusing, but have a look at our code below:

function upcGet(upc, callback)
{
        var getURL = "https://api.nutritionix.com/v1_1/item?upc=" + upc + "&appId=" + appID + "&appKey=" + appKEY; 

    var xmlHttp = new XMLHttpRequest();
    xmlHttp.onreadystatechange = function() { 
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
            callback(xmlHttp.responseText);
    }
    xmlHttp.open("GET", getURL, true); // true for asynchronous 
    xmlHttp.send(null);
}

function postResults(response)
{
$("[name='results']").html(response);
}

In this case postResult() is our callback function, and all it does is find the element with the name “results” and change the html inside that element to match the variable called “response,” which gets passed to it. The function called upcGet() is the function that we expect to take some time. You can see that we’re passing two parameters to upcGet(): upc and callback. The parameter named upc is what we’ll use to pass in the contents of the upc entry field; the “callback” parameter will be used to pass the callback function. In this context, whenever we call the function callback(), it actually calls whatever function name that we passed in as the “callback” parameter.

The first line of the code in upcGet() simply concatenates a bunch of strings together to format a valid GET request. Included are our variables for upc, appID and appKEY. Next, we create an object called “xmlHttp” to represent a new Http request. Then we set up an event handler, so that we can tell the script what to do when the ready state of the request changes. In this case, we check to see if we got a good response and we launch our callback function. Now that our event listener in ready to catch the response, we can finally launch our request! We do this using the .open method.

The last thing to do is to make the button kick off this process:

$(document).on("click", "[name='search']", function(){
upcGet($("[name='upc']").val(), postResults);
})

Here we’re just using the JQuery .on() method to attach an event to our button. When the button is clicked, the upcGet() function will get called with the value of our text input named “upc” as the first argument, and the name of our callback function as the second argument. When we spin this all up and enter a UPC, it looks a little like this:

screenshot of the resulting webpage

May not look like much, but it’s got it where it counts

Not a bad start - but that JSON response looks like a real mess. What if I just want to pick a few values from this list and display them? Well, first we need to modify our form so that there are fields for each of the line items that we want to isolate:

<input name="upc"/>
<input type="button" name="search" value="Food Me"/><br/><br/>
<textarea name="calories" rows=1 style="width: 153px;"></textarea>&nbsp;&nbsp;Calories<br/>
<textarea name="fat" rows=1 style="width: 153px;"></textarea>&nbsp;&nbsp;Fat<br/>
<textarea name="sodium" rows=1 style="width: 153px;"></textarea>&nbsp;&nbsp;Sodium<br/>
<textarea name="sugars" rows=1 style="width: 153px;"></textarea>&nbsp;&nbsp;Sugars<br/>

As you can see, we now have a text area for each of four separate values: calories, total fat, sodium and sugars. That’s easy enough, but the real challenge is teasing out each of those values and slotting them into those fields… or is it? Since our response is received in JSON format, it’s actually trivial to pick particular values out of the list by using the JSON.parse() method. We just need to make a few minor adjustments to our postResults() function:

function postResults(response)
{
    var calories;
  var fat;
  var sodium;
  var sugars;

  var foodItem = JSON.parse(response);  

    $("[name='calories']").html(foodItem.nf_calories);
    $("[name='fat']").html(foodItem.nf_total_fat);
    $("[name='sodium']").html(foodItem.nf_sodium);
    $("[name='sugars']").html(foodItem.nf_sugars);
}

Notice how by creating a new object called “foodItem” to contain the parsed JSON data, I was able to reference each of the values by appending the key name to my foodItem object. The key-value pairs in our JSON document were turned into a Javascript object such that, in this case, foodItem.nf_calories is now equal to the number of calories in the food item. And now our page looks like this:

a screenshot of the page after modifications

Hey! It works!

Alright, now our page really does something. But before we call it a day, why not make it look pretty? We can replace the boring text area fields with flashy dials using the jQuery-Knob library. All we need to do is replace our text area tags with input tags of the class “dial.” But there’s a little more styling we can do; here’s what the markup ended up looking like:

<div style="text-align: center;">
Enter a UPC
</div>

<div style="text-align: center;">
<input name="upc"/>
</div>
<br/>

<div style="text-align: center;" name="productName">

</div><br/>

<div style="text-align: center;">
<div style="text-align: center; display: inline-block;">
Calories<br/>
<input type="text" name="calories" class="dial" data-min="0" data-max="500" data-fgColor="#66CC66" data-angleOffset="-125" data-angleArc="250" data-readOnly="true" data-width="100">
</div>

<div style="text-align: center; display: inline-block;">
Fat
<br/>
<input type="text" name="fat" class="dial" data-min="0" data-max="500" data-fgColor="#66CC66" data-angleOffset="-125" data-angleArc="250" data-readOnly="true" data-width="100">
</div>

<div style="text-align: center; display: inline-block;">
Sodium
<br/>
<input type="text" name="sodium" class="dial" data-min="0" data-max="500" data-fgColor="#66CC66" data-angleOffset="-125" data-angleArc="250" data-readOnly="true" data-width="100">
</div>

<div style="text-align: center; display: inline-block;">
Sugars
<br/>
<input type="text" name="sugars" class="dial" data-min="0" data-max="500" data-fgColor="#66CC66" data-angleOffset="-125" data-angleArc="250" data-readOnly="true" data-width="100">
</div>
</div>

Part of the reason that this seems like a lot more markup (even though it really isn’t) is because the JQuery Knob library makes liberal use of data-* attributes in order to style the dial elements. I’ve also added the “productName” block, deleted the search button and wrapped everything into a number of other blocks to better organize it. Once the library itself has been included in the page, there isn’t a lot of code modification that has to happen. First, removing the search button means I have to trigger the getUPC function some other way. Personally, I like when you can hit the ‘Enter’ key on a form:

$(document).on("keydown", "[name='upc']", function(e){
console.log(e);
if (e.keyCode === 13) {
        upcGet($("[name='upc']").val(), postResults);
    }
});

In this chunk of code, all I’m doing is checking every time a key is pressed in the upc field, whether the key that’s pressed is the ‘Enter’ key. When it is, I launch my function. The next thing to do is to initialize the JQuery Knobs:

    $(function() {
        $(".dial").knob();
    });

 $('.dial')
        .val(0)
        .trigger('change');

This is just attaching the hooks from my .dial class elements to the Knob library. Then I set all of them to 0 so they have a consistent look. Finally, we make a minor change to the postResults() function:

function postResults(response)
{
    var calories;
  var fat;
  var sodium;
  var sugars;

  var foodItem = JSON.parse(response);

    $("[name='calories']").val(foodItem.nf_calories);
  $("[name='calories']").trigger('change');
    $("[name='fat']").val(foodItem.nf_total_fat);
    $("[name='fat']").trigger('change');
  $("[name='sodium']").val(foodItem.nf_sodium);
  $("[name='sodium']").trigger('change');
  $("[name='sugars']").val(foodItem.nf_sugars);
  $("[name='sugars']").trigger('change');

  $("[name='productName']").html(foodItem.item_name);

}

Now, instead of posting the results to a number of text fields, we’re posting the results to our newly created dial elements. We’re also extracting the product name from the JSON and displaying it in the productName block. We can further customize the look of our page by adding a stylesheet:

@font-face {
  font-family: 'Varela Round';
  font-style: normal;
  font-weight: 400;
  src: local('Varela Round Regular'), local('VarelaRound-Regular'), url(https://fonts.gstatic.com/s/varelaround/v8/APH4jr0uSos5wiut5cpjrugdm0LZdjqr5-oayXSOefg.woff2) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215;
}

*{
  font-family: 'Varela Round';
  font-size: 110%;
  color: #66CC66;
  background-color: white;
}

input[name="upc"]:focus,
input[name="upc"]{
  background-color: transparent;
  border: solid;
  border-width: 0 0 5px 0;
  outline: 0;
  -webkit-box-shadow: none;
          box-shadow: none;
  text-align: center;
}

This stylesheet adds a Google font, removes the borders from our upc field and changes the color of a few elements. Let’s see what the page looks like now:

a screenshot of the page after another round of modifications

Pretty solid, but does it work?

a screenshot of the same page displaying a list of results

It does!

I’d say we know pretty well how to interact with the Nutritionix API now! My next step is going to be installing nodeJS on a Raspberry Pi and integrating our little page with input from the hardware. I’ll dig into this in the next installment of this project but in the meantime, I’d be happy to talk Javascript in the comments section! We sometimes gloss over web programming here on the SparkFun blog, but with IoT rising in popularity, it’s going to become more and more important even to makers who stick to embedded platforms. What are your feelings about Javascript/nodeJS? Is it the new hotness, or just another language/toolset?

comments | comment feed

Watch the Toa Mata Band perform a cover of Kraftwerk

via Arduino Blog

By now, you are probably familiar with the Toa Mata Band–the world’s first LEGO robotic band controlled by Arduino Uno, which is hooked up to a MIDI sequencer.

Now a few years since Toa Mata Band’s debut, Italian producer Giuseppe Acito has shared the group’s latest music video: a cover of Kraftwerk’s “The Robots.” As you can see below, Acito himself performs the 1978 track’s vocals while the LEGO Bionicle figures play the tunes using a variety of gadgets, including pocket synths, drum pads, xylophone keys, and iPhones running apps.

This is my inspiration to this Kraftwerk ‘s tribute, regarding their vision of the future and to their brilliant approaching to the music with a touch of irony that even now is still influencing a thousand of music producers around the world. My video contribution from Toa Mata Band (a tiny orchestra of Lego robots driven by Arduino) represents a version of the iconic tune “The Robots” using nowadays open-source technologies, microcontrollers, 3D printer, robot arm, music DAW, smarthphones, pocket synthesizers and more… trying to give a new way to read the message we got from them 40 years ago.

#FreePCB via Twitter to 2 random RTs

via Dangerous Prototypes

IRToy-600x369

Every Tuesday we give away two coupons for the free PCB drawer via Twitter. This post was announced on Twitter, and in 24 hours we’ll send coupon codes to two random retweeters. Don’t forget there’s free PCBs three times a every week:

  • Hate Twitter and Facebook? Free PCB Sunday is the classic PCB giveaway. Catch it every Sunday, right here on the blog
  • Tweet-a-PCB Tuesday. Follow us and get boards in 144 characters or less
  • Facebook PCB Friday. Free PCBs will be your friend for the weekend

Some stuff:

  • Yes, we’ll mail it anywhere in the world!
  • Check out how we mail PCBs worldwide video.
  • We’ll contact you via Twitter with a coupon code for the PCB drawer.
  • Limit one PCB per address per month please.
  • Like everything else on this site, PCBs are offered without warranty.

We try to stagger free PCB posts so every time zone has a chance to participate, but the best way to see it first is to subscribe to the RSS feed, follow us on Twitter, or like us on Facebook.

Robocod

via Raspberry Pi

Fishbowl existence is tough. There you are, bobbing up and down in the same dull old environment, day in, day out; your view unchanging, your breakfast boringly identical every morning; that clam thing in the bottom of the tank opening and closing monotonously – goldfish can live for up to 20 years. That’s a hell of a long time to watch a clam thing for.

fishbowl on wheels

Two fish are in a tank. One says “How do you drive this thing?”

Indeed, fishbowl existence is so tough that several countries have banned the boring round bowls altogether. (There’s a reason that your childhood goldfish didn’t live for 20 years. You put it in an environment that bored it to death.) So this build comes with a caveat – we are worried that this particular fish is being driven from understimulus to overstimulus and back again, and that she might be prevented from making it to the full 20 years as a result. Please be kind to your fish.

What’s going on here? Over in Pittsburgh, at Carnegie Mellon University, Alex Kent and friends have widened the goldfish’s horizons, by giving it wheels. Meet the free-range fish.

Just Keep Swimming

Build18 @CMU . . . . . . . . . . . . * Jukin Media Verified * Find this video and others like it by visiting https://www.jukinmedia.com/licensing/view/949380 For licensing / permission to use, please email licensing(at)jukinmedia(dot)com.

Alex K, negligent fishparent, says that the speed and direction of the build is determined by the position of the fish relative to the centre of the tank. The battery lasts for five hours, and by all accounts the fish is still alive. Things are a bit jerky in this prototype build. Alex explains:

The jerking is actually caused by the Computer Vision algorithm losing track of the fish because of the reflection off of the lid, condensation on the lid, water ripples, etc.

Alex and co: before you look at more expensive solutions, try fixing a polarising filter to the camera you’re using.

All the code you’ll need to torture your own fish is available at GitHub.

Of course, Far Side fans will observe that there is nothing new under the sun.

Fishbowl on wheels by Gary Larson

Image from Gary Larson, The Far Side.

If you’ve got any good fish puns, let minnow in the comments.

 

The post Robocod appeared first on Raspberry Pi.

Your Arm Is The Ideal Controller

via hardware – Hackaday

With interest and accessibility to both wearable tech and virtual reality approaching an all-time high, three students from Cornell University — [Daryl Sew, Emma Wang, and Zachary Zimmerman] — seek to turn your body into the perfect controller.

That is the end goal, at least. Their prototype consists of three Kionix tri-axis accelerometer, gyroscope and magnetometer sensors (at the hand, elbow, and shoulder) to trace the arm’s movement. Relying on a PC to do most of the computational heavy lifting, a PIC32 in a t-shirt canister — hey, it’s a prototype! — receives data from the three joint positions, transmitting them to said PC via serial, which renders a useable 3D model in a virtual environment. After a brief calibration, the setup tracks the arm movement with only a little drift in readings over a few minutes.

[Sew, Wang and Zimmerman] see their project as an easy-to-implement alternative to the high-end systems currently extant in the gaming, virtual reality, fitness and medical industries. We can’t wait ’till we can combine this with tracking individual fingers.

If seeing this project has warmed you up to the topic of rapid prototyping, check out [Ben Krasnow’s] advice on the topic from his SuperCon talk. We’d also like to point out [Bodo Hoenen’s]  talk about a system that uses electromyography to pick up the movement of the muscles in the arm.


Filed under: hardware, Virtual Reality

PSU burner

via Dangerous Prototypes

pics-DSC_8259-600

Bob writes:

What does one do when designing a power supply? Well, build a power supply tester, of course. One of the simplest things to build is a constant current load. This will allow for testing of the endurance of the power supply, as most of the designs out there are using slow components.
However, I wanted to make a better one: one that I could hook up to my Analog Discovery and generate a test waveform to be able to connect and disconnect the load fast. This is a weekend project, so all parts are not the best for the purpose, just what I had around.

More details at Electro Bob project page.