arduino-libraries / WiFi101

Wifi library for the Arduino WiFi 101 Shield
158 stars 128 forks source link

AP & WEP doesn't work for all devices connected #107

Closed 3nt0 closed 8 years ago

3nt0 commented 8 years ago

When the configuration of the MKR1000 is on Access Point with WEP security some devices connect and get the IP from DHCP, other devices not. If I change the AP with no security every devices connect and get IP.

The password is 10 character long.

How can I debug that? Where do the system hangs?

sandeepmistry commented 8 years ago

Hi @3nt0,

Can you please share the sketch you are using the MKR1000? Also, have you tried changing the WEP key index?

Which devices are having issues?

3nt0 commented 8 years ago

No, I only tried with WEP and no security wifi. The best would be WPA but I've read that's not working.

Works with WEP? YES - OPO with paranoid android NO - OPO with CyanogenOS NO - One Plus Three CyanogenOS YES - Motorola Moto G 1st gen with motorola ROM NO - MacMini 2012 NO - HP laptop with Win7

My phone is the first OPO so I never noticed.

Here it is:

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

// ----------------------------------------------------------

int led =  LED_BUILTIN;
char ssid[] = "EJ775EP"; // created AP name
char pass[] = "1234567890";      // AP password (needed only for WEP, must be exactly 10 or 26 characters in length)
int keyIndex = M2M_WIFI_SEC_WEP;                // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
WiFiServer server(80);
bool nocltoAP;
// the IP address for the shield: (solo per collegamenti ad access point esistenti)
IPAddress ip(192, 168, 90, 150);
//IPAddress dns(8, 8, 8, 8);
//IPAddress gw(0, 0, 0, 0);
//IPAddress mask(255, 255, 255, 0);
char buffer[50];

// ----------------------------------------------------------

uint8_t address = 0x6D;
MCP342x adc = MCP342x(address);

// ----------------------------------------------------------

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.println("Access Point Web Server");

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

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

  WiFi.config(ip);

  // 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:
  if (WiFi.beginAP(ssid, keyIndex, pass, 4) != WL_AP_LISTENING) {
    Serial.println("Creating access point failed");
    // don't continue
    while (true);
  }

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

  // start the web server on port 80
  server.begin();

  // you're connected now, so print out the status
  printWifiStatus();

  Wire.begin();
  // Reset devices
  MCP342x::generalCallReset();
  delay(1); // MC342x needs 300us to settle, wait 1ms

  // Check device present
  Wire.requestFrom(address, (uint8_t)1);
  if (!Wire.available()) {
    Serial.print("No device found at address ");
    Serial.println(address, HEX);
    while (1)
      ;
  }
}

// ----------------------------------------------------------

void loop() {
  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

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {

            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();

            client.print("{");
            sprintf(buffer, "\"Pressione_T_1\": \"%i\",", leggi_analog(1));
            client.print(buffer);
            sprintf(buffer, "\"Pressione_T_2\": \"%i\",", leggi_analog(2));
            client.print(buffer);
            sprintf(buffer, "\"Pressione_T_3\": \"%i\",", leggi_analog(3));
            client.print(buffer);
            sprintf(buffer, "\"Pressione_R_1\": \"%i\",", leggi_analog(4));
            client.print(buffer);
            sprintf(buffer, "\"Pressione_R_2\": \"0\"");
            client.print(buffer);
            sprintf(buffer, "\"Pressione_R_3\": \"0\"");
            client.print(buffer);
            client.print("}");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            break;
          }
          else {      // if you got a newline, then clear currentLine:
            currentLine = "";
          }
        }
        else if ((c != '\r') ) {    // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    Serial.println(currentLine);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }

  if (WiFi.RSSI() == -100 && nocltoAP == false) {
    Serial.println("Nessun dispositivo collegato al WiFi!");
    printWifiStatus();
    nocltoAP = true;
  }

  if (WiFi.RSSI() > -100 && nocltoAP == true) {
    Serial.println("Nuovo dispositivo collegato al WiFi!");
    printWifiStatus();
    nocltoAP = false;
    Serial.println("Riavvio WEB Server");
    server.begin();
    Serial.println("WEB Server Riavviato");
  }
}

long leggi_analog(int numero_analog) {
  long value = 0;
  MCP342x::Config status;
  // Initiate a conversion; convertAndRead() will wait until it can be read
  if (numero_analog == 1) {
    uint8_t err = adc.convertAndRead(MCP342x::channel1, MCP342x::oneShot, MCP342x::resolution12, MCP342x::gain1, 1000000, value, status);
  }else if (numero_analog ==2) {
    uint8_t err = adc.convertAndRead(MCP342x::channel2, MCP342x::oneShot, MCP342x::resolution12, MCP342x::gain1, 1000000, value, status);
  }else if (numero_analog ==3) {
    uint8_t err = adc.convertAndRead(MCP342x::channel3, MCP342x::oneShot, MCP342x::resolution12, MCP342x::gain1, 1000000, value, status);
  }else if (numero_analog ==4) {
    uint8_t err = adc.convertAndRead(MCP342x::channel4, MCP342x::oneShot, MCP342x::resolution12, MCP342x::gain1, 1000000, value, status);
  }
  // ATTENZIONE al gain che cambia se il sensore è 0..5VDC o 0..10VDC o 4..20mA
  uint8_t err = adc.convertAndRead(MCP342x::channel1, MCP342x::oneShot, MCP342x::resolution12, MCP342x::gain1, 1000000, value, status);
  if (err) {
    Serial.print("Convert error: ");
    Serial.println(err);
  }
  else {
    //Serial.print("Value: ");
    //Serial.println(value);
  }

  delay(300);
  return value;
}

void printWifiStatus() {
  if (WiFi.status() ==  WL_AP_LISTENING){
    Serial.println("WL_AP_LISTENING");
  }
  if (WiFi.status() ==  WL_AP_CONNECTED){
    Serial.println("WL_AP_CONNECTED");
  }
  if (WiFi.status() ==  WL_AP_FAILED){
    Serial.println("WL_AP_FAILED");
  }

  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rss);
  Serial.println(" dBm");
  // print where to go in a browser:
  Serial.print("To see this page in action, open a browser to http://");
  Serial.println(ip);
}
sandeepmistry commented 8 years ago

@3nt0 thanks for the details. Atmel needs to provide a firmware update to support WPA in AP mode(see #79), so it is not something we change in this library for now.

Have you tried changing the key index or channel?

WiFi.beginAP(ssid, keyIndex, pass, channel);

keyIndex can be 1 to 4, channel 1 to 12, the default channel is 1 if not provided.

If that doesn't make a difference, I think it's a firmware issue so we would have to report it to Ateml. Let me know.

3nt0 commented 8 years ago

Hi @sandeepmistry I think the error is this one: int keyIndex = M2M_WIFI_SEC_WEP; I don't know why but I've put that "M2M_WIFI_SEC_WEP", a mistake for sure. =(

Now it seems working on, but I'll continue testing until I have a good number of devices... and that takes me a week or two.

sandeepmistry commented 8 years ago

@3nt0 thanks for the update. I'll close this for now, we can re-open if you still face issues with other devices.