The following is a guest post from Kurt Busch, CEO, and Mariano Goluboff, Principal Field Applications Engineer at Lantronix.
Background
Google Analytics makes it easy to create custom dashboards to present data in the format that most helps to drive business processes. We’ve put together a solution that will make several of our devices (networking and remote access devices) easily configurable to enable delivery of end device data to Google Analytics. We use the Lantronix PremierWave family of devices to connect to an end device via a serial port like RS-232/485, or Ethernet, intelligently extract useful data, and send it to Google Analytics for use in M2M applications.
What you need
To get started, grab the Pyserial module, and load it on your Lantronix PremierWave XC HSPA+. You’ll also want a device with a serial port that sends data you want to connect to Google Analytics. A digital scale like the 349KLX is a good choice.
Architecture overview
With the Measurement Protocol, part of Universal Analytics, it is now possible to connect data from more than web browsers to Analytics.
Lantronix integrated the Measurement Protocol by using an easy to deploy Python script. By being able to natively execute Python on PremierWave and xSenso devices, Lantronix makes it very easy to deploy intelligent applications leveraging Python’s ease of programming and extensive libraries.
The demonstration consists of a scale with an RS-232 output, connected to a Lantronix PremierWave XC HSPA+. The Python script running on the PremierWave XC HSPA+ parses the data from the scale, and sends the weight received to Google Analytics, where it can then be displayed.
The technical details
The Python program demonstrated by Lantronix uses the Pyserial module to parse this data. The serial port is easily initialized with Pyserial:
class ser349klx:
# setup the serial port. Pass the device as '/dev/ttyS1' or '/dev/ttyS2' for
# serial port 1 and 2 (respectively) in PremierWave EN or XC HSPA+
def __init__(self, device, weight, ga):
while True:
try:
serstat = True
ser = serial.Serial(device,2400, interCharTimeout=0.2, timeout=1)
except Exception:
serstat = False
if serstat:
break
self.ser = ser
self.weight = weight
self.ga = ga
The scale used constantly sends the current weight via the RS-232 port, with each value separated by a carriage return:
def receive_line(self):
buffer = ''
while True:
buffer = buffer + self.ser.read(self.ser.inWaiting())
if '\r' in buffer:
lines = buffer.split('\r')
return lines[-2]
The code that finds a new weight is called from a loop, which then waits for 10 equal non-zero values to wait for the weight to settle before sending it to Google Analytics, as shown below:
# This runs a continuous loop listening for lines coming from the
# serial port and processing them.
def getData(self):
count = 0
prev = 0.0
#print self.ser.interCharTimeout
while True:
time.sleep(0.1)
try:
val = self.receive_line()
weight.value=float(val[-5:])*0.166
if (prev == weight.value):
count += 1
if (count == 10) and (str(prev) != '0.0'):
self.ga.send("{:.2f}".format(prev))
else:
count = 0
prev = weight.value
except Exception:
pass
Since the Google Analytics Measurement Protocol uses standard HTTP requests to send data from devices other than web browsers, the ga.send method is easily implemented using the Python urllib and urllib2 modules, as seen below:
class gaConnect:
def __init__(self, tracking, mac):
self.tracking = tracking
self.mac = mac
def send(self, data):
values = { 'v' : '1',
'tid' : self.tracking,
'cid' : self.mac,
't' : 'event',
'ec' : 'scale',
'ea' : 'weight',
'el' : data }
res = urllib2.urlopen(urllib2.Request("http://www.google-analytics.com/collect", urllib.urlencode(values)))
The last piece is to initialize get a Google Analytics connect object to connect to the user’s Analytics account:
ga = gaConnect("UA-XXXX-Y", dev.mac)
The MAC address of the PremierWave device is used to send unique information from each device.
Results
With these pieces put together, it’s quick and easy to get data from the device to Google Analytics, and then use the extensive custom reporting and modeling that is available to view the data. For example, see the screenshot below of real-time events:
Using Lantronix hardware, you can connect your serial devices or analog sensors to the network via Ethernet, Wi-Fi, or Cellular. Using Python and the Google Analytics Measurement Protocol, the data can be quickly and easily added to your custom Google Analytics reports and dashboards for use in business intelligence and reporting.
Posted by Aditi Rajaram, the Google Analytics team
No comments:
Post a Comment