#pragma config(Sensor, S4, NXT2WIFI, sensorHighSpeed)
#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 dataStrings[4];
tHugeByteArray data;
int sizeOfReceivedData;
task updateScreen()
{
while(true)
{
nxtDisplayTextLine(0, "Stat: %s", connStatus);
nxtDisplayTextLine(1, "%s",IPaddress);
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);
}
}
void processData()
{
string line = "";
string singleword = "";
int screenLine = 0;
dataStrings[0] = "";
dataStrings[1] = "";
dataStrings[2] = "";
dataStrings[3] = "";
for (int i = 0; i < sizeOfReceivedData; i++)
{
if (RS485rxbuffer[i] > 0x20)
{
strncat(singleword, &RS485rxbuffer[i], 1);
}
else if ((RS485rxbuffer[i] == 0x20) || (RS485rxbuffer[i] == 0x0A))
{
if ((strlen(line) + strlen(singleword)) > 16)
{
dataStrings[screenLine++] = line;
memcpy(line, singleword, sizeof(singleword));
singleword = "";
}
else
{
if (strlen(line) > 0)
strcat(line, " ");
strcat(line, singleword);
singleword = "";
}
if (RS485rxbuffer[i] == 0x0A)
{
dataStrings[screenLine++] = line;
line = "";
}
}
}
}
task main ()
{
string BOFHserver = "192.168.0.100";
int BOFHport = 6666;
string dataString;
getFriendlyName(dataString);
int avail = 0;
nNxtButtonTask = -2;
StartTask(updateScreen);
RS485initLib();
memset(RS485rxbuffer, 0, sizeof(RS485rxbuffer));
memset(RS485txbuffer, 0, sizeof(RS485txbuffer));
N2WDisconnect();
N2WchillOut();
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);
wait1Msec(1000);
while (true)
{
while (nNxtButtonPressed != kEnterButton) EndTimeSlice();
while (nNxtButtonPressed != kNoButton) EndTimeSlice();
if (N2WTCPOpenClient(1, BOFHserver, BOFHport)) {
data[0] = 0x0A;
N2WTCPWrite(1, data, 1);
txbytes++;
avail = N2WTCPAvail(1);
if (avail > 0)
{
sizeOfReceivedData = avail;
rxbytes += avail;
N2WchillOut();
N2WTCPRead(1, avail);
processData();
for (int i = 0; i < avail; i++)
{
writeDebugStream("%c", RS485rxbuffer[i]);
}
writeDebugStreamLine("");
}
N2WchillOut();
N2WTCPClose(1);
}
}
}