Skip to content
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

WEP/WPA-PSK on old router and STA to AP switch #3132

Closed
maxdd opened this issue Apr 11, 2017 · 1 comment
Closed

WEP/WPA-PSK on old router and STA to AP switch #3132

maxdd opened this issue Apr 11, 2017 · 1 comment

Comments

@maxdd
Copy link

maxdd commented Apr 11, 2017

Basic Infos

Hardware

Hardware: Amica NodeMCU ESP8266

Description

Switching from AP to STA and directly connecting to STA is not working if the router is in WPA-PSK/WEP?
Dots everywhere.
Basic explanation of the code:

  1. The ESP8266 check if there is a known connection <--if auth is passed immediately at the beginning it still keeps showing dots
  2. If there isn't, starts an AP mode and open a UDP sockect
  3. It waits for SSID and PW to be populated
  4. After population it tries to connect to the given network <-- auth is correct but it keeps showing dots

A wifi scan from the module and a compare with SSID retrieved from UDP returns true, hence the SSID is correctly found. I'm trying both with WEP and WPA-PSK

Settings in IDE

Module: NodeMCU ESP8266 Module
CPU Frequency: 80Mhz

Sketch

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

WiFiUDP UDPTestServer;
unsigned int UDPPort = 1337;

byte packetBuffer[256];
String SSID = "";
String PW = "";
String TOKEN = "asdasd";

bool sent = false;
bool initStatus = true;

void setup() {

  Serial.begin(115200);
  delay(1000);
  Serial.printf("\nTry connecting to WiFi with SSID '%s'\n", WiFi.SSID().c_str());

  WiFi.mode(WIFI_STA);
  WiFi.begin(WiFi.SSID().c_str(), WiFi.psk().c_str()); // reading data from EPROM, last saved credentials

  /* Sometimes it doesn't connect and it keeps . . . . . . . . . . . . indefinitely
    if (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }*/

  if (WiFi.status() == WL_CONNECTED) {
    Serial.printf("Connected successfully to SSID '%s'\n", WiFi.SSID().c_str());
  } else {
    WifiAuthAndTokenExchange();
  }

  UDPTestServer.begin(UDPPort);
}

bool WifiAuthAndTokenExchange() {

  Serial.printf("\nCannot connect, starting AP mode\n");
  WiFi.disconnect();
  WiFi.mode(WIFI_AP);
  boolean result = WiFi.softAP("Test", "Test");

  UDPTestServer.begin(UDPPort);
  Serial.printf("UDP Server started\n");

  //wait for UDP to populate SSID and PW
  while (SSID.length() <= 0 || PW.length() <= 0 || TOKEN.length() <= 0) {
    //    Serial.printf("Waiting for Auth..\n");
    handleUDPServer();
    //    Serial.printf("SSID '%s', PW '%s', TOKEN '%s'", SSID.c_str(), PW.c_str(), TOKEN.c_str());
    delay(10);
  }

  Serial.printf("Auth received\n");
  
  UDPTestServer.stop();
  Serial.printf("UDP Server stopped\n");

  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID.c_str(), PW.c_str());
  Serial.printf("Try connecting to WiFi with SSID '%s'\n", SSID.c_str());

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

void loop() {

  if (WiFi.status() != WL_CONNECTED) {
    //reset micro
  }

  handleUDPServer();

  if (!sent && !initStatus) {
    sendNotificationToDevice();
    Serial.println("Sent");
    sent = true;
  }
}

void sendNotificationToDevice() {
//....
  
}
This was referenced Apr 11, 2017
@devyte
Copy link
Collaborator

devyte commented Nov 8, 2019

This issue is old. A lot has changed since it was reported, including fixes that are relevant.
Closing.
If you think the problem still exists, please open a new issue and follow the issue template instructions.

@devyte devyte closed this as completed Nov 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants