Go Down

Topic: MKR1000 Server in AP Mode, socket connection problem (Read 810 times) previous topic - next topic

spidermanpc

Greetings to all,

I have a problem with my MKR1000. Im trying to make a tcp server in AP Mode. The Wifi network in AP Mode is created successfully. The first time i connected to the tcp server with code in my laptop the connection is made normally. After that i disconnect from AP, reconnect to it, and then i try to make a new tcp connection to the MKR1000 resulting in a timeout connection from my laptop.

Is there any problem with mkr1000 tcp server in AP mode?


ballscrewbob

Are you on the latest FIRMWARE and BOARDS ?

You might want to post the sketch also so somebody else can try it and see if its an issue there.
BTW use code tags to post any code / errors / text files ( </> )

It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

spidermanpc

Thanks for your response. Im using the latest firmware on MKR1000

The code is the a modification of the AP example for MKR1000:

Code: [Select]

#include <SPI.h>
#include <WiFi101.h>

int led =  LED_BUILTIN;

char ssid[] = "TESTAP"; // created AP name
char pass[] = "1020304050";      // AP password (needed only for WEP, must be exactly 10 or 26 characters in length)
int keyIndex = 0;                // your network key Index number (needed only for WEP)

int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial);
  Serial.println("Access Point Server");

  pinMode(led, OUTPUT);      // set the LED pin mode

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // by default the local IP address of will be 192.168.1.1
  // you can override it with the following:
  WiFi.config(IPAddress(10, 0, 0, 1));
 

  // print the network name (SSID);
  Serial.print("Creating access point named: ");
  Serial.println(ssid);

  // Create open network. Change this line if you want to create an WEP network:
  status = WiFi.beginAP(ssid,keyIndex,pass);
  if (status != WL_AP_LISTENING) {
    Serial.println("Creating access point failed");
    // don't continue
    while (true);
  }

  // wait 10 seconds for connection:
  //delay(10000);

  // start the server on port 9000
  server.begin();
}


void loop() {
  // compare the previous status to the current status
  if (status != WiFi.status()) {
    // it has changed update the variable
    status = WiFi.status();

    if (status == WL_AP_CONNECTED) {
      Serial.println("Device connected to AP");
    } else {
      // a device has disconnected from the AP, and we are back in listening mode
      Serial.println("Device disconnected from AP");
    }
  }
 
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character
            currentLine = "";
        } else {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }

    // close the connection:
    client.flush();
    client.stop();
    Serial.println("client disconnected");
  }
}

sandeepmistry

Try adding:

Code: [Select]

    if (status == WL_AP_CONNECTED) {
      Serial.println("Device connected to AP");
      server.begin(); // <- NEW LINE
    } else {


Once the AP connection is lost, the server socket is no longer valid.

spidermanpc

Thanks sandeepmistry!!!!, i havent see that point. With the line added it works perfectly.


Greetings

Go Up