|
| 1 | +"""note-python binary upload example. |
| 2 | +
|
| 3 | +This example uploads binary data to a Notehub proxy route using the |
| 4 | +high-speed chunked upload mechanism. The data is staged through the |
| 5 | +Notecard's binary buffer and sent to Notehub via web.post. |
| 6 | +
|
| 7 | +Before running this example: |
| 8 | +1. Create a Proxy Route in your Notehub project (e.g. pointing to |
| 9 | + https://httpbin.org/post or your own endpoint). |
| 10 | +2. Set PRODUCT_UID below to your Notehub product UID. |
| 11 | +3. Set ROUTE_ALIAS to the alias of your proxy route. |
| 12 | +
|
| 13 | +Supports MicroPython, CircuitPython, and Raspberry Pi (Linux). |
| 14 | +""" |
| 15 | +import sys |
| 16 | + |
| 17 | + |
| 18 | +PRODUCT_UID = 'com.your-company.your-project' |
| 19 | +ROUTE_ALIAS = 'my-upload-route' |
| 20 | +USE_UART = True |
| 21 | + |
| 22 | + |
| 23 | +def on_progress(info): |
| 24 | + """Print upload progress after each chunk.""" |
| 25 | + print(f' Chunk {info["chunk"]}/{info["total_chunks"]} ' |
| 26 | + f'- {info["percent_complete"]:.1f}% ' |
| 27 | + f'- {info["avg_bytes_per_sec"]:.0f} B/s ' |
| 28 | + f'- ETA {info["eta_secs"]:.1f}s') |
| 29 | + |
| 30 | + |
| 31 | +def run_example(): |
| 32 | + """Connect to Notecard and upload binary data to Notehub.""" |
| 33 | + biggest_notecard_response = 400 |
| 34 | + |
| 35 | + if sys.implementation.name == 'micropython': |
| 36 | + from machine import UART |
| 37 | + from machine import I2C |
| 38 | + from machine import Pin |
| 39 | + import board |
| 40 | + |
| 41 | + if USE_UART: |
| 42 | + port = UART(board.UART, 9600) |
| 43 | + port.init(9600, bits=8, parity=None, stop=1, |
| 44 | + timeout=3000, timeout_char=100, |
| 45 | + rxbuf=biggest_notecard_response) |
| 46 | + else: |
| 47 | + port = I2C(board.I2C_ID, scl=Pin(board.SCL), sda=Pin(board.SDA)) |
| 48 | + elif sys.implementation.name == 'circuitpython': |
| 49 | + import busio |
| 50 | + import board |
| 51 | + |
| 52 | + if USE_UART: |
| 53 | + port = busio.UART(board.TX, board.RX, baudrate=9600, |
| 54 | + receiver_buffer_size=biggest_notecard_response) |
| 55 | + else: |
| 56 | + port = busio.I2C(board.SCL, board.SDA) |
| 57 | + else: |
| 58 | + import os |
| 59 | + |
| 60 | + sys.path.insert(0, os.path.abspath( |
| 61 | + os.path.join(os.path.dirname(__file__), '..', '..'))) |
| 62 | + |
| 63 | + from periphery import I2C |
| 64 | + import serial |
| 65 | + |
| 66 | + if USE_UART: |
| 67 | + port = serial.Serial('/dev/ttyACM0', 9600) |
| 68 | + else: |
| 69 | + port = I2C('/dev/i2c-1') |
| 70 | + |
| 71 | + import notecard |
| 72 | + from notecard import hub |
| 73 | + from notecard.upload import upload |
| 74 | + |
| 75 | + if USE_UART: |
| 76 | + card = notecard.OpenSerial(port, debug=True) |
| 77 | + else: |
| 78 | + card = notecard.OpenI2C(port, 0, 0, debug=True) |
| 79 | + |
| 80 | + # Connect the Notecard to Notehub. |
| 81 | + hub.set(card, product=PRODUCT_UID, mode='continuous') |
| 82 | + |
| 83 | + # Generate some test data (1 KB of a repeating pattern). |
| 84 | + data = bytearray(range(256)) * 4 |
| 85 | + print(f'Uploading {len(data)} bytes to route "{ROUTE_ALIAS}"...') |
| 86 | + |
| 87 | + result = upload( |
| 88 | + card, |
| 89 | + data, |
| 90 | + route=ROUTE_ALIAS, |
| 91 | + label='test_data.bin', |
| 92 | + progress_cb=on_progress, |
| 93 | + ) |
| 94 | + |
| 95 | + print(f'Upload complete: {result["bytes_uploaded"]} bytes ' |
| 96 | + f'in {result["chunks"]} chunks, ' |
| 97 | + f'{result["duration_secs"]:.1f}s ' |
| 98 | + f'({result["bytes_per_sec"]:.0f} B/s)') |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + run_example() |
0 commit comments