WPA2 Enterprise Connection with Static IP

itscarolinep
Posts: 1
Joined: Wed Aug 22, 2018 2:05 pm

WPA2 Enterprise Connection with Static IP

Postby itscarolinep » Wed Aug 22, 2018 5:07 pm

Hi everyone! I'm new to the forum and to ESP32.

Currently, I'm trying to connect my ESP32 to a WPA2 Enterprise Network using static IP. I'm using this code as basis:

Code: Select all

#include "esp_wpa2.h"
#include <WiFi.h>

#define LED_BUILTIN 2

const char* ssid     		= "mySSID";
#define EAP_ID         		"myID"
#define EAP_USERNAME   	"myUsername"
#define EAP_PASSWORD   	"myPassword"
const char* host     		= "example.com";
const char* url      		= "/index.html";

IPAddress local_IP(192, 168, 1, 100);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional

void setup(){
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);

  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("STA Failed to configure");
  }

  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.disconnect(true);
  WiFi.mode(WIFI_STA);
  esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_ID, strlen(EAP_ID));
  esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_USERNAME, strlen(EAP_USERNAME));
  esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD));
  esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();
  esp_wifi_sta_wpa2_ent_enable(&config);
  
  
  WiFi.begin(ssid);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    //Serial.print(WiFi.status());
  }

  Serial.println("");
  Serial.println("WiFi connected!");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.print("ESP Mac Address: ");
  Serial.println(WiFi.macAddress());
  Serial.print("Subnet Mask: ");
  Serial.println(WiFi.subnetMask());
  Serial.print("Gateway IP: ");
  Serial.println(WiFi.gatewayIP());
  Serial.print("DNS: ");
  Serial.println(WiFi.dnsIP());
  Serial.println();
}

void loop(){
  delay(5000);

  Serial.print("Connecting to ");
  Serial.println(host);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }

  Serial.print("Requesting URL: ");
  Serial.println(url);

  // This will send the request to the server
  digitalWrite(LED_BUILTIN, HIGH);
  // delay(5000);
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }

  // Read all the lines of the reply from server and print them to Serial
  while (client.available()) {
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }

  Serial.println();
  Serial.println("closing connection");
  digitalWrite(LED_BUILTIN, LOW);
  Serial.println();
}
At first, I tried only the WiFi connection with static IP and it worked fine. Then I added the WPA2 Enterprise code and the connection never happens. It gets stuck on:

Code: Select all

Connecting to mySSID
.............................
I also tried the connection without static IP but then I get "Guru Meditation Error", which I solved with the line WiFi.mode(WIFI_STA). And then it goes back to never connecting to the WiFi again.

I've looked another topics here such as this one viewtopic.php?f=2&t=3108&hilit=wpa2 but no success.

Does anyone have any idea on what can I do here? Thanks.

Who is online

Users browsing this forum: No registered users and 82 guests