Skip to content

WiFiSTAClass #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: Unit Test Results
on:
workflow_run:
workflows: [Run tests in hardware]
branches-ignore: [master]

types:
- completed
Expand All @@ -11,6 +12,9 @@ jobs:
unit-test-results:
name: Unit Test Results
runs-on: ubuntu-latest
if: |
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion != 'skipped'
steps:
- name: Download and Extract Artifacts
env:
Expand Down
1 change: 1 addition & 0 deletions libraries/AsyncUDP/src/AsyncUDP.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "IPAddress.h"
#include "IPv6Address.h"
#include "Print.h"
#include "Stream.h"
#include <functional>
extern "C" {
#include "lwip/ip_addr.h"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
#include <WiFi.h> //Wifi library
#include "esp_wpa2.h" //wpa2 library for connections to Enterprise networks
#define EAP_IDENTITY "login" //if connecting from another corporation, use [email protected] in Eduroam
#define EAP_USERNAME "login" //oftentimes just a repeat of the identity
#define EAP_PASSWORD "password" //your Eduroam password
const char* ssid = "eduroam"; // Eduroam SSID
const char* host = "arduino.php5.sk"; //external server domain for HTTP connection after authentification
int counter = 0;

// NOTE: For some systems, various certification keys are required to connect to the wifi system.
// Usually you are provided these by the IT department of your organization when certs are required
// and you can't connect with just an identity and password.
// Most eduroam setups we have seen do not require this level of authentication, but you should contact
// your IT department to verify.
// You should uncomment these and populate with the contents of the files if this is required for your scenario (See Example 2 and Example 3 below).
//const char *ca_pem = "insert your CA cert from your .pem file here";
//const char *client_cert = "insert your client cert from your .crt file here";
//const char *client_key = "insert your client key from your .key file here";

void setup() {
Serial.begin(115200);
delay(10);
Expand All @@ -13,11 +25,17 @@ void setup() {
Serial.println(ssid);
WiFi.disconnect(true); //disconnect form wifi to set new wifi connection
WiFi.mode(WIFI_STA); //init wifi mode
esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide identity
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_IDENTITY, strlen(EAP_IDENTITY)); //provide username --> identity and username is same
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD)); //provide password
esp_wifi_sta_wpa2_ent_enable();
WiFi.begin(ssid); //connect to wifi

// Example1 (most common): a cert-file-free eduroam with PEAP (or TTLS)
WiFi.begin(ssid, WPA2_AUTH_PEAP, EAP_IDENTITY, EAP_USERNAME, EAP_PASSWORD);

// Example 2: a cert-file WPA2 Enterprise with PEAP
//WiFi.begin(ssid, WPA2_AUTH_PEAP, EAP_IDENTITY, EAP_USERNAME, EAP_PASSWORD, ca_pem, client_cert, client_key);

// Example 3: TLS with cert-files and no password
//WiFi.begin(ssid, WPA2_AUTH_TLS, EAP_IDENTITY, NULL, NULL, ca_pem, client_cert, client_key);


while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
Expand Down
62 changes: 62 additions & 0 deletions libraries/WiFi/src/WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ extern "C" {
#include "lwip/dns.h"
#include <esp_smartconfig.h>
#include <esp_netif.h>
#include "esp_wpa2.h"
}

// -----------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -145,6 +146,67 @@ wl_status_t WiFiSTAClass::status()
return (wl_status_t)xEventGroupClearBits(_sta_status_group, 0);
}

