int main(void) { uint16_t dat_p; ADC_init(); _delay_ms ( 100 ); //DDRB |= (1<<PB0); initTimer(); //initialize the hardware driver for the enc28j60 enc28j60Init(mymac); enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz _delay_loop_1(0); // 60us enc28j60PhyWrite(PHLCON,0x476); //init the ethernet/ip layer: init_udp_or_www_server(mymac,myip); www_server_port(MYWWWPORT); while(1) { // read packet, handle ping and wait for a tcp packet: dat_p=packetloop_arp_icmp_tcp(buf,enc28j60PacketReceive(BUFFER_SIZE, buf)); // dat_p will be unequal to zero if there is a valid http get if(dat_p==0) { // no http request continue; } // tcp port 80 begin if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0) { // head, post and other methods: dat_p=http200ok(); dat_p=fill_tcp_data_p(buf,dat_p,PSTR("<h1>200 OK</h1>")); www_server_reply(buf,dat_p); continue; } // just one web page in the "root directory" of the web server if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0) { //dat_p=print_webpage(buf); dat_p=printIndex(buf); www_server_reply(buf,dat_p); } else if (strncmp("/now.htm", (char *)&(buf[dat_p+4]),8) == 0 ) { dat_p = printNowHTML ( buf ); www_server_reply( buf, dat_p ); } else if (strncmp("/min0.htm", (char *)&(buf[dat_p+4]),9) == 0 ) { dat_p = printMinHTML ( buf, 0 ); www_server_reply( buf, dat_p ); } else if (strncmp("/min1.htm", (char *)&(buf[dat_p+4]),9) == 0 ) { dat_p = printMinHTML ( buf, 1 ); www_server_reply( buf, dat_p ); } else if (strncmp("/wlcm.htm", (char *)&(buf[dat_p+4]),9) == 0 ) { dat_p = printWelcomeHTML ( buf ); www_server_reply( buf, dat_p ); } else { 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>")); www_server_reply(buf,dat_p); } } return (0); }
int main(void){ uint16_t dat_p; // 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. #if 0 CLKPR=(1<<CLKPCE); CLKPR=0; // 8 MHZ #endif _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); //init the ethernet/ip layer: init_udp_or_www_server(mymac,myip); www_server_port(MYWWWPORT); while(1){ // handle ping and wait for a tcp packet: dat_p=packetloop_arp_icmp_tcp(buf,enc28j60PacketReceive(BUFFER_SIZE, buf)); // dat_p will be unequal to zero if there is a valid http get if(dat_p==0){ // do nothing continue; } // tcp port 80 begin 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; } if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){ dat_p=http200ok(); dat_p=fill_tcp_data_p(buf,dat_p,PSTR("<p>OK, works great!</p>\n")); goto SENDTCP; } // all other URLs: 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>")); SENDTCP: www_server_reply(buf,dat_p); // send web page data } return (0); }
/* * Main entry point */ int main(void) { //=====init green led LEDInit(); LEDSet(1); //=====setup eth interface uint16_t plen = 0, dat_p = 0; //initialize enc28j60 enc28j60Init(mymac); _delay_ms(100); // 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); _delay_ms(100); //init the ethernet/ip layer: init_udp_or_www_server(mymac,myip); www_server_port(MYWWWPORT); register_ping_rec_callback(PingCallback); while(1){ // handle ping and wait for a tcp packet: dat_p = packetloop_arp_icmp_tcp(buf, enc28j60PacketReceive(BUFFER_SIZE, buf)); // dat_p will be unequal to zero if there is a valid http get if(dat_p==0){ // do nothing continue; } // tcp port 80 begin if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){ dat_p = fill_tcp_data_p(buf, 0, PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n")); dat_p = fill_tcp_data_p(buf, dat_p, PSTR("<h1>200 OK</h1>")); goto SENDTCP; } if (strncmp("/ ",(char *) & (buf[ dat_p + 4] ), 2) == 0 ){ nAccessCount++; dat_p = fill_tcp_data_p(buf, 0, PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n")); // webcontent char szWebText[130] = {0}; sprintf(szWebText, "<b>ATmega8 Webserver / ENC28J60</b><br><br>Server stats:<br>Acc:%d Png:%d<br><br>(C) Radu Motisan 2012, www.pocketmagic.net<br/>Tomasz Jokiel", nAccessCount, nPingCount); dat_p = fill_tcp_data(buf, dat_p, szWebText); // goto SENDTCP; } // all other URLs: 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>")); SENDTCP: www_server_reply(buf, dat_p); // send web page data } return (0); }
/*---------------------------------------------------------------------------*/ int main(void) { uint16_t rval; /* init hardware layer */ init_hci(); timer1_init(); init_serial(); sei(); led1_high(); led2_high(); /* init protothreads */ PT_INIT(&blink_pt); PT_INIT(&nrf24_pt); PT_INIT(&www_client_pt); PT_INIT(&www_server_pt); PT_INIT(&udp_server_pt); PT_INIT(&coap_server_pt); PT_INIT(&temperature_pt); PT_INIT(&udp_broadcast_pt); /* greeting message */ dbg(PSTR("> Hello World!\r\n")); /* init the ethernet chip */ enc28j60Init(mymac); enc28j60PhyWrite(PHLCON,0x476); /* get automatic IP */ rval=0; init_mac(mymac); while(rval==0) { plen=enc28j60PacketReceive(BUFFER_SIZE, buf); rval=packetloop_dhcp_initial_ip_assignment(buf,plen,mymac[5]); } dhcp_get_my_ip(myip,netmask,gwip); client_ifconfig(myip,netmask); /* learn the MAC address of the gateway */ get_mac_with_arp(gwip,0,&arpresolver_result_callback); while(get_mac_with_arp_wait()) { plen=enc28j60PacketReceive(BUFFER_SIZE, buf); packetloop_arp_icmp_tcp(buf,plen); } /* set WWW server port */ www_server_port(MYWWWPORT); dbg(PSTR("> System is ready.\r\n")); led1_low(); led2_low(); /* main loop */ while(1) { if(enc28j60linkup()) { /* poll hardware ethernet buffer */ plen = enc28j60PacketReceive(BUFFER_SIZE,buf); /* terminate the buffer */ buf[BUFFER_SIZE]='\0'; /* handle DHCP messages if neccessary */ plen = packetloop_dhcp_renewhandler(buf,plen); /* handle and analyse the packet slightly */ dat_p = packetloop_arp_icmp_tcp(buf,plen); if( dat_p == 0) { udp_client_check_for_dns_answer(buf,plen); } new_packet = 0xFF; PT_SCHEDULE(blink_thread(&blink_pt)); PT_SCHEDULE(nrf24_thread(&nrf24_pt)); PT_SCHEDULE(www_server_thread(&www_server_pt)); PT_SCHEDULE(udp_server_thread(&udp_server_pt)); PT_SCHEDULE(www_client_thread(&www_client_pt)); PT_SCHEDULE(coap_server_thread(&coap_server_pt)); PT_SCHEDULE(temperature_thread(&temperature_pt)); PT_SCHEDULE(udp_broadcast_thread(&udp_broadcast_pt)); } } return 0; }