Initial commit

This commit is contained in:
2024-12-21 19:54:30 +01:00
commit b813bdcf99
9 changed files with 319 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@@ -0,0 +1,6 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
settings.h.my-ignore

10
.vscode/extensions.json vendored Normal file
View File

@@ -0,0 +1,10 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"platformio.platformio-ide"
],
"unwantedRecommendations": [
"ms-vscode.cpptools-extension-pack"
]
}

41
README.md Normal file
View File

@@ -0,0 +1,41 @@
# WLAN Beispiel für ESP32
Dieses Beispiel-Skript ermöglicht es, mehrere SSIDs anzugeben, die der Reihe nach durchprobiert werden. Dabei wird **nicht** das stärkste Signal bevorzugt, sondern es wird strikt der erste WLAN-Zugangspunkt (SSID) verwendet, der gefunden wird. Bei Verlust wird die Verbindungssuche erneut gestartet. Während die Verbindung aufgebaut wird werden vortlaufen Informationen zum Status auf dem Display und am seriellen Monitor angezeigt.
## Funktionsweise
- Das Skript geht alle angegebenen SSIDs in der Reihenfolge durch, in der sie im Code hinterlegt sind.
- Sobald ein WLAN-Netzwerk gefunden wird, wird sofort eine Verbindung zu diesem hergestellt.
- Das Beispiel-Skript ist besonders hilfreich, wenn es eine visuelle Rückmeldung über den Verbindungsstatus geben soll (z.B. über ein LCD-Display), während die verschiedenen Netzwerke ausprobiert werden.
- Im Gegensatz zur Standard-MultiWiFi-Library, die mehr wie eine Blackbox funktioniert und nicht viel Feedback während des Verbindungsprozesses bietet, ermöglicht dieses Beispiel, die Verbindungsschritte sichtbar zu machen und zu steuern.
## Konfiguration
Passe die `settings.h` auf deine Bedürfnisse an
```cpp
// ######################## WLAN-Daten ########################
const char* hostname = "ESP32_WLAN_Test";
const char* ssidList[] = {
"SSID_1", // Netzwerk 1
"SSID_2", // Netzwerk 2
"SSID_3" // Netzwerk 3
// Weitere SSID hier hinzufügen
};
const char* passwordList[] = {
"Passwort_1", // Passwort für SSID 1
"Passwort_2", // Passwort für SSID 2
"Passwort_3" // Passwort für SSID 3
// Weitere Passwörter hier hinzufügen
};
// ############################################################
```
## Vorteile dieser Methode
- Flexibilität: Du kannst beliebig viele SSIDs hinzufügen, die dein ESP32 nacheinander durchsuchen wird.
- Visualisierung des Prozesses: Während der Verbindung wird der Status z.B. auf einem Display oder durch LEDs angezeigt.
- Einfache Integration: Der Code kann leicht mit weiteren Funktionen wie MQTT, HTTP-Requests oder Sensoren kombiniert werden.

39
include/README Normal file
View File

@@ -0,0 +1,39 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

46
lib/README Normal file
View File

@@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into executable file.
The source code of each library should be placed in an own separate directory
("lib/your_library_name/[here are source files]").
For example, see a structure of the following two libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

19
platformio.ini Normal file
View File

@@ -0,0 +1,19 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:nodemcu-32s]
platform = espressif32
board = nodemcu-32s
framework = arduino
monitor_speed = 921600
lib_deps =
HTTPClient
LiquidCrystal_I2C
WiFi

123
src/main.cpp Normal file
View File

