ethernet shield can't send data [+PN532]

i use PN532 + ethernet shield with arduino UNO

first, i change some ethernet shield web client example code to make loop send some data and get some data back

it’s don’t have a problem

this is my code

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

String mxx="data" ;                                                       //mydata
int LED = 8;
int BUTTON = 4;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192,168,2,115);
IPAddress ip(192,168,2,145);
EthernetClient client;

void setup() {

  Serial.begin(9600);
  pinMode(LED,OUTPUT);
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }
  delay(1000);
  Serial.println("connecting...");

}

void loop()
{
  httpRequest();
}


void httpRequest() {
    client.connect(server, 80);
    client.println("POST /checkPass.php?somePass="+mxx);
    client.println("Host: 192.168.2.115");
    client.println("Name: <input type=\"submit\">");
    client.println();

  
    char c = client.read();
    client.available();
    Serial.print(c);
    if(c=='/'){                                                    //check data from server and led on
      Serial.println("ok");
      digitalWrite(LED,HIGH);
    }
  
    if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    delay(3000);                                      // delay led around 3 sec before sent data to server again
    digitalWrite(LED,LOW);
    client.stop();
    }
}

now i use this code with NDEF P2P Receive code

i want
✓ [arduino waiting for ndef messgae from androidphone]
✓ [touch androidphone to beam ndef message to arduino]
✗ [sent ndef message from arduino to server]
✗ [get some data back from server]
✗ [if some data is correct led on]
✓ [arduino waiting for ndef messgae again and looping like this]

this is NDEF P2P Receive code + web client and it’s have a problem

#include "SPI.h"
#include "PN532_SPI.h"
#include "snep.h"
#include "NdefMessage.h"
#include "Wire.h"
#include <SPI.h>
#include <Ethernet.h>

String mxx ;
int LED = 8;
int BUTTON = 4;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192,168,2,115);
IPAddress ip(192,168,2,145);
EthernetClient client;

PN532_SPI pn532spi(SPI, 9);
SNEP nfc(pn532spi);
uint8_t ndefBuf[128];

void setup() {
    Serial.begin(9600);
    Serial.println("NFC Peer to Peer Example - Receive Message");
    
  pinMode(LED,OUTPUT);
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }
  delay(1000);
  Serial.println("connecting..."); 
}

void loop() {
    Serial.println("Waiting for message from a peer");
    int msgSize = nfc.read(ndefBuf, sizeof(ndefBuf));
    if (msgSize > 0) {
        NdefMessage msg  = NdefMessage(ndefBuf, msgSize);

            
        NdefRecord record = msg.getRecord(0);

        int payloadLength = record.getPayloadLength();
        byte payload[payloadLength];
        record.getPayload(payload);        
        
        int startChar = 0;        
        if (record.getTnf() == TNF_WELL_KNOWN && record.getType() == "T") { // text message
          // skip the language code
          startChar = payload[0] + 1;
        } else if (record.getTnf() == TNF_WELL_KNOWN && record.getType() == "U") { // URI
          startChar = 1;
        }
                         
        String payloadAsString = "";
        for (int c = startChar; c < payloadLength; c++) {
          payloadAsString += (char)payload[c];
        }
        Serial.print(payloadAsString);
        mxx=payloadAsString;
        
        httpRequest();                                                      // call webclient function
        
        Serial.println("\nSuccess");
    } else {
        Serial.println("Failed");
    }
    
    delay(3000);
}

