esp8266 / esp8266/Arduino

Client not reconnecting to AP (AP ISSUE)

Open
#5,950 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
16.7k
Forks
13.1k
PR merge metrics
No merged PRs in 30d

Description

I have an AP and Client using the wifi library and communicates over UDP. The code runs on first upload to both AP and Client, but as soon as I disconnect the AP from power or restart the board, the client can't connect anymore. I know the issue isn't the Client because I can disconnet the Client from power or restart the board and it will reconnect everytime.

Can someone explain why I need to upload the code everytime I turn off/ reboot the AP?

**AP Code:**
```
#include
#include

const char *ssid = "CoffeeMaker8266";
const char *pass = "CoffeePassword";

unsigned int localPort = 2000; // local port to listen for UDP packets

IPAddress ServerIP(192, 168, 4, 1);
IPAddress ClientIP(192, 168, 4, 2);

// A UDP instance to let us send and receive packets over UDP
WiFiUDP udp;

char packetBuffer[255]; //buffer to hold incoming packet
char ReplyBuffer[] = "L";

int sw1 = 0;
int BS = LOW;

void setup()
{
Serial.begin(9600);
Serial.println();
WiFi.softAP(ssid, pass); //Create Access point

//Start UDP
Serial.println("Starting UDP");
udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(udp.localPort());

pinMode(sw1, INPUT);
}

void loop()
{

int packetSize = udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
//If serial data is recived send it to UDP
int len = udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
delay(20);
Serial.println("Contents:");
Serial.println(packetBuffer);
}
delay(20);
BS = digitalRead(sw1);
delay(20);
if (BS == HIGH) {
strcpy(ReplyBuffer, "\0");
strcpy(ReplyBuffer, "L");
} else {
strcpy(ReplyBuffer, "\0");
strcpy(ReplyBuffer, "H");
}
//Serial.println(ReplyBuffer);
//data sent to client
delay(30);

udp.beginPacket(ClientIP, 2000);
udp.write(ReplyBuffer);
udp.endPacket();
}
```
**Client Code:**
```
#include
#include

const char *ssid = "CoffeeMaker8266";
const char *pass = "CoffeePassword";

unsigned int localPort = 2000; // local port to listen for UDP packets

IPAddress ServerIP(192, 168, 4, 1);
IPAddress ClientIP(192, 168, 4, 2);

// A UDP instance to let us send and receive packets over UDP
WiFiUDP udp;

char packetBuffer[255]; //buffer to hold incoming packet
char ReplyBuffer[] = "Client";

int LED_BUILTINR = 5;
//======================================================================
// Setup
//======================================================================
void setup()
{

Serial.begin(9600);
Serial.println();

WiFi.begin(ssid, pass); //Connect to access point

Serial.println("");

// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());

//Start UDP
Serial.println("Starting UDP");
udp.begin(localPort);
Serial.print("Local port: ");
Serial.println(udp.localPort());

pinMode(LED_BUILTIN, OUTPUT);
pinMode(LED_BUILTINR, OUTPUT);
}
//======================================================================
// MAIN LOOP
//======================================================================
void loop()
{

int packetSize = udp.parsePacket();
if (packetSize) {
//Serial.print("Received packet of size ");
//Serial.println(packetSize);
//If serial data is recived send it to UDP
int len = udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
delay(20);
Serial.println("Contents:");
Serial.println(packetBuffer);
if (packetBuffer[0] == 'L') {
delay(20);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(LED_BUILTINR, LOW);
} else {
delay(20);
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(LED_BUILTINR, HIGH);
}
delay(20);
udp.beginPacket(ServerIP, 2000);
udp.write(ReplyBuffer);
udp.endPacket();
}
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.