Hi all,
I am trying to send data to my webserver written in c#.
By calling the service from the browser this works correctly.
The arduino is a Portenta H7 although I don't think it makes much difference.
The code is a collage of articles found on the net.
#include <PortentaEthernet.h>
#include <Ethernet.h>
char server[] = "localhost";
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 177);
IPAddress myDns(192, 168, 1, 1);
EthernetClient client;
// Variables to measure the speed
unsigned long beginMicros, endMicros;
unsigned long byteCount = 0;
bool printWebData = true; // set to false for better speed measurement
void setup()
{
Serial.begin(9600);
while (!Serial) { ; }
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin() == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
// try to congifure using IP address instead of DHCP:
Ethernet.begin(ip, myDns);
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.print("connecting to ");
Serial.print(server);
Serial.println("...");
// if you get a connection, report back via serial:
if (client.connect(server, 8080)) {
Serial.print("connected to ");
Serial.println(client.remoteIP());
// Make a HTTP request:
//client.println("GET /webservice.asmx/query?id=Bot&nserial=1&qry=SELECT * FROM Data&ver=1&flg=0&par1=&par2=&par3= HTTP/1.0");
client.println("GET /localhost/webservice.asmx/query?id=Bot&nserial=1&qry=SELECT * FROM Data&ver=1&flg=0&par1=&par2=&par3=HTTP/1.0");
client.println("Host: " + String(server));
client.println("Connection: close");
client.println();
// //client.println("POST /webservice/webservice.asmx/query HTTP/1.0");
// client.println("POST /webservice.asmx/query HTTP/1.0");
// client.println("Host: " + String(server));
// client.println("Content-Type: application/x-www-form-urlencoded");
// client.println("Content-Length:69");
// client.println("id=Bot&nserial=1&qry=SELECT * FROM Data&ver=1&flg=0&par1=&par2=&par3=");
// client.println("Connection: close");
// client.println();
client.stop();
} else {
Serial.println("connection failed");
}
beginMicros = micros();
}
void loop()
{
int len = client.available();
if (len > 0) {
byte buffer[80];
if (len > 80)
len = 80;
client.read(buffer, len);
if (printWebData) {
Serial.write(buffer, len); // show in the serial monitor (slows some boards)
}
byteCount = byteCount + len;
}
if (!client.connected()) {
endMicros = micros();
Serial.println();
Serial.println("disconnecting.");
client.stop();
Serial.print("Received ");
Serial.print(byteCount);
Serial.print(" bytes in ");
float seconds = (float)(endMicros - beginMicros) / 1000000.0;
Serial.print(seconds, 4);
float rate = (float)byteCount / seconds / 1000.0;
Serial.print(", rate = ");
Serial.print(rate);
Serial.print(" kbytes/second");
Serial.println();
// do nothing forevermore:
while (true) {
delay(1);
}
}
}
I have tried both GET and POST but cede I have problems with passing parameters I have tried various solutions but to no avail.
initially my asmx service was in a subdirectory I tried moving it to the root of the local server but nothing changes.
In most cases I get a 400 bad request error, or the client connects but returns no message.
Initialize Ethernet with DHCP:
DHCP assigned IP 192.168.1.154
connecting to 192.168.1.169...
connected to 192.168.1.169
disconnecting.
Received 0 bytes in 0.0000, rate = 0.00 kbytes/second
What is the correct syntax for connecting to the webserver by passing parameters?
Many thanks to those who would like to help me.
Lenny