学习完本章节后,您的设备将能够:
Core2 for AWS IoT Kit 参考硬件套件自带几个可以随时使用的传感器。针对这一解决方案,您需要使用本地应用程序代码从温度传感器和麦克风读取数据。应用程序将跟踪传感器读数和状态标志,以便在显示器中提供汇总数据。
您的智能恒温器将使用已经创建并且包含在捆绑软件组件中的代码来对集成传感器进行采样。在第一步中,您只需要获取相关值并将其打印到日志记录中,后续我们会将传感器值发布到 AWS IoT Core。
您可以很容易地从附带的 MPU6886 模块的温度传感器读取数值。以下是代码片段:
// 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);
}
如果您要使用这段代码来构建、烧录和监控设备日志,那么设备会从温度传感器读取一个读数并将其写入日志记录,然后停止。要在不阻塞其他进程的前提下实现每秒连续采样,您要创建单独的 FreeRTOS task 任务并将其固定到 MCU 核心,如下所示:
#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);
}
已经为您准备了一个示例应用程序,您可以使用 Visual Studio Code 和 PlatformIO extension 构建并上传到您的设备。如果你有任何其他项目已经在 VS Code 中打开,首先打开一个新窗口(文件 → 新窗口)以使用一个干净的文件资源管理器和工作环境。
对于本教程,您将使用智能恒温器项目。在您的新的 VS Code 窗口中:
Core2-for-AWS-IoT-EduKit/Smart-Thermostat
文件夹,然后点击 open。
接下来,您需要在 VS Code 中打开一个新的 PlatformIO CLI 终端窗口:
从 Blinky 项目中复制配置,编译设备固件,以及上传到您的设备中去,根据您的主机操作系统,参照下面的步骤完成本章节:
在进入下一章节之前,您可以通过查看终端窗口中的串行输出来验证你的设备是否按照预期进行了配置:
I (16128) MAIN: On Device: roomOccupancy false
I (16132) MAIN: On Device: hvacStatus STANDBY
I (16137) MAIN: On Device: temperature 64.057533
I (16143) MAIN: On Device: sound 8
如果符合预期,我们就进入到 数据同步 部分。
AWS IoT Kit now features direct access to
AWS re:Post
, which is a community-driven, questions-and-answers service. Search re:Post using the
AWS IoT Kit
tag to see if your question has been asked and answered. If not, ask a new question using the AWS IoT Kit
tag.