Tag Archives: IoT

Video: Windows IoT Core on Raspberry Pi 2


The Raspberry Pi 2 is a low cost, credit-card sized computer that plugs into a computer monitor or TV, and uses a standard keyboard and mouse. The Raspberry Pi 2 also supports running Windows 10 Core!

In this video we will create a Capacitive Touch Screen Clicker using Windows IoT Core on Raspberry Pi 2. This video will show how to create a Windows IoT Core project from (File – New) and go over using GPIO to control a 5v relay to simulate clicking a capacitive touch screen to collect points in the popular game “Cookie Clicker”.

Connecting the IoT and NodeJS to IFTTT


Connecting the IoT and Node.js to IFTTT
By Patrick

IFTTT has huge potential in its ability to connect devices quickly and easily. There was one thing it had been missing for a while – the ability to send and receive generic HTTP GET and POST requests. If you wanted to use IFTTT for something, the specific use for it had to be defined by IFTTT and published on their site within a channel. That is, until now!IFTTT recently released the Maker Channel. It is exactly what developers have been waiting for! It allows you to define triggers that are set off when they receive a HTTP request, along with actions that can make a HTTP request to a defined location. This opens up IFTTT to be used for virtually anything. It is now completely up to the imagination of the developer community.

To show what the Maker channel is capable of, we are going to set up a simple Arduino to communicate back and forth with IFTTT via Node.js. To experiment with sending triggers to IFTTT, we will toggle a LIFX lightbulb on and off via an Arduino powered light switch. To try out an IFTTT Maker action, we will connect an RGB LED to our Arduino which will change color any time we are mentioned on Twitter. Don’t like Twitter or don’t have a LIFX bulb? Not a problem at all, switch out the action/trigger with something else on IFTTT. Work with the Facebook channel instead of Twitter or trigger actions on your phone instead of a lightbulb. This is a lot of fun to tinker with.

If you are new to IFTTT, I previously covered the basics in my article on Connecting LIFX Light Bulbs to the IoT Using IFTTT. This article assumes you know all about triggers and actions, and now are ready to take it to the next level!

The code

If you’re keen to get straight into the code and try it out, you can find it here on GitHub.

How This Will Work

We are going to have a local Node.js server running with an Arduino connected via USB. Pressing a button on the Arduino set up will trigger a HTTP request to IFTTT to tell it to toggle our LIFX light. Our Node.js server will also have Express running to handle any incoming HTTP requests. Whenever IFTTT sees a new mention of us on Twitter, it’ll make a POST request to our server to trigger our Arduino’s LED.

Our Arduino Sketch

We’ll be connecting up a simple button and an RGB LED to an Arduino.
sshot-10586.png

Setting Up The Maker Channel

To get started, we will need to go to the Maker Channel on IFTTT and click “Connect”. Once it is set up, you’ll reach a screen that looks like so:
1435391743makerkey.jpg
On this screen, it provides your secret key you’ll need to trigger IFTTT actions via HTTP commands. Of course, you’ll need a bit more info than just the key, we need a URL to POST to that’ll trigger the action. To find this, click the link that says “How to Trigger Events”. It’ll open up a page that contains the URL you’ll want to use, conveniently with your key attached to it.
1435392055triggeringmakerevents.jpg
The URL we’ll be using for our LIFX bulb looks like this: http://maker.ifttt.com/trigger/light_switch/with/key/{{yourkeyhere}}. The URL part which says light_switch is our event name. We use this when putting together our IFTTT recipe. If you’re not triggering a LIFX bulb, choose a name that matches your event more accurately.

Toggling IFTTT Using Maker Triggers

Our Maker channel is active and ready to be used. We will begin by looking at how to toggle our IFTTT action – toggling a LIFX lightbulb on and off using a button from an Arduino.
Making the recipe itself is quite simple:

  1. We go to the Create New Recipe page on IFTTT.
  2. Select “Maker” as our Trigger Channel.
  3. Click on “Receive a web request” as our trigger type.
  4. Enter our event name of light_switch.
  5. Choose “LIFX” as our Action Channel.
  6. Click on “Toggle lights on/off” as our action type.
  7. Choose the LIFX lights we’d like this to affect.
  8. Give the recipe a title and click “Create Recipe”!

