Open Menu

Edge AI

Percepxion™ Edge AI Solution prototype’s seamless integration with the Qualcomm AI Hub, this integrated solution delivers a robust platform for optimized model performance, supporting the rapid development, deployment and acceleration of edge AI solutions.

Software

Centralized Management Software

Services

Out-of-Band Management

AI-Driven Automation and Console Access for Enterprise Networks

Software

Lantronix Centralized Management Software

Services

Software

Centralized Management Software

Services

Services

Software

Resources

From training , to white papers, videos, and more, you’ll find what you need to design, develop, deploy and manage powerful, innovative remote networking and IT infrastructure management applications and solutions.

Support

Visit the Technical Resource Center for all of your support needs

Lantronix
June 5, 2014

Sending data from Lantronix to Google Analytics

Google and Lantronix

The following is a copy of a guest post from Kurt Busch, CEO, and Mariano Goluboff, Principal Field Applications Engineer at Lantronix as originally posted on Google’s corporate blog at: https://analytics.blogspot.com/2014/03/sending-data-from-lantronix-to-google.html

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 hardware setup is show in the picture below.

 M2M applications data for Google Analytics

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("https://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:

data from the device to Google Analytics

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.

Edge AI

Percepxion™ Edge AI Solution prototype’s seamless integration with the Qualcomm AI Hub, this integrated solution delivers a robust platform for optimized model performance, supporting the rapid development, deployment and acceleration of edge AI solutions.

Software

Centralized Management Software

Services

Out-of-Band Management

AI-Driven Automation and Console Access for Enterprise Networks

Software

Lantronix Centralized Management Software

Services

Resources

From training , to white papers, videos, and more, you’ll find what you need to design, develop, deploy and manage powerful, innovative remote networking and IT infrastructure management applications and solutions.

Support

Visit the Technical Resource Center for all of your support needs

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close