#pragma config(Sensor, S4, NXT2WIFI, sensorHighSpeed)
#define __RS485_DEBUG__
#include "drivers/common.h"
#include "drivers/benedettelli-nxt2wifi.h"
long txbytes = 0;
long rxbytes = 0;
string IPaddress = "0.0.0.0";
string connStatus = "disconnected";
string returnMsg;
string dataStrings[5];
tBigByteArray buffer;
task updateScreen()
{
while(true)
{
nxtDisplayTextLine(0, "Stat: %s", connStatus);
nxtDisplayTextLine(1, "%s",dataStrings[4]);
nxtDisplayTextLine(2, "-------------------");
nxtDisplayTextLine(3, "%s", dataStrings[0]);
nxtDisplayTextLine(4, "%s", dataStrings[1]);
nxtDisplayTextLine(5, "%s", dataStrings[2]);
nxtDisplayTextLine(6, "%s", dataStrings[3]);
nxtDisplayTextLine(7, "RX/TX: %d/%d", rxbytes, txbytes);
wait1Msec(100);
}
}
task main ()
{
int index;
int BOFHport = 6666;
string dataString;
getFriendlyName(dataString);
int avail = 0;
StartTask(updateScreen);
RS485initLib();
N2WchillOut();
N2WsetDebug(true);
N2WchillOut();
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();
if (!N2WTCPOpenServer(1, BOFHport))
{
writeDebugStreamLine("Err open port %d", BOFHport);
PlaySound(soundException);
while(bSoundActive) EndTimeSlice();
StopAllTasks();
}
while (true)
{
avail = N2WTCPAvail(1);
if (avail > 0)
{
rxbytes += avail;
N2WchillOut();
PlaySound(soundFastUpwardTones);
sprintf(dataStrings[0], "%d bytes from", avail);
N2WTCPRead(1, avail);
N2WchillOut();
dataStrings[2] = "Remote MAC:";
N2WTCPClientMAC(1, dataStrings[3]);
writeDebugStream("MAC: ");
writeDebugStreamLine(dataStrings[3]);
N2WchillOut();
N2WTCPClientIP(1, dataStrings[1]);
N2WchillOut();
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();
N2WTCPDetachClient(1);
N2WchillOut();
wait1Msec(1000);
}
wait1Msec(50);
}
}