Arduino Ethernet Shield. Successful TCP Conn, but can not send a TCP data Packet

Dear community,

I am using arduino uno + ethernet shield.

Arduino is connected to my laptop over an ethernet cable.

On the laptop I am running a simple server.

From Arduino I establish a connection to that server. In particular, I want to work with interrupts, i.e. I dont want to use EthernetClient but rather socket.cpp and W5100.cpp.

I can establish a connection (I see a TCP Handshake in the wireshark and checking the Socket n Status Register is successful), but I am unable to send the data.

Doing a little bit of debugging I figured out that after executing a send command with:

W5100.execCmdSn(s, Sock_SEND);

an SEND_OK bit in the Socket n Interrupt Register is not set ending up in the endless loop.

I don't know what am I doing wrong, may be some settings are not correct. I would be very thankful for any help or advice. Here is my code (I am using Eclipse with AVR Plugin):

#include <Arduino.h>
#include <SPI.h>         // needed for Ethernet library communication with the W5100 (Arduino ver>0018)
#include <Ethernet.h>
#include <utility/w5100.h>
#include <utility/socket.h>
#include <util.h>

#include <avr/interrupt.h>

#include "ownutils.h"
#include "tcp.h"

#define PACKET_SIZE 200
SOCKET s; // our socket 0 that will be opened in RAW mode
byte rbuf[1000]; // receive buffer
byte sbuf[1000]; // send buffer
int rbuflen; // length of data to receive
int sbuflen; // length of data to send

// set the src mac address
uint8_t smac[] = {  0x90, 0xA2, 0xDA, 0x0F, 0x46, 0x94 };
// set the dst mac address
uint8_t dmac[] = {  0x00, 0x21, 0xcc, 0x70, 0xf3, 0xae};

uint8_t src_ip[4] = {  192, 168, 0, 2};
uint8_t dst_ip[4] = {  192, 168, 0, 1};
uint8_t subnet_mask[4] = {  255, 255, 255, 0};
uint8_t gateway_ip[4] = {  192, 168, 0, 1};


uint8_t* src_mac = smac;
uint8_t* dst_mac = dmac;

uint8_t* dst_ip_addr = dst_ip;
uint8_t* src_ip_addr = src_ip;

uint16_t src_port = 3333;
uint16_t dst_port = 2222;

uint8_t data = 0x49;
uint8_t* pdata;

uint8_t ledPin = 13;
uint32_t counterValue = 0;
uint8_t sreg;
uint16_t asmcounter = 1;

void setup()   {

  Serial.begin(115200);
  Serial.println("\r\nSetup...");
  Serial.print("\r\nInitializing the w5100 chip and open a TCP socket...");
  W5100.init();                        //
  W5100.writeSnMR(s, SnMR::TCP);     // set type of socket in Mode Register

  W5100.setMACAddress(smac);           // set own mac address
  W5100.setIPAddress(src_ip_addr);     // set own ip address
  W5100.writeSnPORT(s, src_port);      // set own port (for each socket different)
  W5100.setSubnetMask(subnet_mask);
  W5100.setGatewayIp(gateway_ip);


  W5100.writeSnDHAR(s, dst_mac);       // set destination mac address
  W5100.writeSnDIPR(s, dst_ip_addr);   // set destination ip
  W5100.writeSnDPORT(s, dst_port);     // set destination port

  W5100.writeTMSR(0x55);
  W5100.writeRMSR(0x55);

  W5100.execCmdSn(s, Sock_OPEN);
  Serial.print("\r\nDone opening socket");

  W5100.execCmdSn(s, Sock_CONNECT);

 if ( (W5100.readSnSR(s) & SnSR:: ESTABLISHED) != SnSR::ESTABLISHED) // check Status Register to find out if the connection was established
 Serial.print("\r\nConnection established");
 else
 Serial.print("\r\nCould not establish connection!!!");

 Serial.print("\r\nSending Data...");

  uint16_t ret=0;
  uint8_t len = 1;
  if ( len > W5100.SSIZE)
    ret = W5100.SSIZE; // check size not to exceed MAX size.
  else
    ret = len;

  if ( (W5100.readSnSR(s) & SnSR:: ESTABLISHED) != SnSR::ESTABLISHED) // check Status Register to find out if the connection was established
  Serial.print("\r\nConnection established");
  else
  Serial.print("\r\nCould is terminated!!!");

  if ( (W5100.readSnSR(s) & SnSR:: CLOSE_WAIT) != SnSR::CLOSE_WAIT) // check Status Register to find out if the connection was established
    Serial.print("\r\nStill in touch with the remote peer");
    else
    Serial.print("\r\nConnection termination request is sent by the remote peer but not disconnected");
  Serial.print("\r\n ret=");
  Serial.print(ret,10);

  W5100.send_data_processing(s, pdata, ret); // before executing the send command we need to fill the TX memory
  W5100.execCmdSn(s, Sock_SEND);
  Serial.print("\r\nExecute send command...");

  uint8_t flag = 1;
  /* +2008.01 bj */
  while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK )
  {
    /* m2008.01 [bj] : reduce code */
    if ( W5100.readSnSR(s) == SnSR::CLOSED )
    {
      close(s);
      Serial.print("\r\nConnection closed my the remote peer");
      flag = 0;
      break;
    }
  }
  /* +2008.01 bj */
  if(flag){
  W5100.writeSnIR(s, SnIR::SEND_OK);
  Serial.print("\r\nData is sent");
  }
}

