wifi - dl3ebb/OpenIot GitHub Wiki
In most cases, it makes sense to connect IoT devices to a network. OpenIot uses the built-in Wi-Fi of the ESP32 to establish this connection.
Two different Wi-Fi networks can be configured. Upon startup, OpenIot scans the available Wi-Fi networks and connects to the stronger one if both are reachable. The connection to the network is checked every 5 seconds by pinging the gateway. If the gateway cannot be reached for more than 25 seconds, the device will reboot.
If no Wi-Fi is configured or none of the configured networks are reachable, the device will start an access point, allowing direct access to the device. The 10 Web Frontend explains how to set up a web frontend with configuration pages, making configuration easier. If you prefer not to use a web frontend, you can configure the device directly in the code.
It is also worth mentioning that all configuration data is stored in the NV-RAM. This means the settings need to be configured only once and will be retrieved from NV-RAM after a reboot.
/**
* Demo of the Wifi Usage
*/
#define SWITCH_PIN 25
#define LED_PIN 32
#define WLAN_SSID "your_ssid"
#define WLAN_PASSWD "your_passwd"
#include <OpenIot.h>
#include <Connector/DigitalInConnector.h>
#include <Connector/DigitalOutConnector.h>
#include <Logger/SerialLogger.h>
#include <Modules/Wifi.h>
void setup() {
serialLogger->setBaudrate(115200);
openIot.setLogger(serialLogger);
wifi->setEnablePrimary(true);
wifi->setSsid1(WLAN_SSID);
wifi->setPassword1(WLAN_PASSWD);
/**
* Create the Elements and register them to the connectors
*/
BoolElement *switch1 = new BoolElement("Switch1");
digitalInConnector->registerElement(switch1, SWITCH_PIN, true);
BoolElement *led1 = new BoolElement("Led1");
digitalOutConnector->registerElement(led1, LED_PIN);
eventManager.addListener<NewBoolValueEvent>(NewBoolValue, "Switch1", [led1](NewBoolValueEvent *event) {
led1->setValue(event->newValue);
});
openIot.setup("OpenIotWifi", 1);
}
void loop() {
openIot.loop();
}
The example demonstrates the use of Wi-Fi.
To try out the example, adjust the definitions of WLAN_SSID
and WLAN_PASSWD
to match your environment.
The Wi-Fi configuration is done using the following lines:
wifi->setEnablePrimary(true);
wifi->setSsid1(WLAN_SSID);
wifi->setPassword1(WLAN_PASSWD);
This enables the primary Wi-Fi and sets the SSID and password for the network.
Once the connection to the Wi-Fi is established, the console will display the following output:
INFO - ---- Wifi settings : ------
INFO - Accesspoint SSID : OPEN-IOT-28486CA4AE30
INFO - Accesspoint Password :
INFO - Accesspoint Open : true
INFO - Wifi Primary Enabled : true
INFO - Primary ssid : iot
INFO - Primary Password : verysecret
INFO - Wifi Seconday Enabled : false
INFO - Secondary ssid :
INFO - Secondary Password :
INFO - ---- Wifi status : ------
INFO - Network : Station Mode
INFO - SSID : IoT
INFO - RSSI : -52
INFO - Channel : 11
INFO - IP address : 10.103.229.124
INFO - Gateway IP address : 10.103.0.1
The device has successfully connected to the Wi-Fi network and is accessible at the IP address 10.103.229.124
. (The network segment 10.103/16
is my iot VLAN.)
| ← Previous Page (Logging) | ↑ Tutorial Main Page | Next Page (MQTT) → |