Ruby

Summarized using AI

Fun with Ruby and Arduino

Anmol Agarwal • January 10, 2015 • Earth

Summary of "Fun with Ruby and Arduino"

In this video, Anmol Agarwal presents a talk on the potential of utilizing Ruby in conjunction with Arduino within the Internet of Things (IoT) space. Anmol, a self-taught coder and UX/UI designer, shares his experiences and insights learned over the years about integrating Ruby into hardware projects, particularly when most existing projects are dominated by languages like C++, Python, and JavaScript.

Key Points Discussed:

  • Introduction to IoT: Anmol begins by discussing the IoT concept, which involves connecting everyday devices with on and off switches to the internet. He highlights its capabilities, saying, "You can make those things talk to you—they can understand you."
  • Ruby’s Role in IoT: Despite the popularity of other programming languages in hardware projects, Anmol emphasizes that Ruby offers significant possibilities. He points out three gems—Serial Port, R2, and Dino—that facilitate working with Arduino in Ruby.
  • Demo Projects: The presentation features various live demonstrations, each illustrating practical applications of Ruby with Arduino. Key projects include:
    • LED Control: The simplest project, showing how to blink an LED, is referred to as the "Hello World" of hardware programming.
    • Button Control: This project connects a physical button to control an LED's state, demonstrating basic input/output operations.
    • Rails Integration: Anmol shows how to control an LED through a Ruby on Rails application.
    • Real-time Control with PubNub: He demonstrates controlling an LED from a button in a different location, utilizing the PubNub service for real-time communication.
    • Sensor Implementations: He explains using sensors to gather data and control outputs based on environmental changes, showcasing projects like a package delivery notifier, automatic street lights, and an earthquake warning system.
  • Practical Applications: Anmol shares real-world implications of his projects, emphasizing the potential for innovations in daily life through IoT, driven by Ruby.
  • Resources for Learning: He concludes by recommending resources such as books, web services, and databases that are useful for IoT development, along with announcing a blog series called "Ruby Bits" focusing on Ruby and modular hardware.

Main Takeaways:

  • There is a significant opportunity for Ruby in the IoT arena, despite the existing dominance of other languages.
  • Anmol’s practical demonstrations highlight the ease of learning and integrating Ruby with Arduino through concise coding practices.
  • The video inspires viewers to explore the capabilities of Ruby in hardware projects, encouraging creativity and innovation in IoT development.

Overall, "Fun with Ruby and Arduino" serves as a motivational session for anyone interested in developing IoT projects without having to learn a different programming language.

Fun with Ruby and Arduino
Anmol Agarwal • January 10, 2015 • Earth

By, Anmol Agarwal

“Internet of things” is the concept of basically connecting any device with an on and off switch to the Internet. IoT has been possible through devices like Arduino, Raspberry Pi and many more. Although most of the APIs to work with them are available in Ruby, I haven’t seen many projects programmed in Ruby, but C++, Python or JS. I would like to share my approach, resources etc I learned from and show things that are possible. I hope audience would learn new possibilities with Ruby and not have to spent time learning a new language to interact with hardware.

Help us caption & translate this video!

http://amara.org/v/GF2l/

Garden City Ruby 2015

