Go Down

Topic: Ethercard + ENC28j60 webserver (Read 6201 times) previous topic - next topic

Archelon

Hello Arduino folks!
I'm in need for some help with writing a webserver script for my arduino nano which is using a ENC28J60 module.
I just started out yesterday and after some tries I got this code working:
Code: [Select]
#include <EtherCard.h>

#define STATIC 0  // set to 1 to disable DHCP (adjust myip/gwip values below)

// mac address
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// ethernet interface ip address
static byte myip[] = { 192,168,0,200 };
// gateway ip address
static byte gwip[] = { 192,168,0,1 };

// LED to control output
int ledPin10 = 5;

byte Ethernet::buffer[700];

char page[] PROGMEM =
"HTTP/1.0 503 Service Unavailable\r\n"
"Content-Type: text/html\r\n"
"Retry-After: 600\r\n"
"\r\n"
"<html>"
  "<head><title>"
    "Arduino example"
  "</title></head>"
  "<body>"
    "<h3>Arduino example</h3>"
    "<p><em>"
      "Arduino example<br />"
      "Arduino example"
    "</em></p>"
  "</body>"
"</html>"
;

void setup () {
  pinMode(ledPin10, OUTPUT);

  Serial.begin(9600);
  Serial.println("Trying to get an IP...");

  Serial.print("MAC: ");
  for (byte i = 0; i < 6; ++i) {
    Serial.print(mymac[i], HEX);
    if (i < 5)
      Serial.print(':');
  }
  Serial.println();
 
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");

#if STATIC
  Serial.println( "Getting static IP.");
  if (!ether.staticSetup(myip, gwip)){
    Serial.println( "could not get a static IP");
    blinkLed();     // blink forever to indicate a problem
  }
#else

  Serial.println("Setting up DHCP");
  if (!ether.dhcpSetup()){
    Serial.println( "DHCP failed");
    blinkLed();     // blink forever to indicate a problem
  }
#endif
 
  ether.printIp("My IP: ", ether.myip);
  ether.printIp("Netmask: ", ether.mymask);
  ether.printIp("GW IP: ", ether.gwip);
  ether.printIp("DNS IP: ", ether.dnsip);
}

void loop () {

  word len = ether.packetReceive();
  word pos = ether.packetLoop(len);

  // IF LED10=ON turn it ON
  if(strstr((char *)Ethernet::buffer + pos, "GET /?LED10=ON") != 0) {
      Serial.println("Received ON command");
      digitalWrite(ledPin10, HIGH);
    }

    // IF LED10=OFF turn it OFF 
    if(strstr((char *)Ethernet::buffer + pos, "GET /?LED10=OFF") != 0) {
      Serial.println("Received OFF command");
      digitalWrite(ledPin10, LOW);
    }

    // show some data to the user
    memcpy_P(ether.tcpOffset(), page, sizeof page);
    ether.httpServerReply(sizeof page - 1);
}

void blinkLed(){
  while (true){
    digitalWrite(ledPin10, HIGH);
    delay(500);
    digitalWrite(ledPin10, LOW);
    delay(500);
  }
}


I know the basics of the Arduino language but I'm not familiar with this ethercard library and webservers.
I need your help to create a script that includes the following:

Two buttons that call two different functions when pressed.
A textline that displays an analog input value.

This shouldn't be to complicated I guess but I really need your help guys. Thank you very much!



tbradford1980

I too am seeking to achieve the same and struggling, I can get http on off led working and have an example http analogue output example but I'm unable to get the page to display both.

include <EtherCard.h>

#define STATIC 1  // set to 1 to disable DHCP (adjust myip/gwip values below)

#if STATIC
// ethernet interface ip address
static byte myip[] = { 10,23,4,1 };
// gateway ip address
static byte gwip[] = { 192,168,1,1 };
#endif

// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
#define LED 3  // define LED pin
bool ledStatus = false;

byte Ethernet::buffer[500]; // tcp/ip send and receive buffer
BufferFiller bfill;

void setup(){
  Serial.begin(57600);
  Serial.println("\n[backSoon]");
 
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");
#if STATIC
  ether.staticSetup(myip, gwip);
#else
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");
#endif

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip); 
  ether.printIp("DNS: ", ether.dnsip); 
}
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='5'/>"
        "<title>Ethercard LED</title>"
        "ledStatus: <a href=\"?led=$F\">$F</a>"),  /////////////////     <<<<<< Trying to add additional bits here but failing !
        http_OK,
        ledStatus?PSTR("off"):PSTR("on"),
        ledStatus?PSTR("ON"):PSTR("OFF"));
}

