Data acquisition

Chapter introduction

By the end of this chapter, your device will do the following:

  • Power on and start the local smart thermostat application.
  • Report to the logs the current temperature sensed, ambient noise level measured, an HVAC status of heating/cooling/standby, and an indicator of room occupation.

The Core2 for AWS IoT EduKit reference hardware kit has several sensors ready to use. For this solution, you will take readings from the temperature sensor and microphone using local application code. The application will keep track of sensor readings and status flags that are used to render a summary to the display.

How to program the thermostat application

Your smart thermostat will sample the integrated sensors using code already created and included in the bundled software components. In this first step, you will simply capture the values and print them out to the logger before we move on to publishing sensor values up to AWS IoT Core.

Reading from the temperature sensor of the included MPU6886 module is easy. Here’s a code snippet:

// include libraries for interfacing with the kit's modules
#include "freertos/FreeRTOS.h"
#include "core2forAWS.h"
#include "mpu6886.h"
#include "esp_log.h"

// the application to run on the device
void app_main()
{
  // initialize a float to store our temperature reading
  float temperature = 0.0f;

  // initialize the kit modules
  Core2ForAWS_Init();
  MPU6886_Init();

  // read from the temperature sensor
  MPU6886_GetTempData(&temperature);

  // convert the reading to Fahrenheit and apply a calibration offset of -50
  temperature = (temperature * 1.8)  + 32 - 50;

  // write the value to the ESP32 logger
  ESP_LOGI("thermostat", "measured temperature is: %f", temperature);
}

If you were to build, flash, and monitor your device logs with this code, the device would take one reading from the temperature sensor, write it to the logger, and stop. To continuously sample every second without blocking other processes, you would create a separate FreeRTOS task and pin it to an MCU core, like so:

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "core2forAWS.h"
#include "mpu6886.h"
#include "esp_log.h"

// store our application logic in a task function
void temperature_task(void *arg) {
  float temperature = 0.0f;

  // loop forever!
  for (;;) {
    MPU6886_GetTempData(&temperature);
    temperature = (temperature * 1.8)  + 32 - 50;
    ESP_LOGI("thermostat", "measured temperature is: %f", temperature);

    // sleep for 1000ms before continuing the loop
    vTaskDelay(1000 / portTICK_RATE_MS);
  }
}

void app_main()
{
  Core2ForAWS_Init();
  MPU6886_Init();

  // FreeRTOS concept: operations that run in a continuous loop are done in tasks
  xTaskCreatePinnedToCore(&temperature_task, "temperature_task", 4096, NULL, 5, NULL, 1);
}

Compiling and Uploading the Smart Thermostat Firmware

A sample application has already been prepared for you to build and upload to your device using Visual Studio Code and the PlatformIO extension. If you have any other project already open in VS Code, first open a new window (File → New Window) to have a clean file Explorer and working environment.

For this tutorial, you will use the Smart-Thermostat project. In your new VS Code window:

  1. Click the PlatformIO logo in the VS Code activity bar (left most menu).
  2. Select Open from the left PlatformIO menu.
  3. Click Open Project.
  4. Navigate to the Core2-for-AWS-IoT-EduKit/Smart-Thermostat folder, and click open.
    PlatformIO home screen

    1 - Open PIO menu, 2 - Open PIO home, 3 - Open the project folder

Next, you’ll need to open a new PlatformIO CLI terminal window in VS Code:

  1. Click the PlatformIO logo on the VS Code activity bar (left most menu).
  2. From the Quick Access menu, under Miscellaneous, select New Terminal.
PlatformIO CLI terminal in VS Code

1 - Open PIO menu, 2 - Open new PIO Terminal, 3 - Verify you're in the 'PlatformIO CLI' terminal session, 4 - Paste the commands into terminal, 5 - If you encounter an error autodetecting the port, open the Platform.ini file and follow instructions to manually add the serial port.

To copy the configuration from the Blinky project, compile the device firmware, and upload it to your device, then follow the steps for your host machine’s OS to complete this chapter:

Ubuntu or macOS
Windows

Validation

Before you move on to the next chapter, you can validate that your device is configured as intended by viewing the serial output in the terminal window which should look like the following:

I (16128) shadow: On Device: roomOccupancy false
I (16132) shadow: On Device: hvacStatus STANDBY
I (16137) shadow: On Device: temperature 64.057533
I (16143) shadow: On Device: sound 8

If these are working as expected, let’s move on to Data sync.


Community support Report bugs