Commit 32b3ab9f authored by Peter Vennemann's avatar Peter Vennemann
Browse files

Initial commit.

parent 339676d7
This diff is collapsed.
This diff is collapsed.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''Data acquision from a laboratory balance (Kern 440)
Receive data via serial cable (COM/USB adapter) and send it over the IoT.
Hints:
- check if user is in group »dialout« by executing:
groups username
- start automatically by calling this script in /etc/rc.local
(make sure, python libraries are accessible by root)
- receive data anywhere in the network by executing:
mosquitto_sub -h host-ip -t topic
'''
__version__ = '2021.09.16'
from serial import Serial
from paho.mqtt import publish
from datetime import datetime
# address of data broker
SERVER = '10.42.10.86'
# MAC address of current device
MAC = 'e4:5f:01:20:e4:72'
# MQTT topic
TOPIC = 'sensor/' + MAC
def read_serial(port):
raw_data = port.readline()
data = raw_data[:-5].strip()
try:
m = float(data)
except:
m = 0.0
return(m)
data = {'id':MAC, 'time':None, 'mass_a_gram':None, 'mass_b_gram':None}
try:
kernA = Serial(port='/dev/ttyUSB0',
baudrate=9600)
kernB = Serial(port='/dev/ttyUSB1',
baudrate=9600)
except Exception as e:
print('Problem while connecting balance:')
print(e)
else:
while True:
data['time'] = datetime.now().isoformat()
data['mass_a_gram'] = read_serial(kernA)
data['mass_b_gram'] = read_serial(kernB)
publish.single(TOPIC, str(data), hostname=SERVER)
finally:
kernA.close()
print('Serial port A closed.')
kernB.close()
print('Serial port B closed.')
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment