esp8266 connection works initially then fails

Initially wifi connection for ESP8266 works but then moments later comes up as "WIFI DISCONNECT" and "FAIL" thus can't connect Station to AP with being CWJAP:1,why could this be and possible solutions for this please? Station IP is displayed as 0.0.0.0
I get on station part as
---------- Connecting to AP ----------
ssid = esp8266 pwd =
AT+CWJAP_DEF="esp8266",""
WIFI CONNECTED
AT+CWJAP_DEF="esp826+CWJAP:1
FAIL
WIFI DISCONNECT
6I DISCONNECT
+CWJAP:1
FAIL
WIFI DISCONNECT
---------- Connection Information ----------
AT+CIFSR
+CIFSR:STAIP,"0.0.0.0"
+CIFSR:STAMAC,"60:01:94:09:83:ec"
OK
---------- Connecting to AP ----------
ssid = esp8266 pwd =
AT+CWJAP_DEF="esp8266",""
WIFI CONNECTED
AT+CWJAP_DEF="esp826+CWJAP:1
FAIL
WIFI DISCONNECT
6I DISCONNECT
+CWJAP:1
FAIL
WIFI DISCONNECT
---------- Connection Information ----------
AT+CIFSR
+CIFSR:STAIP,"0.0.0.0"
+CIFSR:STAMAC,"60:01:94:09:83:ec"
OK
I have attached code for softAP and station parts
I get on station part as
---------- Connecting to AP ----------
ssid = esp8266 pwd =
AT+CWJAP_DEF="esp8266",""
WIFI CONNECTED
AT+CWJAP_DEF="esp826+CWJAP:1
FAIL
WIFI DISCONNECT
6I DISCONNECT
+CWJAP:1
FAIL
WIFI DISCONNECT
---------- Connection Information ----------
AT+CIFSR
+CIFSR:STAIP,"0.0.0.0"
+CIFSR:STAMAC,"60:01:94:09:83:ec"
OK
---------- Connecting to AP ----------
ssid = esp8266 pwd =
AT+CWJAP_DEF="esp8266",""
WIFI CONNECTED
AT+CWJAP_DEF="esp826+CWJAP:1
FAIL
WIFI DISCONNECT
6I DISCONNECT
+CWJAP:1
FAIL
WIFI DISCONNECT
---------- Connection Information ----------
AT+CIFSR
+CIFSR:STAIP,"0.0.0.0"
+CIFSR:STAMAC,"60:01:94:09:83:ec"
OK
I have attached code for softAP and station parts
Code: Select all
#include "mbed.h"
#include <stdio.h>
#include <string.h>
Serial serial(USBTX, USBRX);
Serial esp(P4_28, P4_29); // tx, rx
DigitalOut reset(P0_4);
DigitalOut blueLed(P2_3);
DigitalOut greenLed(P2_4);
DigitalOut redLed(P2_5);
DigitalOut vibr(P2_12);
Timer t;
int count,ended,timeout;
char buf[4096];
char snd[255];
char sendStr[11];
char ssid[32] = ""; // enter WiFi router ssid inside the quotes
char pwd [32] = ""; // enter WiFi router password inside the quotes
void SendCMD();
void getReply();
int main() {
//Make sure all feedback mechanisms are turned off
blueLed=0;
greenLed=0;
redLed=0;
vibr=0;
//set buad rates for comms
esp.baud(115200);
serial.baud(115200);
// Setup a serial interrupt function to capture received data
esp.attach(&getReply, Serial::RxIrq);
//Device doesn't appear to need a reset upon waking up so the device is not reset (i.e. reset = 1);
reset=1;
// Give the user some to setup the virtual terminal
wait(5);
serial.printf("\n---------- Clear Buffer ----------\r\n"); //Simple way to clear the buffer
strcpy(snd, "AT+GMR\r\n"); //View version info
timeout=3;
SendCMD();
getReply();
serial.printf(buf);
serial.printf("\n---------- set default mode ----------\r\n"); //
strcpy(snd, "AT+CWMODE_DEF=3\r\n");
timeout=2;
SendCMD();
getReply();
serial.printf(buf); //Prints out response, should be "OK"
serial.printf("\n---------- check mode ----------\r\n"); //
strcpy(snd, "AT+CWMODE?\r\n");
timeout=2;
SendCMD();
getReply();
serial.printf(buf); //Prints out response, should be "OK"
serial.printf("\n---------- Turn off DHCP for Station----------\r\n"); //
strcpy(snd, "AT+CWDHCP_DEF=1,0\r\n");
timeout=2;
SendCMD();
getReply();
serial.printf(buf); //Prints out response, should be "OK"
serial.printf("\n---------- check DHCP settings----------\r\n"); //
strcpy(snd, "AT+CWDHCP?\r\n");
timeout=2;
SendCMD();
getReply();
serial.printf(buf); //Prints out response, should be "OK"
serial.printf("\n---------- Set static Station IP Address ----------\r\n"); //
strcpy(snd, "AT+CIPSTA_DEF=\"192.168.0.5\",\"192.168.4.1\",\"255.255.255.0\"\r\n");
timeout=5;
SendCMD();
getReply();
serial.printf(buf); //Prints out response, should be "OK"
serial.printf("\n---------- check mode ----------\r\n"); //
strcpy(snd, "AT+CIPSTA_DEF?\r\n");
timeout=2;
SendCMD();
getReply();
serial.printf(buf); //Prints out response, should be "OK"
while(1) {
}
}
//This function sends the command string to the ESP8266
void SendCMD()
{
esp.printf("%s", snd);
}
//This function establishes the space for the received string, starts a timer so that if things get stuck, the
//app won't hang.
void getReply()
{
memset(buf, '\0', sizeof(buf));
t.start();
ended=0;
count=0;
while(!ended) {
if(esp.readable()) {
buf[count] = esp.getc();
count++;
}
if(t.read() > timeout) {
ended = 1;
t.stop();
t.reset();
}
}
serial.printf(buf);
}
Code: Select all
#include "mbed.h"
#include <stdio.h>
#include <string.h>
Serial serial(USBTX, USBRX);
Serial esp(P4_28, P4_29); // tx, rx
DigitalOut reset(P0_4);
DigitalOut blueLed(P2_3);
DigitalOut greenLed(P2_4);
DigitalOut redLed(P2_5);
DigitalOut vibr(P2_12);
Timer t;
int count,ended,timeout;
char buf[4096];
char snd[255];
char sendStr[11];
char ssid[32] = "esp8266"; // enter WiFi router ssid inside the quotes
char pwd [32] = ""; // enter WiFi router password inside the quotes
void SendCMD();
void getReply();
DigitalOut myled(LED1);
int main() {
//Make sure all feedback mechanisms are turned off
blueLed=0;
greenLed=0;
redLed=0;
vibr=0;
//set buad rates for comms
esp.baud(115200);
serial.baud(115200);
esp.attach(&getReply, Serial::RxIrq); // This needs to be define only once
reset = 1; // This is pin P0_14 in the Egg.
wait(2);// Give the user some to setup the virtual terminal
serial.printf("\n---------- Clear Buffer ----------\r\n"); //Simple way to clear the buffer
strcpy(snd, "AT+GMR\r\n");
timeout=3;
SendCMD();
getReply();
serial.printf(buf);
serial.printf("\n---------- set default mode ----------\r\n"); //
strcpy(snd, "AT+CWMODE_DEF=1\r\n");
timeout=2;
SendCMD();
getReply();
serial.printf(buf); //Prints out response, should be "OK"
serial.printf("\n---------- check mode ----------\r\n"); //
strcpy(snd, "AT+CWMODE?\r\n");
timeout=2;
SendCMD();
getReply();
serial.printf(buf); //Prints out response, should be "OK"
serial.printf("\n---------- Connecting to AP ----------\r\n");
serial.printf("ssid = %s pwd = %s\r\n",ssid,pwd);
strcpy(snd, "AT+CWJAP_DEF=\"");
strcat(snd, ssid);
strcat(snd, "\",\"");
strcat(snd, pwd);
strcat(snd, "\"\r\n");
timeout=15;
SendCMD();
getReply();
serial.printf(buf);
//Check to see that the connection has been established
serial.printf("\n---------- Connection Information ----------\r\n");
strcpy(snd, "AT+CIFSR\r\n"); //get local IP address
timeout=5;
SendCMD();
getReply();
serial.printf(buf);
while(1) {
}
}
//This function sends the command string to the ESP8266
void SendCMD()
{
esp.printf("%s", snd);
}
//This function establishes the space for the received string, starts a timer so that if things get stuck, the
//app won't hang.
void getReply()
{
memset(buf, '\0', sizeof(buf));
t.start();
ended=0;
count=0;
while(!ended) {
if(esp.readable()) {
buf[count] = esp.getc();
count++;
}
if(t.read() > timeout) {
ended = 1;
t.stop();
t.reset();
}
}
serial.printf(buf);
}