Skip to main content

IoT Lab - Wi-Fi Connection

The controller board that we're currently using (ESP32) has built-in Wi-Fi adapter which able to 2.4GHz WPA Wi-Fi. Connecting Wi-Fi can extend more use cases of the board by connecting, sending and receiving data from the internet.

In this workshop, we are going to send the measured humidity and temperature sensor to Node.js server and display the information of  humidity/temperature log to the webpage.

Connecting to Wi-Fi

We have provisioned Wi-Fi network for you to use with your controller board:

  • SSID: DSI-IoT
  • Password: 1212312121

To connect Wi-Fi from your board, try the code example by searching:

  • esp32 wifi
  • esp32 wifi code example

And you will get code something like:

#include<WiFi.h>

#define WIFI_STA_NAME "Wi-Fi SSID"
#define WIFI_STA_PASS "Wi-Fi Password"
#define LED_BUILTIN 2

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WIFI_STA_NAME);

  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_STA_NAME, WIFI_STA_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
  }

  digitalWrite(LED_BUILTIN, HIGH);
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() { }

The code above do the following functions:

  1. Define constant variable WIFI_AP_NAME to value of SSID. Example: #define WIFI_STA_NAME "DSI-IoT".
  2. Define constant variable WIFI_STA_PASS to value of Wi-Fi password.
  3. Ln 16: Set Wi-Fi mode to Wi-Fi station mode (Wi-Fi client for connecting to the server)
  4. Ln 17: Connect Wi-Fi to the defined constant variable Wi-Fi and password
  5. Ln 19-23: Loop until the Wi-Fi status is connected and then output the IP address to serial moniter.