Need helpfor code with ENC28J60 using ethercard.h

I'm new to try the ENC28J60 board and want to understand how the library ethercard.h is working.
First I found some demo code and it seems all hardware works fine. But I'm stucked when I try to add more functions to the code.
My problem is, I want to read the temperature and turn on/off a relay. I found a code for the relay function which works fine, but want to add the temperature. I attach my code and ask for help to insert the temp into my code.

// Ethercard REL example
#include <EtherCard.h>
#include <OneWire.h>
#include <EEPROM.h>
#include <DallasTemperature.h>

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,1,177 };

#define BUFFER_SIZE 500
byte Ethernet::buffer[BUFFER_SIZE];
BufferFiller bfill;

#define REL 5  // define REL pin
bool RELStatus = false;
int temp;

#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

const char http_OK[] PROGMEM =
    "HTTP/1.0 200 OK\r\n"
    "Content-Type: text/html\r\n"
    "Pragma: no-cache\r\n\r\n";

const char http_Found[] PROGMEM =
    "HTTP/1.0 302 Found\r\n"
    "Location: /\r\n\r\n";

const char http_Unauthorized[] PROGMEM =
    "HTTP/1.0 401 Unauthorized\r\n"
    "Content-Type: text/html\r\n\r\n"
    "<h1>401 Unauthorized</h1>";

void homePage()
{
    bfill.emit_p(PSTR("$F"
      "<meta http-equiv='refresh' content='10'/>"
      "<title>Temp Server</title>" 
      "<body bgcolor=""#99CCFF"">"
      "<center>"
      "
"
      "<font size=""7"">"
      "Temperature: <b>"
      "23.5 &deg;C"           // <<<< This need to be dynamic (variable = temp)
      //"$D.0 &deg;C"
      "
" 
      "
"
      "</b>"
      "Relay Status: <a href=\"?REL=$F\">$F</a><b>"), http_OK, RELStatus?PSTR("off"):PSTR("on"), RELStatus?PSTR("ON"):PSTR("OFF"));
}

void setup()
{
    Serial.begin(9600);
    pinMode(REL, OUTPUT);
    if (ether.begin(BUFFER_SIZE, mymac) == 0)
    Serial.println("Cannot initialise ethernet.");
    else
    Serial.println("Ethernet initialised.");
    ether.staticSetup(myip);
}

void loop()
{
  digitalWrite(REL, RELStatus);   // write to REL digital output
  delay(1);   // necessary for my system
  word len = ether.packetReceive();   // check for ethernet packet
  word pos = ether.packetLoop(len);   // check for tcp packet
  sensors.requestTemperatures();
  temp = (sensors.getTempCByIndex(0));  
   if (pos) {
        bfill = ether.tcpOffset();
        char *data = (char *) Ethernet::buffer + pos;
        
        if (strncmp("GET /", data, 5) != 0) {
            // Unsupported HTTP request
            // 304 or 501 response would be more appropriate
            bfill.emit_p(http_Unauthorized);
        }
        else {
            data += 5;
            
            if (data[0] == ' ') {
                // Return home page
                homePage();
            }
            else if (strncmp("?REL=on ", data, 8) == 0) {
                // Set RELStatus true and redirect to home page
                RELStatus = true;
                bfill.emit_p(http_Found);
            }
            else if (strncmp("?REL=off ", data, 9) == 0) {
                // Set RELStatus false and redirect to home page
                RELStatus = false;
                bfill.emit_p(http_Found);
            }
            else {
                // Page not found
                bfill.emit_p(http_Unauthorized);
            }
        }
      ether.httpServerReply(bfill.position());    // send http response
    }
}

It is hard for me to read the code, it mixes to much things together.

Are you using the newest Arduino 1.0.5 ?
Did you use this library ? GitHub - njh/EtherCard: EtherCard is an IPv4 driver for the ENC28J60 chip, compatible with Arduino IDE
When looking for examples, take first a look at the examples that came with that library.

Is the hardware working ? Can you run a webpage ? Does DHCP work ?

When calling bfill.emit_p(), you can write the integers as parameters.
Like in this example:

First of all, write the webpage with const PROGMEM, just like the other ones (the http_OK, http_Found, and so on).
After that, don't call 'homePage();', but something like: 'bfill.emit_p( http_homePage, temp);'.
The '$D' will be filled with the 'temp'.

What is the "$F" for ?
The Ethercard supports also other formats. But I think "$F" is for floating point, that is not enabled by default in the Ethercard library.

My problem is, I want to read the temperature and turn on/off a relay.

What does this have to do with the ethernet card? Your not reading the temperature from a web server, are you?

Please look at the code. Everything is working but I need to read the temperature dynamic, not at a static value as shown in code.

Okay, it is already working. That's very good.
Can you read my previous post again (moving the webpage to const PROGMEM, using bfill.emit_p() with parameters).

I think my problem is this code line:
"Relay Status: <a href="?REL=$F">$F"), http_OK, RELStatus?PSTR("off"):PSTR("on"), RELStatus?PSTR("ON"):PSTR("OFF"));
How do I insert the value temp?

FYI! I can control the LED (on/off), but I neet to read out the temperature. I want to catch the temp variable.
How does it work ?

For the third time: the function bfill.emit_p() accepts parameters like the temperature.