00:00:16.160 Good afternoon, everyone! I hope you are enjoying the conference.
00:00:21.760 First of all, just a quick show of hands: how many people here have worked with Arduino or Raspberry Pi?
00:00:28.080 Some hands are raised. What languages did you use?
00:00:36.640 Speak up! I heard JavaScript, Ruby—okay, great. There’s someone from C++. Anything else?
00:00:44.160 Python? Python? Okay, so some of you have worked with Ruby. Nice!
00:00:51.520 So because most of the people here might not be aware of Ruby, this is a perfect opportunity to introduce you to the Internet of Things and how it can be enabled with Ruby.
00:01:04.879 My name is Anmol Agarwal, and I am a UX/UI designer. I started coding about a year and a half ago with Ruby on Rails.
00:01:11.280 I have been working on side projects, and over time, I have gained some experience. I am autodidactic, which means everything I know is self-taught. In my free time, I sketch and play basketball.
00:01:30.159 First, I'd like to show you a video that got me hooked on the Internet of Things. After watching that video, I decided that I needed to learn how this was even happening. It's just a two-minute video.
00:02:06.840 Okay.
00:03:39.760 Hi! I'm Jay, and I'm Eric. We're graduate students at MIT Media Lab. We have a dream that everyone is an inventor.
00:03:46.159 So we created Makey Makey, to let you invent just by alligator clipping stuff, like bananas, to your Makey Makey. When you touch the banana, your computer thinks you are touching the keyboard.
00:04:03.040 On the front, there are arrow keys, a spacebar, and mouse left-click. When you're ready for more, flip the Makey Makey over, and you get more keyboard keys and support for the mouse. You can even use the board like an Arduino.
00:04:21.199 With no programming required—no breadboard—just plug it in via USB. Order your Makey Makey today and start changing how the world works!
00:04:32.000 Never thought I would see a day when I could play bananas as a piano.
00:04:37.120 So this is the video that my friend showed me, and I was immediately hooked, thinking, 'I need to learn this stuff. How is this even happening?'
00:04:44.240 This concept is called the Internet of Things, also known as IoT. You might be wondering, 'What is IoT?' There are numerous explanations, and everyone might explain it differently.
00:05:00.479 I'll give you the simplest definition: IoT is basically connecting any device with an on and off switch to the internet or to each other.
00:05:04.479 So, what does this mean? Usually, smartphones and laptops communicate with servers and store our data into databases. But with the Internet of Things, it’s not just other smartphones, tablets, or desktops.
00:05:11.759 You can use everyday things that you see around you every day, like lamps, cameras, bicycles, washing machines, coffee makers, and much more. Basically, you can make those things talk to you—they can understand you.
00:05:22.479 For example, suppose you're coming home from work; your home understands that you have just arrived. It can then turn on the fan, the lights for you, and maybe even play some relaxing music in the background.
00:05:39.040 So, non-living things or everyday materials can communicate with you. That is the Internet of Things. I hope that by the end of this talk, you will feel excited about IoT and the many projects and libraries possible in Ruby.
00:05:58.080 Now, what about Ruby? How does Ruby fit into the landscape of IoT? When I started learning about IoT, I found out that it is currently dominated by three languages: C, C++, Python, and JavaScript.
00:06:12.639 Surprisingly, there were not many projects in Ruby. I found this quite surprising because Ruby supports modular programming, allowing us to reuse code and utilize gems all the time. Although I know JavaScript, I love Ruby too much to code IoT in JavaScript.
00:06:30.639 So, I wanted to focus on Ruby. I began to research to see if someone had contributed to this field. I searched for Ruby and Arduino on YouTube, and I came across three people: Austin Vance, Ron Evans, and Jim Verick.
00:06:48.319 I’ll tell you what those individuals have accomplished. Currently, there are three Ruby gems available for working with Arduino: Serial Port, R2, and Dino.
00:07:05.039 For those of you who haven’t worked with Internet of Things before, if you have a microcontroller, like an Arduino board, you define what components are connected to which pins and set their values as either digital high or digital low.
00:07:26.319 The same concepts of digital high, digital low, read, and write work in Ruby, similar to C and C++. That’s what the Serial Port gem is about.
00:07:40.319 Next, we have R2, a very extensive Ruby library that I highly recommend you check out. R2 is Ruby on Robots, and as you can see, we believe it is time to bring Ruby and robots together.
00:08:00.160 It is extensive because it supports platforms not just for Arduino but also Raspberry Pi, BeagleBone, joysticks, and even drones. There’s so much you can build with it.
00:08:17.919 The last gem I want to discuss is Dino, which I started learning about. This gem was created by Austin Vance and has been discussed by Ron Evans and Jim Verick.
00:08:32.640 Dino is a Ruby gem that helps you bootstrap prototyping with Arduino. It’s a very simple gem, and I will be using it to show my demos in the coming slides because I find it easier to read and understand.
00:08:49.360 Our first demo will focus on LEDs. I love working with LEDs for a few reasons: they are simple light bulbs with two legs – one for the signal and one for the ground.
00:09:05.920 They come in a variety of colors, and as a sketch artist, I really love colors. Finally, you can create many effects with just that simple component. You can turn it on, turn it off, make it blink faster or slower, or even blink in a specific pattern like Morse code.
00:09:22.160 Just with a single component, you can achieve so many things.
00:09:30.880 I want to talk a bit about LittleBits, pun intended. LittleBits is a company that makes electronics that look like Legos.
00:09:45.839 These components snap together, having magnets at the ends that make it impossible to connect them wrong, which speeds up the prototyping process.
00:10:05.839 You simply snap them together and write the code. They work even without code; they just light up. They have modules for almost everything: inputs, outputs, sensors, and Arduino boards. I think you should really check it out.
00:10:43.120 I want to thank LittleBits for donating the kit that made this talk possible. Let's see how this makes rapid prototyping even more fun in the upcoming demos.
00:10:50.880 Enough talk; let’s demo! Whenever we start learning any programming language, we usually begin with a 'Hello World.' So, what is the 'Hello World' of hardware programming? It is called the LED blink, which is, as the name states, just blinking the LED.
00:11:01.920 Let's take a look at the code for LED blink. First, we define whatever gems are required. Here we are using the Dino gem. I'm not going to get into the details of how to install it, but it's a very simple three-step process.
00:11:15.639 You could go through the documentation: install the gem, then use a command line terminal to generate a sketch that you upload to Arduino. The purpose of that file is to help your Arduino understand the Ruby file that you will execute.
00:11:39.479 Now, I'm connecting the Arduino. If you briefly look at the code here, it’s very simple. First of all, we define our board from the gem itself. Then, I'm defining an LED, which is a new component located on pin one of the board.
00:11:58.639 Here is a block where we are cycling between on and off values, and at any given time, that value is sent to the LED for half a second.
00:12:05.680 If I try to run this...
00:12:20.360 Okay! I think I won't be able to show the photo booth because the cable is a little short, but I could show the video directly.
00:12:30.760 Here’s the code and how LittleBits work. I’m connecting it live for you; you can see how it sticks together because of the magnets at the end. Now I am running the same Ruby file.
00:12:50.720 And there it is blinking! We got the Hello World working in Ruby on Arduino.
00:13:04.120 You can see how small the code was—under 10 lines!
00:13:10.960 Next, we'll look at a project where we control the LED with a button. The code is almost the same as the LED blink, but we add a button. Initially, we turn the LED off.
00:13:18.120 The methods for each component are built into the gem, so it’s very straightforward. We call a 'down' method on the button, which doesn’t mean pressing the button down; rather it means that the voltage is down.
00:13:25.480 That’s one gotcha to keep in mind! When the voltage is low, the button indicates it’s not pressed, and the LED remains off. If the button is pressed, the voltage goes up, and the LED turns on.
00:13:39.440 Let's take a look at the video for that.
00:13:50.440 This is almost the same code. I’m connecting it live again, and here the pink color represents the button while the green color shows the LED. All the components are color-coded.
00:14:03.040 So, as I press the button, the LED turns on and off. If we check the terminal, we can see it’s running through the code. This could run without code, but in this instance, we're running it through programming.
00:14:14.320 Next, let's take it to another level and control the LED through Rails in a local server.
00:14:21.360 You can see what I’m building on from our last program. Let’s look at the code.
00:14:32.160 Here’s the Rails app that I generated. It doesn't have any models since I am not storing any values in the databases or anything; it just consists of a controller and views.
00:14:45.360 The most important file in this program is the LED controller. It has three actions only: index, on, and off. When the LED is turned on, we redirect to the root path, and the same applies when it's turned off.
00:14:59.640 Before executing these actions, we define the LED, doing so in the line right before the actions to ensure it is ready.
00:15:15.840 I'll show you the video of this in action.
00:15:26.480 Here I am running the Rails app. I'm starting the server, and it’s ready.
00:15:42.880 You see the on and off buttons displayed. I'm just clicking them to observe what happens on the screen.
00:15:58.960 As you can see, the LED is turning on and off through Rails on a local server.
00:16:11.520 It’s impressive because this is very close to what we will actually build in real life.
00:16:20.960 Now let’s take it to another level by controlling the LED through a physical button using PubNub. PubNub is a real-time service that uses the WebSockets protocol and the publish-subscribe model.
00:16:42.480 With PubNub, you can send and receive messages in real time and anywhere in the world. PubNub claims messages can travel anywhere in the world within 0.25 seconds, and it is true.
00:16:56.280 Though I could’ve done it with Rails, it would have looked the same as before. This involves a physical button, making it more interesting.
00:17:07.360 Let’s check out the code before we dive into the video.
00:17:19.360 The code is a bit lengthy because of the integration with PubNub. Similar to our last program, we define the board, button, and LED. Here, we initialize PubNub.
00:17:31.840 We create a new instance of PubNub with a subscribe key and a publish key. The publish key is responsible for sending messages to the channel, while the subscribe key retrieves them.
00:17:44.000 We have a subscriber block in this instance; it connects to the button channel. Based on the data we receive, we determine the button's status and publish the corresponding message.
00:18:00.960 If the button is pressed, it sends a message to indicate the button status is pressed, and when it's not pressed, it sends that status as well.
00:18:14.960 Let's run the video to see this in action.
00:18:28.040 Here I’m going through the code again, making sure everything is connected. This example illustrates that you have one button here in Bangalore, and the corresponding LED is connected to an Arduino halfway around the world.
00:18:51.840 By subscribing to the same channel, you can turn it on from here, and it will activate in real time—within 0.25 seconds.
00:19:04.240 I’m confirming that both devices are on the same channel and the same page.
00:19:12.560 Now, I’m running the program. It takes a moment because it's creating a handshake between the program and PubNub.
00:19:21.920 Once it’s connected, it shows me a confirmation.
00:19:31.840 We got connected, and now I can turn the LED on. The response time is very fast, even though it is utilizing the servers around the world.
00:19:42.320 Here’s the terminal showing that the messages are being published. It demonstrates how pressing the button sends a message indicating whether it’s pressed or not.
00:19:54.560 We have gone from LED blink to controlling it with a button, then creating a Rails server, and finally enabling real-time control!
00:20:07.040 Now, the world is not just full of LEDs; we need to interact with our environment somehow. This is where sensors come into play.
00:20:24.160 If we look into the code involving sensors, it’s straightforward: we define the board and a sensor, which has just one method called 'data_received.'
00:20:39.120 The purpose of all sensors is to gather data specific to their functionality—whether it’s measuring humidity, temperature, motion, or anything else.
00:20:56.960 We invoke this method each time we receive data, intending to display it on the terminal.
00:21:07.920 I'm using a pressure sensor to illustrate this; it will measure the pressure exerted on it.
00:21:23.040 As I run the program, you can see the values displayed in real-time. At first, it’s zero, indicating no pressure.
00:21:37.760 Now, I’m applying pressure on the sensor, and you can notice the changes on the terminal—a demonstration of the sensor's sensitivity.
00:21:56.000 Next, let’s connect the input from the sensor to the output LED. This is really simple, as we will replace the button control and use sensors instead.
00:22:05.920 Now the LED is controlled by the sensor instead of the button.
00:22:27.040 Additionally, I aimed to control the LED via voice, but unfortunately, Siri is not open source. Integrating Google Now with Ruby proved nearly impossible.
00:22:33.360 However, I discovered an open-source library called Wit.ai, which supports natural language processing, making my task much easier since it is compatible with Ruby.
00:22:44.720 Let’s run through the code: I'm using Wit.ai’s API, initializing it with an access token.
00:22:58.560 I begin a loop where I use a method called 'voice_query_auto' to activate the microphone and start recording.
00:23:02.640 The recorded data is sent to the API, and I receive the corresponding response.
00:23:14.760 Based on that response, I run the logic necessary to control the LED.
00:23:30.080 Just a quick overview of what NLP involves: it comprises intents and expressions. For example, if you want to turn on the LED, the intent is 'turn on the lights.'
00:23:44.480 However, there are various ways to express this: you could say 'lights on,' 'turn on the lights,' or 'do not turn off the lights.'
00:24:00.640 These variations capture different ways of saying the same thing, which is what we aim to match.
00:24:10.960 Now let’s check a video to see this in action. 'Voice, lights off!'
00:24:34.080 Turning on the lights... and turning them off.
00:24:50.720 In real-time, you can see the updates in the command terminal as well.
00:25:12.960 That was fun! It felt a bit like magic.
00:25:25.760 Now that we have covered the fundamentals, let’s take a look at the practical applications.
00:25:45.920 One of the projects I worked on was a package delivery detector. Whenever mail or a parcel arrives in your mailbox, you will get an SMS or email notifying you that a package has arrived, and reminding you to pick it up.
00:26:02.480 I won’t dive into the code now, but I will show you the demo. The code for this project is available.
00:26:13.280 I’m using a pressure sensor to verify if the package has arrived. Right now, the sensor is empty.
00:26:31.920 Let’s run the program. I’ll place a parcel on the sensor now.
00:26:49.760 If I check the email, I receive the message stating that a package has arrived—'Don't forget to pick it up.'
00:27:03.840 Another practical application is street lighting; when it gets dark, the street lights turn on automatically.
00:27:16.560 This is also straightforward: I’m using a light sensor to detect ambient light.
00:27:24.640 Right now, lights are on, and the sensor values are being tracked in the terminal.
00:27:35.920 When I turn off the lights, the LED automatically turns on, demonstrating how the sensor responds to light levels.
00:27:42.560 That is pretty cool.
00:27:52.960 The final demo I will show is an earthquake warning system. As we know, earthquakes occur with very little warning.
00:28:06.960 For this project tailored to India, several hurdles presented themselves. I found that the Indian Meteorological Survey does not provide APIs, so I’ve had to create a crawling model that checks for updates every 10-20 seconds.
00:28:23.520 Earthquake notifications must be timely, as every second matters, especially when running a real-time alert system.
00:28:38.480 I’ve developed a complete solution that involves sensing, monitoring, controlling, and warning about earthquakes.
00:28:59.680 I won’t go into the code details now; just know that it will be available to you. Here's the dashboard I am using via freeboard.io.
00:29:09.760 Currently, it is not receiving any data. I will run the code to simulate an earthquake effect using the pressure sensor.
00:29:25.520 As you can see, I'm generating pressure to replicate the seismic experience. The terminal now reflects a high reading, indicating an earthquake alert.
00:29:42.400 If the earthquake register reads above six, the alert will trigger.
00:29:56.000 The terminal indicates earthquake detection; you can monitor this in real time via the PubNub developer console.
00:30:12.640 You can see live data coming in and check how the graphs are moving in response.
00:30:25.960 I wanted to demonstrate what’s possible with Ruby and Arduino.
00:30:39.040 In conclusion, I'd like to share a few resources. For books, I recommend 'Getting Started with Arduino' and Ruby, as there is probably only that one book available on the market specific to this topic.
00:30:52.560 You can read Make magazines for ideas and inspiration about what others are doing in the IoT industry.
00:31:04.800 For web services, you can use Temboo, WebShell, Mashape, and IFTTT, which may not directly support Ruby but are great for rapid prototyping.
00:31:16.720 For dashboards, I used Freeboard; you can also try InfluxDB or Keen.io, which looks good according to their examples.
00:31:29.600 For real-time services, you can use PubNub, Pusher, or Socket.IO.
00:31:41.680 For databases, NoSQL is usually the best choice for IoT. If you develop locally, you could use MongoDB or a graph database like Neo4j.
00:31:54.560 If you're concerned about cloud databases, Firebase or Parse are great options.
00:32:07.920 At the end, I'm excited to announce Ruby Bits, my own blog post series that I've recently started.
00:32:20.800 It’s about Ruby and LittleBits. Ruby's modular approach to programming aligns well with the modular approach to hardware offered by LittleBits.
00:32:34.560 You can find it at tinkerforlife.com, and all the source code from the demos I showed is available on GitHub, along with the slides.
00:32:49.040 I can't wait to see what you all come up with, and if you build something, feel free to share it with me, and I'll spread the word!
00:33:03.280 Thank you!
Explore all talks recorded at Garden City Ruby 2015
+8