Professional Documents
Culture Documents
CHICAGO
DETECTION AND ESTIMATION
T H EO RY
G O W T H A M S I VA K U M A R
UIN:674422979
CONTENTS
Tab 1
Report Notebook
Introduction.1
The Setup ....2
API details.3
Setting up a server to handle triggers.12
Using the Flow Designer...15
Estimating Parameters..........18
Future Work..25
INTRODUCTION
M2X Data Service is a cloud-based, time series data storage solution for
network connected Machine-to-Machine (M2M) devices, applications, and services.
M2X makes it easier for developers to gather real-time data from various M2M
sources and translate it into meaningful information, making operational detections
based on the data, and share the data for estimating parameters.
Flow Designer is a cloud based, multi-tenant development and execution
service for network connected IoT devices. Flow Designer provides support for
common IoT protocols. It helps developers create custom business logic and integrate
it with external services such as analytics, visualization and other services.
The intent of this project is to gather insights on using the M2X Data Service
and Flow Designer APIs and integrate it onto IoT applications.
My execution is targeted toward developing a flow, curl and a web-based REST
client and use any programming language or REST client. In addition, I have used the
TI MSP432 LaunchPad with the CC3100MOD BoosterPack and the Flow Designer,
to connect to a network and make HTTP requests.
In order to compile the framework, I used:
THE SETUP
In order to call the M2X Data Service APIs, an M2X Beta account has to be
created. Once that is done, we can create new Devices and begin to push data into
the service.
An M2X Device defines the attributes associated with an IP enabled device,
application, or service that will be sending data to the M2X service. Once the
Device has been tested, it can be used with multiple devices of the same type,
which is accomplished through creating a Distribution and launching it.
In this project, I first set up an M2X Device and then sent data to it. Finally,
I made a request to obtain all of the data that the Device has collected.
To make HTTP requests, I used graphical client such as the Postman addon for Chrome, and the curl command line tool.
Conventions used
The following formatting conventions are used to differentiate requests:
API DETAILS
Sending Data
To send one piece of data from a Device, make an HTTP PUT request to this URL:
http://api-m2x.att.com/v2/devices/{device-ID}/streams/speed/value
where {device-ID} is the ID of the Device you created. The request contains the
following headers:
Retrieving Data
To retrieve data that was sent, make an HTTP GET request to this URL:
http://api-m2x.att.com/v2/devices/{device-ID}/streams/speed/values
where {device-ID} is the ID of the feed you created when creating the Device. The
request should contain the following header:
Creating a Device
The M2X Data Service can receive, store, and react to data from a data source. In
order to prepare the service for the data source, the first step is to set up a Device,
which is a way to prototype the Device before launch.
Go to https://m2x.att.com/devices.
You will be asked to set up your first Device as either a physical device or a
virtual device. We will work on a Virtual Device.
The device will have a device ID and an API key associated with it:
Now we can add a data stream for the device. That is, the parameter it is estimating.
Type in presstime for the Stream ID. For the display name, type Seconds
Pressed. Then click the Aa button on the Units & Symbols line. Click the letter
S, and then choose Second.
/*
lab2.ino
AT&T DevLab pushbutton demo
*/
#include <aJSON.h>
#include "SPI.h"
#include "WiFi.h"
#include "M2XStreamClient.h"
char ssid[] = "MiCrOn"; // your network SSID (name)
char pass[] = "Welcome!"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;
// Connect to
WiFi.begin(ssid, pass);
while ( WiFi.status() != WL_CONNECTED) {
// print dots while we wait to connect
Serial.print(".");
delay(300);
}
Serial.println("\nYou're connected to the network");
Serial.println("Waiting for an ip address");
while (WiFi.localIP() == INADDR_NONE) {
// print dots while we wait for an ip addresss
Serial.print(".");
delay(300);
}
Serial.println("\nIP Address obtained");
// you're connected now, so print out the status
printWifiStatus();
}
void loop() {
// Read the pushbutton state. 1 for not pushed, 0 for pushed.
btn1 = digitalRead(PUSH1);
if (btn1 == 0 && !pushed) {
// If pushed, then store the time
pushed = true;
whenPushed = millis();
} else if (btn1 == 1 && pushed) {
10
// Reset pushed
pushed = false;
// Released. Calculate the time elapsed
double seconds = (millis() - whenPushed) / 1000.0;
Serial.print("Seconds pushed: ");
Serial.println(seconds);
// Send to M2X
int response = m2xClient.updateStreamValue(deviceId, streamName, seconds);
Serial.print("M2X client response code: ");
Serial.println(response);
// If the response is an error, then send into infinite loop to stop loop
if (response == -1)
while (1)
;
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
/*This will set the device up*/
11
From the File menu, select Upload. (Or click the Upload button. ) This
will upload the code to the device and run it. This will take a couple of minutes.
When it is done, you will see text at the bottom of the window that says Done
uploading and Success.
From the Tools menu, select Serial Monitor to check if the board is
connected to the network.
12
Position the board so that the writing is right-side-up and the micro-USB cord
is away from you. Youll see two pushbuttons on the side closest to you. Quickly
press the one on the left.
Return to the M2X website. The first point should have appeared on your
graph, indicating how long the button press lasted. Try it again, holding the
button down longer and watch as the data appears. It takes 5 to 10 seconds for
the new data to appear in the graph.
13
This will create a URL that we can use when creating a trigger. Save the
page so to copy and paste the URL.
14
Setting up a trigger
Now that we have a place to receive the trigger POST, lets set up the trigger.
Use the following steps to create a trigger for the condition.
In your browser, open your Device page in another tab or browser and
click the Trigger tab. Then click Add Trigger.
For the Trigger Name, type open door. For the Stream, choose
Seconds Pressed. For the Condition, choose > is greater than. For the
threshold value, type 5. For the Callback URL, copy and paste the URL from
the RequestBin page. Click Save.
15
Using a trigger
Press the button for more than 5 seconds. Check your RequestBin website
and refresh it by clicking on the circle next to your URL, in the top right corner of
the page.
You will see the data that was posted to the URL, which will take the
following form. As you can see, it contains the device ID, the stream name, the
trigger name, the condition and threshold, as well as the value that crossed the
threshold and its timestamp. If this were a real server that it was posting data to,
you could parse the incoming data and take action.
{"trigger":"open door","timestamp":"2016-0215T21:05:31.454Z","event":"fired","device":{"id":"c656bd781d367d3
517e4f 0aa2d64b8c8","name":"MPS432
Launchpad","serial":null},"conditions":{"presstime":{"gt":5.0}},"
values ":{"presstime":{"value":6.15,"timestamp":"2016-0215T21:05:31.454Z","unit":"s or
"}},"timeframe":0,"custom_data":null}
16
17
To respond to the button trigger in M2X, the flow will look like this
18
Copy the HTTPS URL and paste it onto the Callback URL in the M2X device.
The device has been setup to detect the parameters and trigger warnings if
there are any outliers
19
Using the extracted CSV file, we can use Graphlab by DATO to estimate and visualize
the data. Coding in python, this is the algorithm I used:
import graphlab
sales = graphlab.SFrame('presstime.gl/')
import numpy as np # note this allows us to refer to numpy as np instead
def get_numpy_data(data_sframe, features, output):
data_sframe['constant'] = 1 # this is how you add a constant column to an SFrame
# add the column 'constant' to the front of the features list so that we can extract it along
with the others:
20
features_sframe = data_sframe[features]
# the following line will convert the features_SFrame into a numpy matrix:
feature_matrix = features_sframe.to_numpy()
# assign the column of data_sframe associated with the output to the SArray output_sarray
output_sarray = data_sframe['times pressed']
# the following will convert the SArray into a numpy array by first converting it to a list
output_array = output_sarray.to_numpy()
return(feature_matrix, output_array)
def predict_output(feature_matrix, weights):
# assume feature_matrix is a numpy matrix containing the features as columns and weights is
a corresponding numpy array
# create the predictions vector by using np.dot()
predictions = np.dot(feature_matrix, weights)
return(predictions)
def feature_derivative_ridge(errors, feature, weight, l2_penalty, feature_is_constant):
# If feature_is_constant is True, derivative is twice the dot product of errors and feature
if (feature_is_constant == True):
derivative = 2 * np.dot(errors, feature)
# Otherwise, derivative is twice the dot product plus 2*l2_penalty*weight
21
else:
derivative = 2 * (np.dot(errors, feature) + l2_penalty * weight)
return derivative
(example_features, example_output) = get_numpy_data(seconds, ['trigger'], 'times pressed')
my_weights = np.array([1., 10.])
test_predictions = predict_output(example_features, my_weights)
errors = test_predictions - example_output # prediction errors
22
iterations = 0
#while not reached maximum number of iterations:
while (iterations < max_iterations):
# compute the predictions based on feature_matrix and weights using your
predict_output() function
predictions = predict_output(feature_matrix, weights)
# compute the errors as predictions - output
errors = predictions - output
for i in xrange(len(weights)): # loop over each weight
# Recall that feature_matrix[:,i] is the feature column associated with weights[i]
# compute the derivative for weight[i].
#(Remember: when i=0, you are computing the derivative of the constant!)
if i == 0:
derivative_weight = feature_derivative_ridge(errors, feature_matrix[:,i], weights[i],
l2_penalty, True)
else:
derivative_weight = feature_derivative_ridge(errors, feature_matrix[:,i], weights[i],
l2_penalty, False)
# subtract the step size times the derivative from the current weight
weights[i] = weights[i] - (step_size * derivative_weight)
iterations += 1
23
return weights
simple_features = ['trigger']
my_output = 'time_pressed'
train_data,test_data = sales.random_split(.8,seed=0)
(simple_feature_matrix, output) = get_numpy_data(train_data, simple_features, my_output)
(simple_test_feature_matrix, test_output) = get_numpy_data(test_data, simple_features, my_output)
initial_weights = np.array([0., 0.])
step_size = 1e-12
max_iterations=1000
simple_weights_0_penalty = ridge_regression_gradient_descent(simple_feature_matrix, output,
initial_weights, step_size, 0, max_iterations)
print simple_weights_0_penalty
24
predictions_initial =
predict_output(simple_test_feature_matrix, initial_weights)
25
multiple_weights_0_penalty =
ridge_regression_gradient_descent(feature_matrix, output, initial_weights, step_size, 0.0,
max_iterations)
print multiple_weights_0_penalty
multiple_weights_high_penalty = ridge_regression_gradient_descent(feature_matrix, output,
initial_weights, step_size, 1e11, max_iterations)
print multiple_weights_high_penalty
predictions_multiple_weights_high_penalty = predict_output(test_feature_matrix,
multiple_weights_high_penalty)
26
predictions_multiple_weights_high_penalty[0]
#End code
Here, I have plotted a graph to predict outputs of presstime based on the current
dataset with different stepsizes and the RSS associated with it is estimated and the
times pressed vs the featers graph is visualized:
27
28
FUTURE WORK
This project is an MVP for my passion to integrate IoT, Analytics and Behavioral
sciences. By using the principles of lean methodologies, my next focus is on creating
an SaaS (software as a service) delivery model with mobile capabilities(as an app) that
is a culmination layer that combines lifestyle tools, health tools with work tools to
create a work-life balance for employees. In short, a solution that provides real time
insights by regressing data obtained from various IoT devices on dimensions having a
strong correlation with a holistic lifestyle.