@@ -0,0 +1,123 @@
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <settings.h>
int numNetworks = sizeof(ssidList) / sizeof(ssidList[0]); // Anzahl der Netzwerke
int current_ap = 0; // Start mit der ersten SSID
bool reconnecting = false; // Flag für den Reconnect-Status
// LCD initialisieren (16x2 Display)
LiquidCrystal_I2C lcd(0x27, 16, 2);
// WLAN Symbol
byte customChar_wlan[] = {
B00000,
B01110,
B10001,
B00100,
B01010,
B00000,
B00100,
B00000
};
// Funktion, um zu einem der Netzwerke zu verbinden
void connectToWiFi() {
Serial.println("WLAN Verbindungsaufbau zu: " + String(ssidList[current_ap]));
lcd.setCursor(0, 0);
lcd.print(" Bitte warten ");
lcd.setCursor(0, 1);
lcd.print("WLAN verbinden ");
lcd.createChar(0, customChar_wlan); // WLAN-Symbol anzeigen
// WLAN-Symbol anzeigen
lcd.setCursor(15, 1); // Zum Beispiel in der rechten oberen Ecke
lcd.write(byte(0)); // Das Symbol mit Code 0 anzeigen
// Verbinde mit dem aktuellen AP (SSID und Passwort aus den Arrays)
WiFi.begin(ssidList[current_ap], passwordList[current_ap]);
// Warte auf die Verbindung
int attempt = 0;
while (WiFi.status() != WL_CONNECTED && attempt < 10) {
delay(250);
Serial.print("."); // Punkt ausgeben, um anzuzeigen, dass es versucht, sich zu verbinden
lcd.setCursor(15, 1);
lcd.print(" ");
delay(250);
lcd.setCursor(15, 1);
lcd.write(byte(0));
attempt++;
}
// Wenn keine Verbindung hergestellt werden konnte, wechsle zum nächsten Netzwerk
if (WiFi.status() != WL_CONNECTED) {
WiFi.disconnect();
delay(500);
Serial.println("");
Serial.println("Verbindung fehlgeschlagen.");
current_ap = (current_ap + 1) % numNetworks; // Wechsel zur nächsten SSID, nach der letzten wieder zur ersten
reconnecting = false; // Zurücksetzen des Flags, falls alle Netzwerke fehlgeschlagen sind
}
else {
Serial.print("\nWLAN verbunden: ");
Serial.print(WiFi.SSID());
Serial.print(" ");
Serial.println(WiFi.RSSI());
lcd.setCursor(0, 0);
lcd.print(" WLAN verbunden ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(WiFi.SSID());
reconnecting = false; // Verbindung erfolgreich, setze reconnecting zurück
delay(2500);
}
}
void setup() {
Serial.begin(921600); // Baudrate auf 921600 gesetzt
delay(10);
lcd.init();
lcd.backlight();
Serial.println("Bereit");
WiFi.mode(WIFI_STA); // ESP32 im STA-Modus (Client-Modus)
WiFi.setHostname(hostname); // Setze Hostnamen
}
void loop() {
// Prüfe, ob die Verbindung verloren wurde
if (WiFi.status() != WL_CONNECTED && !reconnecting) {
Serial.println("WLAN Verbindung verloren. Versuche erneut zu verbinden...");
reconnecting = true; // Setze Flag, dass ein Reconnectversuch läuft
connectToWiFi(); // Versuche eine neue Verbindung
}
// Wenn alle Netzwerke durchlaufen wurden und noch keine Verbindung besteht, setze das reconnecting-Flag zurück
if (WiFi.status() != WL_CONNECTED && current_ap == 0) {
reconnecting = false;
}
delay(1000);
}

24
src/settings.h Normal file
View File

@@ -0,0 +1,24 @@
#ifndef SETTINGS_H
#define SETTINGS_H
// ######################## WLAN-Daten ########################
const char* hostname = "ESP32_WLAN_Test";
const char* ssidList[] = {
"SSID_1", // Netzwerk 1
"SSID_2", // Netzwerk 2
"SSID_3" // Netzwerk 3
// Weitere SSID hier hinzufügen
};
const char* passwordList[] = {
"Passwort_1", // Passwort für SSID 1
"Passwort_2", // Passwort für SSID 2
"Passwort_3" // Passwort für SSID 3
// Weitere Passwörter hier hinzufügen
};
// ############################################################
#endif // SETTINGS_H

11
test/README Normal file
View File

@@ -0,0 +1,11 @@
This directory is intended for PlatformIO Test Runner and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html