From 1dd221103b426b6eda753dfc8b903576d11acf1a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Mar 2026 22:45:46 +0000 Subject: [PATCH 1/2] Initial plan From 70c2f2393889b10e7ad7497bd828f85b6ab7fabe Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 9 Mar 2026 22:51:53 +0000 Subject: [PATCH 2/2] Improve documentation: clarify document vs payload structure in insert.py and README.md Co-authored-by: sgbaird <45469701+sgbaird@users.noreply.github.com> --- README.md | 34 ++++++++++++++++++++++++---------- insert.py | 34 ++++++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 381a909..0d06a06 100644 --- a/README.md +++ b/README.md @@ -108,20 +108,34 @@ https://github.com/ACC-HelloWorld/5-data-logging-sgbaird/settings/secrets/codesp ## Insert -Update [`insert.py`](./insert.py) to iteratively run color experiments and insert the data into your MongoDB database. You will need to: +Update [`insert.py`](./insert.py) to iteratively run color experiments and upload the data to your MongoDB database via the AWS Lambda function. You will need to: -1. Connect to MongoDB using the provided URI -2. Run each experiment in `payload_dicts` using the `run_color_experiment()` function -3. Insert the results into the database +1. Import `urequests` and `json` (MicroPython equivalents of `requests` and `json`) +2. Loop through each entry in the `documents` list +3. Run each experiment using the `run_color_experiment()` function with the R, G, B values from the entry's `"command"` dictionary +4. Build a **document** dictionary containing the command, sensor data, experiment ID, and your course ID +5. Wrap the document in a **payload** dictionary that tells the Lambda function which database and collection to use (see the `test_lambda_function_url` function in [`mongodb_credentials_test.py`](./mongodb_credentials_test.py) for reference) +6. POST the payload to the Lambda function URL -The document structure should be of the form: +> **Important: document vs. payload** +> The *document* is the data you want to store in MongoDB (command, sensor data, experiment ID, course ID). The *payload* wraps the document with metadata (database name, collection name) so the Lambda function knows where to insert it. + +The **document** (what gets stored in MongoDB) should be of the form: +```python +{ + "command": {"R": ..., "G": ..., "B": ...}, + "sensor_data": {"ch410": ..., "ch440": ..., ..., "ch670": ...}, + "experiment_id": "...", + "course_id": "...", +} +``` + +The **payload** (what gets sent to the Lambda function) should be of the form: ```python { -"command": {"R": ..., "G": ..., "B": ...}, -"sensor_data": {"ch410": ..., "ch440": ..., ..., "ch670": ...}, -"experiment_id": "...", -"course_id": "...", -"timestamp": "..." + "database": DATABASE_NAME, + "collection": COLLECTION_NAME, + "document": , } ``` diff --git a/insert.py b/insert.py index 835b52c..83adb0d 100644 --- a/insert.py +++ b/insert.py @@ -10,7 +10,7 @@ LAMBDA_FUNCTION_URL, ) -# TODO: other imports here +# TODO: import urequests and json here ... # Connect to WiFi @@ -55,13 +55,27 @@ def run_color_experiment(R, G, B): ] # fmt: on -# TODO: for each of the commands above, run the corresponding dummy color -# experiment and upload a document containing your course ID (use `course_id` as -# the key), the original command, the experiment ID, and the sensor data to your -# MongoDB collection. The dictionary should be of the form: -# { -# "command": {"R": ..., "G": ..., "B": ...}, -# "sensor_data": {"ch410": ..., "ch440": ..., ..., "ch670": ...}, -# "experiment_id": "...", -# } +# TODO: for each document, do the following steps: +# 1. Run the color experiment using `run_color_experiment()` with the R, G, B +# values from the document's "command" dictionary. +# 2. Build a document dictionary containing the following keys: +# - "command": the original command (e.g., {"R": ..., "G": ..., "B": ...}) +# - "sensor_data": the result from `run_color_experiment()` +# - "experiment_id": the experiment ID from the original document +# - "course_id": use the `COURSE_ID` variable from `my_secrets` +# 3. Wrap the document in a payload dictionary for the Lambda function. The +# payload is NOT the same as the document—it includes metadata about where +# to store the document. See `test_lambda_function_url` in +# `mongodb_credentials_test.py` for reference. The payload should be: +# { +# "database": DATABASE_NAME, +# "collection": COLLECTION_NAME, +# "document": , +# } +# 4. POST the payload to the Lambda function URL using `urequests.post()`: +# response = urequests.post( +# LAMBDA_FUNCTION_URL, +# headers={"Content-Type": "application/json"}, +# data=json.dumps(payload), +# ) ...