This is an old revision of the document!
[pczekalski]Dokończyć
In the following scenario, you will learn how to connect to the MQTT broker and publish a message.
To implement this scenario, it is necessary to get familiar with at least one of the following scenarios first:
and obligatory:
[pczekalski]Tu trzeba dodać bibliotekę do MQTT, pewnie pub-sub client
Connect to the “internal IoT” WiFI access point as presented in the scenario IOT2: Reading IP address of the WiFi—present connection status on the display (for tracing). Once connected to the networking layer, connect to the MQTT broker and present the connection status on the display, then publish an MQTT message.
Check if you can clearly see a full display (of your choice) in your video stream. Book a device and create a dummy Arduino file with void setup()…
and void loop()…
.
Implement a connection to the “internal IoT” network as a client. Refer to the supervisor or the technical documentation on credentials (SSID, passphrase). We do not provide the exact code on how to connect to the WiFi as it is a part of IOT2: Reading IP address of the WiFi scenario.
Include the WiFi management library in your source code:
#include <WiFi.h>
The WiFi library automatically initialises a singleton class, WiFi,
which you can use to set up working mode, read MAC and IP, and perform many other operations.
Declare some constants, including AP SSID and passphrase and a variable to store IP:
const char* ssid = "REPLACE_WITH_CORRECT_SSID"; const char* pass = "REPLACE_WITH_CORRENT_PASSPHRASE"; IPAddress localIP;
Both ssid
and pass
are to be obtained from the hardware reference, available here: SUT ESP32 Laboratory Node Hardware Reference.
Set your device in the STA mode and connect to the WiFi AP:
WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { //Drop some info on display about connecting delay(1000); }
The WiFi.begin(…)
returns the following statuses (value, enumerator: meaning):
WL_IDLE_STATUS:
temporary status assigned when WiFi.begin() is calledWL_NO_SSID_AVAIL:
when no SSID are availableWL_SCAN_COMPLETED:
scan networks is completedWL_CONNECTED:
when connected to a WiFi networkWL_CONNECT_FAILED:
when the connection fails for all the attemptsWL_CONNECTION_LOST:
when the connection is lostWL_DISCONNECTED:
when disconnected from a network
Reading the IP as a String
is as easy as simply calling:
localIP = WiFi.localIP();
The IPAddress
class contains the toString()
method that formats the IP and returns in standard, dot-separated, four-octet text format.
Explicitly disconnect from the WiFi AP to free resources:
WiFi.disconnect();
Some useful WiFi functions are listed below:
WiFi.reconnect()
- reconnects the connection that was dropped, note it uses ssid
and pass
previously specified in the WiFi.begin(…)
, here you do not provide one. WiFi.RSSI()
- once connected, you can get signal strength as int8_t
integer.WiFi.setHostname(const char * hostname)
- set host name for the ESP32 chip. Remember to use this function before connecting to the AP.You should be able to connect to the WiFi and present the dynamically assigned IP address by the DHCP server.
I set a hostname, but it does appear on the router level.: There are many possible reasons; one is an active registration in the router (AP) that keeps an old IP address, so you need to wait until it expires; other reason is you have changed the hostname when you were connected to the AP.
Can I use a manually configured IP?: Actually, you can, but we strongly discourage it. This is because you may accidentally overlap this IP address with some other device, router, or other infrastructure, blocking its operation.