void loop()
{

}

int main(void) {

  init();
  setup();

  while(true) {
    loop();
  }
}

The problem is solved.

I made a mistake: instead of continuously checking the connection status until it is established (in a while loop), I used an if statement.

Moreover, using "!=" in a while statement is correct, but it is not correctly used in my if statement. For this reason I didn't see that the connection at the moment of examining the if statement was actually not established.

Printing content of Interrupt, Status, TX Read Pointer and TX Write Pointer Registers to the serial port has helped me to debug the program and find where I was wrong. The correct code is:

Serial.print("\r\nExec a connect command");
W5100.execCmdSn(s, Sock_CONNECT);

while ( (W5100.readSnSR(s) & SnSR:: ESTABLISHED) != SnSR::ESTABLISHED){	// check Status Register to find out if the connection was established

}
Serial.print("\r\nConnection established");

Serial.print(", SnSR=0x");
Serial.print(W5100.readSnSR(s),HEX);

Serial.print(", SnIR=0x");
Serial.print(W5100.readSnIR(s),HEX);

Serial.print(", TX_WR=0x");
Serial.print(W5100.readSnTX_WR(s),HEX);

Serial.print(", TX_RD=0x");
Serial.print(W5100.readSnTX_RD(s),HEX);

I am trying to send rfid data to my website using ethernet shield
It shows successful connection with server but i am not getting the data

here is my code:

#include <SPI.h>
#include <MFRC522.h>
#include <Ethernet.h>

//#define SS_PIN 10
//#define RST_PIN 9

#define RST_PIN 9
#define SS_PIN 8
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

byte mac[] = { 0x50, 0x7B, 0x9D, 0x00, 0x4A, 0xAA };
byte ip[] = {192,168,0,101};//Enter the IP of ethernet shield
char serv[] = "www.princebpatel.com" ;//Enter the IPv4 address

EthernetClient client;

void setup()
{
Serial.begin(9600); // Initiate a serial communication
SPI.begin(); // Initiate SPI bus
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("Approximate your card to the reader...");
Serial.println();
Ethernet.begin(mac, ip);
}

void loop()
{

// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{

return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{

return;
}
//Show UID on serial monitor
Serial.println("UID tag :");
String content= "";
byte letter;
for (byte i = 0; i < mfrc522.uid.size; i++)
{

content.concat(String(mfrc522.uid.uidByte < 0x10 ? " 0" : " "));
_ content.concat(String(mfrc522.uid.uidByte*, HEX));_
_
}*_

* content.toUpperCase();*
* Serial.println(content);*
* loop1(content);*

}
void loop1(String rfid) {
* int hum = 10;//Reading the humidity and storing in hum*
float temp = 10.0;
* //Connecting at the IP address and port we saved before*
* if (client.connect(serv, 80)) {*
* Serial.println("connected");*
* client.print("PUT /data.aspx?rfid=2/ HTTP/1.1");*
* client.print("HOST: ");*
* client.println (serv); *
* client.println();*
_ /client.print("POST /data.php?");_
_
client.print("rfid=");_
_
client.print(rfid);_
_
client.print("&humidity=");_
_
client.println(hum);_
_
client.println("HTTP/1.1");_
_
client.println("Host: 192.168.0.100");_
_
client.print("Content-Type: application/x-www-form-urlencoded");_
_
client.println("Connection: close");_
_ client.println();/_

//Printing the values on the serial monitor
* Serial.print("RFID= ");*
* Serial.println(rfid);*
* client.stop(); //Closing the connection*
* }*
* else {*
* // if you didn't get a connection to the server:*
* Serial.println("connection failed");*
* }*

}