Beispiel #1
0
void setup() {
  char buf[64];

  Serial.begin(115200);
  Serial.println("Starting");
  Serial.print("Free memory: ");
  Serial.println(wifly.getFreeMemory(),DEC);

  wifiSerial.begin(9600);
  if (!wifly.begin(&wifiSerial, &Serial)) {
    Serial.println("Failed to start wifly");
  }

  if (!wifly.isAssociated()) {
    Serial.println("Joining network");
    wifly.setSSID(mySSID);
    wifly.setPassphrase(myPassword);
    wifly.enableDHCP();

    if (wifly.join()) {
      Serial.println("Joined wifi network");
    } else {
      Serial.println("Failed to join wifi network");
    }
  } else {
    Serial.println("Already joined network");
  }

  Serial.println("WiFly ready");

  Serial.print("MAC: ");
  Serial.println(wifly.getMAC(buf, sizeof(buf)));
  Serial.print("IP: ");
  Serial.println(wifly.getIP(buf, sizeof(buf)));
  Serial.print("Netmask: ");
  Serial.println(wifly.getNetmask(buf, sizeof(buf)));
  Serial.print("Gateway: ");
  Serial.println(wifly.getGateway(buf, sizeof(buf)));
  Serial.println("Set DeviceID");
  wifly.setDeviceID("Wifly-TCP");
  Serial.print("DeviceID: ");
  Serial.println(wifly.getDeviceID(buf, sizeof(buf)));

  wifly.setIpProtocol(WIFLY_PROTOCOL_TCP);

  if (wifly.isConnected()) {
    Serial.println("Old connection active. Closing");
    wifly.close();
  }
}
void setup()
{

    Serial.begin(115200);

#ifdef DEBUG
    Serial.println(F("Starting"));
    Serial.print(F("Free memory: "));
    Serial.println(wifly.getFreeMemory(),DEC);
#endif

    wifiSerial.begin(9600);
    if (!wifly.begin(&wifiSerial, &Serial)) {
#ifdef DEBUG
        Serial.println(F("Failed to start wifly"));
#endif
	wifly.terminal();
    }

	wifly.leave();

	delay(1000);

	wifly.scan(scanbuf, sizeof(scanbuf));


#ifdef DEBUG
	Serial.println(scanbuf);
#endif
	
	snprintf_P(scanbuf, sizeof(scanbuf), PSTR("%s b 13 -71 c 00:1c:58:10:1c:90 | a b 10 -93 c 00:12:43:8a:ea:22 | a b 11 -92 c d8:30:62:5f:f2:b1"), "a");

    /* Join wifi network if not already associated */
    if (!wifly.isAssociated()) {
	/* Setup the WiFly to connect to a wifi network */
#ifdef DEBUG
		Serial.println(F("Joining network"));
#endif
		wifly.setSSID(mySSID);
		wifly.setPassphrase(myPassword);
		wifly.enableDHCP();
		wifly.setDHCP(1);
		wifly.save();

		if (wifly.join()) {
#ifdef DEBUG
		    Serial.println(F("Joined wifi network"));
#endif
		} else {
#ifdef DEBUG
		    Serial.println(F("Failed to join wifi network"));
#endif
		    wifly.terminal();
		}
    } else {
#ifdef DEBUG
        Serial.println(F("Already joined network"));
#endif
    }

    wifly.setBroadcastInterval(0);	// Turn off UPD broadcast

#ifdef DEBUG
    Serial.print(F("MAC: "));
    Serial.println(wifly.getMAC(macaddr, sizeof(macaddr)));
    Serial.print(F("IP: "));
    Serial.println(wifly.getIP(buf, sizeof(buf)));
#endif

    wifly.setDeviceID("Wifly-WebServer");

    if (wifly.isConnected()) {
#ifdef DEBUG
        Serial.println(F("Old connection active. Closing"));
#endif
	    wifly.close();
    }

    wifly.setProtocol(WIFLY_PROTOCOL_TCP | WIFLY_PROTOCOL_UDP);
    if (wifly.getPort() != 80) {
        wifly.setPort(80);
	    wifly.save();
#ifdef DEBUG
	    Serial.println(F("Set port to 80, rebooting to make it work"));
#endif
	    wifly.reboot();
	    delay(3000);
    }
    Serial.println(F("Ready"));

}