Want to see/hear how far the closest Uber is from you? All you have to do is press a button on an AWS IoT connected device. I created this project to allow anyone to have access to Uber even without a smartphone and for those with different capabilities.
OverviewThis project sends a button press on an Arduino to AWS IoT over MQTT using the custom "uber" topic. Lambda is waiting for a message payload to match a request. When it matches, the request gets sent to the Uber API, data gets processed, and sent back to the Arduino. The Arduino then beeps the buzzer to represent the estimated time of arrival (ETA) for your Uber request.
PartsAll of the parts that are needed are in the Seeeduino Cloud and Grove Starter Kit for Arduino powered by AWS IoT. I wanted to use only things found in the kit so you can reproduce this project yourself.
- Grove Button
- Grove Buzzer
- Grove Shield
- Seeeduino Cloud or Arduino Yun
- Connect the Grove Shield to the Seeeduino Cloud (if connecting the shield the Arduino Yun make sure the shield does not touch the Ethernet jack housing)
- Connect cable from the Grove Buzzer to D2 on the shield
- Connect cable from the Grove Button to D3 on the shield
- Create Thing on AWS IoT
- Select Arduino Yun
- Download new certificate and SDK
- Place the AWS IoT library in the Arduino library folder
- Connect Arduino Yun with USB cable
- Start up Arduino IDE
- Select Arduino board and com port
- Edit AWS IoT configuration info to match AWS IoT settings
- Flash the project code to the Arduino
- Open serial monitor
We are going to user Uber's API to request service estimates. A core concept of the Uber API is "product_id". Uber offers many products such as uberX and UberBLACK and these products are not all available in your area. Using the Uber API endpoint for products you can figure out what services are available and their IDs.
GET https://api.uber.com/v1/products?latitude=42.300080&longitude=-71.350349
- Visit Uber API (https://developer.uber.com)
- Register app
- Save CLIENT ID, CLIENT SECRET, and SERVER TOKEN
- Note "product_id" for the Uber services in your area
- Note Bearer Token
- Request Ride Estimate info by sending a start location (lat/long)
- Create new function
- Handler: index.handler
- Role: Basic execution role
- Next
- Create function
- Note functionARN (Top Right Corner)
- Add it to AWS IoT "thing"
- Click on your Thing's name
- Click Create a rule
- Enter a Name and Description
- Attribute: *
- Topic filter: uber_request
- SELECT * FROM 'uber' WHERE command = 'estimate'
- Under Choose an action select "Insert this message into a code function and execute it (Lambda)"
- Under Function name select, "UberRequest"
- Click add action
- Click Create
Source Code for Lambda Function
var AWS = require('aws-sdk');
var https = require('https');
// Set options for AWS IoT
// - make sure credentials have AWSIoTDataAccess permissions policy
var CONFIG_AWSIOT = {
endpoint: 'XXX.iot.us-east-1.amazonaws.com',
region: 'us-east-1',
accessKeyId: 'XXX',
secretAccessKey: 'XXX'
};
// Set options for Uber API
var CONFIG_UBER = {
uber_token: "XXX",
product_id: "XXX",
start_latitude: 42.300080,
start_longitude: -71.350349
};
function callUber(event, context) {
var data = {
product_id: CONFIG_UBER.product_id,
start_latitude: CONFIG_UBER.start_latitude,
start_longitude: CONFIG_UBER.start_longitude
};
data = JSON.stringify(data);
var headers = {
'Authorization': 'Bearer ' + CONFIG_UBER.uber_token,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)
};
var options = {
host: 'api.uber.com',
path: '/v1/requests/estimate',
method: 'POST',
headers: headers
};
var req = https.request(options, function(res) {
res.on('data', function (chunk) {
uberData = JSON.parse(chunk);
iotdata = new AWS.IotData(CONFIG_AWSIOT);
params = {
topic: 'uber_response',
payload: uberData.pickup_estimate.toString(),
qos: 0
};
iotdata.publish(params, function(err, data) {
if (err) {
console.log(err, err.stack);
context.fail(event);
}
else {
console.log(data);
context.succeed(event);
}
});
console.log(chunk.toString());
});
});
req.write(data);
req.end();
req.on('error', function(e) {
context.fail(event);
});
}
exports.handler = callUber;
Take it FurtherI learned so much by creating this project. I also came up with several new ideas on how to take this project further. Maybe you can use this to create something awesome.
Here are a few of my next steps:
- Send Philips Hue colors to indicate how close Uber is
- Interface to other API such as ThingSpeak for data collection and analysis
- Built IoT gateway for AWS IoT services
- Build Lambda and SNS alerts
- Build Lambda and S3 integration
Comments