Project

General

Profile

EtherCard: POST request via tcpSend() working in DHCP mode but not with a Static IP

Added by Fisheye_Wout almost 3 years ago

Hi guys, I've got a weird issue. I'm sending POST messages to a video device to switch input ports. In DHCP everything works, but when I configure everything to static IP it doesn't.

I've found some advice online to put a delay(100); after the static ip initialization, but this also didn't work.

I output some debug data to serial and the settings are exactly the same for both methods, more importantly subnetmask is set and the same.

My full code is below. I'm using a recently downloaded version of EtherCard, using the .net micro framework in VS2015 with the SDK selected Arduino 1.6/1.8.

I'm using the Velleman VMA04 ethernet shield for Arduino.

(Excuse the dirty switch with the double code below, it was a quick fix :)

#include <EtherCard.h>

// Pins 10 and 2 in use by Ethernet shield

byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 10, 0, 1, 95 };
static byte gwip[] = { 10, 0, 1, 1 };
static byte dnsip[] = { 8, 8, 8, 8 };
static byte maskip[] = { 255, 255, 255, 0 };
static byte hisip[] = { 10, 0, 1, 96 };

byte Ethernet::buffer[700];
uint32_t timer;
Stash stash;

void setup () {
  Serial.begin(57600);
  Serial.println("\n[webClient]");

  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
    Serial.println( "Failed to access Ethernet controller");
  if (!ether.staticSetup(myip, gwip, dnsip, maskip))
      Serial.println("static setup failed");
  //if (!ether.dhcpSetup())
     // Serial.println("DHCP failed");

  ether.copyIp(ether.hisip, hisip);

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip);  
  ether.printIp("DNS: ", ether.dnsip);
  ether.printIp("NET: ", ether.netmask);
  ether.printIp("SRV: ", ether.hisip);
  Serial.println(ether.hisport);
}

void loop () {
  ether.packetLoop(ether.packetReceive());

  if (millis() > timer) {
    timer = millis() + 10000;
    sendToWebServer(0);
  }

}

void sendToWebServer(int source)
{
    if (source == 0) {
        Serial.println("Sending POST Source 0");

        char sd[] = "{\"Input\":0}";

        Stash::prepare(PSTR("POST /PreferredInput.cgx HTTP/1.1" "\r\n"
            "Host: 10.0.1.96" "\r\n"
            "Content-Type: text/plain" "\r\n"
            "Content-Length: $D" "\r\n"
            "\r\n"
            "$S"),
            strlen(sd), sd);

        // send the packet - this also releases all stash buffers once done
        ether.tcpSend();
    }
    else {
        Serial.println("Sending POST Source 1");

        char sd2[] = "{\"Input\":1}";

        Stash::prepare(PSTR("POST /PreferredInput.cgx HTTP/1.1" "\r\n"
            "Host: 10.0.1.96" "\r\n"
            "Content-Type: text/plain" "\r\n"
            "Content-Length: $D" "\r\n"
            "\r\n"
            "$S"),
            strlen(sd2), sd2);

        // send the packet - this also releases all stash buffers once done
        ether.tcpSend();
    }

}


Replies (1)

RE: EtherCard: POST request via tcpSend() working in DHCP mode but not with a Static IP - Added by Fisheye_Wout almost 3 years ago

I did some testing, and found out that if I first call dhcpSetup() and then staticSetup() on a local network with a DHCP server, the Arduino will get a static IP in the end on the address that I specify and the tcpSend() will work.

I verify this with a network scan using Fing on another device.

The problem is that when I directly connect the Arduino to the target device, the dhcpSetup() of course fails because there's no router involved, but then afterwards the tcpSend() will not work. I tried using a regular and a crossover ethernet cable.

I tried setting flags that the dhcpSetup() also calls, like updateBroadcastAddress(), and lowering the requirements needed to send a TCP message, changing the line:

if (tcp_client_state==TCP_STATE_SENDSYN && (waitgwmac & WGW_HAVE_GW_MAC))

to

if (tcp_client_state==TCP_STATE_SENDSYN)

... but to no avail.

    (1-1/1)