Low on Gas

Summary

This tutorial will show you that you can use signals from your car to trigger other events in an android application.

What You're Building

This is an application that will automatically send you a text when you turn off your car if your fuel level is below a certain percentage.

Before Starting

In order for your application to make use of the data from your car, you need to download and install the OpenXC enabler app on your phone. This application is what controls the connection between the source (the simulator or you car) and your android device. You can find the OpenXC application here. YOUR APPLICATION WILL NOT WORK WITHOUT IT If you want to test your application but don't have a car, that isn't a problem. OpenXC has a vehicle simulator program that you can run from you computer. Follow this link to try it for yourself.

Set up the components

The interface for this application is very simple since most of the application's functionality happens behind the scene. The picture below shows what the interface will ultimately look like. Layout
Component Type Palette Group What You'll Name It Purpose of Component Component Settings
Label User Interface FuelLevelLabel This label's text will update whenver the car's fuel level changes FontSize:20
Vertical Arrangement Layout VerticalArrangement1 Used to center the label AlignHorizontal:Center, AlignVertical:Center, Height:400px
OpenXC Sensors OpenXC1 This component gives you access to the car's data
Texting Social Texting1 This is what allows the application to send the automatic text message

Add behavior to the components

First, we're going to build the the block that causes the FuelLevelLabel to update whenever the car's fuel level changes.

Blocks 1

At this point, the label should give real time feedback about the car's fuel level. If you ave your android device hooked up to the vehicle simulator or to a car, then you should be able to see the text change when the car's fuel level changes.

Now, we're going to build the block that automatically sends the text message. When the car's ignition status changes, the application will check to make sure that the car is off and that the fuel percentage is below the number you specify. In the picture below, the number is set to .2 but you're free to set the value to whatever you'd like as long it's it's between 0 and 1.

Blocks 2

Here's a screenshot of the completed application. Now, if the application is running, your phone will automatically text you when you turn the car off if your gas is too low.

Blocks Final

Optional Changes

Based on what you've learned here, you can modify the application to let someone know that you made it to your destination safely. Try adding a block that sends a text message to someone else when your car turns off.

Download Source Code

If you'd like to work with this sample in App Inventor, download the source code ? (make this a hyperlink) to your computer, then open App Inventor, go to the My Projects page, and choose More Actions | Upload Source.

Done with Low on Gas? Try the other OpenXC tutorial, Don't Text and Drive, or return to to the other App Inventor 2 tutorials.