#pragma config(Sensor, S1, WifiBlockPort, sensorI2CCustom)
#include "drivers/humarobotics-wifiblock.h";
#define WIFIBLOCK_IP "192.168.43.100"
#define WIFIBLOCK_MASK "192.168.43.255"
#define WIFIBLOCK_GW "192.168.43.1"
#define WIFIBLOCK_SSID "AndroidAP"
#define WIFIBLOCK_KEY ""
#define DO_WIFI_CONFIG 0
#define DO_TEST_HTTP_GET 1
#define DO_TEST_HTTP_POST 0
#define DO_TEST_TCP 0
#define DO_TEST_UDP 0
void dumpRXData(tBigByteArray &data, ubyte size)
{
string tmpBuff;
int bytesleft = size;
int datalen = 0;
for (int i = 0; i < ((size/18) + 1); i++)
{
datalen = (bytesleft > 18) ? 18 : bytesleft;
memset(tmpBuff, 0, 20);
memcpy(tmpBuff, &data[i*18], datalen);
writeDebugStream("BUFFER: ");
writeDebugStreamLine(tmpBuff);
nxtDisplayTextLine(i+4, tmpBuff);
bytesleft -= 18;
}
}
void StartupDisplay()
{
nxtDisplayCenteredBigTextLine(0, "WiFi");
nxtDisplayCenteredTextLine(3, "Program to test");
nxtDisplayCenteredTextLine(4, "Network with ");
nxtDisplayCenteredTextLine(5, "WifiBlock");
wait1Msec(2000);
eraseDisplay();
return;
}
task main () {
StartupDisplay();
#if (DO_WIFI_CONFIG == 1)
tNetworkInfo netInfo;
stringToIp(WIFIBLOCK_IP, (tIPaddr)netInfo.IP);
stringToIp(WIFIBLOCK_GW, (tIPaddr)netInfo.gateway);
stringToIp(WIFIBLOCK_MASK, (tIPaddr)netInfo.netmask);
netInfo.wifi_type = HRWB_SECURITY_OPEN;
netInfo.wifi_ssid = WIFIBLOCK_SSID;
netInfo.wifi_pass = WIFIBLOCK_KEY;
HRWBconfigNetwork(WifiBlockPort, netInfo);
#endif
#if (DO_TEST_HTTP_GET == 1)
string tmpURL;
tGetRequest getrequest;
stringToIp("192.168.43.68", (tIPaddr)getrequest.IP[0]);
getrequest.port = 80;
tmpURL = "/wifiblock.html";
memcpy(getrequest.URL[0], tmpURL, strlen(tmpURL) + 1);
HRWBdoGET(WifiBlockPort, getrequest);
dumpRXData(getrequest.RXData, getrequest.RXDataLen);
#elif (DO_TEST_HTTP_POST == 1)
tPostRequest postrequest ;
string tmpString;
stringToIp("192.168.43.68", (tIPaddr)postrequest.IP);
postrequest.port = 80;
tmpString = "msg=hello";
memcpy(postrequest.TXData, tmpString, strlen(tmpString) + 1);
postrequest.TXDataLen = strlen(tmpString) + 1;
tmpString = "/post.php";
memcpy(postrequest.URL, tmpString, strlen(tmpString) + 1);
HRWBdoPOST(WifiBlockPort, postrequest);
#elif (DO_TEST_TCP == 1)
tTCPRequest tcprequest;
stringToIp("192.168.43.68", (tIPaddr)tcprequest.IP);
tcprequest.port = 8080;
tmpString = "Hi from WiFiBlock\n";
memcpy(tcprequest.TXData, tmpString, strlen(tmpString) + 1);
tcprequest.TXDataLen = strlen(tmpString) + 1;
HRWBdoTCP(WifiBlockPort, tcprequest);
dumpRXData(tcprequest.RXData, tcprequest.RXDataLen);
#elif (DO_TEST_UDP == 1)
tUDPRequest udprequest;
stringToIp("192.168.43.68", (tIPaddr)udprequest.IP);
udprequest.dport = 8080;
udprequest.sport = 5000;
tmpString = "Hi from WiFiBlock\n";
memcpy(udprequest.TXData, tmpString, strlen(tmpString) + 1);
udprequest.TXDataLen = strlen(tmpString) + 1;
HRWBdoUDP(WifiBlockPort, udprequest);
dumpRXData(udprequest.RXData, udprequest.RXDataLen);
#endif
PlaySound(soundBeepBeep);
while(true) EndTimeSlice();
}