Skip to content

Commit

Permalink
First script: reads weight from scale
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-c committed Apr 27, 2020
1 parent 7a1c821 commit bf5dd21
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
55 changes: 55 additions & 0 deletions mts-iot-apps/OpenMts.AutomaticCheckIn/AutomaticCheckIn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import time
import serial
import RPi.GPIO as GPIO
import sys
import configparser
sys.path.append('/home/pi/MFRC522-python')
from mfrc522 import SimpleMFRC522

def connect_to_scale(port):
ser = serial.Serial("/dev/ttyUSB0", timeout=1)
return ser

def convert_value(scale_response):
low = int(scale_response.find(' ')+1)
high = int(scale_response.find('k'))
quantity = float(scale_response[low:high])
return quantity

# Get config
config = configparser.ConfigParser()
config.read('./config.ini')
print(config.sections())
server = config["Server"]["Endpoint"]
apiKey = config["Server"]["ApiKey"]
scalePort = config["Scale"]["UsbPort"]

# Output config
print("Configuration:")
print(" + Server: " + server)
print(" + API-Key: " + apiKey)
print(" + Scale USB port: " + scalePort)

# Start reading
reader = SimpleMFRC522()
print("Pleace hold material-card next to the reader")
try:
while True:
id, matnr_card = reader.read()
ser = connect_to_scale(scalePort)

# Get weight
raw_decode = bytes('S','utf-8')
ser.write(raw_decode)
scale_response = ser.readline()
print(scale_response)
quantity = convert_value(str(scale_response))
weight_formated = '{0:0.2f}'.format(quantity)
print(" - Read weight: " + str(quantity) + )
time.sleep(1)
print("Pleace hold material-card next to the reader")
except KeyboardInterrupt:
GPIO.cleanup()
except Exception as exception:
print(exception)
GPIO.cleanup()
15 changes: 15 additions & 0 deletions mts-iot-apps/OpenMts.AutomaticCheckIn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# AutomaticCheckIn

Tested on a Raspberry Pi 4 Model B.

## Installation

### Python Packages

Install on Raspberry Pi:

- [MFRC522-python](https://github.com/pimylifeup/MFRC522-python)

Install on Raspberry Pi with pip3:

- `pyserial`
6 changes: 6 additions & 0 deletions mts-iot-apps/OpenMts.AutomaticCheckIn/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[Server]
Endpoint = "http://localhost:5000"
ApiKey = "todo"

[Scale]
UsbPort = "/dev/ttyUSB0"

0 comments on commit bf5dd21

Please sign in to comment.