[Problem solved, see the last post in this thread]
Does anyone have an example how to retrieve a webpage from a regular HTTP server with the GET HTTP/1.1 request with the WIFI Plus module?
I do succeed in everything (including making a TCP connection to the HTTP server but I get an "Bad request 400" error from it...
Thanks in advance.
This is the full anwer I got from the server:
followed by:HTTP/1.0 400 Bad Request
Content-Length: 182
Content-Type: text/html; charset=utf-8
This is my source code. I do use the mini32 board. I also do use Uart1 as debug input/output: All that comes in via Uart1 is sent to the HTTP server, all that is received from the HTTP server is sent to Uart1.<HTML><HEAD><TITLE>400 Bad Request (ERR_INVALID_REQ)</TITLE></HEAD><BODY><H1>400 Bad Request</H1><BR>ERR_INVALID_REQ<HR><B>Webserver</B> Sat, 22 Apr 2023 17:14:12 GMT</BODY></HTML>
The Wifi Plus module is connected to Uart2.
Code: Select all
{*
* Project name:
WiFi_HTTP_Client
* Copyright:
none.
* Revision History:
* Description:
* Test configuration:
MCU: P32MX534F064H
http://ww1.microchip.com/downloads/en/DeviceDoc/61156G.pdf
Dev.Board: MINI-32 Board
http://www.mikroe.com/eng/products/view/711/mini-32-board/
Oscillator: XT-PLL10x, 80.000MHz
Ext. Modules: None.
SW: mikroPascal PRO for PIC32
http://www.mikroe.com/eng/categories/view/89/pic32-compilers/
* NOTES:
- None.
*}
program WiFi_HTTP_Client;
uses UartReceiver_P32;
const UartBuffSize = 250; // for UartReceiver
var UartBuffer: array[UartBuffSize] of byte; // for UartReceiver
var Redled_lat: sbit at LATG6_bit;
Redled_tris: sbit at TRISG6_bit;
OrangeLed_lat: sbit at LATD6_bit;
OrangeLed_tris: sbit at TRISD6_bit;
var cnt, kk : byte;
var readbuff : array[64] of byte;
var writebuff : array[64] of byte;
var Priority: byte;
var Net_Wireless_MCW1001_Chip_RST: sbit at LATE1_bit;
Net_Wireless_MCW1001_Chip_RST_Direction: sbit at TRISE1_bit;
var
wifiStatus,
SocketHandle,
response,
bindResult,
listenresponse,
backlog: byte;
var strSSID: string[50];
var wpaPassword: string[50];
var channels: array[11] of byte;
var myMacAddr: array[6] of byte;
var ipMask: array[4] of byte;
var remoteIP: array[4] of byte;
var remotePort: word;
var localPort: word;
var myIpAddr: array[4] of byte;
var gwIpAddr: array[4] of byte;
var wifiTmr: integer;
netStatusTmr: word;
procedure Tmr1_int(); iv IVT_TIMER_1; ilevel 6; ics ICS_SOFT;
begin
if T1IF_bit then
begin
T1IF_bit := 0;
netStatusTmr := netStatusTmr + 1;
wifiTmr := wifiTmr + 1;
if wifiTmr >= 25 then
begin
Net_Wireless_MCW1001_Time := Net_Wireless_MCW1001_Time + 1;
wifiTmr := 0;
end;
end;
end;
procedure Uart1_Int(); iv IVT_UART_1; ilevel 7; ics ICS_SRS;
begin
UartReceiverInterrupt;
end;
procedure Uart2_int(); iv IVT_UART_2; ilevel 5; ics ICS_SOFT;
begin
if U2RXIF_Bit then
begin
U2RXIF_bit := 0;
if Uart_Data_ready = 1 then Net_Wireless_MCW1001_Receive;
end;
if U2TxIF_bit then U2TXIF_bit := 0;
end;
procedure Uart1_Write_Text_CRLF(var str: string);
begin
Uart1_Write_Text(Str);
Uart1_write(#13);
Uart1_write(#10);
end;
procedure InitTimer1();
begin
T1CON := 0x8020;
T1IP0_bit := 1;
T1IP1_bit := 1;
T1IP2_bit := 1;
T1IF_bit := 0;
PR1 := 50000;
TMR1 := 0;
end;
procedure initNetwork;
begin
(*
/*
* Before compiling this project, it's mandatory to modify
* this file acording to access point configurations
* to which the connection will be established.
*/
*)
// set mac address
myMacAddr[0] := 0x00;
myMacAddr[1] := 0x04;
myMacAddr[2] := 0xA3;
myMacAddr[3] := 0x00;
myMacAddr[4] := 0x80;
myMacAddr[5] := 0x95;
// set my IP address
myIpAddr[0] := 192;
myIpAddr[1] := 168;
myIpAddr[2] := 123;
myIpAddr[3] := 85;
// set remote IP address (e.g. PC IP address)
{-$DEFINE Mine}
{$IFDEF Mine}
remoteip[0] := 192;
remoteip[1] := 168;
remoteip[2] := 123;
remoteip[3] := 69;
{$ELSE}
remoteip[0] := 192; // modem/router
remoteip[1] := 168;
remoteip[2] := 123;
remoteip[3] := 254;
{$ENDIF}
// set gateway address
gwIpAddr[0] := 192;
gwIpAddr[1] := 168;
gwIpAddr[2] := 123;
gwIpAddr[3] := 254;
// set subnet mask
ipMask[0] := 255;
ipMask[1] := 255;
ipMask[2] := 255;
ipMask[3] := 0;
channels[0] := 1;
channels[1] := 2;
channels[2] := 3;
channels[3] := 4;
channels[4] := 5;
channels[5] := 6;
channels[6] := 7;
channels[7] := 8;
channels[8] := 9;
channels[9] := 10;
channels[10] := 11;
strSSID:= 'xxxxxx';
wpaPassword := 'yyyyyy';
remotePort := 80;
localPort := 40000;
end;
Procedure init_wifi();
begin
Uart1_Write_Text('Configuration in Progress');
Net_Wireless_MCW1001_TimeToWait := 2;
Response:=1;
while Response > 0 do
begin
response:= Net_Wireless_MCW1001_SetMode(_NET_WIRELESS_MCW1001_CP_1, _NET_WIRELESS_MCW1001_MODE_INFRASTRUCTURE);
//wordtoStr(Response, Str);
//Uart1_Write_Text_CRLF(Str);
Uart1_write('.');
end;
Uart1_write_text_CRLF('');
Net_Wireless_MCW1001_TimeToWait := 10;
//Net_Wireless_MCW1001_InitDHCP();
if response = 0 then response := Response or Net_Wireless_MCW1001_SetChannelList(1 {sizeof(channels)} , channels); // <-------------
if Response = 0 then Response := Response or Net_Wireless_MCW1001_SetSecurity_WPA(_NET_WIRELESS_MCW1001_CP_1, _NET_WIRELESS_MCW1001_WPA_PSK2, wpapassword);
if response = 0 then response := Response or Net_Wireless_MCW1001_SetSSID(_NET_WIRELESS_MCW1001_CP_1, strSSID);
if response = 0 then response := Response or Net_Wireless_MCW1001_SetNetworkMask(ipMask);
if response = 0 then response := Response or Net_Wireless_MCW1001_SetGatewayIP(gwIpAddr);
if response = 0 then response := Response or Net_Wireless_MCW1001_SetIP(myIpAddr);
if response = 0 then response := Response or Net_Wireless_MCW1001_SetMAC(myMacAddr);
if response = 0 then response := Response or Net_Wireless_MCW1001_SetArpTime(1);
if response = 0 then response := Response or Net_Wireless_MCW1001_SetRetryCount(5, 255); // Retrying the network connection forever.
if (response > 0) then // Error
begin
Uart1_write_text_CRLF('Init Wifi ERROR!');
while true do
begin
delay_ms(500);
Redled_lat := not redled_lat;
end;
end
else
begin
Uart1_Write_Text_CRLF('Init Wifi Success!');
end;
end;
Procedure ConnectToAp();
var Res: string[10];
begin
Uart1_Write_Text_CRLF('Connecting to AP, takes a while...');
// Connect to AP
Net_Wireless_MCW1001_TimeToWait := 40;
response := Net_Wireless_MCW1001_Connect(_NET_WIRELESS_MCW1001_CP_1, wifiStatus);
Net_Wireless_MCW1001_TimeToWait := 5;
while ((response <> 0) or (wifiStatus <> 1) ) do // Probably need to edit networkPrameters.h file
begin
Uart1_Write_Text_CRLF('Connecting to AP FAILED');
wordtoStr(Response, Res);
Uart1_write_text('response: ');
Uart1_Write_Text_CRLF(Res);
wordtoStr(WiFiStatus, Res);
Uart1_write_text('Wifi status: ');
Uart1_Write_Text_CRLF(Res);
Net_Wireless_MCW1001_TimeToWait := 40;
response := Net_Wireless_MCW1001_Connect(_NET_WIRELESS_MCW1001_CP_1, wifiStatus);
end;
if response = 0 then
begin
Uart1_write_text_CRLF('Connecting to AP success');
wordtoStr(WiFiStatus, Res);
Uart1_write_text('Wifi status: ');
Uart1_Write_Text_CRLF(Res);
end;
end;
//
procedure TCP_Setup();
var ConnectCnt: integer;
res: string[10];
ConnectResponse: byte;
begin
response := Net_Wireless_MCW1001_SocketCreate(SocketHandle,_NET_WIRELESS_MCW1001_SOCKET_TYPE_TCP);
if(response = _NET_WIRELESS_MCW1001_NO_ERROR) then
begin
Uart1_Write_Text_CRLF('Socket is OK');
Uart1_write_text('Handle: ');
bytetostr(SocketHandle, res);
uart1_write_text_CRLF(res);
end;
response := Net_Wireless_MCW1001_SocketBind(SocketHandle, localPort, bindResult);
if(response = 0) then
begin
Uart1_Write_Text_CRLF('Bind is OK');
Uart1_write_text('Bindresult: ');
bytetostr(bindresult, res);
uart1_write_text_CRLF(res);
for connectCnt := 0 to 4 do
begin
response := Net_Wireless_MCW1001_TCP_Connect(SocketHandle, remotePort, remoteIp {Net_Wireless_MCW1001_Properties.gatewayAddress}, connectResponse);
Uart1_write_text('TCP_Connect response: ');
Bytetostr(connectresponse, res);
Uart1_write_text_CRLF(Res);
if(connectResponse = 0) then // success
begin
Uart1_write_text_CRLF('TCP_Connect Success ');
break;
end
else
begin
delay_ms(500);
end;
end;
if (connectResponse <> 0) then Uart1_write_text_CRLF('TCP_Connect Failed');
end else
begin
Uart1_Write_Text_CRLF('Bind FAILED');
end;
end;
procedure CheckTcpReceive();
var
response, i : byte;
dat, TxBuffer : string[255];
numOfReceiveBytes : word;
numOfSentBytes : word;
Str: string[10];
ConnectResponse: byte;
begin
//response := Net_Wireless_MCW1001_TCP_Connect(SocketHandle, remotePort, remoteIp {Net_Wireless_MCW1001_Properties.gatewayAddress}, connectResponse);
numOfReceiveBytes := 0;
response := Net_Wireless_MCW1001_TCP_ReadBytes(SocketHandle, SizeOf(dat), @dat[0], numOfReceiveBytes);
//bytetostr(response, Str);
//Uart1_write_text_CRLF(Str);
//exit;
// If there is no error, and number of received bytes is greater than zero
if( (response = _NET_WIRELESS_MCW1001_NO_ERROR) and (numOfReceiveBytes <> 0) ) then
begin
for i := 0 to numOfReceiveBytes - 1 do Txbuffer[I] := Dat[i];
{
NumOfSentBytes := NumOfReceiveBytes;
Net_Wireless_MCW1001_TCP_SendBytes(socketChild, @txBuffer, numOfReceiveBytes, numOfSentBytes);
}
Uart1_Write_Text_CRLF('Received: ');
txBuffer[numOfReceiveBytes + 1] := 0;
Uart1_Write_Text_CRLF(txBuffer);
end;
end;
var Sendtxt: string[100];
procedure CheckToSend;
var receive, NumOfSentBytes, I, L: byte;
txBuffer : string[255];
str: string[10];
begin
while UartCharPresent do
begin
OrangeLed_Lat := not orangeLed_Lat;
Receive := ReadUartByte;
if (receive <> 13) and (receive <> 10) then
begin
SendTxt := Sendtxt + char(Receive); // fill buffer
end
else
if (receive = 13) // CR
then
begin // send command to the host
RedLed_Lat := not RedLed_Lat;
memset(@TxBuffer, 0, sizeOf(TxBuffer));
L := length(SendTxt);
for I := 0 to L-1 do Txbuffer[I] := SendTxt[I];
TxBuffer[L] := #13;
TxBuffer[L+1] := #10;
TxBuffer[L+2] := #13;
TxBuffer[L+3] := #10;
// echo the text back to the uart
Uart1_write_text(TxBuffer);
Net_Wireless_MCW1001_TCP_SendBytes(SocketHandle, @txBuffer, length(TxBuffer), numOfSentBytes);
SendTxt := '';
end;
end;
end;
procedure InitMcu;
begin
AD1PCFG := 0xFFFF; // Configure AN pins as digital I/O
Redled_tris := 0;
Orangeled_tris := 0;
Redled_lat := 0;
Orangeled_Lat := 0;
TRISE.0 := 0;
// timer1
InitTimer1;
Priority := 6;
T1Ip0_bit := Priority.0;
T1IP1_bit := Priority.1;
T1IP2_Bit := Priority.2;
// timer1 interrupt enabled later (after initNetwork)
Uart2_Init(115200);
Uart1_init(115200);
delay_ms(500);
Uart_Set_Active(@UART2_Read, @UART2_Write, @UART2_Data_Ready, @UART2_Tx_Idle);
// Uart1
Priority := 7;
U1IP0_bit := Priority.0;
U1IP1_bit := Priority.1;
U1IP2_bit := Priority.2;
U1RXIF_Bit := 0;
U1RXIE_bit := 1;
// Uart2
Priority := 5;
U2IP0_bit := Priority.0;
U2IP1_bit := Priority.1;
U2IP2_bit := Priority.2;
U2RXIF_Bit := 0;
U2RXIE_bit := 1;
SendTxt := '';
UartReceiverInit;
EnableInterrupts;
end;
procedure Scan;
var resp: byte;
Str: string[50];
I, J: word;
Len: byte;
numOfScanResult: byte;
begin
// scan
NumOfScanResult := 0;
Resp := Net_Wireless_MCW1001_ScanStart(_NET_WIRELESS_MCW1001_CP_1, NumOfScanResult);
// Show results
bytetostr(NumOfScanResult, Str);
uart1_write_text('Scanresults: ');
Uart1_write_text_CRLF(Str);
for i := 1 to NumOfScanResult do
begin
response := Net_Wireless_MCW1001_GetScanResult(i);
Len := Net_Wireless_MCW1001_ScanResult.SSIDLength;
Str := ' ';
for J := 0 to Len - 1 do Str[J] := Net_Wireless_MCW1001_ScanResult.SSID[J];
Rtrim(Str);
Uart1_Write_text(Str);
Uart1_Write_text(' ');
bytetostr(Net_Wireless_MCW1001_ScanResult.Channel, Str);
uart1_write_text_CRLF(str);
end;
end;
begin
InitMcu;
Uart1_write_text_CRLF('');
Uart1_Write_Text_CRLF('Started WiFi HTTP Client');
Net_Wireless_MCW1001_HwReset();
InitNetwork;
T1Ie_bit := 1; // enable timer1
init_wifi;
Scan;
ConnectToAP;
TCP_Setup;
while true do
begin
CheckToSend;
CheckTcpReceive;
end;
end.