Talks
Internet of Things: Connecting Rails with the Real World
Summarized using AI

Internet of Things: Connecting Rails with the Real World

by Karim Butt

In this presentation titled "Internet of Things: Connecting Rails with the Real World" at RailsConf 2015, Karim Butt explores the integration of the Internet of Things (IoT) with Rails development. He shares insights from his personal journey into coding and IoT, discussing both practical applications and the broader implications of connecting everyday objects to the internet.

Key points include:
- Introduction to IoT: Karim describes the IoT as connecting physical objects through code, leading to enhanced interactivity, increased data collection, and the proliferation of connected devices.
- Personal IoT Application: He showcases his own Rails-based IoT application that controls various home devices, such as lights, a coffee maker, and security systems using a Raspberry Pi and Arduino setups.
- Technical Stack: The application features an Angular frontend served through Divshot (similar to Heroku) that consumes an API from a Rails app running on Raspberry Pi. Ngrok is used to connect the local server to the web.
- Smart vs. Dumb Devices: The application not only connects to traditional smart objects like smart bulbs and speakers but also integrates dumb objects through Arduino, illustrating cost-effective device integration.
- Data Collection and Analysis: Karim emphasizes the importance of data generated through IoT devices, mentioning potential applications ranging from health monitoring to infrastructure maintenance.
- Performance Considerations: He discusses challenges faced during development, such as performance issues with HTTP APIs, and solutions like utilizing WebSockets for real-time interactions.
- Future of IoT: Reflecting on the infancy of IoT, Karim anticipates a future with centralized systems that seamlessly integrate a variety of smart devices, enhancing user experiences.

In conclusion, Karim encourages Rails developers to engage in the IoT revolution, highlighting that building IoT applications is accessible and practical, thanks to frameworks like Action Cable. The goal is to advance towards a future where technology intuitively enhances day-to-day life, reminiscent of scenes from the Jetsons. This presentation not only underscores the potential of IoT but inspires developers to leverage their existing skills in the burgeoning field of connected technology.

