#!/usr/bin/env python3 # ------------------------------------ UPSCALE UDP ------------------------------------- # --------------- UP-SCALES.COM UDP BROADCAST LISTENER FOR THE WEIGHT (PYTHON) ---------- # -------------------------------------------------------------------------------------- # THE LIVE WEIGHT OF EVERY UP↑SCALE SCALE IN YOUR WIFI NETWORK THAT SENDS IT AS A UDP # BROADCAST - NO CONNECTION, NO ADDRESS NEEDED: LISTEN ON THE PORT AND READ. # PYTHON STANDARD LIBRARY ONLY, NOTHING TO INSTALL. # -------------------------------------------------------------------------------------- # ^^^ A WORKING EXAMPLE - COPY IT AND CHANGE IT AS NEEDED! # -------------------------------------------------------------------------------------- # WEIGHT STREAMS FOR MACHINES: https://up-scales.com/manual.html#WiFiStreams # ALL EXAMPLES FOR YOUR IT: https://up-scales.com/hardware/integration-lims-webhook-api-examples.html#examples # -------------------------------------------------------------------------------------- # (C) Johann Anderson, 2025-2026 http://farm23.es # -------------------------------------------------------------------------------------- # MIT License - Copyright (c) 2025-2026 Johann Anderson # Permission is hereby granted, free of charge, to any person obtaining a copy of this # software and associated documentation files (the "Software"), to deal in the Software # without restriction, including without limitation the rights to use, copy, modify, # merge, publish, distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to the following # conditions: The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, # INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR # PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE # FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. # -------------------------------------------------------------------------------------- # # This script will: # Listen for the broadcast of the scales on UDP port 20420 (or yours). # Read every datagram in the three formats of the scale (the 10-byte weight packet # of the Decent Scale protocol, the line of WIRELESS for all, a JSON line). # Print one line per weight with the address of the scale - several scales at once. # # -------------------------------------------------------------------------------------- # 1st: What you need: # -------------------------------------------------------------------------------------- # # 1.) The scale in your WiFi network, and in the app: LINK -> 🔗 Webhook & JSON API -> 🔌 JSON API & # weight streams -> UDP broadcast ON (FREE; port, format and how often there). # Plain on purpose (like WIRELESS for all): anyone in your network can read it. # 2.) This computer in the same network segment (a broadcast does not pass a router) and # the port open in its firewall (Linux: sudo ufw allow 20420/udp). # 3.) Python 3.6 or newer: # # python3 upscale-udp.py (port 20420) # python3 upscale-udp.py --port 20421 --seconds 10 # # The JSON format also carries the Device ID ("d") and the time of the scale ("t", # 0 = no time yet): with several scales, ask for the JSON line in the app. # -------------------------------------------------------------------------------------- import argparse, json, socket, struct, time def decode(data): """One datagram -> (weight, unit, stable, device ID or '') or None.""" if len(data) == 10 and data[0] == 0x03: # Decent Scale weight packet: [0] 03 [1] CE stable / CA moving [2-3] int16 big endian 0.1 g ... [9] XOR of [0-8] x = 0 for b in data[:9]: x ^= b if x != data[9]: return None return struct.unpack('>h', data[2:4])[0] / 10.0, 'g', data[1] == 0xCE, '' s = data.decode('utf-8', 'replace').strip() if s.startswith('{'): try: j = json.loads(s) return float(j['w']), j.get('u', 'g'), bool(j.get('s')), j.get('d', '') except (ValueError, KeyError, TypeError): return None # WIRELESS for all: "ST" stable / "US" moving, ",", sign + 10 characters, unit (3, right-aligned) if len(s) >= 15 and s[2] == ',': try: return float(s[3:14]), s[14:].strip(), s[:2] == 'ST', '' except ValueError: return None return None def main(): ap = argparse.ArgumentParser(description='UDP weight broadcast of UP-SCALE scales') ap.add_argument('--port', type=int, default=20420) ap.add_argument('--seconds', type=float, default=0, help='stop after this (0: until Ctrl+C)') a = ap.parse_args() s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(('', a.port)) s.settimeout(0.5) print('listening on UDP port %d (Ctrl+C ends)' % a.port) end = time.time() + a.seconds if a.seconds else 0 count = 0 try: while not end or time.time() < end: try: data, peer = s.recvfrom(512) except socket.timeout: continue w = decode(data) if w: count += 1 print('%s %-15s %10.2f %-3s %-6s %s' % (time.strftime('%H:%M:%S'), peer[0], w[0], w[1], 'stable' if w[2] else 'moving', w[3])) else: print('%s %-15s ? %r' % (time.strftime('%H:%M:%S'), peer[0], data[:40])) except KeyboardInterrupt: pass s.close() print('%d weights' % count) if __name__ == '__main__': main()