void loop(){
  // DHCP expiration is a bit brutal, because all other ethernet activity and
  // incoming packets will be ignored until a new lease has been acquired
  //if (!STATIC && ether.dhcpExpired()) {
//'   Serial.println("Acquiring DHCP lease again");
//  ' ether.dhcpSetup();
//' }
    digitalWrite(LED, ledStatus);
    Serial.println(ledStatus);
  // wait for an incoming TCP packet, but ignore its contents
   word len = ether.packetReceive();
   word pos = ether.packetLoop(len);
  if (pos) {
      // write to LED digital output
   
    delay(1);   // necessary for my system
        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("?led=on ", data, 8) == 0) {
                // Set ledStatus true and redirect to home page
                ledStatus = true;
                bfill.emit_p(http_Found);
            }
            else if (strncmp("?led=off ", data, 9) == 0) {
                // Set ledStatus false and redirect to home page
                ledStatus = false;
                bfill.emit_p(http_Found);
            }
            else {
                // Page not found
                bfill.emit_p(http_Unauthorized);
            }
        }
       
        ether.httpServerReply(bfill.position());    // send http response
    }

  }

tbradford1980

I have my variable input added below but how do I add sensorValue to the http page, I have added bits of code all over the home page area but it then just fails to load the web page, also I see $F what is that and where does it get it's info from

if I could set sensor Value to $a and as below, guess that would be too easy, well it's doesn't work anyhow

"ledStatus: <a href=\"?led=$F\">$F</a>$a"),




#include <EtherCard.h>
#include <EEPROM.h> // allow settings to save to flash
const int  lowhot = 25;
const int  highhot = 40;
const int fudginghot = 50 ;
const int lowcool = 18;
const int highcool = 24;
const int freezing = 17;
const int ledPin = 13;
byte epread1;
int incomingByte;


#define STATIC 1  // set to 1 to disable DHCP (adjust myip/gwip values below)

#if STATIC
// ethernet interface ip address
static byte myip[] = { 10,23,4,1 };
// gateway ip address
static byte gwip[] = { 192,168,1,1 };
#endif

// ethernet mac address - must be unique on your network
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
#define LED 3  // define LED pin
bool ledStatus = false;

byte Ethernet::buffer[500]; // tcp/ip send and receive buffer
BufferFiller bfill;

void setup(){
  Serial.begin(9600);
  Serial.println("\n[backSoon]");
 
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");
#if STATIC
  ether.staticSetup(myip, gwip);
#else
  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");
#endif

  ether.printIp("IP:  ", ether.myip);
  ether.printIp("GW:  ", ether.gwip); 
  ether.printIp("DNS: ", ether.dnsip); 
}
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='5'/>"
        "<title>Ethercard LED</title>"
        "ledStatus: <a href=\"?led=$F\">$F</a>"),
        http_OK,
        ledStatus?PSTR("off"):PSTR("on"),
        ledStatus?PSTR("ON"):PSTR("OFF"));
       
}

void loop(){
  // DHCP expiration is a bit brutal, because all other ethernet activity and
  // incoming packets will be ignored until a new lease has been acquired
  //if (!STATIC && ether.dhcpExpired()) {
//'   Serial.println("Acquiring DHCP lease again");
//  ' ether.dhcpSetup();
//' }
int sensorValue = analogRead(A0);

    digitalWrite(LED, ledStatus);
    Serial.println(ledStatus);
  // wait for an incoming TCP packet, but ignore its contents
   word len = ether.packetReceive();
   word pos = ether.packetLoop(len);
  if (pos) {
      // write to LED digital output
   
    delay(1);   // necessary for my system
        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("?led=on ", data, 8) == 0) {
                // Set ledStatus true and redirect to home page
                ledStatus = true;
                bfill.emit_p(http_Found);
            }
            else if (strncmp("?led=off ", data, 9) == 0) {
                // Set ledStatus false and redirect to home page
                ledStatus = false;
                bfill.emit_p(http_Found);
            }
            else {
                // Page not found
                bfill.emit_p(http_Unauthorized);
            }
        }
       
        ether.httpServerReply(bfill.position());    // send http response
    }

  }

PaulS

Quote
I have my variable input added below but how do I add sensorValue to the http page

Make appropriate changes to homePage();

Quote
I have added bits of code all over the home page area

But, I'm not going to show you the changes.

Quote
but it then just fails to load the web page

I refuse to play guessing games.

Quote
also I see $F

See above. Where do you see this?

Quote
what is that and where does it get it's info from

See above.

Quote
if I could set sensor Value to $a

You could, if you were using PHP. But, you aren't, so you can't.

PS: I also refuse to read any more of your posts until you learn to post code correctly. The stickies at the top of the forum explain how. You were supposed to read them FIRST.
The art of getting good answers lies in asking good questions.

tbradford1980

shame your not so keen to assist the original poster as you are to post sarcastic comments.

bye bye 

Go Up