|
| 1 | + |
| 2 | + |
| 3 | +import time |
| 4 | +import requests |
| 5 | + |
| 6 | +class BlynkClient(): |
| 7 | + """ |
| 8 | + Ref: |
| 9 | + https://docs.blynk.io/en/blynk.cloud/device-https-api/upload-set-of-data-with-timestamps-api |
| 10 | +
|
| 11 | + """ |
| 12 | + |
| 13 | + def __init__(self, |
| 14 | + token, |
| 15 | + hostname='blynk.cloud', |
| 16 | + protocol='https', |
| 17 | + ): |
| 18 | + |
| 19 | + self._telemetry_url = protocol + '://' + hostname + '/external/api/batch/update?token=' + token |
| 20 | + |
| 21 | + |
| 22 | + def post_telemetry(self, values : list[dict[str, float]]): |
| 23 | + """ |
| 24 | + Send multiple telemetry values. |
| 25 | + The 'time' key should be in Unix milliseconds. |
| 26 | + """ |
| 27 | + stream_values = {} |
| 28 | + |
| 29 | + # Blynk HTTP API currently cannot send multiple timestamped values on multiple streams |
| 30 | + # so we shuffle the data into being organized per-stream |
| 31 | + for datapoint in values: |
| 32 | + if 'time' in datapoint.keys(): |
| 33 | + t = datapoint['time'] |
| 34 | + for key, value in datapoint.items(): |
| 35 | + if key == 'time': |
| 36 | + continue |
| 37 | + if key not in stream_values.keys(): |
| 38 | + stream_values[key] = [] |
| 39 | + stream_values[key].append((t, value)) |
| 40 | + else: |
| 41 | + print('WARNING: ignored datapoint without time') |
| 42 | + |
| 43 | + # NOTE: if no timestamps are provided (and there are multiple values) |
| 44 | + # then regular batch update would be better, since it could be done in 1 request |
| 45 | + # https://docs.blynk.io/en/blynk.cloud/device-https-api/update-multiple-datastreams-api |
| 46 | + |
| 47 | + for key, datapoints in stream_values.items(): |
| 48 | + self.post_timestamped_values(key, datapoints) |
| 49 | + |
| 50 | + def post_timestamped_values(self, pin : str, values : list[tuple[int, float]]): |
| 51 | + """ |
| 52 | + Post multiple values from different times, for 1 stream |
| 53 | + Each entry in values must be a tuple with (timestamp, value) |
| 54 | + """ |
| 55 | + |
| 56 | + payload = values |
| 57 | + url = self._telemetry_url+f'&pin={pin}' |
| 58 | + r = requests.post(url, json=payload) |
| 59 | + assert r.status_code == 200, (r.status_code, r.content) |
| 60 | + |
| 61 | +def unix_time_seconds(): |
| 62 | + timestamp = time.time() |
| 63 | + epoch_year = time.gmtime(0)[0] |
| 64 | + |
| 65 | + if epoch_year == 2020: |
| 66 | + # seconds between 2000 (MicroPython epoch) and 1970 (Unix epoch) |
| 67 | + epoch_difference = 946684800 |
| 68 | + timestamp = timestamp + epoch_difference |
| 69 | + elif epoch_year == 1970: |
| 70 | + pass |
| 71 | + else: |
| 72 | + raise ValueError('Unknown epoch year') |
| 73 | + |
| 74 | + return float(timestamp) |
| 75 | + |
| 76 | +def main(): |
| 77 | + # bc3ab311-4e92-11ef-b45a-8f71ad378839 |
| 78 | + BLYNK_AUTH_TOKEN = 'Cxvp01Mvo2-A8er9mGRWLfHnPcTNvaTP' |
| 79 | + api = BlynkClient(token=BLYNK_AUTH_TOKEN) |
| 80 | + |
| 81 | + t = int(unix_time_seconds() * 1000) |
| 82 | + |
| 83 | + values = [] |
| 84 | + for s in range(0, 60, 10): |
| 85 | + v = {'time': t-(s*1000), 'V1': 78.0+s} |
| 86 | + values.append(v) |
| 87 | + |
| 88 | + api.post_telemetry(values) |
| 89 | + print(values) |
| 90 | + print('Posted telemetry') |
| 91 | + |
| 92 | +if __name__ == '__main__': |
| 93 | + main() |
| 94 | + |
| 95 | + |
0 commit comments