Send readings from a Python script
Send readings from your own Python code, such as a bench script or a sensor with no connector. They show as live channels.
Before you start
- Python 3 on the same computer as Phloem. No extra packages needed.
- The push address from Send your own data.
Steps
Open Inputs and press Add input.
Choose Push endpoint.
Set Codec to
batch.Press Add & start.
Save this script as
send_readings.py:import json, math, sys, time, urllib.request URL = sys.argv[1] # the push address, with batch as the codec for i in range(600): t_us = time.time_ns() // 1000 body = [ {"path": "/bench/temp_c", "samples": [{"t_us": t_us, "value": {"f64": 21.5 + math.sin(i / 10)}}]}, {"path": "/bench/step", "samples": [{"t_us": t_us, "value": {"i64": i}}]}, ] req = urllib.request.Request( URL, data=json.dumps(body).encode(), headers={"Content-Type": "application/json"}) with urllib.request.urlopen(req) as r: print(r.read().decode()) time.sleep(0.1)Run it with the push address,
<codec>replaced bybatch:python3 send_readings.py <push address>
Check it worked
Each POST prints {"codec":"batch","batches":2,"samples":2}. On Inputs,
the Push endpoint row counts samples. On Live, /bench/temp_c and
/bench/step appear in the channel list.
| The script prints | Do |
|---|---|
HTTP Error 403 | no Push endpoint input is open for batch — steps 1–4 |
Connection refused | Phloem is not running on this computer |
HTTP Error 400 | the body is not valid batch — compare with the script above |
Tip
Ran the script before adding the input? Inputs lists it under Sending to this device. Press Enable instead of steps 1–4.
Send your own dataEvery codec, body format and response codeName channels and unitsName a signal, set its unitVerify that your data is arrivingRead an input's row when nothing shows
Updated