Go Down

Topic: simple toggle web server (Read 4886 times) previous topic - next topic

mkiriti

hey guys i wanted to create a simple webserver whith toggle to turn of multiple leds 5 maybe
i am using enc28j60 breakout board

i came across a toggle web server code using ethershield library, it was easy to understand but that library is not working with my breakout board i dont know whats the problem :(


so i tried other library called ethercard and it seems to work fine, but i cant understand it properly to write a code to make run toggle webserver to control leds

Code: [Select]
// Present a "Will be back soon web page", as stand-in webserver.
// 2011-01-30 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php

#include <EtherCard.h>

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

#if STATIC
// ethernet interface ip address
static byte myip[] = { 192,168,1,200 };
// 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 };

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

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>"
    "Service Temporarily Unavailable"
  "</title></head>"
  "<body>"
    "<h3>This service is currently unavailable</h3>"
    "<p><em>"
      "The main server is currently off-line.<br />"
      "Please try again later."
    "</em></p>"
  "</body>"
"</html>"
;

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); 
}

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();
  }
   
  // wait for an incoming TCP packet, but ignore its contents
  if (ether.packetLoop(ether.packetReceive())) {
    memcpy_P(ether.tcpOffset(), page, sizeof page);
    ether.httpServerReply(sizeof page - 1);
  }
}


here is the basic code which just displays service unavailable so can any one  help me to write a code using this library, it would be really useful :)

and the functionality can be as simple as shown in this video http://www.youtube.com/watch?feature=player_embedded&v=SU0m_MJh6mY

thank you

mkiriti


zoomkat


help me any  one :)


The enc28j60 appears to be for very experienced programmers. On ebay the wiznet shields are now within a couple of $$ of the enc28j60 and are much easier to work with with less programming experience.
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

mkiriti

thanks ,but they are in hongkong and it will easily take one month to ship....... but i have to complete my project under a month :(

so maybe a simple code would do to control 2 leds using the ethercard library

mkiriti

and also the ethershield library does not work only with my module,

connecting si-11,so-12,sck-13, cs-10

why isnt normal ethershield library working with my module?? am i missing anything ?? or do i have to wire something extra, if u can help me on making ethershield library work with my enc28j60 module it would be really awesome :)

pescadito

hey,
i use http://jeelabs.net/projects/cafe/wiki/EtherCard and work well
more info is in http://www.lucadentella.it/category/enc28j60-arduino/
i connect the enc28j60 to arduino-nano like this
-----------------------------------------------------
          Interface Nano - ENC28J60 - SPI
-              CLI / OUT   -         INT       d03
-                      WOL  -          SO        d12
d11                       SI  -         SCK        d13
d10       CS ChipSelect  -       RESET       rst
3v3                    VCC -          GND      gnd
-----------------------------------------------------

here some examples
------------------------------------------------------------------------------------------------------------------------
// ip from dhcp
//
#include <EtherCard.h>
static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x01};
char website[] PROGMEM = "google.com";

byte Ethernet::buffer[700];

void setup () {

  Serial.begin(57600);
  Serial.println("DHCP Demo");

  if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
    Serial.println( "Failed to access Ethernet controller");
else
   Serial.println("Ethernet controller initialized");

  if (!ether.dhcpSetup())
    Serial.println("Failed to get configuration from DHCP");
  else
    Serial.println("DHCP configuration done");

  ether.printIp("IP Address:\t", ether.myip);
  ether.printIp("Netmask:\t", ether.mymask);
  ether.printIp("Gateway:\t", ether.gwip);
 
  if (!ether.dnsLookup(website))
    Serial.println("DNS failed");
 
  Serial.print("google.com"); ether.printIp(" SRV: ", ether.hisip); 
  Serial.println("------------------------");
}

void loop() {

  ether.packetLoop(ether.packetReceive());
}
------------------------------------------------------------------------------------------------------------------------
// Ping a remote server, also uses DHCP and DNS.   (1)
// 2011-06-12 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php

#include <EtherCard.h>

// 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;

// called when a ping comes in (replies to it are automatic)
static void gotPinged (byte* ptr) {
  ether.printIp(">>> ping from: ", ptr);
}

void setup () {
  Serial.begin(57600);
  Serial.println("\n[pings]");
 
  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);

#if 1
  // use DNS to locate the IP address we want to ping
  if (!ether.dnsLookup(PSTR("www.google.com")))
    Serial.println("DNS failed");
#else
  ether.parseIp(ether.hisip, "74.125.77.99");
#endif
  ether.printIp("SRV: ", ether.hisip);
   
  // call this to report others pinging us
  ether.registerPingCallback(gotPinged);
 
  timer = -9999999; // start timing out right away
  Serial.println();
}

void loop () {
  word len = ether.packetReceive(); // go receive new packets
  word pos = ether.packetLoop(len); // respond to incoming pings
 
  // report whenever a reply to our outgoing ping comes back
  if (len > 0 && ether.packetLoopIcmpCheckReply(ether.hisip)) {
    Serial.print("  ");
    Serial.print((micros() - timer) * 0.001, 3);
    Serial.println(" ms");
  }
 
  // ping a remote server once every few seconds
  if (micros() - timer >= 5000000) {
    ether.printIp("Pinging: ", ether.hisip);
    timer = micros();
    ether.clientIcmpRequest(ether.hisip);
  }
}
------------------------------------------------------------------------------------------------------------------------
// Ping a remote server, (2)
// 2011-06-12 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
//
#include <EtherCard.h>
static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x00};
static byte myip[] = {192,168,1,15};

// static byte staticmask[] = {255,255,255,128};
//  ether.copyIp(mymask, staticmask);

byte Ethernet::buffer[700];


void setup () {
  Serial.begin(57600);
  Serial.println("PING Demo");

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

  if (!ether.staticSetup(myip))
    Serial.println("Failed to set IP address");
}

void loop() {

  ether.packetLoop(ether.packetReceive());
}

------------------------------------------------------------------------------------------------------------------------
// Simple demo for feeding some random data to Pachube.
// 2011-07-08 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php

#include <EtherCard.h>

// change these settings to match your own setup
#define FEED    "use yours"
#define APIKEY  "use yours"
byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
char website[] PROGMEM = "api.pachube.com";

byte Ethernet::buffer[700];
uint32_t timer;
Stash stash;

void setup () {
  Serial.begin(57600);
  Serial.println("\n[webClient]");

  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("api.pachube.com SRV: ", ether.hisip);
  Serial.println("-------------------------    ---------");
 
}

void loop () {
  ether.packetLoop(ether.packetReceive());
 
  if (millis() > timer) {
    timer = millis() + 10000;
   
    // generate two fake values as payload - by using a separate stash,
    // we can determine the size of the generated message ahead of time
    byte sd = stash.create();
    stash.print("0,");
    stash.println((word) millis() / 123);
    stash.print("1,");
    stash.println((word) micros() / 456);
    stash.save();
   
    // generate the header with payload - note that the stash size is used,
    // and that a "stash descriptor" is passed in as argument using "$H"
    Stash::prepare(PSTR("PUT http://$F/v2/feeds/$F.csv HTTP/1.0" "\r\n"
                        "Host: $F" "\r\n"
                        "X-PachubeApiKey: $F" "\r\n"
                        "Content-Length: $D" "\r\n"
                        "\r\n"
                        "$H"),
            website, PSTR(FEED), website, PSTR(APIKEY), stash.size(), sd);

    // send the packet - this also releases all stash buffers once done
    ether.tcpSend();
  }
}
------------------------------------------------------------------------------------------------------------------------

best regards, pescadito

Go Up