Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": <your document from above>,
}
```

Expand Down
34 changes: 24 additions & 10 deletions insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
LAMBDA_FUNCTION_URL,
)

# TODO: other imports here
# TODO: import urequests and json here
...

# Connect to WiFi
Expand Down Expand Up @@ -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": <your document from step 2>,
# }
# 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),
# )
...