00:00:11.920 Thanks for coming out, everyone, and checking out my presentation on the Internet of Things (IoT). It's a topic I'm very fascinated by, even though it's not something I work with on a day-to-day basis. However, I've been tinkering with IoT and reading a lot about it, so I wanted to share that with you today.
00:00:30.640 Today, I'm going to walk you through a simple Rails-based IoT application that I've built. I'll discuss some of the background and context around IoT, as well as the future of it and where I see it going.
00:00:49.680 As a quick introduction, I’ve had quite a roller-coaster career. I started off as a political science major, then moved into business, trying management consulting and venture capital. Eventually, I taught myself to code, loved it, and attended the Flatiron School for formal education. Now, I work at a small legal tech startup called Caseflex. I may not have as much experience as many of you here, but I love what I do.
00:01:21.759 On a day-to-day basis, I use Rails and Angular. As an IoT enthusiast, I've gotten really into tinkering a lot with Raspberry Pis and Arduinos. Before I delve into the presentation, let me give you a quick disclaimer: I brought all my setup here with me, but I think the demo might be better if you can see it on video. So I'll show you the recorded video of the app.
00:02:03.600 The video shows both the actual web app on the left and my IoT setup at home on the right. In the morning, I wake up and turn my lights on, but I’m really cranky at that time, so I need to dim my lights a bit. As the day progresses, it's beneficial to use a redder type of light for your circadian rhythm, so I start changing the colors of my lights. My next step is to get some caffeine, so I go to the appliances tab and turn on my coffee maker.
00:02:48.720 As you can see in the video, I begin brewing my coffee using an Arduino setup. Once I have enough, I can turn off the coffee maker and then turn on my favorite music playlist. I often play ‘Happy’ to try and boost my mood in the morning, even though it typically doesn't work.
00:03:17.760 Before I leave my apartment, I set up my security motion detector. You’ll see a little graph on the right that lights up every time it detects motion. It tracks the count of motions over time. I can also set it to alert me either silently or with a loud alarm. Additionally, I connect it with my Sonos speaker, allowing them to interact with each other.
00:04:10.239 While I’m coding during the day, I generally listen to beats. At the end of the week, say Friday or Saturday, I like to unwind and throw a little party. That’s a quick overview of the app I’ve built, and I’ll go into more detail about how it works shortly.
00:04:40.479 The high-level stack of this IoT application consists of an Angular application running in the cloud, served using Divshot. If you haven’t used Divshot before, it’s like Heroku, but for static applications, making it easy to push your application up. The Angular application consumes the API from my Rails app, which runs locally on a Raspberry Pi.
00:05:23.760 The reason it's running locally on the Raspberry Pi is because I need it to connect to various smart objects in my home network, like Sonos speakers and Lifx light bulbs, for which I need access to their APIs. Currently, I'm serving the application through ngrok, which is a simple tunneling tool to expose the development environment to the web.
00:05:56.559 My Rails app connects both smart objects—those already connected to the internet like smart bulbs or speakers—and some dumb objects—regular devices I’ve connected via Arduino. The smart objects may have a significantly higher price tag compared to traditional devices—like a dollar for a regular light bulb versus a hundred dollars for a connected one. My application can connect to the Lifx light bulb and the Sonos, as well as control a $5 motion detector using Arduino.
00:07:01.599 To control my coffee maker, which is a 10-cup standard machine, I can turn it on and off from the app. The motion detector I built is a simple five-dollar device. I’ve managed to splice circuits with Arduino to send signals to turn devices on and off.
00:07:40.799 The beauty of this setup is that since the front end resides in the cloud, I can control my devices from anywhere, whether I’m at the office or elsewhere, ensuring things are turned off. I’m even starting to connect a slow cooker, allowing me to turn it on and have my food ready by the time I get home.
00:08:38.880 So, what exactly is the Internet of Things? It’s about connecting to physical objects through code. We are currently witnessing the rise of IoT applications ranging from health and fitness monitors, home security devices, connected cars, to household appliances.
00:09:03.440 There are also numerous non-consumer applications. For instance, local governments utilize sensors on their infrastructures to detect leaks before damage occurs. The importance of this technology can be distilled into three main reasons. First, IoT allows for enhanced interaction with the world around us. Take the Nest thermostat, for example: rather than adjusting a wall unit, we can use an app that learns our routines and optimizes temperature settings.
00:09:57.681 Second, IoT is already making a significant impact. There’s been explosive growth in connected devices, and research suggests that by 2020, there could be 40 to 80 billion connected objects, equating to ten devices for every person on the planet. Recently, I saw a report stating that the IoT healthcare market will be worth around $117 billion by 2020.
00:10:47.679 The most crucial reason IoT is vital is that it increases the amount of available data. While we typically think of big data as information generated online, IoT enables data collection directly from physical interactions, including light bulbs, thermostats, and wearables. This collection of data allows us to analyze and improve our daily lives, encapsulated in the idea of the quantified self.
00:11:49.120 For example, there’s a startup in the Netherlands that has developed sensors for cows, allowing farmers to track various health metrics and behaviors. A cow can generate 200 megabytes of data yearly. This highlights the potential scale of data collection from human activities and interactions.
00:12:09.960 When I started building my IoT application, I wasn't sure what topics to present. I began with a standard HTTP API but quickly ran into performance issues. For example, each time I wanted to turn on a light or change its brightness, my Rails app needed to connect to it, causing excessive requests. If a user moved the brightness slider rapidly, it could create hundreds of requests, significantly slowing down response times.
00:12:57.120 To address these performance issues, I explored WebSockets, which DHH discussed in his recent talk about Action Cable. WebSockets maintain an open connection between the client and server for more efficient interactions. One key advantage is that they allow server-generated events, enabling real-time updates on the front end without being tied to a request-response cycle.
00:13:52.320 In the Rails app I built, requests interface with physical objects through socket controllers. These controllers work with models to save interactions to the database, allowing analysis of motions, light states, and more. Given we are at RailsConf, it’s interesting that I use a dispatcher created on the frontend with Action Cable to manage events through the Rails application.
00:14:44.320 Each event maps to the controllers just like standard HTTP routing. For instance, creating an event could involve turning an object on, while destroying it would turn it off. Updating an object would change its attributes and retrieving its status would display its current settings.
00:15:06.639 In the update action of my application, I extract RGB values from the front end, save them to the database, and interface with the physical objects. The models involved include Sonos players, motion detectors, coffee makers, and lights with varying attributes.
00:15:48.400 The interface classes in the application serve as the intermediary between the Rails app and physical objects. This is relatively straightforward for smart objects with existing APIs. However, dumblected installation separates the process, using Arduino libraries and circuit design.
00:16:45.120 For example, my motion detector utilizes two-way communication whereby the Rails app receives data. This uses multi-threading to avoid freezing my app, continuously checking the Arduino for input. When a motion is detected, it signifies that all codes synchronize the state through server-generated events.
00:17:38.480 Reflecting forward, IoT remains in its infancy, resembling the transition of computers masquerading as better versions of paper, as articulated by Alan Kay. Currently, the IoT industry often operates by offering smart versions of commonplace objects, such as connected light switches, yet the value is not yet substantial.
00:18:30.080 We observe numerous dumb devices existing parallel to their smart counterparts, often requiring separate apps to run. The next phase involves centralized systems capable of integrating multiple brands and products into single functioning platforms. We are already seeing nascent versions of these centralized controls manifest in products like Apple’s HomeKit and Amazon Echo.
00:19:12.080 Ultimately, what we desire in IoT technology is seamless integration among varied smart objects, eliminating brand barriers. Imagine returning home late from work, where your home anticipates your return—heating food, turning on lights, unlocking doors, and ensuring a smooth experience.
00:20:05.279 My goal today was to demonstrate that building an IoT app is easier than you'd think. The app I developed didn’t take much time to create—it’s quite simple. As Rails developers, we hold valuable skills to create such applications, especially with the introduction of Action Cable making IoT development even more streamlined.
00:20:49.919 I encourage all of you to join this IoT revolution, building your applications to push us towards a future reminiscent of the Jetsons instead of the Flintstones. Thank you so much for listening.
Explore all talks recorded at RailsConf 2015
+117