Hello everyone,
I'm using NativeEthernet as weblient to post data to IIS.
It works great as long as the length of data is less than 2'019 characters.
If the string is longer than 2'019 characters the httpRequest() function hang.
To be sure that IIS doesn't have a problem. I sent a string with a length of 7'200 characters from Postman. It succeeds.
To simulate this behavior, just increase the DATA_LEN const.
The host char array is also wrong
here is the code:
===========================================================
#include <NativeEthernet.h>
uint8_t mac[6];
void teensyMAC(uint8_t *mac);
// initialize the library instance:
EthernetClient client;
char host[] = "myDomain.com";
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds
constexpr int DATA_LEN = 2'020;
char data[DATA_LEN];
void setup()
{
data[0] = '"';
for(int i = 1; i<DATA_LEN-2; ++i)
data = 'a';
data[DATA_LEN-2] = '"';
data[DATA_LEN-1] = '\0';
teensyMAC(mac);
Serial.begin(9600);
while (!Serial) { ; }
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 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:
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.write(c);
}
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
// this method makes a HTTP connection to the server:
void httpRequest()
{
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
Serial.printf("%s %d\n %s\n", "Data-Len: ", strlen(data), data);
if (client.connect(host, 443, true))
{
Serial.println("connecting...");
// send the HTTP GET request:
client.printf("%s %s HTTP/1.0\r\n", "POST", "/api/Data/Test");
client.printf("Host: %s\r\n", host);
client.println("Connection: close");
client.println("Accept: text/plain");
client.println("Content-type: application/json; charset=utf-8");
client.printf("Content-length: %d\r\n", strlen(data));
client.println();
client.print(data);
// note the time that the connection was made:
lastConnectionTime = millis();
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
void teensyMAC(uint8_t *mac)
{
for(uint8_t by=0; by<2; by++) mac[by]=(HW_OCOTP_MAC1 >> ((1-by)*8)) & 0xFF;
for(uint8_t by=0; by<4; by++) mac[by+2]=(HW_OCOTP_MAC0 >> ((3-by)*8)) & 0xFF;
Serial.printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
===========================================================
Can you please give me a hint.
Thank you in advance
Harry
I'm using NativeEthernet as weblient to post data to IIS.
It works great as long as the length of data is less than 2'019 characters.
If the string is longer than 2'019 characters the httpRequest() function hang.
To be sure that IIS doesn't have a problem. I sent a string with a length of 7'200 characters from Postman. It succeeds.
To simulate this behavior, just increase the DATA_LEN const.
The host char array is also wrong
here is the code:
===========================================================
#include <NativeEthernet.h>
uint8_t mac[6];
void teensyMAC(uint8_t *mac);
// initialize the library instance:
EthernetClient client;
char host[] = "myDomain.com";
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds
constexpr int DATA_LEN = 2'020;
char data[DATA_LEN];
void setup()
{
data[0] = '"';
for(int i = 1; i<DATA_LEN-2; ++i)
data = 'a';
data[DATA_LEN-2] = '"';
data[DATA_LEN-1] = '\0';
teensyMAC(mac);
Serial.begin(9600);
while (!Serial) { ; }
// start the Ethernet connection:
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac) == 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:
} else {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
}
// give the Ethernet shield a second to initialize:
delay(1000);
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.write(c);
}
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
// this method makes a HTTP connection to the server:
void httpRequest()
{
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
Serial.printf("%s %d\n %s\n", "Data-Len: ", strlen(data), data);
if (client.connect(host, 443, true))
{
Serial.println("connecting...");
// send the HTTP GET request:
client.printf("%s %s HTTP/1.0\r\n", "POST", "/api/Data/Test");
client.printf("Host: %s\r\n", host);
client.println("Connection: close");
client.println("Accept: text/plain");
client.println("Content-type: application/json; charset=utf-8");
client.printf("Content-length: %d\r\n", strlen(data));
client.println();
client.print(data);
// note the time that the connection was made:
lastConnectionTime = millis();
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
void teensyMAC(uint8_t *mac)
{
for(uint8_t by=0; by<2; by++) mac[by]=(HW_OCOTP_MAC1 >> ((1-by)*8)) & 0xFF;
for(uint8_t by=0; by<4; by++) mac[by+2]=(HW_OCOTP_MAC0 >> ((3-by)*8)) & 0xFF;
Serial.printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}
===========================================================
Can you please give me a hint.
Thank you in advance
Harry