task main ()
{
  int index;
  // port to use for the socket
  int BOFHport = 6666;
  string dataString;

  // get our bluetooth name
  getFriendlyName(dataString);

  int avail = 0;

  StartTask(updateScreen);

  // initialise the port, etc
  RS485initLib();
  N2WchillOut();
  N2WsetDebug(true);
  N2WchillOut();

  // Disconnect if already connected
  N2WDisconnect();
  wait1Msec(100);

  if (!N2WCustomExist())
  {
    StopTask(updateScreen);
    wait1Msec(50);
    eraseDisplay();
    PlaySound(soundException);
    nxtDisplayCenteredBigTextLine(1, "ERROR");
    nxtDisplayTextLine(3, "No custom profile");
    nxtDisplayTextLine(4, "configured!!");
    while(true) EndTimeSlice();
  }

  N2WLoad();
  wait1Msec(100);
  N2WConnect(true);
  connStatus = "connecting";

  while (!N2WConnected())
    wait1Msec(1000);

  connStatus = "connected";
  PlaySound(soundBeepBeep);

  wait1Msec(3000);
  N2WgetIP(IPaddress);
  memcpy(dataStrings[4], IPaddress, strlen(IPaddress) + 1);

  wait1Msec(1000);

  N2WTCPClose(0);
  N2WchillOut();
  RS485clearRead();
  N2WchillOut();
  // Open a listening socket on port 6666
  if (!N2WTCPOpenServer(1, BOFHport))
  {
    writeDebugStreamLine("Err open port %d", BOFHport);
    PlaySound(soundException);
    while(bSoundActive) EndTimeSlice();
    StopAllTasks();
  }

  while (true)
  {
    // Check if anyone has sent us anything
    avail = N2WTCPAvail(1);
    if (avail > 0)
    {
      rxbytes += avail;
      N2WchillOut();


      PlaySound(soundFastUpwardTones);

      // Read what the client sent us
      sprintf(dataStrings[0], "%d bytes from", avail);
      N2WTCPRead(1, avail);
      N2WchillOut();

      // Get the MAC address of the client
      dataStrings[2] = "Remote MAC:";
      N2WTCPClientMAC(1, dataStrings[3]);

      writeDebugStream("MAC: ");
      writeDebugStreamLine(dataStrings[3]);
      N2WchillOut();

      // check the IP address of the client
      N2WTCPClientIP(1, dataStrings[1]);
      N2WchillOut();

      // Send back a hearty "Hi there, <IP>!"
      index = 0;
      returnMsg = "Hi there, ";
      index = RS485appendToBuff(buffer, index, returnMsg);
      index = RS485appendToBuff(buffer, index, dataStrings[1]);
      returnMsg = "\n";
      index = RS485appendToBuff(buffer, index, returnMsg);
      txbytes += index;
      N2WTCPWrite(1, (tHugeByteArray)buffer, index);
      N2WchillOut();

      // Terminate the connection to the client
      N2WTCPDetachClient(1);
      N2WchillOut();
      wait1Msec(1000);
	  }
	  // Wait a bit
    wait1Msec(50);
  }
}
task main ()
{
  // initialise the port, etc
  RS485initLib();
  displayTextLine(0, "Stat: disconnected");
  displayTextLine(2, "-------------------");

  N2WchillOut();
  N2WsetDebug(true);
  N2WchillOut();

  // Disconnect if already connected
  N2WDisconnect();
  sleep(100);

  // Delete any pre-existing custom profiles and reset the device
  N2WDelete();
  N2WchillOut();
  N2WReset();
  sleep(4000);

  // enable DHCP
  if (useDHCP)
  {
    N2WsetDHCP(true);
  }
  else
  {
    N2WsetDHCP(false);
    N2WchillOut();
    N2WsetIPAddress(ipaddress);
    N2WchillOut();
    N2WsetMask(netmask);
    N2WchillOut();
    N2WsetDNS1(DNS1);
    N2WchillOut();
    N2WsetDNS2(DNS2);
    N2WchillOut();
    N2WsetGateway(gateway);
  }

  sleep(100);
  // Enable or disable AdHoc
  N2WsetAdHoc(useAdHoc);
  sleep(100);
  // SSID to connect to
  N2WsetSSID(ssid);
  sleep(100);

  if (useWPA)
    N2WSecurityWPAPassphrase(passphrase);
  else if (useWPA2)
    N2WSecurityWPA2Passphrase(passphrase);
  else if (useWEP104)
    N2WSecurityWEP104(passphrase);
  else if (useOpen)
    N2WSecurityOpen();

  displayTextLine(0, "Stat: Calculating");
  displayTextLine(3, "This can take up");
  displayTextLine(4, "to 30 seconds");
  sleep(100);

  // Save this profile to the custom profile
  N2WSave();
  sleep(100);
  // Load the custom profile
  N2WLoad();

  sleep(100);
  N2WConnect(true);
  displayTextLine(0, "Stat: Connecting");

  while (!N2WConnected())
    sleep(500);

  sleep(3000);

  N2WgetIP(ipaddress);
  displayTextLine(3, "My IP address is");
  displayTextLine(4, ipaddress);
  displayTextLine(0, "Stat: Configured");
  N2WchillOut();
  N2WSave();
  playSound(soundBeepBeep);
  while(true) sleep(1);
}
task main() {
  string mac;
  string IP;

  eraseDisplay();
  nxtDisplayTextLine(0, "NXT2WIFI SETUP" );
  nxtDisplayTextLine(7, "IP   SETUP   MAC" );

  nNxtButtonTask = -2;

  // initialise the port, etc
  RS485initLib();

  memset(RS485rxbuffer, 0, sizeof(RS485rxbuffer));
  memset(RS485txbuffer, 0, sizeof(RS485txbuffer));

  // Disconnect if already connected


  N2WsetDebug(true); // enable debug stream on computer terminal


  while(true)
  {
		// PRESS RIGHT BUTTON TO RETRIEVE MAC ADDRESS
		if (nNxtButtonPressed == kRightButton)
		{
			N2WgetMAC(mac);
			nxtDisplayTextLine(4, mac);
			// Debounce button
			while (nNxtButtonPressed != kNoButton) EndTimeSlice();
		}
		// PRESS RIGHT BUTTON TO TEST NETWORK CREATION AND CONNECTIVITY
		if (nNxtButtonPressed == kEnterButton)
		{
			N2WDisconnect();
			N2WchillOut();
			N2WDelete();
			CreateCustomWIFI();
			N2WchillOut();

			N2WConnect(true);  // connect to custom profile
			wait1Msec(1000);

			nxtDisplayTextLine(3, "Connecting...");
			while (!N2WConnected())
			  wait1Msec(1000);

			nxtDisplayTextLine(3, "Connected!");
			PlayTone(523, 90);  // C5
			wait1Msec(100);
			PlayTone(659,90);   // E5
			wait1Msec(100);
			PlayTone(784,90);
			wait1Msec(100);
			N2WgetIP(IP);
			nxtDisplayTextLine(4, IP);
			while (nNxtButtonPressed != kNoButton) EndTimeSlice();
		}
	  // PRESS LEFT BUTTON TO RETRIEVE IP ADDRESS
		if (nNxtButtonPressed == kLeftButton)
		{
			N2WgetIP(IP);
			nxtDisplayTextLine(4, IP);
			while (nNxtButtonPressed != kNoButton) EndTimeSlice();
		}
	}
}