enc28j60 error HTTP/1.1 404 Not Found

I have tried to post sensors data to local web server.
I’m using nano v3 + enc28j60, and web server on WR703n.

my sketch

#include <EtherCard.h>
#include "DHT.h"
#include <Wire.h>
#include <BMP085.h>

#define DHTPIN 8
#define DHTTYPE DHT11   // DHT 11 
BMP085 dps = BMP085(); 

long pr = 0, al = 0, tm = 0;
unsigned long time1=0;

// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };

byte Ethernet::buffer[700];
static uint32_t timer;

char website[] PROGMEM = "";
static byte hisip[] = {192,168,1,110};
boolean use_hisip = true; 

// ether.hisip[0] = 192; 
// ether.hisip[1] = 168; 
 //ether.hisip[2] = 1; 
// ether.hisip[3] = 110;

// called when the client request is complete
static void my_callback (byte status, word off, word len) {
  Ethernet::buffer[off+300] = 0;
  Serial.print((const char*) Ethernet::buffer + off);

void setup () {
  dps.init(MODE_STANDARD, 19000, true);

   if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0) 
   Serial.println( "Failed to access Ethernet controller");
   if (!ether.dhcpSetup())
   Serial.println("DHCP failed");

    ether.printIp("IP:  ", ether.myip);
    ether.printIp("GW:  ", ether.gwip);  
    ether.printIp("DNS: ", ether.dnsip);  

    if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
    ether.printIp("SRV: ", ether.hisip);

void loop () {
  if (millis() > timer) {
    timer = millis() + 3000;
    int t = dht.readTemperature();
    int h = dht.readHumidity();
    int p = pr/133.322368;
    int a = al/100;
    char buff[1];
    sprintf(buff, "?t=%d&h=%d&a=%d&p=%d", t, h, a, p);
    Serial.print("<<< REQ ");
    ether.browseUrl(PSTR("/update.php"), buff, website, my_callback);

and I have an error

<<< REQ >>>
[b]HTTP/1.1 404 Not Found[/b]
Connection: close
Content-Type: text/plain
Transfer-Encoding: chunked

But if I change string

sprintf(buff, "?t=%d&h=%d&a=%d&p=%d", t, h, a, p);


sprintf(buff, "?t=%d&h=%d&p=%d", t, h, p);

it works perfectly

If I post http://192.168.XXX.XXX/update.php?t=27&h=36&a=195&p=754
it works

What is the problem?

Thanks in advance

sprintf constructs a character string and puts it into the first arg (buff, in your case). You define buff to be just one char large though, and the string sprintf is creating is much larger. Make sure buff is large enough for everything you're putting into it.

But how it is possible that command


with configuration

char buff[1];

has as a result


I can not understand this

Writing past the end of an array like that is undefined. Do not rely on it working, even if it appears to be printing out ok.

I found out one stranger feature If I change string

int a = al/100;


int a = al/100+dht.readHumidity()-dht.readHumidity();

it works i just added dht.readHumidity and subtracted it

Maybe some problems with data type? and in case with

int a = al/100;

"a" is not converting to int?

When sprintf constructs its string, it puts it into the buffer you give it. In your case the buffer is just one byte large. The string it builds is much larger than one byte.

So, where do you suppose it's putting all those extra bytes?

Ok. I’ve got your point
But how can it works with following parameters?

char buff[1];
sprintf(buff, "?t=%d&h=%d&p=%d", t, h, p);

Ok. I've got your point But how can it works with following parameters?

Clearly, you didn't get the point. Your array size is one. You need it to be bigger than 1. It doesn't take a rocket surgeon to figure out that the buffer is too small. It is then trivial to fix the problem.

Would you like to see where the extra characters are put?

char buff[1];
char testBuf[16];
sprintf(buff, "?t=%d&h=%d&p=%d", t, h, p);
strcpy(testBuf,"Bad data");

Last time I checked, testBuf should show all characters except the '?', and the last line should show "?Bad data". You have just been lucky the SRAM was not being used before.

I suggest you stop using sprintf() and start using snprintf() instead, and make sure the size you pass in is the actual size of the buffer.

Yes, PaulS, SurferTim, PeterH Thanks for useful suggestions, it works