How to use the Arduino-ESP32 Library as an ESP-IDF Component

Related Documentation


Prepare Environment

Currently, the latest Master version of the arduino-esp32 SDK requires the usage of ESP-IDF SDK environment version v4.4.


Next, we will demonstrate how to use the arduino-esp32 library as an ESP-IDF SDK component on a Windows environment. This includes:

  • Using the arduino-esp32 library as a component in the project
  • Using the arduino-esp32 library as a component in the ESP-IDF SDK libraries

1、 Using the arduino-esp32 library as a component in the project:

  • Create a custom project
  • Create a component folder for the current project
  • Clone the arduino-esp32 library as a component for the current project
  • Make modifications to the project file names
  • Make modifications to the project configuration options
  • Compile and flash the current project for testing

1.1 Creating a custom project:

You can based on the ESP-IDF SDK to copy a project for testing. For example, copy the hello-world project. and rename the project name as hello-world_Arduino.
在这里插入图片描述

1.2 Create a component folder for the current project

You can use the following command to create a component folder for the current project:

cd hello-world_Arduino
mkdir components

You can use the following command to create a component folder for the custom project:

1.3 Clone the arduino-esp32 library as a component for the current project

  • Goto the components directory, running the following commands to clone the arduino-esp32 library into the components directory
cd components

git clone https://github.com/espressif/arduino-esp32.git

在这里插入图片描述

cd arduino-esp32

git submodule update --init --recursive

在这里插入图片描述
After completing the above steps, the project structure will be as follows:
在这里插入图片描述


1.4 Make modifications to the project file names

We will use Arduino’s setup() and loop() functions within the hello-world_Arduino project to demonstrate.

  • In the “hello-world_Arduino” project directory, rename the file “main.c” to “main.cpp” . As follows:

    在这里插入图片描述

  • In the main folder within the project directory, open the file CMakeLists.txt and change the name of the file main.c to main.cpp . As follows:
    在这里插入图片描述

  • In the hello-world_Arduino project, you can write test code based on the Arduino library in the hello_world_main.cpp file as follows:

#include "Arduino.h"

#define RGB_BUILTIN 26

void setup() {
  // No need to initialize the RGB LED
  Serial.begin(115200);
  pinMode(RGB_BUILTIN, OUTPUT);
  Serial.printf("GPIO is %d \r\n", RGB_BUILTIN);
}

// the loop function runs over and over again forever
void loop() {
#ifdef RGB_BUILTIN
  digitalWrite(RGB_BUILTIN, HIGH);
   Serial.printf("Light on \r\n ");
  delay(1000);
  
  digitalWrite(RGB_BUILTIN, LOW);   // Turn the RGB LED off
  Serial.printf("Light off \r\n");
  delay(1000);

#endif
}

在这里插入图片描述


1.5 Make modifications to the project configuration options

  • Modify the CONFIG_FREERTOS_HZ configuration in the sdkconfig file to 1000. The default value is 100.

    在这里插入图片描述

  • In the project directory, run the command idf.py menuconfig to enter the project configuration options interface. Enable the Autostart Arduino setup and loop on boot configuration option.

    在这里插入图片描述

    idf.py menuconfig → Arduino Configuration [*] Autostart Arduino setup and loop on boot

    在这里插入图片描述

1.6 Compile and flash the current project for testing

  • In the current project directory, run the following command to compile the project:

    idf.py build
    

    在这里插入图片描述

    After the firmware compilation is completed, the following log will be printed, indicating the compiled firmware and its corresponding download address.

在这里插入图片描述

  • In the current project directory, run the following command to download the firmware and print the firmware running logs.

    idf.py -p COM4 flash monitor
    

    在这里插入图片描述


2、Using the Arduino-ESP32 Library as an ESP-IDF Component

  • Create the components-Arduino folder in the esp-idf SDK directory
  • Clone the arduino-esp32 SDK into the components-Arduino folder
  • In the CMakeLists.txt file located in the project directory, add the path to the arduino-esp32 component

