element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Can anyone be kind enough to help me modify and add my code for me??
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Suggested Answer
  • Replies 3 replies
  • Answers 1 answer
  • Subscribers 416 subscribers
  • Views 589 views
  • Users 0 members are here
Related

Can anyone be kind enough to help me modify and add my code for me??

e14 Contributor
e14 Contributor over 13 years ago

hi to all of you mates.. im new to arduino and im working on this project web switch which can control on and off of appliance via web but my problem is i cant get to add another button for another relay channel, please help meeee imageimageimage tnx mates!!!

 

 

 

#include "etherShield.h"

 

 

 

 

// please modify the following two lines. mac and ip have to be unique

// in your local area network. You can not have the same numbers in

// two devices:

static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x25};

static uint8_t myip[4] = {192,168,1,25};

static char baseurl[]="http://192.168.1.25/";

static uint16_t mywwwport =80; // listen port for tcp/www (max range 1-254)

 

 

 

 

 

 

#define BUFFER_SIZE 600

static uint8_t buf[BUFFER_SIZE+1];

#define STR_BUFFER_SIZE 22

static char strbuf[STR_BUFFER_SIZE+1];

 

 

EtherShield es=EtherShield();

 

 

// prepare the webpage by writing the data to the tcp send buffer

uint16_t print_webpage(uint8_t *buf, byte on_off);

int8_t analyse_cmd(char *str);

 

 

// LED cathode connects the Pin4, anode to 5V through 1K resistor

#define RELAY_PIN  8

 

 

 

 

void setup(){

 

   /* Setup Serial */

   Serial.begin(9600);

   pinMode(RELAY_PIN, OUTPUT);

   digitalWrite(RELAY_PIN, 0);  // switch on LED

   /*initialize ENC28J60/MLENC28J60/ML*/

           es.ES_enc28j60Init(mymac);

   es.ES_enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz

   delay(10);

       

          /* Magjack leds configuration, see ENC28J60/MLENC28J60/ML datasheet, page 11 */

          // LEDA=greed LEDB=yellow

          //

          // 0x880 is PHLCON LEDB=on, LEDA=on

          // enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);

          es.ES_enc28j60PhyWrite(PHLCON,0x880);

          delay(500);

          //

          // 0x990 is PHLCON LEDB=off, LEDA=off

          // enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);

          es.ES_enc28j60PhyWrite(PHLCON,0x990);

          delay(500);

          //

          // 0x880 is PHLCON LEDB=on, LEDA=on

          // enc28j60PhyWrite(PHLCON,0b0000 1000 1000 00 00);

          es.ES_enc28j60PhyWrite(PHLCON,0x880);

          delay(500);

          //

          // 0x990 is PHLCON LEDB=off, LEDA=off

          // enc28j60PhyWrite(PHLCON,0b0000 1001 1001 00 00);

          es.ES_enc28j60PhyWrite(PHLCON,0x990);

          delay(500);

          //

  // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit

  // enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10);

  es.ES_enc28j60PhyWrite(PHLCON,0x476);

          delay(100);

       

  //init the ethernet/ip layer:

  es.ES_init_ip_arp_udp_tcp(mymac,myip,mywwwport);

 

 

}

 

 

void loop(){

  uint16_t plen, dat_p;

  int8_t cmd;

  byte on_off = 1;

 

 

 

 

  plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);

 

 

          /*plen will ne unequal to zero if there is a valid packet (without crc error) */

  if(plen!=0){

                    

    // arp is broadcast if unknown but a host may also verify the mac address by sending it to a unicast address.

    if(es.ES_eth_type_is_arp_and_my_ip(buf,plen)){

      es.ES_make_arp_answer_from_request(buf);

      return;

    }

 

 

    // check if ip packets are for us:

    if(es.ES_eth_type_is_ip_and_my_ip(buf,plen)==0){

      return;

    }

   

    if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V){

      es.ES_make_echo_reply_from_request(buf,plen);

      return;

    }

   

    // tcp port www start, compare only the lower byte

    if (buf[IP_PROTO_P]==IP_PROTO_TCP_V&&buf[TCP_DST_PORT_H_P]==0&&buf[TCP_DST_PORT_L_P]==mywwwport){

      if (buf[TCP_FLAGS_P] & TCP_FLAGS_SYN_V){

         es.ES_make_tcp_synack_from_syn(buf); // make_tcp_synack_from_syn does already send the syn,ack

         return;    

      }

      if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V){

        es.ES_init_len_info(buf); // init some data structures

        dat_p=es.ES_get_tcp_data_pointer();

        if (dat_p==0){ // we can possibly have no data, just ack:

          if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V){

            es.ES_make_tcp_ack_from_any(buf);

          }

          return;

        }

        if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){

                    // head, post and other methods for possible status codes see:

            // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

            plen=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n<h1>200 OK</h1>"));

            goto SENDTCP;

        }

           if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){

                plen=print_webpage(buf, on_off);

            goto SENDTCP;

         }

        cmd=analyse_cmd((char *)&(buf[dat_p+5]));

       

        //Debug

        Serial.print(cmd, DEC);

        Serial.println("");

      

        if (cmd==2 || cmd==5){

                on_off=1;

                digitalWrite(RELAY_PIN, HIGH);  // switch on LED

                Serial.println("Turning LED ON");

        }

        else if (cmd==3 || cmd==4 ){

                on_off=0;

                  digitalWrite(RELAY_PIN, LOW);  // switch off LED

                Serial.println("Turning LED OFF");

        }

       

        plen=print_webpage(buf, on_off);

       

        //plen=print_webpage(buf, on_off);

       