void httpRequest() {

    client.connect(server, 80);

    Serial.println(mxx);
    client.println("POST /checkPass.php?somePass="+mxx);
    client.println("Host: 192.168.2.115");
    client.println("Name: <input type=\"submit\">");
    client.println();

  
    char c = client.read();
    client.available();
    Serial.print(c);
    if(c=='/'){
      Serial.println("ok");
      digitalWrite(LED,HIGH);
    }


   if (!client.connected()) {
     Serial.println();
     Serial.println("disconnecting.");
     //delay(1000);
     digitalWrite(LED,LOW);
     client.stop();
     }

it’s don’t sent data to server

i don’t know why

thanks

jame35: i use PN532 + ethernet shield with arduino UNO

first, i change some ethernet shield web client example code to make loop send some data and get some data back

it's don't have a problem

Yes, it does. You are not reading the response from the server, and you may not be closing the connection before attempting a new connection.

Try these examples. The first on this page is a GET and the second is a POST. http://playground.arduino.cc/Code/WebClient

Then you need to post a link to the PN532 hardware you have, and how you have it connected to the Uno.

i use POST request and it fail again

code

#include "SPI.h"
#include "PN532_SPI.h"
#include "snep.h"
#include "NdefMessage.h"
#include "Wire.h"

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

byte mac[] = {  
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

//Change to your server domain
char serverName[] = "192.168.2.115";

// change to your server's port
int serverPort = 80;

// change to the page on that server
char pageName[] = "/checkPass.php?somePass=fa42be0504a2e78dd09e9f72f306c811f351f516809db065c70f7d7748c6aba1";

EthernetClient client;
int totalCount = 0; 
// insure params is big enough to hold your variables
char params[32];

// set this to the number of milliseconds delay
// this is 30 seconds
#define delayMillis 5000UL

unsigned long thisMillis = 0;
unsigned long lastMillis = 0;


PN532_SPI pn532spi(SPI, 9);
SNEP nfc(pn532spi);
uint8_t ndefBuf[128];

void setup() {
    Serial.begin(9600);
    Serial.println("NFC Peer to Peer Example - Receive Message");
    
    
      // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.print(F("Starting ethernet..."));
  if(!Ethernet.begin(mac)) Serial.println(F("failed"));
  else Serial.println(Ethernet.localIP());

  delay(2000);
  Serial.println(F("Ready"));
    
}

void loop() {
    Serial.println("Waiting for message from a peer");
    int msgSize = nfc.read(ndefBuf, sizeof(ndefBuf));
    if (msgSize > 0) {
        NdefMessage msg  = NdefMessage(ndefBuf, msgSize);
        //msg.print();
            
        NdefRecord record = msg.getRecord(0);

        int payloadLength = record.getPayloadLength();
        byte payload[payloadLength];
        record.getPayload(payload);        
        
        int startChar = 0;        
        if (record.getTnf() == TNF_WELL_KNOWN && record.getType() == "T") { // text message
          // skip the language code
          startChar = payload[0] + 1;
        } else if (record.getTnf() == TNF_WELL_KNOWN && record.getType() == "U") { // URI
          startChar = 1;
        }
                         
        String payloadAsString = "";
        for (int c = startChar; c < payloadLength; c++) {
          payloadAsString += (char)payload[c];
        }
        Serial.print(payloadAsString);
        functiona();
        
        Serial.println("\nSuccess");
    } else {
        Serial.println("Failed");
    }
    
    delay(3000);
}


void functiona(){
    thisMillis = millis();

  if(thisMillis - lastMillis > delayMillis)
  {
    lastMillis = thisMillis;

    // params must be url encoded.
    sprintf(params,"temp1=%i",totalCount);     
    if(!postPage(serverName,serverPort,pageName,params)) Serial.print(F("Fail "));
    else Serial.print(F("Pass "));
    totalCount++;
    Serial.println(totalCount,DEC);
  } 
}

byte postPage(char* domainBuffer,int thisPort,char* page,char* thisData)
{
  int inChar;
  char outBuf[64];

  Serial.print(F("connecting..."));

  if(client.connect(domainBuffer,thisPort) == 1)
  {
    Serial.println(F("connected"));

    // send the header
    sprintf(outBuf,"POST %s ",page);
    client.println(outBuf);
    sprintf(outBuf,"Host: %s",domainBuffer);
    client.println(outBuf);
    client.println(F("Connection: close\r\nContent-Type: application/x-www-form-urlencoded"));
    sprintf(outBuf,"Content-Length: %u\r\n",strlen(thisData));
    client.println(outBuf);

    // send the body (variables)
    client.print(thisData);
  } 
  else
  {
    Serial.println(F("failed"));
    return 0;
  }

  int connectLoop = 0;

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
    if(inChar=='/'){
      Serial.println("ok");
      digitalWrite(8,HIGH);
    }
      Serial.write(inChar);
      connectLoop = 0;
    }

    delay(1);
    connectLoop++;
    if(connectLoop > 10000)
    {
      Serial.println();
      Serial.println(F("Timeout"));
      client.stop();
    }
  }

  Serial.println();
  delay(3000);
  digitalWrite(8,LOW);
  Serial.println(F("disconnecting."));
  client.stop();
  return 1;
}