Thanks, could you explain how, by insert the "temp" value into my code line: "Relay Status: <a href="?REL=$F">$F"), http_OK, RELStatus?PSTR("off"):PSTR("on"), RELStatus?PSTR("ON"):PSTR("OFF"));
. This will help a lots to understand how it works. Alternative please give me a link to read about it. I have search for it with out luck.

You will be using the temperature like this ?
"Temperature: $D.0 °C"

I see that the $F is for a string, not for floating point as I assumed earlier.
The first $F will be filled with http_OK.
The second parameter will be the temperature.
The third and fourth are both $F and is the status for the relay.

So your line will be like:

"Relay Status: <a href=\"?REL=$F\">$F</a><b>"), http_OK, temp, RELStatus?PSTR("off"):PSTR("on"), RELStatus?PSTR("ON"):PSTR("OFF"));

Since 'temp' is a global variable, you can use it just like that.

I still think your sketch can look better, if you move the webpage code out of the homePage() function and make it like the three other const PROGMEM texts.
Also the ?REL=$F">$F and RELStatus?PSTR("off"):PSTR("on") are confusing. They could be rewritten into a few lines of code.

Thanks for your help. Problem solved it, works. FYI I tried to insert the temp before http_OK but show error at webpage. Is it important where the variable is inserted?

I really want to understand the way it works, variable parsed to webpage. Could you explain how to parse variables into a webpage, or show a link to a page to read more.

Question: Is it possible to parse float directly or do I need a conversion to String before?

I think you have to change a setting in the library for floating point.
You can use dtostrf(), but you have to create a buffer for the output string.

Could you study the way printf or sprintf works ?
It uses the same flexible way of parameters.
(the function sprintf does not support float in the Arduino environment).

When bfill.emit_p() sees a $D, it grabs an integer from the parameters.
When bfill.emit_p() sees a $F, it grabs a pointer to a string.

So when you do this:

int x = 3;
bfill.emit_p( "$D $F", x, "hello");

It matches, the 'x' is an integer and "hello" is a string.

Thanks, my code works fine even with float. I attach the code for other to be used.

// Ethercard REL example
#include <EtherCard.h>
#include <OneWire.h>
#include <EEPROM.h>
#include <DallasTemperature.h>

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,1,177 };

#define BUFFER_SIZE 500
byte Ethernet::buffer[BUFFER_SIZE];
BufferFiller bfill;

#define REL 5  // define REL pin
bool RELStatus = false;
float temp;
char temp_str[10];

#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

const char http_OK[] PROGMEM =
    "HTTP/1.0 200 OK\r\n"
    "Content-Type: text/html\r\n"
    "Pragma: no-cache\r\n\r\n";

const char http_Found[] PROGMEM =
    "HTTP/1.0 302 Found\r\n"
    "Location: /\r\n\r\n";

const char http_Unauthorized[] PROGMEM =
    "HTTP/1.0 401 Unauthorized\r\n"
    "Content-Type: text/html\r\n\r\n"
    "<h1>401 Unauthorized</h1>";

void homePage()
{
    bfill.emit_p(PSTR("$F"
      "<meta http-equiv='refresh' content='10'/>"
      "<title>Temp Server</title>" 
      "<body bgcolor=""#99CCFF"">"
      "<center>"
      "
"
      "<font size=""7"">"
      "Temperature: <b>"
      "$S &deg;C"
      "
" 
      "
"
      "</b>"
      "Relay Status: <a href=\"?REL=$F\">$F</a><b>"), http_OK, temp_str, RELStatus?PSTR("off"):PSTR("on"), RELStatus?PSTR("ON"):PSTR("OFF"));
}

void setup()
{
    Serial.begin(9600);
    pinMode(REL, OUTPUT);
    if (ether.begin(BUFFER_SIZE, mymac) == 0)
    Serial.println("Cannot initialise ethernet.");
    else
    Serial.println("Ethernet initialised.");
    ether.staticSetup(myip);
}

void loop()
{
  digitalWrite(REL, RELStatus);   // write to REL digital output
  delay(1);   // necessary for my system
  word len = ether.packetReceive();   // check for ethernet packet
  word pos = ether.packetLoop(len);   // check for tcp packet
  sensors.requestTemperatures();
  temp = (sensors.getTempCByIndex(0));  
  dtostrf(temp, 6, 2, temp_str);
  
   if (pos) {
        bfill = ether.tcpOffset();
        char *data = (char *) Ethernet::buffer + pos;
        
        if (strncmp("GET /", data, 5) != 0) {
            // Unsupported HTTP request
            // 304 or 501 response would be more appropriate
            bfill.emit_p(http_Unauthorized);
        }
        else {
            data += 5;
            
            if (data[0] == ' ') {
                // Return home page
                homePage();
            }
            else if (strncmp("?REL=on ", data, 8) == 0) {
                // Set RELStatus true and redirect to home page
                RELStatus = true;
                bfill.emit_p(http_Found);
            }
            else if (strncmp("?REL=off ", data, 9) == 0) {
                // Set RELStatus false and redirect to home page
                RELStatus = false;
                bfill.emit_p(http_Found);
            }
            else {
                // Page not found
                bfill.emit_p(http_Unauthorized);
            }
        }
      ether.httpServerReply(bfill.position());    // send http response
    }
}

I know this is a really old thread but I had to reply to thank you for sharing your working code psnap
I've spent many hours trying to achieve this and got close but this code saved the day.
Thank you again.