SENDTCP:  es.ES_make_tcp_ack_from_any(buf); // send ack for http get

           es.ES_make_tcp_ack_with_data(buf,plen); // send data      

      }

    }

  }

       

}

// The returned value is stored in the global var strbuf

uint8_t find_key_val(char *str,char *key)

{

        uint8_t found=0;

        uint8_t i=0;

        char *kp;

        kp=key;

        while(*str &&  *str!=' ' && found==0){

                if (*str == *kp){

                        kp++;

                        if (*kp == '\0'){

                                str++;

                                kp=key;

                                if (*str == '='){

                                        found=1;

                                }

                        }

                }else{

                        kp=key;

                }

                str++;

        }

        if (found==1){

                // copy the value to a buffer and terminate it with '\0'

                while(*str &&  *str!=' ' && *str!='&' && i<STR_BUFFER_SIZE){

                        strbuf[i]=*str;

                        i++;

                        str++;

                }

                strbuf[i]='\0';

        }

        return(found);

}

 

 

int8_t analyse_cmd(char *str)

{

        int8_t r=-1;

    

        if (find_key_val(str,"cmd")){

                if (*strbuf < 0x3a && *strbuf > 0x2f){

                        // is a ASCII number, return it

                        r=(*strbuf-0x30);

                }

        }

        return r;

}

 

 

 

 

uint16_t print_webpage(uint8_t *buf, byte on_off)

{

 

 

       int i=0;

   

       

        uint16_t plen;

       

 

       

        plen=es.ES_fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"));

        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<center><p><h1> project WebSwitch! </h1></p> "));

        

        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<hr><br><form METHOD=get action=\""));

        plen=es.ES_fill_tcp_data(buf,plen,baseurl);

        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\">"));

        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<h2> Lightbulb is  </h2> "));

        if (on_off){

               plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<h1><font color=\"#FF0000\"> "));

        } else {

            plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<h1><font color=\"#00FF00\">"));

        } 

        

        if(on_off)

                  plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("OFF"));

        else

                  plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("ON"));

       

        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("  </font></h1><br> ") );

       

        if(on_off){

                  plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=3>"));

                  plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Switch off\"></form>"));

#ifdef SECOND_SWITCH                       

                plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<hr><br><form METHOD=get action=\""));

                plen=es.ES_fill_tcp_data(buf,plen,baseurl);

                plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\">"));

                plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=4>"));

                plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Switch on\"></form>"));

#endif               

        }

        else {

                  plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=2>"));

                  plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Switch off\"></form>"));

               

#ifdef SECOND_SWITCH               

                plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<hr><br><form METHOD=get action=\""));

                plen=es.ES_fill_tcp_data(buf,plen,baseurl);

                plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("\">"));

                plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=hidden name=cmd value=5>"));

                  plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"Switch off\"></form>"));

#endif

        }

       

        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("</center><hr> <p> "));

 

        return(plen);

}

  • Sign in to reply
  • Cancel
  • shabaz
    0 shabaz over 13 years ago

    I think you may need to help yourself a little, otherwise you'll be dependant on others each time you want the code tweaked.

    To help you, notice that some of the code has #ifdef SECOND_SWITCH

     

    Also, notice this portion of code:

    if (cmd==2 || cmd==5){

                    on_off=1;

                    digitalWrite(RELAY_PIN, HIGH);  // switch on LED

                    Serial.println("Turning LED ON");

            }

            else if (cmd==3 || cmd==4 ){

                    on_off=0;

                      digitalWrite(RELAY_PIN, LOW);  // switch off LED

                    Serial.println("Turning LED OFF");

            }


     

    You'd have to expand that so that commands 2 and 3 control the first relay, and 4 and 5 control the second relay for example.

    The relay is programmed using the digitalWrite(RELAY_PIN,...) command and RELAY_PIN is defined at the top:

    #define RELAY_PIN  8

    You'd create another definition too, e.g.

    #define RELAY_PINB x

    (Choose x to be a free pin on your microcontroller - probably by looking at the circuit, and the microcontroller or board documentation). Notice the code also contains pinMode(RELAY_PIN, OUTPUT); so you'd need to do something similar for your new definition too.

     

    That's a lot of help. Good luck.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • e14 Contributor
    0 e14 Contributor over 13 years ago in reply to shabaz

    can't figure it out mate please helpimageimageimage i tried every possible way to edit it but no luck

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • shabaz
    0 shabaz over 13 years ago in reply to e14 Contributor

    I don't have an arduino so I wouldn't be able to test it out to confirm for you anyway.

    Nor do I really know in detail what your board looks like, and if you even have an additional

    relay wired up, what pins, nor even what microcontroller, or if the code compiled and

    downloaded successfully.

    There are so many points at which things may go wrong, it is impossible to tell unless you supply

    detail of exactly what doesn't work for you.

     

    If you try to modify the code as suggested above (and insert #define SECOND_SWITCH

    at the top of the file), you could try it.

    Which bit are you stuck on? Does it not compile/generate an error or something? If so, what error did you get?

    Or does it download successfully into your microcontroller, but just does not control the

    relay?

     

    Also, look at the code (the portion that I highlighted) - any part of that you're confused on?

     

    The '||' means 'or', and '==' means 'is equal to' if that is tripping

    you up (we don't use a single '=' except when modifying values).

    The rest is self-explanatory in the portion that I highlighted.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube