Nodebots / Hardware Nodebots / Hardware

Showing Tweets on the LCD Screen

Check out a free preview of the full Nodebots / Hardware course:
The "Showing Tweets on the LCD Screen" Lesson is part of the full, Nodebots / Hardware course featured in this preview video. Here's what you'd learn in this lesson:

Steve pulls in a real-time Twitter stream and sends it to the LCD.

Get Unlimited Access Now

Transcript from the "Showing Tweets on the LCD Screen" Lesson

[00:00:00]
>> Steve Kinney: So I've set up and configured the Twitter API keys. And those are in my env.json. So I can kinda pull that in. I'll say cost env = require(./env) with node, if it's JSON, you can omit the suffix, or you can put .json, either one will work. So now we have that key in there.

[00:00:26] There's another fun little library that I put in your package json, which is node twitter stream. If you have a twitter api key, it will get you a real time feed of every tweet that contains a given word, right? Which is for most words you can think of, if you thought the temperature sensor was a bit of an onslaught then this as well, and they're pretty big JSON objects.

[00:00:52] So we'll just do something really simple. And again, showing, especially now that Twitter is at 256 characters, showing 256 character on a display that could only show 16. [LAUGH] It's definitely a power play but the point is not necessarily that you need yet another way to see a real time stream of twitter in your life.

[00:01:14] It's mostly about pulling in an API and kind of like again we send stuff out into the world and now, I think it would be cool to like pool stuff into the world. And this was the real time API that I chose. So this example in the spark fun, inventors got two where the GPS coordinates of where the International Space Station is currently over.

[00:01:37] That's a little bit better for our screen but they took this one, so I'm gonna use the twitter API, cool. All right, so the other thing I'm gonna pull in is Twitter, I'll call it the require, and the library is the node-tweet-stream. It's a bunch of life fun things you can do with it.

[00:01:58] For me it's like anytime I've done some stuff with real time APIs. I did a talk a few years ago on real time APIs in Ember. It's for a long time been my go-to for real time API? A real time API in Ember made a lot more sense for this, showing the tweets in an Ember app.

[00:02:14] Not a 16 character thing but it is my go-to real time API, so we're gonna go with it. Cool, and then we just wanna instantiate a new instance of that. So we'll say const t = new Twitter.
>> Steve Kinney: And we'll give it that environment variables that I set up before that I am.

[00:02:33]
>> Steve Kinney: And has a lower case t, cuz that's in the json object. So basically you can have an on tweet, an on air and do something when that stuff happens. And then you basically say what you wanna do when that happens. So I'll wait for my board to be set up.

[00:02:50] And I'll say, t.on("tweet").
>> Steve Kinney: And when that tweet comes in-
>> Steve Kinney: I will, let's go get the, we'll get the text of it.
>> Steve Kinney: And I'll get the-
>> Steve Kinney: FirstSixteen.
>> Steve Kinney: And we'll get the const secondSixteen.
>> Steve Kinney: And what we will do is that I will grab one of these we'll print the first few rows.

[00:03:56]
>> Steve Kinney: FirstSixteen, then the secondSixteen.
>> Steve Kinney: Cool, and so-
>> Steve Kinney: FirstSixteen,
>> Steve Kinney: SecondSixteen. And then finally when that's all ready, we'll track a hashtag.
>> Steve Kinney: Hashtag.
>> Steve Kinney: Let's try "#nodebots" this probably won't work esoteric enough that someone's gonna have to tweet, but we can handle that. When I was testing this out on the second I used Pizza, bad choice.

[00:04:40] There's a lot of discussion of pizza happening right now at this very moment. So that's a bit much. If nodebots seems too quiet and we're like worried that our code doesn't work we'll try pizza. But I'm pretty sure that's just gonna be, we're just gonna see letters flying.

[00:04:54] Especially we'll see the first 16 characters, or our first 32 characters have a lot of tweets on Pizza. So do with #nodebots and then somebody's job is once this is running, is to tweet.
>> Steve Kinney: Cool, so I'll do a t2 run playground
>> Steve Kinney: And we'll take that for a spin, see if I made any mistakes along the way.

[00:05:25]
>> Steve Kinney: Oops, I did already. So I put that t.track inside of the on tweet, which will have another order of operations issue. So I'm just gonna move that down.
>> Steve Kinney: Glad I caught that one quickly. All right, cool, so let's switch over.
>> Steve Kinney: We do need to wait a second or two for the code to deploy.

[00:05:51] And then I will either see a row of boxes or I will see some tweets on nodebots. Once this starts up, if you tweet now, it'll be too soon. You've got to wait until it starts up. It is lit up, but not fully there yet. Cool, initialized, we're good to go.

[00:06:10]
>> Steve Kinney: Hi, from my nodebots. Looks I had it off by one error on that string slicing so, it is, hi from my nodebots, but I missed that b. So it should start from the 16th one and move forward. Somebody else wants to tweet, it will realtime push to this device.

[00:06:27]
>> Steve Kinney: Awesome.
>> Speaker 2: That's fast.
>> Steve Kinney: Yeah. [LAUGH]
>> Steve Kinney: You can use this library for anything you want. You can also just run that code in a node script and you can do Pizza and see what happens then, anything you want. Yeah, so as tweets coming in, they push to a device.