Esp32: знакомимся, пишем и запускаем первую прошивку

The Partition Table

Once you’ve compiled your project, the «build» directory will contain a binary file with a name like «my_app.bin». This is an ESP32 image binary that can be loaded by the bootloader.

A single ESP32’s flash can contain multiple apps, as well as many different kinds of data (calibration data, filesystems, parameter storage, etc). For this reason a partition table is flashed to offset 0x4000 in the flash.

Each entry in the partition table has a name (label), type (app, data, or something else), subtype and the offset in flash where the partition is loaded.

The simplest way to use the partition table is to and choose one of the simple predefined partition tables:

  • «Single factory app, no OTA»
  • «Factory app, two OTA definitions»

In both cases the factory app is flashed at offset 0x10000. If you then it will print a summary of the partition table.

For more details about partition tables and how to create custom variations, view the file.

Scanning for WiFi networks

Before connecting to an WiFi network, we will do a scan of the surrounding networks. Along with it, we will print some parameters for those networks, such as the network name (SSID), the signal strength, the MAC and the encryption type. As said before, this will be implemented in a function called scanNetworks.

To start performing a scan of networks, we just need to call the  function of the previously mentioned WiFi extern variable. This call will initiate a scan and return the number of networks found upon a successful execution.

Note that this function receives two Boolean arguments which indicate if the scan should be performed in asynchronous mode and if hidden networks should be shown. Note however that in the  of the class where the function is implemented both of the arguments have default values of false, so we can call the function in our code without passing any input parameter. For simplicity, that’s what we will do.

int numberOfNetworks = WiFi.scanNetworks();

Note that we stored the number of networks found in a variable. This will be needed for iterating the data structures where the information about those networks will be stored.

So, after the scanning is performed, we can access the parameters of each network with the functions shown . Note that all of them receive as argument an integer with the number of the network, from 0 to the total number of networks detected minus 1.

In our code we will get and print the network name (SSID), the MAC address, the signal strength (RSSI) and the encryption type. Note however that there are also functions to retrieve the channel of the networks.

But before proceeding with that, we need to take in consideration that the encryption type is returned as an enum, which is defined . So, we will define an auxiliary function that will receive the value of this enum and return a textual description indicating the encryption type. This way, we get a human readable result rather than an integer.

So, this function receives as input a variable of type wifi_auth_mode_t, which is the previously mentioned enum, and simply does a switch case that returns the textual description of each possible value. You can check bellow the implementation.

String translateEncryptionType(wifi_auth_mode_t encryptionType) {

  switch (encryptionType) {
    case (WIFI_AUTH_OPEN):
      return "Open";
    case (WIFI_AUTH_WEP):
      return "WEP";
    case (WIFI_AUTH_WPA_PSK):
      return "WPA_PSK";
    case (WIFI_AUTH_WPA2_PSK):
      return "WPA2_PSK";
    case (WIFI_AUTH_WPA_WPA2_PSK):
      return "WPA_WPA2_PSK";
    case (WIFI_AUTH_WPA2_ENTERPRISE):
      return "WPA2_ENTERPRISE";
  }
}

Now that we have a function that allows to translate the encryption types to strings, we can proceed on iterating the data. So, we do a loop using our number of networks variable as stopping condition and calling the previously mentioned functions to get the information of each scanned network.

This is shown bellow, with some additional prints to make the output more readable for the user. Note also the call to the translateEncryptionType function defined before.

for (int i = 0; i < numberOfNetworks; i++) {

    Serial.print("Network name: ");
    Serial.println(WiFi.SSID(i));

    Serial.print("Signal strength: ");
    Serial.println(WiFi.RSSI(i));

    Serial.print("MAC address: ");
    Serial.println(WiFi.BSSIDstr(i));

    Serial.print("Encryption type: ");
    String encryptionTypeDescription = translateEncryptionType(WiFi.encryptionType(i));
    Serial.println(encryptionTypeDescription);

    Serial.println("-----------------------");

  }

Check the full function bellow. Note that there are some additional prints for making the output more complete.

void scanNetworks() {

  int numberOfNetworks = WiFi.scanNetworks();

  Serial.print("Number of networks found: ");
  Serial.println(numberOfNetworks);

  for (int i = 0; i < numberOfNetworks; i++) {

    Serial.print("Network name: ");
    Serial.println(WiFi.SSID(i));

    Serial.print("Signal strength: ");
    Serial.println(WiFi.RSSI(i));

    Serial.print("MAC address: ");
    Serial.println(WiFi.BSSIDstr(i));

    Serial.print("Encryption type: ");
    String encryptionTypeDescription = translateEncryptionType(WiFi.encryptionType(i));
    Serial.println(encryptionTypeDescription);
    Serial.println("-----------------------");

  }
}

Step 11: ESP32 With Arduino IDE Hello World Using Blink

the ESP32 in nano32 sits snugly in the MK302 breadboard. One good feature of nano32 that is worth a highlight, it leaves 2 empty rows parallel to the long side of the board, on the MK302 breadboard. this makes quick prototyping that uses all of the GPIO pins possible without the need of another breadboard.

GPIO22 is used for the convenient sake, because it is sitting next to the GND pin. An LED is inserted without a current limiting resistor for the sake of «quick and dirty» play.

Hold down the GPIO0 pin via the push button, and press download icon on the arduino IDE to compile the customary hello world program aka blink and flash the bin into the ESP32. Details in the screenshot.

The Partition Table

Once you’ve compiled your project, the «build» directory will contain a binary file with a name like «my_app.bin». This is an ESP32 image binary that can be loaded by the bootloader.

