Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Matheus-Garbelini/2cd780aed2eddbe17eb4adb5eca42bd6 to your computer and use it in GitHub Desktop.
Save Matheus-Garbelini/2cd780aed2eddbe17eb4adb5eca42bd6 to your computer and use it in GitHub Desktop.
ESP8266 Wi-Fi WPA2 Enterprise example
#include <ESP8266WiFi.h>
extern "C" {
#include "user_interface.h"
#include "wpa2_enterprise.h"
#include "c_types.h"
}
// SSID to connect to
char ssid[] = "TEST_KRA";
char username[] = "matheus_garbelini";
char identity[] = "matheus_garbelini";
char password[] = "testtest";
uint8_t target_esp_mac[6] = {0x24, 0x0a, 0xc4, 0x9a, 0x58, 0x28};
void setup() {
WiFi.mode(WIFI_STA);
Serial.begin(115200);
delay(1000);
Serial.setDebugOutput(true);
Serial.printf("SDK version: %s\n", system_get_sdk_version());
Serial.printf("Free Heap: %4d\n",ESP.getFreeHeap());
// Setting ESP into STATION mode only (no AP mode or dual mode)
wifi_set_opmode(STATION_MODE);
struct station_config wifi_config;
memset(&wifi_config, 0, sizeof(wifi_config));
strcpy((char*)wifi_config.ssid, ssid);
strcpy((char*)wifi_config.password, password);
wifi_station_set_config(&wifi_config);
wifi_set_macaddr(STATION_IF,target_esp_mac);
wifi_station_set_wpa2_enterprise_auth(1);
// Clean up to be sure no old data is still inside
wifi_station_clear_cert_key();
wifi_station_clear_enterprise_ca_cert();
wifi_station_clear_enterprise_identity();
wifi_station_clear_enterprise_username();
wifi_station_clear_enterprise_password();
wifi_station_clear_enterprise_new_password();
wifi_station_set_enterprise_identity((uint8*)identity, strlen(identity));
wifi_station_set_enterprise_username((uint8*)username, strlen(username));
wifi_station_set_enterprise_password((uint8*)password, strlen((char*)password));
wifi_station_connect();
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
}
@ziogianni
Copy link

ziogianni commented Dec 4, 2022 via email

@ziogianni
Copy link

ziogianni commented Feb 25, 2023 via email

@igurug
Copy link

igurug commented Nov 10, 2023

Thanks a lot! I had so much troubles working with esp8266 in the University. Now the problem is solved

@Teakzieas
Copy link

Teakzieas commented Dec 12, 2023

`#include <ESP8266WiFi.h>

extern "C" {
#include "user_interface.h"
#include "wpa2_enterprise.h"
#include "c_types.h"
}

// SSID to connect to
char ssid[] = "eduroam";
char username[] = "101227402";
char identity[] = "101227402";
char password[] = "passwprd";

uint8_t target_esp_mac[6] = {0x24, 0x0a, 0xc4, 0x9a, 0x58, 0x28};
void setup() {

WiFi.mode(WIFI_STA);
Serial.begin(115200);
delay(1000);
Serial.setDebugOutput(true);
Serial.printf("SDK version: %s\n", system_get_sdk_version());
Serial.printf("Free Heap: %4d\n",ESP.getFreeHeap());

// Setting ESP into STATION mode only (no AP mode or dual mode)
wifi_set_opmode(STATION_MODE);

struct station_config wifi_config;

memset(&wifi_config, 0, sizeof(wifi_config));
strcpy((char*)wifi_config.ssid, ssid);
strcpy((char*)wifi_config.password, password);

wifi_station_set_config(&wifi_config);
wifi_set_macaddr(STATION_IF,target_esp_mac);

wifi_station_set_wpa2_enterprise_auth(1);

// Clean up to be sure no old data is still inside
wifi_station_clear_cert_key();
wifi_station_clear_enterprise_ca_cert();
wifi_station_clear_enterprise_identity();
wifi_station_clear_enterprise_username();
wifi_station_clear_enterprise_password();
wifi_station_clear_enterprise_new_password();

wifi_station_set_enterprise_identity((uint8*)identity, strlen(identity));
wifi_station_set_enterprise_username((uint8*)username, strlen(username));
wifi_station_set_enterprise_password((uint8*)password, strlen((char*)password));

wifi_station_clear_enterprise_cert_key();
wifi_station_connect();
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}

Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void loop() {
}`
####################################################

I am trying to connect to eduroam in my university,
It uses MSCHAPV2 as the Phase 2 Authentication,
And do not Validate the Ca Certificate

I keep getting the error below and it loops forever,
.reconnect
...scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 0 (12)
.reconnect
...scandone
state: 0 -> 2 (b0)

If anyone can please guide me or link me to any post to solve this issue, I really do appreciate it very much.

@venetanji
Copy link

@Teakzieas did you try my version? works ok on my uni's eduroam:

https://gist.github.com/venetanji/d71dc271ebf51236ec6ce99aa48eee26

One difference with your code is I don't set the password with:

strcpy((char*)wifi_config.password, password);

Try commenting that line as it is for regular wpa. The wpa2 enterprise password is actually set with this line below:

wifi_station_set_enterprise_password((uint8*)password, strlen((char*)password));

@Teakzieas
Copy link

Teakzieas commented Dec 13, 2023

#include <ESP8266WiFi.h>

// include wpa2 enterprise code
extern "C" {
#include "user_interface.h"
#include "wpa2_enterprise.h"
}

// SSID, Username and password. Update with yours!

static const char* ssid = "eduroam";
static const char* username = "101227402"; //
static const char* password = "password";

void setup() {

Serial.begin(115200);
delay(1000);
Serial.setDebugOutput(true);
// Setting ESP into STATION mode only (no AP mode or dual mode)
wifi_set_opmode(STATION_MODE);

struct station_config wifi_config;

memset(&wifi_config, 0, sizeof(wifi_config));
strcpy((char*)wifi_config.ssid, ssid);

wifi_station_set_config(&wifi_config);

// Clean up to be sure no old data is still inside
wifi_station_clear_cert_key();
wifi_station_clear_enterprise_ca_cert();

wifi_station_set_wpa2_enterprise_auth(1);

wifi_station_set_enterprise_username((uint8*)username, strlen(username));
wifi_station_set_enterprise_identity((uint8*)username, strlen(username));
wifi_station_set_enterprise_password((uint8*)password, strlen(password));

wifi_station_connect();

while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}

Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

}

void loop() {
}

##############################################
i did try your code but i endup in the same place
.reconnect
...scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 0 (12)
.reconnect

Could it be the Version of Nodemcu I am using(NodemcuV2)

@setri100
Copy link

setri100 commented Jan 2, 2024

Hey, thanks for the examples, your code works like a charm when I connect one ESP8266 "Amica" NodeMCU to university eduroam. I am sending IOT data to thingspeak.
Weird issue, whenever I connect a second NodeMCU with the same code (sending to a different thingspeak channel), the first one stops sending data. Might there be an issue with conflicting IP addresses?

@igurug
Copy link

igurug commented Feb 27, 2024

Hey, thanks for the examples, your code works like a charm when I connect one ESP8266 "Amica" NodeMCU to university eduroam. I am sending IOT data to thingspeak. Weird issue, whenever I connect a second NodeMCU with the same code (sending to a different thingspeak channel), the first one stops sending data. Might there be an issue with conflicting IP addresses?

It's that part where the MAC is assigned. You've assigned the same MAC to different devices. When one is trying to connect, the other one is kicked from the network. So just change the MAC on one of the devices

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment