Exemplo n.º 1
0
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"));

}