A single ESP32’s flash can contain multiple apps, as well as many different kinds of data (calibration data, filesystems, parameter storage, etc). For this reason a partition table is flashed to offset 0x8000 in the flash.

Each entry in the partition table has a name (label), type (app, data, or something else), subtype and the offset in flash where the partition is loaded.

The simplest way to use the partition table is to and choose one of the simple predefined partition tables:

  • «Single factory app, no OTA»
  • «Factory app, two OTA definitions»

In both cases the factory app is flashed at offset 0x10000. If you then it will print a summary of the partition table.

For more details about partition tables and how to create custom variations, view the file.

Other Useful Functions

There are other more advanced functions to use with the ADC pins that can be useful in other projects.

  • analogReadResolution(resolution): set the sample bits and resolution. It can be a value between 9 (0 – 511) and 12 bits (0 – 4095). Default is 12-bit resolution.
  • analogSetWidth(width): set the sample bits and resolution. It can be a value between 9 (0 – 511) and 12 bits (0 – 4095). Default is 12-bit resolution.
  • analogSetCycles(cycles): set the number of cycles per sample. Default is 8. Range: 1 to 255.
  • analogSetSamples(samples): set the number of samples in the range. Default is 1 sample. It has an effect of increasing sensitivity.
  • analogSetClockDiv(attenuation): set the divider for the ADC clock. Default is 1. Range: 1 to 255.
  • analogSetAttenuation(attenuation): sets the input attenuation for all ADC pins. Default is ADC_11db. Accepted values:

    • ADC_0db: sets no attenuation (1V input = ADC reading of 1088).
    • ADC_2_5db: sets an attenuation of 1.34 (1V input = ADC reading of 2086).
    • ADC_6db: sets an attenuation of 1.5 (1V input = ADC reading of 2975).
    • ADC_11db: sets an attenuation of 3.6 (1V input = ADC reading of 3959).
  • analogSetPinAttenuation(pin, attenuation): sets the input attenuation for the specified pin. The default is ADC_11db. Attenuation values are the same from previous function.
  • adcAttachPin(pin): Attach a pin to ADC (also clears any other analog mode that could be on). Returns TRUE or FALSE result.
  • adcStart(pin), adcBusy(pin) and resultadcEnd(pin): starts an ADC convertion on attached pin’s bus. Check if conversion on the pin’s ADC bus is currently running (returns TRUE or FALSE). Get the result of the conversion: returns 16-bit integer.

There is a very good video explaining these functions that you can watch here.

Wrapping up

We hope you’ve found this getting started guide useful. The blinking LED is just a simple project to get you started with the ESP32. This is also a great way to learn the procedure you need to do to upload code to your board.

If you like ESP32, we have more than 20 projects with the ESP32 you can find in our repository of ESP32 projects:

20+ ESP32 Projects and Tutorials

You may also like:

  • ESP32 Pinout Reference: Which GPIO pins should you use?
  • ESP32 Web Server Tutorial
  • Learn ESP32 with Arduino IDE
  • ESP32 vs ESP8266 – Pros and Cons
  • Best ESP32 Development Boards

If you like ESP32 make sure you subscribe to our blog, so you don’t miss upcoming projects.

Do you have any questions? Leave a comment down below!

Thanks for reading,

Installing the ESP32 Core On – Mac OS

Espressif’s official ESP32 Arduino core is hosted here on GitHub. They don’t have an Arduino board manager install yet (Like we do while installing ESP8266 core on Arduino IDE). It should be available soon. Until then, we have to install it manually.

Let’s proceed with the installing ESP32 Arduino core.

The first thing is having latest Arduino IDE (Arduino 1.8.5 or higher) installed on your PC. If not, we recommend upgrading now.

Next, Open your Terminal and execute the following commands.

Here ~/Documents/Arduino represents your sketch book location as per Arduino IDE > File> Preferences > Sketchbook location. Adjust the command above accordingly if necessary!

If you get the below error, Install the command line dev tools and try above commands again:

Installing Libraries

You need to install a couple of libraries for this project:

  • The DHT and the Adafruit Unified Sensor Driver libraries to read from the DHT sensor.
  • ESPAsyncWebServer and Async TCP libraries to build the asynchronous web server.

Follow the next instructions to install those libraries:

Installing the DHT Sensor Library

To read from the DHT sensor using Arduino IDE, you need to install the DHT sensor library. Follow the next steps to install the library.

  1. Click here to download the DHT Sensor library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get DHT-sensor-library-master folder
  3. Rename your folder from DHT-sensor-library-master to DHT_sensor
  4. Move the DHT_sensor folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Installing the Adafruit Unified Sensor Driver

You also need to install the Adafruit Unified Sensor Driver library to work with the DHT sensor. Follow the next steps to install the library.

  1. Click here to download the Adafruit Unified Sensor library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get Adafruit_sensor-master folder
  3. Rename your folder from Adafruit_sensor-master to Adafruit_sensor
  4. Move the Adafruit_sensor folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Installing the ESPAsyncWebServer library

Follow the next steps to install the ESPAsyncWebServer library:

  1. Click here to download the ESPAsyncWebServer library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get ESPAsyncWebServer-master folder
  3. Rename your folder from ESPAsyncWebServer-master to ESPAsyncWebServer
  4. Move the ESPAsyncWebServer folder to your Arduino IDE installation libraries folder

Installing the Async TCP Library for ESP32

The ESPAsyncWebServer library requires the AsyncTCP library to work. Follow the next steps to install that library:

  1. Click here to download the AsyncTCP library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get AsyncTCP-master folder
  3. Rename your folder from AsyncTCP-master to AsyncTCP
  4. Move the AsyncTCP folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE
Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *