-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First script: reads weight from scale
- Loading branch information
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |