Fake Accelerometer Data Collection and POSTing

The questions below are due on Monday March 10, 2025; 05:00:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Back to Exercise 05

Overview

In the previous exercise, you built a data collection endpoint that lives on a server. Now we want to write the corresponding embedded code. The original plan was to get your accelerometer board working and post real data up to the server, but delays in PCB shipping have made that a little more difficult. C'est la vie — we'll do it next week. For this week, we'll simulate some three-axis accelerometer data and send it up to our server endpoint.

When you push a button, your microcontroller will "collect" a burst of three-axis data and, when done, form and send an appropriately-constructed POST to your running server.

You can likely start from the code you had working in Week 1 where you were already POSTing data to a different server endpoint rather than starting from scratch, though it is your life, and you can do what you want.

Button Setup

Connect IO5 on your board to a button in an active-low configuration (the button connects to ground when pressed). Some relevant code snippets:

#include "driver/gpio.h"  // Don't forget to add "driver" to REQUIRES in CMakeLists.txt

// Global scope
#define BUTTON_PIN  GPIO_NUM_5

// In app_main:
gpio_config_t btn_cfg = {
    .pin_bit_mask = (1ULL << BUTTON_PIN),
    .mode         = GPIO_MODE_INPUT,
    .pull_up_en   = GPIO_PULLUP_ENABLE,
    .pull_down_en = GPIO_PULLDOWN_DISABLE,
    .intr_type    = GPIO_INTR_DISABLE,
};
gpio_config(&btn_cfg);

// Reading the button state:
bool current_state = gpio_get_level(BUTTON_PIN);

Data Collection

Upon pushing the button, the system should collect a certain number of samples at a reasonable sample rate across all three axes. I used 100 samples at a 50 ms sampling period when testing, though in the big picture of your projects this may be too fast or too slow, too many or too few — it's just a decent ballpark for something like an accelerometer.

For now, use this helper function to generate random data in place of real accelerometer readings:

static float rand_float(float range) {
    return ((float)rand() / (float)RAND_MAX) * 2.0f * range - range;
}

Declare the data buffer at global scope to avoid overflowing the stack (or if you really want it in a loop, use a dynamic memory structure via C++ or malloc):

float samples[NUM_SAMPLES][NUM_AXES];

Then to "collect" data, run something like this, which simulates what reading from an accelerometer (or any other sensor) would look like:

for (int i = 0; i < NUM_SAMPLES; i++) {
    for (int axis = 0; axis < NUM_AXES; axis++) {
        samples[i][axis] = rand_float(10.0f);
    }
    vTaskDelay(pdMS_TO_TICKS(SAMPLING_PERIOD));
}

POSTing the Data

After successfully collecting the data, your job is to formulate a JSON body and send it to the server. This should be very similar to what you did in Week 1, except your data is now a much larger, more structured JSON object rather than simple form-encoded data.

Your body should look something like this:

{
  "sampling_period": "50",
  "series": [
    [4.13, 7.18, -1.37, -7.69, 1.87, ...],
    [7.49, -5.25, 5.33, 0.83, 3.74, ...],
    [-4.51, -3.36, 1.13, 3.46, 8.94, ...]
  ]
}

Each inner array in series corresponds to one axis, and each element is one sample.

Make sure to:

  • Set the content type to "application/json" when calling http_request.
  • Set the body size correctly, or you may get a 422 or similar error.

Deliverable

Once everything is working and you're posting ~10-second bursts of three-axis data to your server, run at least 10 collections.

Make sure this is all actually working. We'll be assuming you have a good data collection system in future weeks so we can do some data analysis.

Submit your functioning cpp file
 No file selected

Submit your database with at least ten data sets posted from your ESP32
 No file selected

Back to Exercise 05