设备 esp32_ESP32 设备运行 FreeRTOS IoT 操作系统

 本文将在 ESP32 上运行 FreeRTOS 操作系统,并发送 MQTT 消息到 AWS IoT Core 平台。

FreeRTOS 操作系统常用于 Cortex M3/M4 内核微控制器,可以简化嵌入式设备开发,免于直接编写复杂的裸设备纯 C 代码,也简化处理不同微控制器的差异和直接处理内存空间等。

844950d5c22c589e162bb1007d534ccb.png

本文中所采用的的微控制器设备仍然是 ESP32。

30dc754942c1cfdf4baaef27026e224b.png

我们在之前的实践中采用了 Arduino C 代码对 ESP32 进行编程,是比较简单的,FreeRTOS 则复杂得多。那为什么要使用 FreeRTOS 进行嵌入式设备开发呢?

1、FreeRTOS 或类似 RTOS 操作系统能提供更加细粒度的设备控制,从实现生产级产品设计来说,它比纯 C 代码简单,帮忙开发人员简化了设备控制;

2、相比其他的高级编程语言,基于 C 的 FreeRTOS 开发具有高效率和更小的 code footprint;

3、面对生产级产品开发,高效率,更短的开发周期;

4、相比其他高级编程语言,基于 C 的 FreeRTOS 开发要求较低功耗。

01

设置 ESP32 的开发环境

设置 ESP32 的 FreeRTOS 开发环境需要配置 ESP32 BoardManager 所需的 ToolChain,连接设备的串口转换软件,以及下载 FreeRTOS 软件包等。

设置过程主要参考:

https://docs.aws.amazon.com/freertos/latest/userguide/getting_started_espressif.html

------

设置本地电脑开发环境

设置 ToolChain