/**
* Start Wifi connection with a WPA2 Enterprise AP
* if passphrase is set the most secure supported mode will be automatically selected
* @param ssid const char* Pointer to the SSID string.
* @param method wpa2_method_t The authentication method of WPA2 (WPA2_AUTH_TLS, WPA2_AUTH_PEAP, WPA2_AUTH_TTLS)
* @param wpa2_identity const char* Pointer to the entity
* @param wpa2_username const char* Pointer to the username
* @param password const char * Pointer to the password.
* @param ca_pem const char* Pointer to a string with the contents of a .pem file with CA cert
* @param client_crt const char* Pointer to a string with the contents of a .crt file with client cert
* @param client_key const char* Pointer to a string with the contants of a .key file with client key
* @param bssid uint8_t[6] Optional. BSSID / MAC of AP
* @param channel Optional. Channel of AP
* @param connect Optional. call connect
* @return
*/
wl_status_t WiFiSTAClass::begin(const char* wpa2_ssid, wpa2_auth_method_t method, const char* wpa2_identity, const char* wpa2_username, const char *wpa2_password, const char* ca_pem, const char* client_crt, const char* client_key, int32_t channel, const uint8_t* bssid, bool connect)
{
if(!WiFi.enableSTA(true)) {
log_e("STA enable failed!");
return WL_CONNECT_FAILED;
}

if(!wpa2_ssid || *wpa2_ssid == 0x00 || strlen(wpa2_ssid) > 32) {
log_e("SSID too long or missing!");
return WL_CONNECT_FAILED;
}

if(wpa2_identity && strlen(wpa2_identity) > 64) {
log_e("identity too long!");
return WL_CONNECT_FAILED;
}

if(wpa2_username && strlen(wpa2_username) > 64) {
log_e("username too long!");
return WL_CONNECT_FAILED;
}

if(wpa2_password && strlen(wpa2_password) > 64) {
log_e("password too long!");
}

if(ca_pem) {
esp_wifi_sta_wpa2_ent_set_ca_cert((uint8_t *)ca_pem, strlen(ca_pem));
}

if(client_crt) {
esp_wifi_sta_wpa2_ent_set_cert_key((uint8_t *)client_crt, strlen(client_crt), (uint8_t *)client_key, strlen(client_key), NULL, 0);
}

esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)wpa2_identity, strlen(wpa2_identity));
if(method == WPA2_AUTH_PEAP || method == WPA2_AUTH_TTLS) {
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)wpa2_username, strlen(wpa2_username));
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)wpa2_password, strlen(wpa2_password));
}
esp_wifi_sta_wpa2_ent_enable(); //set config settings to enable function
WiFi.begin(wpa2_ssid); //connect to wifi

return status();
}

/**
* Start Wifi connection
* if passphrase is set the most secure supported mode will be automatically selected
Expand Down
6 changes: 6 additions & 0 deletions libraries/WiFi/src/WiFiSTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
#include "esp_event.h"
#endif

typedef enum {
WPA2_AUTH_TLS = 0,
WPA2_AUTH_PEAP = 1,
WPA2_AUTH_TTLS = 2
} wpa2_auth_method_t;

class WiFiSTAClass
{
Expand All @@ -39,6 +44,7 @@ class WiFiSTAClass

public:

wl_status_t begin(const char* wpa2_ssid, wpa2_auth_method_t method, const char* wpa2_identity=NULL, const char* wpa2_username=NULL, const char *wpa2_password=NULL, const char* ca_pem=NULL, const char* client_crt=NULL, const char* client_key=NULL, int32_t channel=0, const uint8_t* bssid=0, bool connect=true);
wl_status_t begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
wl_status_t begin(char* ssid, char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true);
wl_status_t begin();
Expand Down
11 changes: 5 additions & 6 deletions variants/adafruit_feather_esp32_v2/pins_arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,19 @@ static const uint8_t A9 = 33;
static const uint8_t A10 = 27;
static const uint8_t A11 = 12;
static const uint8_t A12 = 13;
static const uint8_t A13 = 35;

// vbat measure
static const uint8_t BATT_MONITOR = 35;
static const uint8_t A13 = 35;
#define BATT_MONITOR 35

// internal switch
static const uint8_t BUTTON = 38;
#define BUTTON = 38;

// Neopixel
static const uint8_t NEOPIXEL_PIN = 0;
static const uint8_t PIN_NEOPIXEL = 0;
#define PIN_NEOPIXEL 0

// Neopixel & I2C power
static const uint8_t NEOPIXEL_I2C_POWER = 2;
#define NEOPIXEL_I2C_POWER 2

static const uint8_t T0 = 4;
static const uint8_t T1 = 0;
Expand Down
9 changes: 5 additions & 4 deletions variants/adafruit_feather_esp32s2/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ void initVariant(void)
pinMode(NEOPIXEL_POWER, OUTPUT);
digitalWrite(NEOPIXEL_POWER, HIGH);

// This board has a power control pin, and we must set it to output and low
// in order to enable the I2C port.
// turn on the I2C power by setting pin to opposite of 'rest state'
pinMode(PIN_I2C_POWER, INPUT);
delay(1);
bool polarity = digitalRead(PIN_I2C_POWER);
pinMode(PIN_I2C_POWER, OUTPUT);
digitalWrite(PIN_I2C_POWER, LOW);
digitalWrite(PIN_I2C_POWER, !polarity);
}

}
4 changes: 2 additions & 2 deletions variants/adafruit_qtpy_esp32c3/pins_arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#define digitalPinToInterrupt(p) (((p)<NUM_DIGITAL_PINS)?(p):-1)
#define digitalPinHasPWM(p) (p < EXTERNAL_NUM_INTERRUPTS)

static const uint8_t SWITCH = 9;
static const uint8_t NEOPIXEL_PIN = 2;
#define BUTTON 9
#define PIN_NEOPIXEL 2

static const uint8_t TX = 21;
static const uint8_t RX = 20;
Expand Down