Skip to main content
Intro to IoT Class Docs
Tech TLH Discord Code and Coffee TLH GitHub Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Connecting To WiFi

Start From The Beginning

When you first open Arduino IDE, things are pretty stark but give us a little scaffolding to build a new project.

1
2
3
4
5
6
7
8
9
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Instructing The Compiler

The compiler needs to know two things:

  • Where to look to find WiFi code, and
  • What to use to connect to an access point.

#include statements tell the compiler to look through its notes to see where it put code for something. Header files keep those notes, and serve as a reference for the full code that needs to be stuffed in the compiled binary.

#define macros are hard-coded variables that the compiler uses to copy and paste into the final code before it’s compiled. They’re not always the best approach, but appear often in embedded code like ours and serve well for a prototype since they stand out clearly.

Let’s add an #include reference for WiFi and #define statements for connecting it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <WiFi.h>

#define WIFI_SSID "code"
#define WIFI_PASS "andcoffee"

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

A Basic Connection

The setup() method in the sketch tells the board what to do when it first starts. There’s two things we need it to do:

  • Connect to WiFi, and
  • Tell us that it’s doing something so we know what’s going on.

Connecting is easy: that’s one line. Talking about it gets chatty, though.

To keep setup() short and neat, we’re going to move a lot of that to a new method called connect(). This will be useful later on when it needs to run unattended and reconnect on its own.

Note that connect() is placed above setup(): the compiler reads through code like we read a book, and needs to know about it before it reaches the next chapter. It’s just going to print a bunch of dots while it’s waiting for a connection, and then tell us the IP address it gets from the access point.

Debug statements over USB are handled by Serial. It needs a fraction of a second to make sure it sets up properly, so we’ll add a small delay, too. print tells it out type out something and println tells it to hit enter when done.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <WiFi.h>

#define WIFI_SSID "code"
#define WIFI_PASS "andcoffee"

void connect() {
  Serial.print("connecting to wifi...");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(1000); // 1 second delay
  }
  Serial.println("done.");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(10); // 10 millisecond delay
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  connect();
}

void loop() {
  // put your main code here, to run repeatedly:

}

Ready To Burn

Our code isn’t elegant, but we’re throwing together a prototype to get it out quickly.

With the board plugged in to USB, go ahead and upload it.

Ship It!
Ship It!

Compiling will take a minute, but fortunately most of the code is already written for us. When it’s through uploading, open the serial plotter (magnifying glass, top right). You should see it pumping out dots until it connects to WiFi.

Good Job, Board.
Good Job, Board.

That is good enough for now.

Next Up: Getting The Time