int main(void){

        
        uint16_t dat_p;
        int8_t cmd;
        
        // Set the clock speed to "no pre-scaler" (8MHz with internal osc or 
        // full external speed)
        // set the clock prescaler. First write CLKPCE to enable setting 
        // of clock the next four instructions.
        // Note that the CKDIV8 Fuse determines the initial
        // value of the CKKPS bits.
        CLKPR=(1<<CLKPCE); // change enable
        CLKPR=0; // "no pre-scaler"
        _delay_loop_1(0); // 60us


        /*initialize enc28j60*/
        enc28j60Init(mymac);
        enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
        _delay_loop_1(0); // 60us
        
        /* Magjack leds configuration, see enc28j60 datasheet, page 11 */
        // LEDB=yellow LEDA=green
        //
        // 0x476 is PHLCON LEDA=links status, LEDB=receive/transmit
        // enc28j60PhyWrite(PHLCON,0b0000 0100 0111 01 10);
        enc28j60PhyWrite(PHLCON,0x476);
        
        DDRB|= (1<<DDB1); // LED, enable PB1, LED as output
        LEDOFF;

        DDRD&= ~(1<<DDD6); // enable PB6, as input
        PORTD|= (1<<PIND6); // internal pullup resistor on

        init_cnt2();
        sei();

        //init the web server ethernet/ip layer:
        init_ip_arp_udp_tcp(mymac,myip,MYWWWPORT);
        // init the web client:
        client_set_gwip(gwip);  // e.g internal IP of dsl router
        client_set_wwwip(websrvip);
        //
        while(1){
                // handle ping and wait for a tcp packet
                dat_p=packetloop_icmp_tcp(buf,enc28j60PacketReceive(BUFFER_SIZE, buf));
                if(dat_p==0){
                        if (start_web_client==1){
                                LEDON;
                                start_web_client=2;
                                sec=0;
                                web_client_attempts++;
                                // the text to send in the body of the mail:
                                urlencode("The door is open",urlvarstr);
                                // The string "test+emailnotify" is the subject line
                                // of the email. A plus sign translates into a space character.
                                // The MMaccountID is gus123, edit and replace by your own.
                                client_browse_url(PSTR("/cgi-bin/mm?a=gus123&s=test+emailnotify&d="),urlvarstr,PSTR(WEBSERVER_VHOST),&browserresult_callback);
                        }
                        // count how often the switch was triggered at all:
                        if (contact_debounce==0 && bit_is_clear(PIND,PIND6)){
                                contact_onoff_cnt++;
                                contact_debounce=1;
                        }
                        // we send only an email if we are in the right state (do
                        // not flood with email):
                        if (start_web_client==0 && bit_is_clear(PIND,PIND6)){
                                resend=1; // resend once if it failed
                                start_web_client=1;
                        }
                        // Reset after a delay of 3 min to prevent email sending.
                        // We reset only if the contact on PD6 was released.
                        if (start_web_client<=3 && sec==180 && !bit_is_clear(PIND,PIND6)){
                                start_web_client=0;
                        }
                        // Resend the email if it failed:
                        if (start_web_client==2 && sec==7 && resend){
                                start_web_client=1;
                                resend--;
                        }
                        continue;
                }
                        
                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
                        dat_p=http200ok();
                        dat_p=fill_tcp_data_p(buf,dat_p,PSTR("<h1>200 OK</h1>"));
                        goto SENDTCP;
                }
                cmd=analyse_get_url((char *)&(buf[dat_p+4]));
                // for possible status codes see:
                // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
                if (cmd==-1){
                        dat_p=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 401 Unauthorized\r\nContent-Type: text/html\r\n\r\n<h1>401 Unauthorized</h1>"));
                        goto SENDTCP;
                }
                if (cmd==1 && start_web_client==4){
                        // email was off, switch on
                        start_web_client=0;
                }
                if (cmd==0 ){
                        start_web_client=4; // email off
                }
                dat_p=http200ok();
                dat_p=print_webpage(buf);
                //
SENDTCP:
                www_server_reply(buf,dat_p); // send data

        }
        return (0);
}
Esempio n. 2
0
	// ----- http get
void EtherShield::ES_client_browse_url(prog_char *urlbuf, char *urlbuf_varpart, prog_char *hoststr,void (*callback)(uint8_t,uint16_t)) {
	client_browse_url(urlbuf, urlbuf_varpart, hoststr,callback);
}
Esempio n. 3
0
/*---------------------------------------------------------------------------*/
static
PT_THREAD(www_client_thread(struct pt *pt))
{
    PT_BEGIN(pt);    

    while(1)
    {
        /* TODO: Add debouncing ... */
        PT_WAIT_UNTIL(pt,btn2_pressed());
        PT_WAIT_UNTIL(pt,!btn2_pressed());

        dbg(PSTR("> Web client starts.\r\n"));

        retry_count = 0;
        
        do
        {
            /* disable interrupts */
            cli(); 

            /* reset timeout counter */
            www_client_counter = 0; 
            
            /* re-enable interrupts */
            sei(); 

            /* make a dns request */
            dnslkup_request(buf,URL_base_address,gwmac);

            /* wait an answer ... */
            PT_WAIT_UNTIL(pt,dnslkup_haveanswer()||www_client_timeout());

            if(dnslkup_haveanswer())
            {
                /* exit the loop */
                retry_count = 0xFF;              
            }            
            else
            {
                dbg(PSTR("> DNS lookup timeout!\r\n"));
                retry_count++;
            }

        }
        while(retry_count < 5);

        if(dnslkup_haveanswer())
        {
            /* save the received IP to your userspace variable */
            dnslkup_get_ip(otherside_www_ip);

            /* reset callback state */
            www_callback_state = 0;

            /* reset retry count */
            retry_count = 0;

            /* browsing action */
            do
            {
                /* disable interrupts */
                cli(); 

                /* reset timeout counter */
                www_client_counter = 0; 
                
                /* re-enable interrupts */
                sei(); 

                /* increment the trial counter */
                retry_count++;
                
                client_browse_url(
                    PSTR("/ws.php?c="), /* constant part of the URL suffix */
                    "Samsun", /* variable part of the URL which comes after the constant suffix */
                    URL_base_address, /* base-name of the web page we want to browse */
                    &browserresult_callback, /* callback function for our URL browsing attempt */
                    otherside_www_ip, /* IP representation of the webpage host */
                    gwmac /* mac address of our gateway */
                );                

                /* wait ... */
                PT_WAIT_UNTIL(pt,www_client_failed() || www_client_timeout() || www_client_ok() || return_counter() );

                if(www_client_ok())
                {
                    /* exit the loop */
                    retry_count = 0xFF;              
                }
                else if(www_client_failed())
                {
                    dbg(PSTR("> Browsing problem!\r\n"));
                }
                else if(www_client_timeout())
                {
                    dbg(PSTR("> Browsing timeout!\r\n"));
                }
            }
            while(retry_count < 5);

            if(retry_count == 0xFF)
            {
                dbg(PSTR("> Browsing went ok!\r\n"));  
            }
            else
            {
                dbg(PSTR("> Browsing failed!\r\n"));     
            }

            dbg(PSTR("> -------------------------------------\r\n"));
        }
        else
        {
            dbg(PSTR("> Browsing failed!\r\n"));     
            dbg(PSTR("> -------------------------------------\r\n"));
        }
        
    }

    PT_END(pt);
}