ESP8266 Projects

How to connect an ESP8266 or ESP32 to the Internet

With ESP8266 or ESP32 to the Internet-2

Push the door to the Internet of Things wide open! If you need a connection to the Internet for your project, the two controllers ESP8266* and ESP32* will serve you well. All you need is a Wi-Fi network, the appropriate access data and a few lines of code.

Before you can get started, you need to install your ESP8266 or ESP32 in your Arduino IDE. You can find out how to do this in this tutorial on heise.de.

The right library

Depending on whether you want to go online with an ESP8266 or an ESP32, you will need a suitable library. For the former, this is ESP8266WiFi, for the ESP32, the library WiFi.h

Both libraries should already be available in your Arduino IDE – if you have installed your board there. You can include them as follows at the beginning of your sketch – even before the setup:

#include <ESP8266WiFi.h>
#include <WiFi.h>

Your access data

Both libraries work in a similar way. Before your controller can connect to your Wi-Fi network, it needs the correct access data. You can also define these credentials at the beginning of your sketch, for example in unchangeable constants:

const char* ssid = "Name deines WLAN-Netzwerks";
const char* password =  "Dein WLAN-Passwort";

And off to the Internet!

Now you are ready to start. There are several ways to set up the connection and display it in the Serial Monitor. However, the WiFi.begin() function is always central, and that it is executed in the setup of your sketch at best, so that everything is prepared for the loop.

This is how the connection setup could look like:

void setup() {
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Ich verbinde mich mit dem Internet...");
  }
  Serial.println("Ich bin mit dem Internet verbunden!");
}

First the function WiFi.begin(), which you give your access data as arguments. The following While Loop is executed as long as the connection to the internet is not yet established (WiFi.status() != WL_CONNECTED) and writes every second into the serial monitor that the connection is being set up. As soon as the connection is established you will get the success message in the Serial Monitor.

And that’s it, you’re in! 🙂

Exciting projects

Now that your connection to the Internet is up and running, why not try them out with these projects:

Letzte Aktualisierung am 2021-01-09 / Affiliate Links / Bilder von der Amazon Product Advertising API

You may also like

Comments are closed.