|ESP32 In MicroPython: WiFi|
|Written by Mike James & Harry Fairhead|
|Tuesday, 01 August 2023|
Page 1 of 3
Getting started with WiFi on the ESP32 is fairly easy but there are some things that are hard to find out - like how to set the country code. This extract is from Programming the ESP32 in MicroPython, part of the I Programmer Library and it shows you how to make a WiFi connection..
Programming the ESP32in MicroPython
By Harry Fairhead & Mike James
Buy from Amazon.
The ESP32 comes complete with a radio capable of 2.4GHz WiFi and Bluetooth. Most of the time you can ignore the technical details as MicroPython provides easy to use objects and methods which enable you to connect to a WiFi network and exchange data. In this chapter we look at the basics and how to create clients and servers. The topic of Bluetooth is omitted as it is so varied it deserves a book to itself.
You don’t really need to know anything about the ESP32’s WiFi hardware to make use of it. Indeed there is very little information available apart from how to use the WiFi drivers in the C development kit. The ESP32 usually has two processor cores, which are used to run the WiFi and applications simultaneously. This means that WiFi has little impact on the running of your application. The two cores, Core 0 and Core 1, are named Protocol CPU (PRO_CPU) and Application CPU (APP_CPU). The PRO_CPU processor handles the WiFi, Bluetooth and other internal peripherals like SPI, I2C, ADC etc. The APP_CPU runs the application code, including your MicroPython program.
As well as the radio, the ESP32 also supports four cryptographic accelerators to make the implementation of HTTPS (Hypertext Transfer Protocol Secure) and TLS (Transport Layer Security) in general more efficient.
The WLAN Class
Networking in MicroPython is taken care of in the network module which contains specific classes to work with different implementations according to the hardware in use. The custom WiFi class for the ESP32 is WLAN. All you have to do is create an instance of the class, set the WiFi to active and then use the connect method. For example:
import network wifi = network.WLAN(network.STA_IF) wifi.active(True) wifi.connect("ssid","key")
You have to supply the ssid and the key used to connect to the access point. You can set things up in two modes, STA_IF which makes the ESP32 a client or AP_IF which makes the ESP32 an access point. In most cases you want the ESP32 to be a client of another access point.
It is also a good idea to set the country of operation because different WiFi frequencies are used in different countries and if you don’t specify a location not all of the available channels will be used. To set the country you simply call network.country(“XX”) with XX replaced by the two-character country code specified by ISO 3166-1 alpha-2. For example., to set the country to USA you would use:
import network import rp2 network.country("US") wifi = network.WLAN(network.STA_IF) wifi.active(True) wifi.connect("ssid","key")
MicroPython also supports connecting to a specific access point using its bssid, which is the same as the access point’s MAC address, but this is really only needed as a security measure.
To disconnect from an access point you can use the disconnect method.
|Last Updated ( Tuesday, 01 August 2023 )|