2.1 Open the esp-idf CMD environment and create the components-Arduino folder in the esp-idf SDK directory

mkdir components-Arduino

在这里插入图片描述

2.2 Clone the arduino-esp32 SDK into the components-Arduino folder

cd components-Arduino

git clone https://github.com/espressif/arduino-esp32.git

cd arduino-esp32

git submodule update --init --recursive

在这里插入图片描述

2.3 In the CMakeLists.txt file located in the project directory, add the path to the arduino-esp32 component

To include the arduino-esp32 library as a component based on the esp-idf SDK directory, add the path to the arduino-esp32 component in the CMakeLists.txt file of the project directory, as follows:

set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/components-Arduino/arduino-esp32)

在这里插入图片描述

Other steps are exactly the same as Step 1.


【Note】

  • If you need to switch the chip environment, please running the following command in the project directory:
 idf.py set-target esp32s3
  • If you need to use app_main() from ESP-IDF to run the code and call Arduino library API functions, the project file must be named main.cpp. In addition, you need to disable the Autostart Arduino setup and loop on boot configuration option and define app_main() using extern "C" void app_main(), as shown in the example test code below:
#include "Arduino.h"
#include <stdio.h>
#include <inttypes.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_chip_info.h"
#include "esp_flash.h"

#define RGB_BUILTIN 21

