AT+CWJAP_DEF disconnects and so Station IP doesn't display

sc00486
Posts: 1
Joined: Sun Apr 02, 2017 11:53 pm

AT+CWJAP_DEF disconnects and so Station IP doesn't display

Postby sc00486 » Mon Apr 03, 2017 12:09 am

I've been trying to get station to connect to AP with esp8266.but with command AT+CWJAP_DEF, despite connecting initially then disconnects and get STAIP as 0.0.0.0. The top code attached is for station and bottom is for AP. And above code is the outcome on station part which is where currently having issue. I have also attached for how I set up static IP station address, what would be the best way to resolve this please?

Screen Shot 2017-04-02 at 16.10.36.png


Screen Shot 2017-04-02 at 15.39.46.png



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);
   
}

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);
   
}

ESP_Faye
Posts: 1628
Joined: Mon Oct 27, 2014 11:08 am

Re: AT+CWJAP_DEF disconnects and so Station IP doesn't display

Postby ESP_Faye » Thu Apr 06, 2017 10:13 am

Hi,

It seems that you have 2 ESP8266 boards.
The first one (ESP8266-A) is working in station mode and trying to connect to an open AP named "esp8266".
The second one (ESP8266-B) works in station+softAP mode, but it seems not to be named as "esp8266"? Maybe you can set it to be softAP-only mode (AT+CWMODE_DEF=2), and name it as "esp8266" (AT+CWSAP_DEF="esp8266","",5,0), so that the ESP8266-A can connect to the ESP8266-B.

Who is online

Users browsing this forum: No registered users and 2 guests