ToolChain 用于与 ESP32 设备的通信,这个是由开发板的提供商开发的,并且与 FreeRTOS 操作系统兼容适配(大家可以稍微联想一下这意味着什么? >

ToolChain 安装很简单,只需要按照文档链接步骤操作即可。

https://docs.espressif.com/projects/esp-idf/en/v3.3/get-started-cmake/macos-setup.html

pip install --user pyserialbrew install cmake ninja

下载软件包

https://dl.espressif.com/dl/xtensa-esp32-elf-osx-1.22.0-80-g6c4433a-5.2.0.tar.gz

解压、设置环境变量:

mkdir -p ~/espcd ~/esptar -xzf ~/Downloads/xtensa-esp32-elf-osx-1.22.0-80-g6c4433a-5.2.0.tar.gzexport PATH=$HOME/esp/xtensa-esp32-elf/bin:$PATHprintenv PATH

安装 CMake

https://cmake.org/download/

CMake 用于构建基于 FreeRTOS 的 demo ESP32 应用。

------

本地开发环境与设备串口连接

本地笔记本电脑需要与 ESP32 DevKitC 开发板通信,需要安装 USB-to-UART 转换驱动。通过以下链接安装。

https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers

------

02

下载和配置 FreeRTOS

下载  FreeRTOS for ESP32。访问以下链接,下载适配开发板的相应的软件包。如果没有硬件的话,应该可以下载 Windows Simulator 模拟(我没有尝试过)。

https://console.aws.amazon.com/iot/home?region=us-east-1#/software/freertos

90bd70bfa48f4c49844581911e593542.png

配置 aws CLI

aws configure

安装 AWS Python SDK (运行配置脚本用)

pip install tornado nose --userpip install boto3 --user

运行配置脚本,先修改配置文件freertos/tools/aws_config_quick_start/configure.json :

{
        "afr_source_dir":"/Users/jianhong/Downloads/FreeRTOS",    "thing_name":"myESP32",    "wifi_ssid":"712",    "wifi_password":"92861899",    "wifi_security":"eWiFiSecurityWPA2"}

运行 freertos/tools/aws_config_quick_start/SetupAWS.py

python SetupAWS.py setup.

AWS IoT 上自动配置了 Things、Certificate 和 Policy,以及Wifi SSID。(理解 AWS IoT 的 Certificate、Policy 等,可以参考 使用MQTT和AWS IoT Core进行通信 。在之前的不采用 纯C 的代码实现里,我们在 AWS IoT 上创建了 certificate 后,需要手动在我们的 C 代码里处理证书问题。Zenryth 和 MongooseOS 自动帮助我们处理了证书问题。)

c92be9b9befe6dd1b06090a7f0cca9d8.png

查看下列文件,可以看到脚本已经帮助我们处理了连接 AWS IoT 所需的 Wifi、Endpiont 和 certificate 问题。

FreeRTOS/demos/include/aws_clientcredential.h

/* * FreeRTOS V201906.00 Major * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * * http://aws.amazon.com/freertos * http://www.FreeRTOS.org */#ifndef __AWS_CLIENTCREDENTIAL__H__#define __AWS_CLIENTCREDENTIAL__H__/* * Include for device certificate and private key */#include "aws_clientcredential_keys.h"/* * MQTT Broker endpoint. */#define clientcredentialMQTT_BROKER_ENDPOINT "a36swrxk5hxfjq-ats.iot.us-east-1.amazonaws.com"/* Use of a "define" and not a "static const" here to be able to* use pre-compile concatenation on the string. */#define clientcredentialIOT_THING_NAME "myESP32"/* * Port number the MQTT broker is using. */#define clientcredentialMQTT_BROKER_PORT 8883/* * Port number the Green Grass Discovery use for JSON retrieval from cloud is * using. */#define clientcredentialGREENGRASS_DISCOVERY_PORT 8443/* * Wi-Fi network to join. */#define clientcredentialWIFI_SSID       "712"/* * Password needed to join Wi-Fi network. */#define clientcredentialWIFI_PASSWORD   "92861899"/** * @brief Security type * WPA2 Security, @see WIFISecurity_t * Possible values are - eWiFiSecurityOpen, eWiFiSecurityWEP, eWiFiSecurityWPA, * eWiFiSecurityWPA2 */#define clientcredentialWIFI_SECURITY   eWiFiSecurityWPA2#endif

03

Build 和 Flash FreeRTOS

设置好本地开发环境和 ESP32 Package 的 AWS IoT 相关参数后,就可以开始将 FreeRTOS Flash 到 ESP32 硬件中了。

可以通过 cmake 命令 Flash,也可以通过 py 脚本进行 Build 和 Flash。

cmake -DVENDOR=espressif -DBOARD=esp32_wrover_kit -DCOMPILER=xtensa-esp32 -S . -B build

Erase 擦除 ESP32 开发板的 Flash

./vendors/espressif/esp-idf/tools/idf.py erase_flash -B build

Flash 开发板

./vendors/espressif/esp-idf/tools/idf.py flash -B build

Flash 结果:

需要一些时间,这里把全部过程贴出来。

Setting IDF_PATH environment variable: /Users/jianhong/Downloads/FreeRTOS/vendors/espressif/esp-idfChecking Python
  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FreeRTOS is a popular open-source real-time operating system (RTOS) that is often used in embedded systems, including the ESP32 microcontroller. The ESP32 is a powerful and versatile microcontroller developed by Espressif Systems. It has built-in Wi-Fi and Bluetooth capabilities, making it suitable for a wide range of IoT applications. FreeRTOS provides a multitasking environment for the ESP32, allowing you to run multiple tasks concurrently while managing system resources efficiently. It offers features such as task scheduling, synchronization mechanisms (like semaphores and mutexes), and inter-task communication through message queues. To use FreeRTOS with ESP32, you typically need to set up the development environment using tools like the Espressif IoT Development Framework (ESP-IDF). The ESP-IDF provides a set of libraries, drivers, and examples specifically designed for ESP32 development. It includes FreeRTOS as its default RTOS. You can find documentation, tutorials, and code examples on the official Espressif website or community forums. These resources will help you get started with FreeRTOS on ESP32 and guide you through various tasks, such as creating tasks, managing resources, and handling interrupts. Please note that the specifics of working with FreeRTOS on ESP32 may vary depending on the development environment and version of the software you are using. It's always a good idea to refer to the official documentation and community support for the most up-to-date information.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值