In our Node.js code, we use johnny-five to access our Arduino. When the board is ready, we define our button connected to pin 7 in the btn variable:

board.on('ready', function() {
console.log('Board ready');
btn = new five.Button(7);

Within the board’s ready event, we set up a watch for the button’s down event. If we detect the button has been pressed, we log that click to the console for debugging purposes (in the situation where a wire on the breadboard comes loose or something along those lines, having these console logs can save a lot of time!). Then, we use the npm module request to make a HTTP POST request to the URL we found above. We’ve got console logs upon its success for debugging purposes also.

btn.on('down', function(value) {
console.log('Light toggle pressed.');

request.post({
url: 'http://maker.ifttt.com/trigger/light_switch/with/key/{{yourkeyhere}}'
}, function(error, response, body) {
console.log('Body response was ', body);
console.log('Error was ', error);
});
});

Upon running the code, when we click the button and the request is successful, it should switch our LIFX lightbulb on and off! IFTTT will return a nice message that will look like so in our console.log:
Body response was Congratulations! You've fired the light_switch event
It is really simple to put together but has a lot of potential.
One thing to note – if you want to make a call to the IFTTT service via HTTPS, you’ll need a properly secured HTTPS server too. I had errors when using a local test server, so it may need to have a certificate that isn’t self signed.

Responding To IFTTT Triggers

We now can trigger IFTTT actions via Node.js and our Arduino. Let’s look at how to do things the opposite way – making our Arduino respond to IFTTT triggers. We’ll set up IFTTT to tell our Node.js server and Arduino whenever we are mentioned on Twitter.
We put together the recipe the other way around:

  1. We go to the Create New Recipe page on IFTTT.
  2. Select “Twitter” as our Trigger Channel.
  3. Click on “New mention of you” as our trigger type.
  4. Click “Create Trigger”.
  5. Choose “Maker” as our Action Channel.
  6. Click on “Make a web request” as our action type.
  7. Put in your POST request settings (we’ll cover these below) and click “Create Action”!

The screen with our HTTP request settings will look like so:
1435392088makingiftttjsonrequest.jpg

  1. Put in a publically accessible URL for your Node server. I personally use ngrok for testing all of this out. I cover how to use ngrok and similar services in my article on Accessing Localhost From Anywhere. Choose POST for the method (a GET request should work too if you’d prefer it).
  2. Choose “application/json” for the content type as our Node server will be expecting to receive JSON.
  3. Finally, include the JSON message we’d like to send. I put down {"color":"#55acee", "text":Text}. #55acee is the color I found from Twitter’s bird mascot, so I thought it the most appropriate choice!
  4. Text is a field dynamically filled in by IFTTT with the text from the tweet it found mentioning you. These fields are called IFTTT ingredients. We don’t really use it to change our LED color but I wanted to include an IFTTT ingredient in there to show how it’s done.
  5. To add an ingredient into your JSON, click on the body field to enter in your JSON string and you’ll find a test tube icon on the right hand side. If you click it, you’ll open a menu which will let you choose a field to include: 1435392132choosingingredients.jpg

Within our Node code, we set up our LED so that our johnny-five module knows about it and set up the server to listen for that POST request. It will respond to it by changing our LED.
Within the board’s ready event, we define our LED attached to pins 9, 10 and 11 within the variable of led like so:

led = new five.Led.RGB([11,10,9]);

The code might look odd as the pins count down from 11 rather than up. The reason for this is I prefer my wiring to have red, green and then blue from top to bottom on the Arduino board (e.g. 11, then 10 and 9).
We set its initial color to green:

led.color('#00ff00');

Outside of the board’s ready event, we create our routing for the /led POST request using express. Within the callback function for this route, we get the JSON data sent from IFTTT inside req.body and find the color key within it. We give that color to johnny-five‘s led.color() function to change the color of our LED to what we’ve been given. We log the JSON data in our console too.

app.post('/led', function (req, res) {
var response = req.body;
console.log('Response was ', response);
led.color(response.color);
res.send('LED request successful!');
});

Something to keep in mind – If you’re putting this on a publically accessible server that will be available for more than just test purposes, I’d recommend checking that the JSON data being provided is actually a color and is a legitimate request.
If you run the Node server and then recieve a tweet at your account, after a little while (it won’t be instant as it seems to take time for IFTTT to refresh that info from Twitter at times), your LED should go Twitter blue! In your console.log, you should see something like the following:
Response was { color: '#55acee', text: '@thatpatrickguy WOO HOO' }
Then, if you set up various other services along the same lines but with different colors, you can have a wide variety of LED color changes!

Conclusion

Having the ecosystem of IFTTT and all of its channels available to makers of any internet enabled technology that can send and receive HTTP requests is huge. Whilst we’ve been able to do things like include the Twitter API into Node.js applications for a long time now, this provides a whole range of possibilities in an API that is much easier to use and is incredibly modular. Switch out Twitter for something else and everything should still work with only a few adjustments!
If you make some really neat IFTTT Maker Channel creation from this guide, leave a note in the comments or get in touch with me on Twitter (@thatpatrickguy), I’d like to take a look!

Patrick’s latest venture is online at DevDiner.com, a site for developers looking to get involved in emerging tech. He is a SitePoint contributing editor focused on exploring and sharing the possibilities of new technology such as the Internet of Things, virtual/augmented reality and wearables. He is an instructor at SitePoint Premium and O’Reilly, a Meta Pioneer and freelance web developer who loves every opportunity to tinker with something new in a tech demo.

Displaying Web APIs on an Arduino LCD Using NodeJS


Displaying Web APIs on an Arduino LCD Using Node.js
// Kogonuso

LCDs. They’re fun. It’s one of the coolest ways to bring a voice to your Arduino as it speaks through glowing text. In this article we’re going to explore how to bring in data from a web API and display it on your Arduino’s LCD using Node.js.My favourite meetup of every month here in Sydney is the IoT Sydney meetup. It’s a great meetup (you should come along!). At the end of every meetup, there is a random prize draw for those who RSVPed. I thought it was about time that this was done in true IoT style, so I put together a quick Arduino LCD set up that pulls in Meetup.com event RSVP info and then randomly selects a lucky member. Thought it might be a nice surprise and way easier than drawing names out of a hat!
It also falls in neatly with the current series of Internet of Things articles I’m writing here at SitePoint. I’m exploring the many different IoT possibilities out there with various devices. Last week, I looked at using IoT data in the Unity game engine and before that, I looked at how to pull in data from the Jawbone Up.
Let’s get started!

Your Arduino Sketch

The sketch we’ll be using for this example looks like so:
1431043309lcdsketch.png
It is a slightly modified version of the one you’ll find in the Sparkfun Inventors Kit. There are other variations of LCD sketches you’ll find online that rearrange how things are connected up but will work the same way. You might just need to adjust which pins you define in the JavaScript below.

Our JavaScript Code

In our Node.js JavaScript file, we include the following:

var express = require('express'),
app = express(),
server = require('http').Server(app),
port = 5000,
five = require('johnny-five'),
request = require('request'),
_ = require('underscore'),
board = new five.Board(),
lcd;
board.on('ready', function() {
lcd = new five.LCD({
pins: [12, 11, 5, 4, 3, 2],
rows: 2,
cols: 16
});
this.repl.inject({
lcd: lcd
});
});
app.get('/chooseMember/:event_id', function(req, resp) {
request({
url: 'https://api.meetup.com/2/rsvps?key=474cc9332345ea7d7e135f50653c&event_id='+req.params.event_id,
json: true
}, function(error, response, body) {
var members = _.pluck(body.results, 'member'),
randomMember = members[_.random(members.length - 1)];

resp.json(randomMember);
console.log(randomMember.name);
lcd.clear().print(randomMember.name);
});
});
server.listen(port, function() {
console.log('Listening on ' + port);
});

Setting This Up Step By Step

If you’re pretty clued into Node.js, much of that code will already make sense to you. I’ll explain each bit just to ensure everyone is on the same page and provide guidance along the way on anything else we’ll need to set up.
To start with, we set up our express server variables in preparation to run a localhost server on port 5000:

var express = require('express'),
app = express(),
server = require('http').Server(app),
port = 5000,

We’ve then got a rather important thing to include, johnny-five. This is the npm library which gives us access to the functions we’ll need to control our Arduino via Node.js.

five = require('johnny-five'),

After that, we’re including the request module. We’ll be using this to make http requests from our Node.js server to the Meetup API.

request = require('request'),

To keep things very clean and simple, we’ll be using underscore to go through the arrays of data.

_ = require('underscore'),

The final two variables are the board and lcd variables that we’ll use to store the Arduino objects that johnny-five creates.

board = new five.Board(),
lcd;

We begin by waiting for our Arduino board to be ready to access, johnny-five sends a “ready” event when our board is ready to go.

board.on('ready', function() {

Once our board is ready to go, we let johnny-five know what sort of LCD we’ve got connected. We define the pins from top to bottom in an array:
1431043415lcdsketchwithpins.png
We also define how many rows and columns our LCD has. In my case it is a 2×16 LCD. All of this looks like so:

lcd = new five.LCD({
pins: [12, 11, 5, 4, 3, 2],
rows: 2,
cols: 16
});

Then we’ve got code which is optional (but handy to include) that will allow us to access the LCD functions from our command line as it is running (I’ll show this in action later in the article):

this.repl.inject({
lcd: lcd
});

Then, we have our one and only GET request that we’ve set up. In this request, we are expecting an event ID within the URL. For example: http://localhost:5000/chooseMember/221960710. The event ID is the one that you’ll find in the address bar when you visit the event’s page:
1431043678findingeventid.png
Our GET request looking for this ID looks like so:

app.get('/chooseMember/:event_id', function(req, resp) {

Then we get to the complex bit of the code! Our actual HTTP request to the Meetup API. We access this via the request function. We pass in two things here, our http options for the request and our callback function.
The options for our http call contain our url and a boolean to let the request know we’re expecting a JSON object back. This looks like so:

request({
url: 'https://api.meetup.com/2/rsvps?key=089cc9874628ealkjh27dkb50653s&event_id='+req.params.event_id,
json: true
}

Keen observers will have noticed we pass in our event ID from the URL string into the request URL using req.params.event_id. However there’s another string of characters in there which I haven’t explained. In order to access the Meetup API you’ll need an API key. You can find one of these at https://secure.meetup.com/meetup_api/key/:
1431043737gettingapikey.png
Finally, we’ve got our callback function which uses the returned JSON data. I’ve kept it simple and without error handling, however if this is a serious production level creation – add a check for the error variable.
The JSON output that the callback will return in our body variable looks like so:

{
"venue": {
"country": "au",
"city": "Sydney",
...
},
"created": 1428838649000,
"response": "yes",
"member_photo": {
"highres_link": "http://photos1.meetupstatic.com/photos/member/2/c/6/c/highres_161711372.jpeg",
"photo_id": 161711372,
...
},
"guests": 0,
"member": {
"member_id": 098361926,
"name": "Patrick Catanzariti"
},
...

We filter out a lot of that by using the _.pluck() function in underscore. This will help us focus on the important bit of our API – the members who RSVPed. The _.pluck() function goes through the results array and takes only the member info from each one.

function(error, response, body) {
var members = _.pluck(body.results, 'member'),

Then, we use the _.members() underscore function to randomly select a member from the resulting array.

randomMember = members[_.random(members.length - 1)];

We return that data through our express server as a JSON response to the GET request and log the name in our console so we can see who was chosen:

resp.json(randomMember);
console.log(randomMember.name);

Then we use the lcd johnny-five object we set up earlier to print out the name of the member onto our LCD. We start by using lcd.clear() to clear the LCD of anything currently on it and then use lcd.print() to print out the name characters.

lcd.clear().print(randomMember.name);
});

You’ll notice when you run this that some long names will get cut off, rather than wrap to the other line. If you’d like to wrap things onto a second line, try separating your randomMember.name into two 16 character strings, print the first string and then use lcd.cursor(1, 0); to move to the second line before printing the second one. In my case, it wasn’t needed as all we really need is to be able to tell whose name was drawn – a few missing characters usually won’t cause issues here.

Getting That Running On Your Arduino

We’ve got our Node.js code ready. Now, connect up your Arduino and make sure you’ve got the StandardFirmata sketch uploaded on it:
1431043878standardfirmatasketch.png
We’ll also need to make sure we’ve got all the dependencies sorted out. My package.json file looks like so:

{
"name": "MeetupRSVPSelector",
"version": "1.0.0",
"description": "Connecting the Meetup API to our Arduino LCD",
"main": "rsvp-selector.js",
"author": "Patrick Catanzariti",
"dependencies": {
"express": "^4.12.3",
"johnny-five": "^0.8.34",
"request": "^2.55.0",
"underscore": "^1.8.3"
}
}

Feel free to adapt that to your own project, the main thing you’ll want is those dependencies.
Then once you’ve got a package.json file sorted out, open up your console and go to the location of your code. Run this wonderful command:

npm install

Followed by this one:

node rsvp-selector.js

With that running, you should now be able to go to http://localhost:5000/chooseMember/221960710 (substitute your own event ID of course) and you’ll have a random rsvp appear as a result:
1431043936browserresponse.png
If you run it again, a new rsvp will appear. If all is going well, these names should be appearing on your Arduino’s LCD!
1431044034nameresponse.jpg

That Repl Thing We Mentioned

We included a bit of code that said this.repl.inject in our Node.js server earlier. What this does is allow you to run commands from within the terminal whilst your Node.js server is running to set what is being displayed on the LCD screen:
1431044066lcdclear.png
If we type in an lcd.print() we can get some magic happening:
1431044077lcdprint.png
Well… our message almost fit at least!
1431044305sitepointsayshi.jpg

Conclusion

We’ve successfully got a randomly selected member of the event group appearing on our LCD! This same idea could be applied to any number of other APIs out there, just adjust the request call and how you handle the data. Leave spooky messages on an LCD for housemates, keep tabs on the latest NBA playoffs scores or whatever else takes your fancy!
Make something fun based upon this code? Leave a note in the comments and share it around, I’d love to see it!

Patrick’s latest venture is online at DevDiner.com, a site for developers looking to get involved in emerging tech. He is a SitePoint contributing editor focused on exploring and sharing the possibilities of new technology such as the Internet of Things, virtual/augmented reality and wearables. He is an instructor at SitePoint Premium and O’Reilly, a Meta Pioneer and freelance web developer who loves every opportunity to tinker with something new in a tech demo.

Windows IoT Core Remote Wifi Scanner


via Windows IoT Core Remote Wifi Scanner – Falafel Software Blog.

Windows IoT Core has a nice web-based device management interface. This app can configure many aspects of the device, such as connecting the device to a WiFi network. Because the web-based device management was just a web app, I thought it shouldn’t be difficult to write a remote app to do the same.

I ended up with a remote app that could query WiFi networks visible to a Windows IoT Core device and connect or disconnect that device to those networks.
To get started, I connected my browser to the web-based device management app for one of my Windows IoT Core devices (minwinpc2).

Cylon NodeJS (Universal Windows) Sample


via Windows IoT – NodejsWUCylon.

In this sample, you will use Cylon running on a Raspberry Pi 2 to control a servo connected to an Arduino (with Firmata installed).

Using the esp8266 as host for I2C sensors


via How to instantly replace existing Raspberry Pis by the ESP8266 | JavaFX Delight.

For my outdoor sensors I used an RaspberryPi with BMP180 + TSL2561 via I2C transferring the sensor data via MQTT messages.

During an IoT Meetup Session at codecentric office in Frankfurt we discussed the usage of an esp8266and I was thrilled to get one and play with it.

Sending and Receiving Data from the IBM IoT Nodes in Node-RED with the Intel Edison Using the Intel Galileo Quickstart Code


via Sending and Receiving Data from the IBM IoT Nodes in Node-RED with the Intel Edison Using the Intel Galileo Quickstart Code (Enabling the world: IBM Bluemix, the cloud and cool app

A favorite in Bluemix prototyping is the Node-RED interface, a wiring tool that simplifies programming by turning common functions into nodes that can be added, removed, and connected at will.  In particular, Node-RED is a rather magical interface that simplifies MQTT, a publish-suscribe messaging protocol, in such a way that just about anyone can connect a device without much effort.  What does this mean?  I can send data from a device with a few simple lines of code.