Skip to main content

Memfault Chunk Relay

Not all devices have a direct connection to the internet. In such cases, a "gateway" device is used to forward data from the downstream device to Memfault.

Chunk Relay Considerations

When implementing a chunk relay, keep the following in mind:

  • Reliability: Ensure that the chunk relay is reliable and can handle intermittent network connectivity.
  • Downstream Device Serial: The downstream device serial number should be known by the chunk relay. It's used when uploading chunks to Memfault *.

*Note: this is optional- if the downstream device's Memfault Firmware SDK is configured with #define MEMFAULT_EVENT_INCLUDE_DEVICE_SERIAL 1, a UUID can be used in place of the device serial when uploading chunks

Example Chunk Relay

The following is an example of a chunk relay function in Python.

import requests

def relay_chunk(chunk_data, device_serial):
url = f"https://chunks.memfault.com/api/v0/chunks/{device_serial}"
headers = {
"Memfault-Project-Key": "YOUR_PROJECT_KEY",
"Content-Type": "application/octet-stream",
}
response = requests.post(url, headers=headers, data=chunk_data)
response.raise_for_status()

Here's example usage including sample test payloads:

import logging
import os

import requests

# set iso timestamp and file:line number
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s"
)
logger = logging.getLogger(__name__)


def relay_chunk(chunk_data, device_serial):
url = f"https://chunks.memfault.com/api/v0/chunks/{device_serial}"
headers = {
# Memfault Project Key is required for uploading chunks. It's set in an
# environment variable for this example.
"Memfault-Project-Key": os.getenv("MEMFAULT_PROJECT_KEY"),
"Content-Type": "application/octet-stream",
}
response = requests.post(url, headers=headers, data=chunk_data)

logger.info(f"Relay chunk response: {response.status_code} {response.text}")

response.raise_for_status()


# Example chunk data
relay_chunk(
bytearray.fromhex(
"485402a702010301076a5445535453455249414c0a6d746573742d736f667477617265096a312e302e302d746573"
),
"TESTSERIAL",
)
relay_chunk(
bytearray.fromhex(
"802c74066d746573742d686172647761726504a101a1726368756e6b5f746573745f737563636573730131e4"
),
"TESTSERIAL",
)