Last active
July 5, 2023 12:33
-
-
Save morsine/57832b95718fecfc2a3a5e94fd4bf375 to your computer and use it in GitHub Desktop.
Buskool weight API Server
This file contains hidden or 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
# hey there! this is an easy and smiple API server to provide the weight of a buskool. | |
# You'll need an ESP micro controller running JEELABS's esp-link connected to the buskool monitor's RS232 with RS232 <-> TTL converter | |
# سرور ای پی آی جهت استفاده با نمایشگر و تبدیل آر اس 232 به شبکه | |
# Coded by Morsine (https://github.com/morsine) | |
from flask import Flask, jsonify, request | |
import socket | |
from threading import Thread | |
import re | |
s = socket .socket( | |
socket.AF_INET, socket.SOCK_STREAM) | |
s.connect(("192.168.1.144", 23)) #replace with your own IP and port | |
def getweight(): | |
global bweight | |
data = str(s.recv(1024),encoding='utf-8') | |
val1 = (data.replace(" ", "")[:-1]) | |
data = str(s.recv(1024),encoding='utf-8') | |
val2 = (data.replace(" ", "")[:-1]) | |
data = str(s.recv(1024),encoding='utf-8') | |
val3 = (data.replace(" ", "")[:-1]) | |
if val1 == val2 == val3: #error correction | |
bweight = val1.replace('\r', '') | |
class weightcheck(Thread): | |
def __init__(self): | |
Thread.__init__(self) | |
self.daemon = True | |
self.start() | |
def run(self): | |
while True: | |
getweight() | |
weightcheck() | |
# initialize our Flask application | |
app= Flask(__name__) | |
@app.route("/name", methods=["POST"]) | |
def setName(): | |
if request.method=='POST': | |
posted_data = request.get_json() | |
data = posted_data['data'] | |
return jsonify(str("Successfully stored " + str(data))) | |
@app.route("/weight", methods=["GET"]) | |
def weight(): | |
getweight() | |
return jsonify(bweight) | |
# main thread of execution to start the server | |
if __name__=='__main__': | |
app.run(debug=True, host="0.0.0.0") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment