Internet Web Switch

Hello guys, i'm still working on Internet Web Switch.. I'm currently using this code.. but unfortunately it can only control 1 pin from my ethernet shield :frowning: anyone can teach me how to populate the controls?

#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,0x24}; 
static uint8_t myip[4] = {192,168,1,15};
static char baseurl[]="http://192.168.1.15/";
static uint16_t mywwwport =80; // listen port for tcp/www (max range 1-254)



#define BUFFER_SIZE 500
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 LED_PIN  4


void setup(){
  
   /*initialize enc28j60*/
	 es.ES_enc28j60Init(mymac);
   es.ES_enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
   delay(10);
        
	/* Magjack leds configuration, see enc28j60 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,80);
  
 	pinMode(LED_PIN, OUTPUT); 
 	digitalWrite(LED_PIN, LOW);  // switch on LED
}

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]));
        
        if (cmd==2){
                on_off=1;
        	digitalWrite(LED_PIN, LOW);  // switch on LED
        }
        else if (cmd==3){
                on_off=0;
        	digitalWrite(LED_PIN, HIGH);  // switch off LED
        }
        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>Welcome to Arduino Ethernet Shield V1.0  </h1></p> "));
         
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("<hr>
<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> REMOTE LED is  </h2> "));
 				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("ON"));
        else 
        	plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("OFF"));
        
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("  </font></h1>
 ") );
        
        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>"));
        }
        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 on\"></form>"));
        }
        
        plen=es.ES_fill_tcp_data_p(buf,plen,PSTR("</center><hr> <p> V1.0 <a href=\"http://www.nuelectronics.com\">www.nuelectronics.com<a>"));
  
        return(plen);
}

but unfortunately it can only control 1 pin from my ethernet shield

That is not true.

What is true is that your analyse_cmd() function only knows how to extract one kind of data from the GET request. You'll need to fix that to handle more than one kind of GET request.

Of course, your web page will need to have more than one input field, for each pin that you want to toggle. And, of course, you'll need to keep track of the state of more than one pin.

but unfortunately it can only control 1 pin from my ethernet shield

Web server test code that controls more than one arduino pin.

//zoomkat 5-30-15
//simple button GET with iframe code
//open serial monitor to see what the arduino receives
//use the ' instead of " in html ilnes 
//address will look like http://192.168.1.102:84/ when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //ethernet shield mac address
byte ip[] = {192, 168, 1, 102 }; // arduino IP in lan
byte gateway[] = {192, 168, 1, 1 }; // internet access via router
byte subnet[] = {255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(4, OUTPUT); //pin selected to control
  pinMode(5, OUTPUT); //pin selected to control
  pinMode(6, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("multibutton server test 5-30-15"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

            //now output HTML data header
          if(readString.indexOf('?') >=0) { //don't send new page
            client.println("HTTP/1.1 204 Zoomkat\r\n\r\n");
          }
          else {
            client.println("HTTP/1.1 200 OK"); //send new page
            client.println("Content-Type: text/html");
            client.println();

            client.println("<HTML>");
            client.println("<HEAD>");
            client.println("<TITLE>Arduino GET test page</TITLE>");
            client.println("</HEAD>");
            client.println("<BODY>");

            client.println("<H1>Zoomkat's simple Arduino button</H1>");


            client.println("<a href='/?on4' target='inlineframe'>ON 4</a>"); 
            client.println("<a href='/?off4' target='inlineframe'>OFF 4</a>"); 

            client.println("<a href='/?on5' target='inlineframe'>ON 5</a>"); 
            client.println("<a href='/?off5' target='inlineframe'>OFF 5</a>"); 

            client.println("<a href='/?on6' target='inlineframe'>ON 6</a>"); 
            client.println("<a href='/?off6' target='inlineframe'>OFF 6</a>"); 

            client.println("<IFRAME name=inlineframe style='display:none'>");          
            client.println("</IFRAME>");

            client.println("</BODY>");
            client.println("</HTML>");
          }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on4") >0)//checks for on4
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led 4 On");
          }
          if(readString.indexOf("off4") >0)//checks for off4
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led 4 Off");
          }
          if(readString.indexOf("on5") >0)//checks for on5
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led 5 On");
          }
          if(readString.indexOf("off5") >0)//checks for off5
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led 5 Off");
          }          
          if(readString.indexOf("on6") >0)//checks for on6
          {
            digitalWrite(6, HIGH);    // set pin 6 high
            Serial.println("Led 6 On");
          }
          if(readString.indexOf("off6") >0)//checks for off6
          {
            digitalWrite(6, LOW);    // set pin 6 low
            Serial.println("Led 6 Off");
          }
          //clearing string for next read
          readString="";
        }
      }
    }
  }
}

PaulS:
That is not true.

What is true is that your analyse_cmd() function only knows how to extract one kind of data from the GET request. You'll need to fix that to handle more than one kind of GET request.

Of course, your web page will need to have more than one input field, for each pin that you want to toggle. And, of course, you'll need to keep track of the state of more than one pin.

I still can't figure out where part of the code should I modify :frowning:

zoomkat:
Web server test code that controls more than one arduino pin.

//zoomkat 5-30-15

//simple button GET with iframe code
//open serial monitor to see what the arduino receives
//use the ' instead of " in html ilnes
//address will look like http://192.168.1.102:84/ when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //ethernet shield mac address
byte ip[] = {192, 168, 1, 102 }; // arduino IP in lan
byte gateway[] = {192, 168, 1, 1 }; // internet access via router
byte subnet[] = {255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString;

//////////////////////

void setup(){

pinMode(4, OUTPUT); //pin selected to control
  pinMode(5, OUTPUT); //pin selected to control
  pinMode(6, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

//enable serial data print
  Serial.begin(9600);
  Serial.println("multibutton server test 5-30-15"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

//read char by char HTTP request
        if (readString.length() < 100) {

//store characters to string
          readString += c;
          //Serial.print(c);
        }

//if HTTP request has ended
        if (c == '\n') {

///////////////
          Serial.println(readString); //print to serial monitor for debuging

//now output HTML data header
          if(readString.indexOf('?') >=0) { //don't send new page
            client.println("HTTP/1.1 204 Zoomkat\r\n\r\n");
          }
          else {
            client.println("HTTP/1.1 200 OK"); //send new page
            client.println("Content-Type: text/html");
            client.println();

client.println("");
            client.println("");
            client.println("Arduino GET test page");
            client.println("");
            client.println("");

client.println("

Zoomkat's simple Arduino button

");

client.println("ON 4");
            client.println("OFF 4");

client.println("ON 5");
            client.println("OFF 5");

client.println("ON 6");
            client.println("OFF 6");

client.println("");

client.println("");
            client.println("");
          }

delay(1);
          //stopping client
          client.stop();

///////////////////// control arduino pin
          if(readString.indexOf("on4") >0)//checks for on4
          {
            digitalWrite(4, HIGH);    // set pin 4 high
            Serial.println("Led 4 On");
          }
          if(readString.indexOf("off4") >0)//checks for off4
          {
            digitalWrite(4, LOW);    // set pin 4 low
            Serial.println("Led 4 Off");
          }
          if(readString.indexOf("on5") >0)//checks for on5
          {
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led 5 On");
          }
          if(readString.indexOf("off5") >0)//checks for off5
          {
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led 5 Off");
          }         
          if(readString.indexOf("on6") >0)//checks for on6
          {
            digitalWrite(6, HIGH);    // set pin 6 high
            Serial.println("Led 6 On");
          }
          if(readString.indexOf("off6") >0)//checks for off6
          {
            digitalWrite(6, LOW);    // set pin 6 low
            Serial.println("Led 6 Off");
          }
          //clearing string for next read
          readString="";
        }
      }
    }
  }
}

I've already tried that code sir.. But it won't work on my MCU, although I've successfully compiled and uploaded it into my MCU.. I can't access the IP Address.. Maybe those libraries are not compatible on my arduino? or ethernet shield?

Do you have an arduino? If not, maybe another forum for your MCU would have better information.

I still can't figure out where part of the code should I modify

Can you determine the part of the code that generates the HTML page?
Can you determine the part of the code that generates the form?
Can you determine the part of the code that generates the one toggle button that you have now?

If the answers to all three questions are not yes, then you really need to hire someone to write the code for you (in Gigs and Collaboration).

If all three answers are yes, then cloning the last part of code to have two toggles should be relatively simple.

Once you have done that, then you need to look at what the GET request looks like when the first toggle is flipped. Then, look at what the GET request looks like when the second toggle is flipped. The GET request changes should provide clues as to what changes are needed to analyse_cmd().

Maybe those libraries are not compatible on my arduino? or ethernet shield?

No, they are not. zoomkat's code assumes that you have an ethernet shield based on the W5100 chip. However, the IDEAS expressed (except the String class) ARE usable.

Hello, I hope this one helps, I have tried this one and it works. I am able to control my Lights where ever I am since January this year. I used arduino pro mini and ENC28J60 ethernet module. here is the link guys. http://thinkingparts.blogspot.com/2015/08/home-automation-controlling-home.html