Serial monitor respond

NFC Peer to Peer Example - Receive Message
Starting ethernet...192.168.2.145
Ready
Waiting for message from a peer
connecting...failed
Fail 1

Success
Waiting for message from a peer
 fa42be0504a2e78dd09e9f72f306c811f351f516809db065c70f7d7748c6aba1
    
Success
Waiting for message from a peer
connecting...failed
Fail 2

Success
Waiting for message from a peer
 fa42be0504a2e78dd09e9f72f306c811f351f516809db065c70f7d7748c6aba1
    connecting...failed
Fail 3

Success
Waiting for message from a peer
connecting...failed
Fail 4

Success
Waiting for message from a peer
 fa42be0504a2e78dd09e9f72f306c811f351f516809db065c70f7d7748c6aba1
    connecting...failed
Fail 5

Success
Waiting for message from a peer

You didn't post how you have the PN532 connected.

But despite that, you are expecting the DNS server to resolve a localnet IP. That normally causes a DNS fail.

// change this
char serverName[] = "192.168.2.115";
// to this
IPAddress serverName(192,168,2,115);

// and change this
byte postPage(char* domainBuffer,int thisPort,char* page,char* thisData)
// to this
byte postPage(IPAddress domainBuffer,int thisPort,char* page,char* thisData)

my PN532 NDEF P2P Recieve Code

#include "SPI.h"
#include "PN532_SPI.h"
#include "snep.h"
#include "NdefMessage.h"
#include "Wire.h"

PN532_SPI pn532spi(SPI, 9);
SNEP nfc(pn532spi);
uint8_t ndefBuf[128];

void setup() {
    Serial.begin(9600);
    Serial.println("NFC Peer to Peer Example - Receive Message");
}

void loop() {
    Serial.println("Waiting for message from a peer");
    int msgSize = nfc.read(ndefBuf, sizeof(ndefBuf));
    if (msgSize > 0) {
        NdefMessage msg  = NdefMessage(ndefBuf, msgSize);
        //msg.print();
            
        NdefRecord record = msg.getRecord(0);

        int payloadLength = record.getPayloadLength();
        byte payload[payloadLength];
        record.getPayload(payload);        
        
        int startChar = 0;        
        if (record.getTnf() == TNF_WELL_KNOWN && record.getType() == "T") { // text message
          // skip the language code
          startChar = payload[0] + 1;
        } else if (record.getTnf() == TNF_WELL_KNOWN && record.getType() == "U") { // URI
          startChar = 1;
        }
                         
        String payloadAsString = "";
        for (int c = startChar; c < payloadLength; c++) {
          payloadAsString += (char)payload[c];
        }
        Serial.print(payloadAsString);
        
        Serial.println("\nSuccess");
    } else {
        Serial.println("Failed");
    }
    
    delay(3000);
}

Ethernet shield on Arduino UNO
LED on Pin8

PN532 Pin::
SCK >> 13
MISO >> 12
MOSI >> 11
SS >> 9 (because of this problem >> http://forum.arduino.cc/index.php?topic=320313.0)
VCC >> 5V
GND >> GND

OK, did you change the code to the IPAddress instead of a character array?

SurferTim: OK, did you change the code to the IPAddress instead of a character array?

yes, i do and it fail like Serial monitor respond in #2

Have you tried the ethernet code without adding the PN532 library but leaving the PN532 connected? Add just these two lines to the setup of the ethernet code.

    // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

// add this to disable PN532 SPI
  pinMode(9,OUTPUT);
  digitalWrite(9,HIGH);

still the same

Have you tried connecting to the server (192.168.2.115) with a PC on that localnet? From the server itself doesn't count.

The only other test I can suggest is disconnect all the PN532 SPI data lines (D11 to D13) from the Arduino and try the ethernet code. Then connect each line one at a time and try it again. One or more of those pins should cause that fail. Which pin is it? If I had to guess, it will be D12.

edit: Maybe a link to the PN532 breakout board you are using will help.