Difference between revisions of "Weigh Scale MQTT"

From Lantronix Wiki!
Jump to navigation Jump to search
Line 65: Line 65:
 
     prev = 0.0
 
     prev = 0.0
  
#print self.ser.interCharTimeout
+
    #print self.ser.interCharTimeout
 
     while True:
 
     while True:
    time.sleep(0.1)
+
        time.sleep(0.1)
    try:
+
        try:
        val = self.receive_line()
+
            val = self.receive_line()
        weight.value=float(val[-5:])*0.166
+
            weight.value=float(val[-5:])*0.166
        if (prev == weight.value):
+
            if (prev == weight.value):
            count += 1
+
                count += 1
        if (count == 10) and (str(prev) != '0.0'):
+
                if (count == 10) and (str(prev) != '0.0'):
            self.ga.send("{:.2f}".format(prev))
+
                    self.ga.send("{:.2f}".format(prev))
            if supportMqtt:
+
                    if supportMqtt:
self.c2l.send("{:.2f}".format(prev))
+
                        self.c2l.send("{:.2f}".format(prev))
else:
+
            else:
count = 0
+
                count = 0
prev = weight.value
+
                prev = weight.value
except Exception:
+
        except Exception:
pass
+
            pass

Revision as of 23:54, 18 November 2014

Introduction

This demonstration sample shows how easy it is to add local processing to the IoT network edge and communicate with cloud based IoT and Analytics platforms. Here we see the Lantronix PremierWave family of Intelligent Gateways connect to an end device via a serial port like RS-232/485, or Ethernet, intelligently extract useful data, and send it to 2lemetry Cloud Platform and Google Analytics. Standard and scalable M2M/IoT transport protocols such as MQTT and HTTP are easy to integrate within a custom application while relying on the secure remote access and robust device management features of the rugged Cellular M2M Gateways such as Lantronix PremierWave XC HSPA+.

Requirements

  • Python module PySerial - built-in module supported in Python for Lantronix products
  • Tornado webserver package
  • Digital scale with RS232 serial port (Homedics 349KLX)
    Any other serial device could be used and its protocol implemented in Python instead
  • Internet Connection via cellular or Ethernet

Setup

Weigh scale demo.png


The demonstration setup consists of the digital scale with RS232 connected to one of the RS232 ports on PremierWave XC HSPA+. The Python script performs the following actions:

  • Values from digital scale are read over the RS232 port
  • Send SMS to user’s cell phone with the weight read from scale
  • Digital scale readings are posted to 2lemetry cloud via MQTT protocol
  • Digital scale readings are posted to Google Analytics via HTTP
  • Local access to the web server via the browser provides scale read-out

Technical Details

Initialize the Serial Ports via PySerial

The program uses the PySerial module that is built-in the Python support on Lantronix devices. For more details on the module refer to the ]]//Lantronix_Python_Programmers_Guide | Programmer's Guide]].

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, c21, 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.c21 = c21
    self.ga = ga

Read Scale data

The scale constantly sends the current weight via the RS232 port, with each value separate 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]

Processing the new weight readings and taking actions

# 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))
                    if supportMqtt:
                        self.c2l.send("{:.2f}".format(prev))
            else:
                count = 0
                prev = weight.value
        except Exception:
            pass