extern "C" void app_main()
{

      // ESP-IDF API Usage
 printf("Hello world!\n");

    /* Print chip information */
    esp_chip_info_t chip_info;
    uint32_t flash_size;
    esp_chip_info(&chip_info);
    printf("This is %s chip with %d CPU core(s), %s%s%s%s, ",
           CONFIG_IDF_TARGET,
           chip_info.cores,
           (chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "",
           (chip_info.features & CHIP_FEATURE_BT) ? "BT" : "",
           (chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "",
           (chip_info.features & CHIP_FEATURE_IEEE802154) ? ", 802.15.4 (Zigbee/Thread)" : "");

    unsigned major_rev = chip_info.revision / 100;
    unsigned minor_rev = chip_info.revision % 100;
    printf("silicon revision v%d.%d, ", major_rev, minor_rev);
    if(esp_flash_get_size(NULL, &flash_size) != ESP_OK) {
        printf("Get flash size failed");
        return;
    }

    printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024),
           (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");

    printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size());

    for (int i = 5; i >= 0; i--) {
        printf("Restarting in %d seconds...\n", i);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }

    // Arduino-like setup()
  Serial.begin(115200);
   pinMode(RGB_BUILTIN, OUTPUT);
  Serial.printf("GPIO is %d \r\n", RGB_BUILTIN);
 
  // Arduino-like loop()
  while(true){
    #ifdef RGB_BUILTIN
  digitalWrite(RGB_BUILTIN, HIGH);
   Serial.printf("Light on \r\n ");
  delay(1000);
  
  digitalWrite(RGB_BUILTIN, LOW);   // Turn the RGB LED off
  Serial.printf("Light off \r\n");
  delay(1000);

#endif
    
    }

}
  • The setup() function in Arduino is called only once within app_main() and does not require the while(!Serial){} loop.
  • The loop() function in Arduino, when used within app_main(), must be implemented with while(true){} or while(1){} to create an infinite loop.
  • 固件运行日志:
    在这里插入图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Arduino-ESP32ModbusRTU是一个基于Arduino平台和ESP32芯片的Modbus RTU通信协议库。Modbus RTU是一种常用的串行通信协议,用于在不同设备之间实现通信和数据交换。ESP32是一款强大的Wi-Fi和蓝牙芯片,具有丰富的资源和功能。 利用Arduino-ESP32ModbusRTU库,我们可以轻松地在ESP32上实现Modbus RTU通信功能。通过这个库,我们可以将ESP32作为Modbus RTU的主机或从机,并与其他Modbus RTU设备进行通信。在主机模式下,ESP32可以向其他设备发送Modbus RTU命令,并接收其响应。在从机模式下,ESP32可以接收其他设备发送的Modbus RTU命令,并作出相应的响应。 使用Arduino-ESP32ModbusRTU库,我们可以使用简单的代码实现Modbus RTU通信功能。我们可以指定串口用于与其他设备进行通信,并设置自己的Modbus地址。我们可以使用库提供的函数来读取和写入寄存器或线圈,以及进行其他Modbus操作。 Arduino-ESP32ModbusRTU库还支持异步通信功能,可以在不阻塞主线程的情况下进行Modbus通信操作。这使得我们可以同时处理其他任务,而不会影响Modbus通信的性能和稳定性。 总之,Arduino-ESP32ModbusRTU是一个方便实用的库,可以帮助我们在ESP32上实现Modbus RTU通信功能。无论是作为主机还是从机,通过这个库,我们可以轻松地与其他Modbus RTU设备进行数据交换和通信。 ### 回答2: arduino-esp32modbusrtu是一种基于Arduino开发平台和ESP32微控制器的Modbus RTU通信库。Modbus是一种通信协议,常用于工业自动化领域中的设备间通信。 arduino-esp32modbusrtu库为ESP32提供了实现Modbus RTU通信的功能,可以让ESP32作为Modbus RTU主机或从机来与其他设备进行通信。通过该库,用户可以轻松地对Modbus RTU数据进行读取和写入。 使用arduino-esp32modbusrtu库,用户首先需要在Arduino开发平台上导入该库,并在代码中包含相应的头文件。然后,需要设置串口参数,并创建一个Modbus RTU对象。用户可以根据需要选择将ESP32配置为主机或从机,并指定Modbus设备的地址。 在主机模式下,用户可以使用ModbusRTUMaster类的方法来发送读取或写入请求,并获取设备的响应。用户可以指定读取或写入的寄存器地址以及读取的数量或写入的值。在从机模式下,用户可以使用ModbusRTUSlave类的方法来处理主机的请求,并返回相应的数据。 arduino-esp32modbusrtu库提供了许多示例代码和详细的文档,帮助用户快速上手,并实现其Modbus RTU通信需求。使用该库,用户可以自定义设置通信参数、处理各种Modbus功能码,并与其他Modbus RTU设备进行稳定可靠的通信。 总之,arduino-esp32modbusrtu是一款强大而灵活的Modbus RTU通信库,为基于ESP32的项目提供了方便快捷的Modbus功能支持。 ### 回答3: Arduino-ESP32ModbusRTU是一种基于ESP32微控制器的Modbus RTU通信协议库。该库允许使用Arduino编程语言和开发工具与Modbus RTU设备进行通信。 Modbus RTU是一种在串行通信介质上实现的开放式通信协议。使用Modbus RTU协议,可以实现数据在不同设备之间的传输和控制。通常,Modbus RTU是在RS485物理层上实现的,允许多个设备共享通信线路。 Arduino-ESP32ModbusRTU库通过处理Modbus RTU帧格式,实现了Master和Slave两种角色的操作。作为Master,ESP32可以通过发送请求到Slave设备来读取或写入数据。而作为Slave,ESP32可以接收Master设备发送的请求,并根据请求进行数据读取或写入。 使用Arduino-ESP32ModbusRTU库,我们可以通过Arduino编程语言轻松地实现Modbus RTU通信。我们可以设定串行通信参数(如波特率、数据位、停止位等),并使用预定义的函数来读取或写入Modbus寄存器中的数据。此外,该库还支持不同种类的Modbus寄存器,如输入寄存器、保持寄存器、线圈和离散输入寄存器。 总结而言,Arduino-ESP32ModbusRTU库为我们提供了一种简单而高效的方式来实现ESP32与其他Modbus RTU设备之间的通信。无论是作为Master还是Slave,ESP32都可以通过这个库与Modbus RTU设备进行数据的读取和写入。这个库的使用使得我们可以很方便地将ESP32应用于各种Modbus RTU通信场景中,如工业自动化、设备监控等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值