How to Program an ESP32 in Arduino while using ESP-IDF functions

Mark Zachmann
Feb 13, 2018 · 3 min read

I’ve just started using an ESP32 chip and it’s awesome. There are two paths to program it, though.

Image for post
Image for post
An ESP32-based Chip Computer

Arduino

ESP-IDF

How to Program

Here’s an example of two code fragments. Arduino and ESP-IDF to initialize and enable the serial port:

Arduino

Serial.begin(115200);

ESP-IDF

/* Configure parameters of an UART driver,
* communication pins and install the driver */
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
};
uart_param_config(EX_UART_NUM, &uart_config);
//Set UART pins (using UART0 default pins ie no changes.)
uart_set_pin(EX_UART_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
//Install UART driver, and get the queue.
uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart0_queue, 0);

Harder Tasks

Image for post
Image for post
Working sniffer with lots of Arduino Code

Here’s how you set up the wifi port as a sniffer in Arduino:

{}

You can’t.

Here’s my sample code to set up the wifi port as a sniffer in Arduino, while using ESP-IDF

#include "C:\msys32\home\Mark\esp\esp-idf\components\esp32\include\esp_wifi.h"// set up Wifi for promiscuous mode, random channel
void WifiInitialize()
{
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&config);
// Promiscuous works only with station mode esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_set_promiscuous(0);
// Set up promiscuous callback esp_wifi_set_promiscuous_rx_cb(promisc_cb);
esp_wifi_set_promiscuous(1);
esp_wifi_set_channel(1, WIFI_SECOND_CHAN_NONE );
}
// the callback function
void promisc_cb(void *buf, wifi_promiscuous_pkt_type_t ttype)
{
wifi_promiscuous_pkt_t* snifpak = (wifi_promiscuous_pkt_t*)buf;
wifi_pkt_rx_ctrl_t& snifctrl = snifpak->rx_ctrl; // the metadata
... more code
}

The Answer

In the above code I point to my location for the right header. You would normally just set an include folder. The include path I use is: C:/msys32/home/Mark/esp/esp-idf/components/esp32/include. The path is from following Espressif’s installation instructions for the IDF component.

Once the code is written, use Arduino to compile it, which it will happily do — and then run it. There are some limitations to this approach (the Espressif IDF is more current than the Arduino, for example) but for most libraries it works admirably.

Running Linux on Windows

Note: in the GIF above showing MAC addresses I’ve specifically edited my code to not show the right MAC addresses. Instead it’s a random hash based off the MAC address.

Home Wireless

Home automation in the wireless IOT era

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store