// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage(uint8_t *buf)
{
        uint16_t plen;
        char vstr[5];
        plen=http200ok();
        plen=fill_tcp_data_p(buf,plen,PSTR("<h2>Email notification status</h2>\n<pre>\n"));
        plen=fill_tcp_data_p(buf,plen,PSTR("Number of PD6 to GND connections: "));
        // convert number to string:
        itoa(contact_onoff_cnt,vstr,10);
        plen=fill_tcp_data(buf,plen,vstr);
        plen=fill_tcp_data_p(buf,plen,PSTR("\nNumber of email attempts: "));
        // convert number to string:
        itoa(web_client_attempts,vstr,10);
        plen=fill_tcp_data(buf,plen,vstr);
        plen=fill_tcp_data_p(buf,plen,PSTR("\nNumber successful emails: "));
        // convert number to string:
        itoa(web_client_sendok,vstr,10);
        plen=fill_tcp_data(buf,plen,vstr);
        plen=fill_tcp_data_p(buf,plen,PSTR("\nDate of last mail: "));
        plen=fill_tcp_data(buf,plen,datebuf);
        plen=fill_tcp_data_p(buf,plen,PSTR("\nEmail notify is: "));
        if (start_web_client==4){
                plen=fill_tcp_data_p(buf,plen,PSTR("OFF"));
        }else{
                plen=fill_tcp_data_p(buf,plen,PSTR("ON"));
        }
        plen=fill_tcp_data_p(buf,plen,PSTR("\n<form action=/ method=get>\npassw: <input type=password size=8 name=pw><input type=hidden name=mn "));
        if (start_web_client==4){
                plen=fill_tcp_data_p(buf,plen,PSTR("value=1><input type=submit value=\"enable email\">"));
        }else{
                plen=fill_tcp_data_p(buf,plen,PSTR("value=0><input type=submit value=\"disable email\">"));
        }
        plen=fill_tcp_data_p(buf,plen,PSTR("</form>\n</pre><br><hr>tuxgraphics.org"));
        return(plen);
}
Esempio n. 2
0
uint16_t printIndex ( uint8_t *buf )
{
  uint16_t plen;
  plen=http200ok();
  plen=fill_tcp_data_p ( buf, plen, PSTR("<!DOCTYPE html>\n<html>\n<body>\n<a href=\"wlcm.htm\" target=\"data\">Welcome</a><br>\n<a href=\"now.htm\" target=\"data\">Current</a>\n<br>\n<a href=\"min0.htm\" target=\"data\">Minutes</a>\n<br>\n<a href=\"min1.htm\" target=\"data\">Minutes</a>\n<iframe src=\"wlcm.htm\" name=\"data\" style=\"border-color: #0000FF; border: 1; position:absolute; top:10; left:150px; right:0; bottom:10; width:600px; height:95%\"></iframe>\n</body>\n</html>") );
  return( plen );
}
uint16_t print_alarm_config(void)
{
	uint16_t plen;
	plen=http200ok();
	
	// Check if gatewy MAC look-up has been already done
	if (gw_arp_state==GW_ARP_STATE_IN_PROGRESS)
	{
	        plen=fill_tcp_data_p(buf,plen,PSTR("waiting for GW MAC\n"));
		    return(plen);
	}
	plen=fill_tcp_data_p(buf,plen,PSTR("<a href=/>[home]</a>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<h2>Alarm config</h2><pre>\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<form action=/u method=get>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("Enabled:<input type=checkbox value=1 name=ae "));
	if (alarmOn){
		plen=fill_tcp_data_p(buf,plen,PSTR("checked"));
	}
	plen=fill_tcp_data_p(buf,plen,PSTR(">\nName:   <input type=text name=n value=\""));
	plen=fill_tcp_data(buf,plen,myname);
	plen=fill_tcp_data_p(buf,plen,PSTR("\">\nSendto: ip=<input type=text name=di value="));
	mk_net_str(gStrbuf,udpsrvip,4,'.',10);
	plen=fill_tcp_data(buf,plen,gStrbuf);
	plen=fill_tcp_data_p(buf,plen,PSTR("> port=<input type=text name=dp size=4 value="));
	itoa(udpsrvport,gStrbuf,10);
	plen=fill_tcp_data(buf,plen,gStrbuf);
	plen=fill_tcp_data_p(buf,plen,PSTR(">\nPasswd: <input type=password name=pw>\n"));
	
	plen=fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=change></form>\n<hr>"));
	return(plen);
}
uint16_t print_net_config(void)
{
	uint16_t plen;
	plen=http200ok();
	
	// Check if gatewy MAC look-up has been already done
	if (gw_arp_state==GW_ARP_STATE_IN_PROGRESS)
	{
		plen=fill_tcp_data_p(buf,plen,PSTR("waiting for GW MAC\n"));
		return(plen);
	}
	plen=fill_tcp_data_p(buf,plen,PSTR("<a href=/>[home]</a>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<h2>Network config</h2><pre>\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<form action=/u method=get>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("DHCP:<input type=checkbox value=1 name=dh "));
	if (dhcpOn){
		plen=fill_tcp_data_p(buf,plen,PSTR("checked"));
	}
	plen=fill_tcp_data_p(buf,plen,PSTR(">\nIP=<input type=text name=ip value="));
	mk_net_str(gStrbuf,myip,4,'.',10);
	plen=fill_tcp_data(buf,plen,gStrbuf);
	plen=fill_tcp_data_p(buf,plen,PSTR(">\ngwip=<input type=text name=gi value="));
	mk_net_str(gStrbuf,gwip,4,'.',10);
	plen=fill_tcp_data(buf,plen,gStrbuf);
	plen=fill_tcp_data_p(buf,plen,PSTR(">\nHeartbeat timeout=<input type=text name=hb value="));
	itoa(heartbeat_timeout_sec,gStrbuf,10);
	plen=fill_tcp_data(buf,plen,gStrbuf);
	plen=fill_tcp_data_p(buf,plen,PSTR(">\nPasswd: <input type=password name=pw>\n"));
	
	plen=fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=change></form>\n<hr>"));
	return(plen);
}
Esempio n. 5
0
uint16_t printWelcomeHTML ( uint8_t *buf )
{
  uint16_t plen;
  plen=http200ok();
  plen=fill_tcp_data_p ( buf, plen, PSTR("<!DOCTYPE html>\n <html>\n <body>\n <p>Welcome to EnergyMeter v0.1</p>\n <p>Select what data to see on the left</p>\n <p>(c)2013 Maarten van Ingen\n </body>\n </html>\n" ) );
  return( plen );
}
Esempio n. 6
0
// Return to room-2
uint16_t print_webpage_confirmrm2(uint8_t *buf)
{
	uint16_t plen;
	plen=http200ok();
	plen=fill_tcp_data_p(buf,plen,PSTR("<h2>OK</h2> <a href=\"/room2\">-&gt;continue</a></p>\n"));
	return(plen);
}
Esempio n. 7
0
void HttpServer::Listen(word len)
{
	bool pageFound = false;

	// read packet, handle ping and wait for a tcp packet:
	if(len == 0)
	{
		len = ether.packetReceive();
	}

	word pos = ether.packetLoop(len);

	if (pos)
	{
		bfill = ether.tcpOffset();
	    char* data = (char *) Ethernet::buffer + pos;

	    char* arg1 = GetArg(data);
	    //String page = String(arg1);


	    for(int i=0;i<m_iCallBackIndex;i++)
	    {
	    	if (m_callBack[i].page == String(arg1))
			{
	    		http200ok();
				m_callBack[i].func(arg1);
				ether.httpServerReply(bfill.position());
				pageFound = true;
			}
	    }

	if(!pageFound)
	{
      if(m_defaultPage != NULL)
      {
    	 http200ok();
    	 m_defaultPage(arg1);
      }
      else
      {
    	  http404();
      }
	}
	    ether.httpServerReply(bfill.position());
	}
}
int main(void){
        uint16_t dat_p;
        
        // set the clock speed to 8MHz
        // set the clock prescaler. First write CLKPCE to enable setting of clock the
        // next four instructions.
        CLKPR=(1<<CLKPCE);
        CLKPR=0; // 8 MHZ
        _delay_loop_1(0); // 60us
        
        //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);
        
        PORTB = 0xff;
        DDRB = 0xff;
        uart_init();
        sei();

        xfunc_out = (void (*)(char))uart_put;
        xputs(PSTR("AVR-Ethernet test monitor\n"));
        xprintf(PSTR("ENC28J60 Rev.%d\n"), enc28j60getrev());

        //init the ethernet/ip layer:
        init_ip_arp_udp_tcp(mymac,myip,MYWWWPORT);

        while(1){
                // read packet, handle ping and wait for a tcp packet:
                dat_p=packetloop_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>"));
                        goto SENDTCP;
                }
                // 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);
                        goto SENDTCP;
                }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>"));
                        goto SENDTCP;
                }
SENDTCP:
                www_server_reply(buf,dat_p); // send web page data
                // tcp port 80 end
        }
        return (0);
}
Esempio n. 9
0
// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage(uint8_t *buf)
{
	uint16_t plen;
	plen = http200ok();
	plen = fill_tcp_data_p(buf,plen,PSTR("<pre>"));
	plen = fill_tcp_data_p(buf,plen,PSTR("Hi, this is Andrew speaking."));
	plen = fill_tcp_data_p(buf,plen,PSTR("</pre>\n"));
	return(plen);
}
// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage(uint8_t *buf)
{
        uint16_t plen;
        plen=http200ok();
        plen=fill_tcp_data_p(buf,plen,PSTR("<pre>"));
        plen=fill_tcp_data_p(buf,plen,PSTR("<font color='green' size='6'><b>Witaj !</b>\n</font>"));
        plen=fill_tcp_data_p(buf,plen,PSTR("<font color='blue'><i>twój serwer www dzia³a znakomicie</i>\n\n</font>"));
        plen=fill_tcp_data_p(buf,plen,PSTR("<hr><img src=http://www.atnel.pl/atnel_mini.jpg>"));
        plen=fill_tcp_data_p(buf,plen,PSTR("<a href=http://www.atnel.pl><br>www.atnel.pl</a>"));
        plen=fill_tcp_data_p(buf,plen,PSTR("</pre>\n"));
        return(plen);
}
Esempio n. 11
0
uint16_t printNowHTML ( uint8_t *buf )
{
  char buffer[10];
  uint16_t plen;
  uint8_t i;
  plen=http200ok();
  plen=fill_tcp_data_p ( buf, plen, PSTR("<!DOCTYPE html>\n<html>\n<body>\n<pre>\n") );
  itoa (currentUsage, buffer, 10);
  plen=fill_tcp_data(buf, plen, buffer);
  plen=fill_tcp_data_p ( buf, plen, PSTR("</pre>\n</body>\n</html>") );
  return( plen );
}
// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage(uint8_t *buf)
{
        uint16_t plen;
        plen=http200ok();
        //xprintf("%s\n",pc_html);
        //plen=fill_tcp_data_p(buf,plen, pc_html);
        //xprintf(PSTR("Return pc_html\n"));
        plen=fill_tcp_data_p(buf,plen,PSTR("<pre>"));
        plen=fill_tcp_data_p(buf,plen,PSTR("Hi!\nYour web server works great."));
        plen=fill_tcp_data_p(buf,plen,PSTR("</pre>\n"));
        return(plen);
}
Esempio n. 13
0
// main webpage for setting the time and date
uint16_t print_webpage_clk(uint8_t *buf)
{
	uint16_t plen;
	plen=http200ok();
	
	plen=fill_tcp_data_p(buf,plen,PSTR("<style type=\"text/css\">* {padding:0;margin:0;} body {font-size:10pt;font-family:\"georgia\";color:#333333;background:#74888e;}\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("h5 {font-size:0.7em;} #outer {width:340px;border:2px solid #fff;background-color:#fff;margin:0 auto;} #header {background:#2b2b2b;margin-bottom:2px;}#headercontent {bottom:0;padding:0.7em 1em 0.7em 1em;}\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("#headercontent h1 {font-weight:normal;color:#fff;font-size:2.5em;}\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("#content {padding:2em 2em 0 2em;}\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("</style></head><body><div id=\"outer\"><div id=\"header\"><div id=\"headercontent\">"));
	// Webserver header
	plen=fill_tcp_data_p(buf,plen,PSTR("<h1>Klokke / dato</h1></div></div>\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<div id=\"content\">\n"));
	//content
	plen=fill_tcp_data_p(buf,plen,PSTR("<a href=\"/cnf\"><< Tilbake</a><br><br>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<fieldset><legend>Sett ny tid og dato</legend>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<br><form action=/cdclk method=get>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<input type=text size=1 name=h value="));
	sprintf(strbuf,"%02d",ethPacket[pHour]);
	plen=fill_tcp_data(buf,plen,strbuf);
	plen=fill_tcp_data_p(buf,plen,PSTR(">"));
	plen=fill_tcp_data_p(buf,plen,PSTR(":"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<input type=text size=1 name=m value="));
	sprintf(strbuf,"%02d",ethPacket[pMin]);
	plen=fill_tcp_data(buf,plen,strbuf);
	plen=fill_tcp_data_p(buf,plen,PSTR(">"));
	plen=fill_tcp_data_p(buf,plen,PSTR(":"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<input type=text size=1 name=s value="));
	sprintf(strbuf,"%02d",ethPacket[pSec]);
	plen=fill_tcp_data(buf,plen,strbuf);
	plen=fill_tcp_data_p(buf,plen,PSTR(">"));
	plen=fill_tcp_data_p(buf,plen,PSTR("(hh:tt:ss)<br><br>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<input type=text size=1 name=dd value="));
	sprintf(strbuf,"%02d",ethPacket[pDate]);
	plen=fill_tcp_data(buf,plen,strbuf);
	plen=fill_tcp_data_p(buf,plen,PSTR(">"));
	plen=fill_tcp_data_p(buf,plen,PSTR("-"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<input type=text size=1 name=mm value="));
	sprintf(strbuf,"%02d",ethPacket[pMonth]);
	plen=fill_tcp_data(buf,plen,strbuf);
	plen=fill_tcp_data_p(buf,plen,PSTR(">"));
	plen=fill_tcp_data_p(buf,plen,PSTR("-"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<input type=text size=1 name=yy value="));
	sprintf(strbuf,"%02d",ethPacket[pYear]);
	plen=fill_tcp_data(buf,plen,strbuf);
	plen=fill_tcp_data_p(buf,plen,PSTR(">"));
	plen=fill_tcp_data_p(buf,plen,PSTR("(dd-mm-yy)<br><br>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("Passord:<input type=password size=8 name=pw><br>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"OK\"></form><br></fieldset><br>"));
	
	return(plen);
}
Esempio n. 14
0
// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage2(uint8_t *buf)
{
  uint16_t plen;
  plen=http200ok();
  plen=ES_fill_tcp_data(buf,plen,"<html><head><title>Test page</title></head><body>");
  plen=ES_fill_tcp_data(buf,plen,"<center><h1>Second page</h1>");
  plen=ES_fill_tcp_data(buf,plen,"<br>Hello World");
  plen=ES_fill_tcp_data(buf,plen,"<br></font></h2>");
  plen=ES_fill_tcp_data(buf,plen,"</center><hr>");
  plen=ES_fill_tcp_data(buf,plen,"</body></html>");

  return(plen);
}
Esempio n. 15
0
// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage(uint8_t *buf)
{
        uint16_t plen;
        char vstr[15];
        plen=http200ok();
        plen=fill_tcp_data_p(buf,plen,PSTR("<a href=/>[refresh]</a>"));
        plen=fill_tcp_data_p(buf,plen,PSTR("<h2>identi.ca notification status</h2>\n<pre>\n"));
        if (client_waiting_gw()){
                plen=fill_tcp_data_p(buf,plen,PSTR("waiting for GW IP to answer arp.\n"));
                return(plen);
        }
        if (dns_state==1){
                plen=fill_tcp_data_p(buf,plen,PSTR("waiting for DNS answer.\n"));
                return(plen);
        }
        /* // debug
        plen=fill_tcp_data_p(buf,plen,PSTR("\nweb IP:\n"));
        mk_net_str(vstr, dnslkup_getip() ,4,'.',10);
        plen=fill_tcp_data(buf,plen,vstr);
        plen=fill_tcp_data_p(buf,plen,PSTR("\n"));
        */

        plen=fill_tcp_data_p(buf,plen,PSTR("Num of PD6 to GND connections: "));
        // convert number to string:
        itoa(contact_onoff_cnt,vstr,10);
        plen=fill_tcp_data(buf,plen,vstr);
        plen=fill_tcp_data_p(buf,plen,PSTR("\nNum of identi.ca attempts: "));
        // convert number to string:
        itoa(web_client_attempts,vstr,10);
        plen=fill_tcp_data(buf,plen,vstr);
        plen=fill_tcp_data_p(buf,plen,PSTR("\nNum successful identi.ca: "));
        // convert number to string:
        itoa(web_client_sendok,vstr,10);
        plen=fill_tcp_data(buf,plen,vstr);
        plen=fill_tcp_data_p(buf,plen,PSTR("\nDate of last identi.ca: "));
        plen=fill_tcp_data(buf,plen,datebuf);
        plen=fill_tcp_data_p(buf,plen,PSTR("\nTwitter notify is: "));
        if (start_web_client==4){
                plen=fill_tcp_data_p(buf,plen,PSTR("OFF"));
        }else{
                plen=fill_tcp_data_p(buf,plen,PSTR("ON"));
        }
        plen=fill_tcp_data_p(buf,plen,PSTR("\n<form action=/ method=get>\npassw: <input type=password size=8 name=pw><input type=hidden name=mn "));
        if (start_web_client==4){
                plen=fill_tcp_data_p(buf,plen,PSTR("value=1><input type=submit value=\"enable identi.ca msg\">"));
        }else{
                plen=fill_tcp_data_p(buf,plen,PSTR("value=0><input type=submit value=\"disable identi.ca msg\">"));
        }
        plen=fill_tcp_data_p(buf,plen,PSTR("</form>\n</pre><br><hr>tuxgraphics.org"));
        return(plen);
}
Esempio n. 16
0
uint16_t printMinHTML ( uint8_t *buf, char whichSection )
{
  char buffer[10];
  uint16_t plen;
  uint8_t i;
  plen=http200ok();
  plen=fill_tcp_data_p ( buf, plen, PSTR("<!DOCTYPE html>\n<html>\n<body>\n<pre>\n") );
  for ( i = 60*whichSection ; i < 60 * whichSection + 60 ; i++ )
  {
    plen=fill_tcp_data_p(buf,plen,PSTR("\n#"));
    itoa (i, buffer, 10);
    plen=fill_tcp_data(buf, plen, buffer);
    plen=fill_tcp_data_p(buf,plen,PSTR(":"));
    itoa (readings[i], buffer, 10);
    plen=fill_tcp_data(buf, plen, buffer);
  }
  plen=fill_tcp_data_p ( buf, plen, PSTR("</pre>\n</body>\n</html>") );
  return( plen );
}
Esempio n. 17
0
// main site for control
uint16_t print_webpage_config(uint8_t *buf)
{
	uint16_t plen;
	plen=http200ok();
	
	plen=fill_tcp_data_p(buf,plen,PSTR("<style type=\"text/css\">*{padding:0;margin:0;}body{font-size:10pt;font-family:\"georgia\";color:#333333;background:#74888e;}"));
	plen=fill_tcp_data_p(buf,plen,PSTR("h5{font-size:0.7em;}#outer{width:350px;border:2px solid #fff;background-color:#fff;margin:0 auto;}#header{background:#2b2b2b;margin-bottom:2px;}#headercontent{bottom:0;padding:0.7em 1em 0.7em 1em;}"));
	plen=fill_tcp_data_p(buf,plen,PSTR("#headercontent h1{font-weight:normal;color:#fff;font-size:2.5em;}#menu{position:relative;background:#2b2b2b;height:3.5em;padding:0 1em 0 1em;margin-bottom:2px;}#menu ul{position:absolute;top:1.1em;}"));
	plen=fill_tcp_data_p(buf,plen,PSTR("#menu ul li{display:inline;}#menu ul li a{padding:0.5em 1em 0.9em 1em;color:#fff;}#content{padding:2em 2em 0 2em;}"));
	plen=fill_tcp_data_p(buf,plen,PSTR("</style></head><body><div id=\"outer\"><div id=\"header\"><div id=\"headercontent\">"));
	// Webserver header
	plen=fill_tcp_data_p(buf,plen,PSTR("<h1>Kontroll</h1></div></div>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<div id=\"menu\"><ul>"));
	//the "menu" or links
	plen=fill_tcp_data_p(buf,plen,PSTR("<li><a href=\"/\">Hoved</a></li>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<li><a href=\"/room1\">Rom-1</a></li>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<li><a href=\"/room2\">Rom-2</a></li>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<li><a href=\"/clk\">Klokke</a></li>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("</ul></div><div id=\"content\">"));
	
	// Change the IP
	plen=fill_tcp_data_p(buf,plen,PSTR("<fieldset><legend><h4>Endre IP</h4></legend>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<form action=/ipc method=get>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<br>Passord:<input type=password size=8 name=pw><br>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("Ny IP: <input type=text size=12 name=nip value="));
	mk_net_str(strbuf,myip,4,'.',10);
	plen=fill_tcp_data(buf,plen,strbuf);
	plen=fill_tcp_data_p(buf,plen,PSTR("><br>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"OK\"></form><br></fieldset>"));
	
	// Change the password
	plen=fill_tcp_data_p(buf,plen,PSTR("<br><fieldset><form action=/ipc method=get>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<br>Gammelt passord:<input type=password size=8 name=pw><br>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("Nytt passord: <input type=password size=8 name=npw><br>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"OK\"></form><br></fieldset>"));
	
	return(plen);
}
Esempio n. 18
0
uint16_t print_webpage_now(uint8_t *buf)
{
	uint16_t plen;
	plen=http200ok();
	plen=fill_tcp_data_p(buf,plen,PSTR("<style type=\"text/css\">* {padding:0;margin:0;} body {font-size:10pt;font-family:\"georgia\";color:#333333;background:#74888e;}\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("h5 {font-size:0.7em;} #outer {width:340px;border:2px solid #fff;background-color:#fff;margin:0 auto;} #header {background:#2b2b2b;margin-bottom:2px;}#headercontent {bottom:0;padding:0.7em 1em 0.7em 1em;}\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("#headercontent h1 {font-weight:normal;color:#fff;font-size:2.5em;}\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("#content {padding:2em 2em 0 2em;}\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("</style></head><body><div id=\"outer\"><div id=\"header\"><div id=\"headercontent\">"));
	// Webserver header
	plen=fill_tcp_data_p(buf,plen,PSTR("<h1>Ekstra ting</h1></div></div>\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<div id=\"content\">\n"));
	//content
	plen=fill_tcp_data_p(buf,plen,PSTR("<h2>Actions</h2>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<form action=/ack method=get>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("Passw: <input type=password size=8 name=pw><input type=hidden name=rst value=1><input type=submit value=\"reboot host now\"></form>\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<hr><form action=/ack method=get>"));
	if (pingtimer>0){
		plen=fill_tcp_data_p(buf,plen,PSTR("Passw: <input type=password size=8 name=pw><input type=hidden name=stp value=1><input type=submit value=\"stop watchdog now\"></form>"));
	}else{
		plen=fill_tcp_data_p(buf,plen,PSTR("Passw: <input type=password size=8 name=pw><input type=hidden name=srt value=1><input type=submit value=\"start watchdog now\"></form><hr>"));
	}
	return(plen);
}
// main web page
uint16_t print_webpage(void)
{
	uint16_t plen;
	plen=http200ok();
	plen=fill_tcp_data_p(buf,plen,PSTR("<a href=/c>[alarm config]</a> <a href=/n>[network config]</a> <a href=./>[refresh]</a>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<h2>Alarm: "));
	plen=fill_tcp_data(buf,plen,myname);
	plen=fill_tcp_data_p(buf,plen,PSTR("</h2><pre>\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("Last alarm:\n"));
	if (lastAlarm){
		if (gMin>59){
			itoa(gMin/60,gStrbuf,10); // convert integer to string
			plen=fill_tcp_data(buf,plen,gStrbuf);
			plen=fill_tcp_data_p(buf,plen,PSTR("hours and "));
		}
		itoa(gMin%60,gStrbuf,10); // convert integer to string
		plen=fill_tcp_data(buf,plen,gStrbuf);
		plen=fill_tcp_data_p(buf,plen,PSTR("min ago"));
		}else{
		plen=fill_tcp_data_p(buf,plen,PSTR("none in last 14 days"));
	}
	plen=fill_tcp_data_p(buf,plen,PSTR("\n</pre><hr>\n"));
	return(plen);
}
Esempio n. 20
0
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);
}
Esempio n. 21
0
// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage(uint8_t *buf)
{
  uint16_t plen;
  char str_out[15];
  char str_out2[50];
  char str_out3[50];
  int temp;
  unsigned short ent;
  unsigned short part;

  unsigned short ent1;
  unsigned short part1;
  int i=0;

  plen=http200ok();
  plen=ES_fill_tcp_data(buf,plen,"<html><head><title>STM32F103RB ENC28J60 Ethernet Demo</title></head><body>");
  plen=ES_fill_tcp_data(buf,plen,"<center><h1>Welcome to STM32-P103 ENC28J60 Ethernet Demo</h1>");
  plen=ES_fill_tcp_data(buf,plen,"<center><h2>Driver adapted by Sebastien Le Guen</h2>");
  plen=ES_fill_tcp_data(buf,plen,"<br> Control digital outputs (not implemented yet)");
  plen=ES_fill_tcp_data(buf,plen,"<br> Read digital analog inputs (not implemented yet)");

  /***********/
  plen=ES_fill_tcp_data(buf,plen,"<center><h3> Read temperature from BMP085</h3>");

  ent  = temperature / 10;
  part = temperature % 10;

  sprintf(str_out, "%02d.%d", ent,part);

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

  plen=ES_fill_tcp_data(buf,plen,str_out);

  plen=ES_fill_tcp_data(buf,plen,PSTR("  &#176C</font></h1><br> ") );
  /************/

  /***********/
  plen=ES_fill_tcp_data(buf,plen,"<center><h3> Read pressure from BMP085</h3>");

  ent1  = pressure / 100;
  part1 = pressure % 100;

  sprintf(str_out2, "%02d.%d hPa", ent1,part1);

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

  plen=ES_fill_tcp_data(buf,plen,str_out2);

  plen=ES_fill_tcp_data(buf,plen,PSTR("</font></h1><br> ") );
  /************/


  /***********/
  plen=ES_fill_tcp_data(buf,plen,"<center><h3> Read temperature from TMP102</h3>");

  sprintf(str_out3, "%02f", temperatureTMP102);

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

  plen=ES_fill_tcp_data(buf,plen,str_out3);

  plen=ES_fill_tcp_data(buf,plen,PSTR("  &#176C</font></h1><br> ") );
  /************/

  plen=ES_fill_tcp_data(buf,plen,"<br></font></h2>");
  plen=ES_fill_tcp_data(buf,plen,"</center><hr>");
  plen=ES_fill_tcp_data(buf,plen,"</body></html>");

  return(plen);
}
Esempio n. 22
0
void ethernet_process(void)
{
	uint16_t plen;
	uint16_t dat_p;
	uint8_t cmd_pos=0;
	int8_t cmd;
	uint8_t payloadlen=0;
	char str[30];
	char cmdval;

    // get the next new packet:
    plen = enc28j60PacketReceive(BUFFER_SIZE, buf);
    buf[BUFFER_SIZE]='\0';
    dat_p=packetloop_arp_icmp_tcp(buf,plen);
#if 0
    // tcp port 80 begin
    if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0)
    {
      // head, post and other methods:
      dat_p=http200ok();
      dat_p=ES_fill_tcp_data(buf,dat_p,"<h1>200 OK</h1>");
      goto SENDTCP;
    }
#endif
        /*plen will ne unequal to zero if there is a valid
         * packet (without crc error) */
        if(plen==0)
        {
                //continue;
        }

        // arp is broadcast if unknown but a host may also
        // verify the mac address by sending it to
        // a unicast address.
        if(eth_type_is_arp_and_my_ip(buf,plen))
        {
                make_arp_answer_from_request(buf);
                //continue;
        }

        // check if ip packets are for us:
        if(eth_type_is_ip_and_my_ip(buf,plen)==0)
        {
                //continue;
        }
#ifdef _DEBUG_
        // led----------
        if (i){
                /* set output to Vcc, LED off */
                PORTB|= (1<<PORTB1);
                i=0;
        }else{
                /* set output to GND, LED on */
                PORTB &= ~(1<<PORTB1);
                i=1;
        }
#endif
        if(buf[IP_PROTO_P]==IP_PROTO_ICMP_V && buf[ICMP_TYPE_P]==ICMP_TYPE_ECHOREQUEST_V)
        {
                // a ping packet, let's send pong
                make_echo_reply_from_request(buf,plen);
                //continue;
        }
        // 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)
                {
                        make_tcp_synack_from_syn(buf);
                        // make_tcp_synack_from_syn does already send the syn,ack
                        //continue;
                }
                if (buf[TCP_FLAGS_P] & TCP_FLAGS_ACK_V)
                {
                        init_len_info(buf); // init some data structures
                        // we can possibly have no data, just ack:
                        dat_p=get_tcp_data_pointer();
                        if (dat_p==0)
                        {
                                if (buf[TCP_FLAGS_P] & TCP_FLAGS_FIN_V)
                                {
                                        // finack, answer with ack
                                        make_tcp_ack_from_any(buf,1,0);
                                }
                                // just an ack with no data, wait for next packet
                                //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
                                plen=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=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"));
                                plen=fill_tcp_data_p(buf,plen,PSTR("<p>Usage: http://host_or_ip/password</p>\n"));
                                goto SENDTCP;
                        }
                        cmd=analyse_get_url((char *)&(buf[dat_p+5]));
                        // for possible status codes see:
                        // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
                        if (cmd==-1)
                        {
                                plen=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;
                        }
#ifdef _DEBUG_
                        if (cmd==1)
                        {
                                PORTD|= (1<<PORTD7);// transistor on
                        }
                        if (cmd==0)
                        {
                                PORTD &= ~(1<<PORTD7);// transistor off
                        }
#endif
                        if (cmd==-3)
                        {
                                // redirect to add a trailing slash
                                plen=moved_perm(buf);
                                goto SENDTCP;
                        }
#ifndef _DEBUG_
                        // if (cmd==-2) or any other value
                        // just display the status:
                        //plen=print_webpage(buf,(PORTD & (1<<PORTD7)));
                        plen=print_webpage(buf);
                        //
#endif
SENDTCP:
                        plen=print_webpage(buf);
                        make_tcp_ack_from_any(buf,1,0);   // send ack for http get
                        make_tcp_ack_with_data(buf,plen); // send data
                        //continue;
                }

        }
        // tcp port www end
        //
        // udp start, we listen on udp port 1200=0x4B0
        if (buf[IP_PROTO_P]==IP_PROTO_UDP_V&&buf[UDP_DST_PORT_H_P]==4&&buf[UDP_DST_PORT_L_P]==0xb0)
        {
                payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;
                // you must sent a string starting with v
                // e.g udpcom version 10.0.0.24
                if (verify_password((char *)&(buf[UDP_DATA_P])))
                {
                        // find the first comma which indicates
                        // the start of a command:
                        cmd_pos=0;
                        while(cmd_pos<payloadlen)
                        {
                                cmd_pos++;
                                if (buf[UDP_DATA_P+cmd_pos]==',')
                                {
                                        cmd_pos++; // put on start of cmd
                                        break;
                                }
                        }
                        // a command is one char and a value. At
                        // least 3 characters long. It has an '=' on
                        // position 2:
                        if (cmd_pos<2 || cmd_pos>payloadlen-3 || buf[UDP_DATA_P+cmd_pos+1]!='=')
                        {
                                strcpy(str,"e=no_cmd");
                                goto ANSWER;
                        }
                        // supported commands are
                        // t=1 t=0 t=?
                        if (buf[UDP_DATA_P+cmd_pos]=='t')
                        {
                                cmdval=buf[UDP_DATA_P+cmd_pos+2];
                                if(cmdval=='1')
                                {
                                        //PORTD|= (1<<PORTD7);// transistor on
                                        strcpy(str,"t=1");
                                        goto ANSWER;
                                }
                                else if(cmdval=='0')
                                {
                                        //PORTD &= ~(1<<PORTD7);// transistor off
                                        strcpy(str,"t=0");
                                        goto ANSWER;
                                }
                                else if(cmdval=='?')
                                {
                                       // if (PORTD & (1<<PORTD7)){
                                       //         strcpy(str,"t=1");
                                       //         goto ANSWER;
                                       // }
                                        strcpy(str,"t=0");
                                        goto ANSWER;
                                }
                        }
                        strcpy(str,"e=no_such_cmd");
                        goto ANSWER;
                }
                strcpy(str,"e=invalid_pw");
ANSWER:
                make_udp_reply_from_request(buf,str,strlen(str),MYUDPPORT);
        }
}
Esempio n. 23
0
int main(void){

        
        uint16_t plen;
        uint16_t dat_p;
        //uint8_t cmd_pos=0;
        int8_t cmd;
        //uint8_t payloadlen=0;
        //char str[20];
        //char cmdval;

        //char req[16];
        //char i;
        
        DATA_DAIKIN daikin;

        // 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); // enable PB1, LED as output 
        // the transistor on PD7:
        //DDRD|= (1<<DDD7);
        //PORTD &= ~(1<<PORTD7);// transistor off

        //PORTB = 0xff;
        //DDRB = 0xff;

        init_power_settings();
        uart_init();
        init_ir();
        init_airController(&daikin);
        sei();

        xfunc_out = (void (*)(char))uart_put;
        xputs(PSTR("AVR-Ethernet test monitor\n"));
        xprintf(PSTR("ENC28J60 Rev.%d\n"), enc28j60getrev());
xprintf(PSTR("PRR=%02X\n"),PRR);
        //init the web server ethernet/ip layer:
        init_ip_arp_udp_tcp(mymac,myip,MYWWWPORT);

        while(1){

                // handle ping and wait for a tcp packet
                plen=enc28j60PacketReceive(BUFFER_SIZE, buf);
                buf[BUFFER_SIZE]='\0';
                dat_p=packetloop_icmp_tcp(buf,plen);

                if(dat_p==0){
                        // check if udp otherwise continue
                        goto UDP;
                }
                // send data everytime we get a http request        
                xprintf(PSTR("get http request\n"));


                if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){
                        xprintf(PSTR("'GET \n'"));
                        // head, post and other methods:
                        //
                        // for possible status codes see:
                        // http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
                        plen=http200ok();
                        plen=fill_tcp_data_p(buf,plen,PSTR("<h1>200 OK</h1>"));
                        goto SENDTCP;
                }
                if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){
                        plen=http200ok();
                        plen=fill_tcp_data_p(buf,plen,PSTR("<p>Usage: http://host_or_ip/pc or m</p>\n"));
                        goto SENDTCP;
                }
                if ((strncmp("/pc ",(char *)&(buf[dat_p+4]),4)==0)
                     || (strncmp("/pc/ ",(char *)&(buf[dat_p+4]),5)==0)){
                        plen=http200ok();
                        plen=fill_tcp_data_p(buf,plen,pc_html);
                        goto SENDTCP;
                }
                if ((strncmp("/m ",(char *)&(buf[dat_p+4]),3)==0)
                     || (strncmp("/m/ ",(char *)&(buf[dat_p+4]),4)==0)){
                        plen=http200ok();
                        plen=fill_tcp_data_p(buf,plen,mobile_html);
                        goto SENDTCP;
                }
                // GET /ir?power=on&temp=28
                if (strncmp("/ir?",(char *)&(buf[dat_p+4]),4)==0){
                        if(find_key_val(
                                (char *)&(buf[dat_p+8]),
                                gStrbuf,
                                5,
                                "power"
                                )
                        ) {
                                xprintf(PSTR("power=%s\n"),gStrbuf);
                                if(strncmp("on", gStrbuf, 2) == 0){
                                        airController_on(&daikin);
                                        setData(DAIKIN, daikin.buf, 35*8);
                                }else if(strncmp("off", gStrbuf, 3) == 0){
                                        airController_off(&daikin);
                                        setData(DAIKIN, daikin.buf, 35*8);
                                }
                        }
                        if(find_key_val(
                                (char *)&(buf[dat_p+8]),
                                gStrbuf,
                                4,
                                "temp"
                                )
                        ) {
                                xprintf(PSTR("temp=%s\n"),gStrbuf);
                                airController_setTemp(&daikin, atoi(gStrbuf));
                                setData(DAIKIN, daikin.buf, 35*8);
                        }
                        plen=http200ok();
                        plen=fill_tcp_data_p(buf,plen,PSTR("Done\n"));
                        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){
                        plen=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){
                        //PORTD|= (1<<PORTD7);// transistor on
                }
                if (cmd==0){
                        //PORTD &= ~(1<<PORTD7);// transistor off
                }
                if (cmd==2){
                        // favicon:
                        //plen=moved_perm(buf,0);
                        goto SENDTCP;
                }
                if (cmd==-2){
                        // redirect to the right base url (e.g add a trailing slash):
                        plen=moved_perm(buf,1);
                        goto SENDTCP;
                }
                // if (cmd==-2) or any other value
                // just display the status:
                //plen=print_webpage(buf,/*(PORTD & (1<<PORTD7))*/0);
                //
SENDTCP:
                www_server_reply(buf,plen); // send data
                continue;

                // tcp port www end
                // -------------------------------
                // udp start, we listen on udp port 1200=0x4B0
UDP:
//                xprintf(PSTR("here is UDP:\n"));
                // check if ip packets are for us:
                if(eth_type_is_ip_and_my_ip(buf,plen)==0){
  //                      xprintf(PSTR("here is eth_type_is_ip_andd_my_ip\n"));
                        continue;
                }
                /*if (buf[IP_PROTO_P]==IP_PROTO_UDP_V&&buf[UDP_DST_PORT_H_P]==(MYUDPPORT>>8)&&buf[UDP_DST_PORT_L_P]==(MYUDPPORT&0xff)){
                        payloadlen=buf[UDP_LEN_L_P]-UDP_HEADER_LEN;
                        // you must sent a string starting with v
                        // e.g udpcom version 10.0.0.24
                        if (verify_password((char *)&(buf[UDP_DATA_P]))){
                                // find the first comma which indicates 
                                // the start of a command:
                                cmd_pos=0;
                                while(cmd_pos<payloadlen){
                                        cmd_pos++;
                                        if (buf[UDP_DATA_P+cmd_pos]==','){
                                                cmd_pos++; // put on start of cmd
                                                break;
                                        }
                                }
                                // a command is one char and a value. At
                                // least 3 characters long. It has an '=' on
                                // position 2:
                                if (cmd_pos<2 || cmd_pos>payloadlen-3 || buf[UDP_DATA_P+cmd_pos+1]!='='){
                                        strcpy(str,"e=no_cmd");
                                        goto ANSWER;
                                }
                                // supported commands are
                                // t=1 t=0 t=?
                                if (buf[UDP_DATA_P+cmd_pos]=='t'){
                                        cmdval=buf[UDP_DATA_P+cmd_pos+2];
                                        if(cmdval=='1'){
                                                PORTD|= (1<<PORTD7);// transistor on
                                                strcpy(str,"t=1");
                                                goto ANSWER;
                                        }else if(cmdval=='0'){
                                                PORTD &= ~(1<<PORTD7);// transistor off
                                                strcpy(str,"t=0");
                                                goto ANSWER;
                                        }else if(cmdval=='?'){
                                                if (PORTD & (1<<PORTD7)){
                                                        strcpy(str,"t=1");
                                                        goto ANSWER;
                                                }
                                                strcpy(str,"t=0");
                                                goto ANSWER;
                                        }
                                }
                                strcpy(str,"e=inv_cmd");
                                goto ANSWER;
                        }
                        strcpy(str,"e=inv_pw");
ANSWER:
                        make_udp_reply_from_request(buf,str,strlen(str),MYUDPPORT);
                }*/
        }
        return (0);
}
Esempio n. 24
0
int main(void){

        
        uint16_t dat_p,plen;
        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
        //
        while(1){
                // handle ping and wait for a tcp packet
                plen=enc28j60PacketReceive(BUFFER_SIZE, buf);
                dat_p=packetloop_icmp_tcp(buf,plen);
                if(plen==0){
                        // we are idle here
                        if (client_waiting_gw() ){
                                continue;
                        }
                        if (dns_state==0){
                                sec=0;
                                dns_state=1;
                                dnslkup_request(buf,PSTR(WEBSERVER_VHOST));
                                continue;
                        }
                        if (dns_state==1 && dnslkup_haveanswer()){
                                dns_state=2;
                                client_set_wwwip(dnslkup_getip());
                        }
                        if (dns_state!=2){
                                // retry every minute if dns-lookup failed:
                                if (sec > 60){
                                        dns_state=0;
                                }
                                // don't try to use web client before
                                // we have a result of dns-lookup
                                continue;
                        }
                        //----------
                        if (start_web_client==1){
                                LEDON;
                                start_web_client=2;
                                sec=0;
                                web_client_attempts++;
                                // the identi.ca line is status=Url_encoded_string
                                strcat(statusstr,"status=");
                                // the text to send to identi.ca (append after status=):
                                //urlencode("Meals are ready",&(statusstr[7]));
                                urlencode("I like tuxgraphics eth boards, ",&(statusstr[7]));
                                // append a number:
                                itoa(web_client_attempts,&(statusstr[strlen(statusstr)]),10);
                                // The BLOGGACCOUNT Authorization code can be generated from 
                                // username and password of your identi.ca account 
                                // by using this encoder: http://tuxgraphics.org/~guido/javascript/base64-javascript.html
                                client_http_post(PSTR("/api/statuses/update.xml"),PSTR(WEBSERVER_VHOST),PSTR(BLOGGACCOUNT),statusstr,&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 a request if we are in the right state (do
                        // not flood with request):
                        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 message if it failed:
                        if (start_web_client==2 && sec==7 && resend){
                                start_web_client=1;
                                resend--;
                        }
                        continue;
                }
                if(dat_p==0){ // plen!=0
                        // check for incomming messages not processed 
                        // as part of packetloop_icmp_tcp, e.g udp messages
                        udp_client_check_for_dns_answer(buf,plen);
                        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);
}
// analyse the url given
//                The string passed to this function will look like this:
//                ?s=1 HTTP/1.....
//                We start after the first slash ("/" already removed)
int8_t analyse_get_url(char *str)
{
	// the first slash:
	if (*str == 'c'){
		// alarm configpage:
		gPlen=print_alarm_config();
		return(10);
	}
	if (*str == 'n'){
		// network configpage:
		gPlen=print_net_config();
		return(10);
	}	
	if (*str == 'u'){
		if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"pw")){
			urldecode(gStrbuf);
			if (verify_password(gStrbuf)){
				if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"n")){
					urldecode(gStrbuf);
					gStrbuf[MYNAME_LEN]='\0';
					strcpy(myname,gStrbuf);
				}
				if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"di")){
					urldecode(gStrbuf);
					if (parse_ip(udpsrvip,gStrbuf)!=0){
						return(-2);
					}					
					
					// we've found destip, which means this is update from the
					// alarm conf page (this is a mandatory field)
					// Check alarm check box here
					if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"ae")){
						alarmOn=1;
					}else{
						alarmOn=0;
					}
				}
				if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"dp")){
					gStrbuf[4]='\0';
					udpsrvport=atoi(gStrbuf);
				}
				if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"hb")){
					gStrbuf[4]='\0';
					heartbeat_timeout_sec=atoi(gStrbuf);
					
					// we've found heartbeat, which means this is update from the
					// network conf page
					// Check dhcp check box here
					if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"dh")){
						dhcpOn=1;
					}else{
						dhcpOn=0;
					}		
				}
				if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"gi")){
					urldecode(gStrbuf);
					if (parse_ip(gwip,gStrbuf)!=0){
						return(-2);
					}
				}
				if (find_key_val(str,gStrbuf,STR_BUFFER_SIZE,"ip")){
					urldecode(gStrbuf);
					if (parse_ip(myip,gStrbuf)!=0){
						return(-2);
					}
				}
				data2eeprom();
				gPlen=http200ok();
				gPlen=fill_tcp_data_p(buf,gPlen,PSTR("<a href=/>[home]</a>"));
				gPlen=fill_tcp_data_p(buf,gPlen,PSTR("<h2>OK</h2>"));
				return(10);
			}
		}
		return(-1);
	}
	return(0);
}
Esempio n. 26
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);
}
Esempio n. 27
0
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. 28
0
// Control area for room-2
uint16_t print_webpage_room2(uint8_t *buf)
{
	uint16_t plen;
	plen=http200ok();
	plen=fill_tcp_data_p(buf,plen,PSTR("<style type=\"text/css\">* {padding:0;margin:0;} body {font-size:10pt;font-family:\"georgia\";color:#333333;background:#74888e;}\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("h5 {font-size:0.7em;} #outer {width:350px;border:2px solid #fff;background-color:#fff;margin:0 auto;} #header {background:#2b2b2b;margin-bottom:2px;}#headercontent {bottom:0;padding:0.7em 1em 0.7em 1em;}\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("#headercontent h1 {font-weight:normal;color:#fff;font-size:2.5em;}\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("#content {padding:2em 2em 0 2em;}\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("</style></head><body><div id=\"outer\"><div id=\"header\"><div id=\"headercontent\">"));
	// Webserver header
	plen=fill_tcp_data_p(buf,plen,PSTR("<h1>Rom-2</h1></div></div>\n"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<div id=\"content\"><a href=\"/cnf\"><< Tilbake</a><br><br>\n"));
	
	// Room 2 status
	plen=fill_tcp_data_p(buf,plen,PSTR("Temperatur n&aring;:  "));
	// Temperatur is blue for lower then 15 degrees, red for higher
	if (ethPacket[pFieldsModules+pTemp]<=15)
	{
		plen=fill_tcp_data_p(buf,plen,PSTR("<font color=blue> "));
		itoa(ethPacket[pFieldsModules+pTemp],strbuf,10);
		plen=fill_tcp_data(buf,plen,strbuf);
	}
	else
	{
		plen=fill_tcp_data_p(buf,plen,PSTR("<font color=red> "));
		itoa(ethPacket[pFieldsModules+pTemp],strbuf,10);
		plen=fill_tcp_data(buf,plen,strbuf);
	}
	plen=fill_tcp_data_p(buf,plen,PSTR("</font>&deg;C<br>"));
	
	plen=fill_tcp_data_p(buf,plen,PSTR("&Oslash;nsket temp: "));
	itoa(sm[1].temp,strbuf,10);
	plen=fill_tcp_data(buf,plen,strbuf);
	plen=fill_tcp_data_p(buf,plen,PSTR("&deg;C<br><br>"));
	
	// Check for slave status, 0 = off, 1 = on , 2 = no change
	plen=fill_tcp_data_p(buf,plen,PSTR("Status satt til: "));
	if (sm[1].status==0)
	{
		plen=fill_tcp_data_p(buf,plen,PSTR("<font color=red> "));
		plen=fill_tcp_data_p(buf,plen,PSTR("Off"));
	}
	else if (sm[1].status==1)
	{
		plen=fill_tcp_data_p(buf,plen,PSTR("<font color=green> "));
		plen=fill_tcp_data_p(buf,plen,PSTR("On"));
	}
	plen=fill_tcp_data_p(buf,plen,PSTR("</font>"));
	
	plen=fill_tcp_data_p(buf,plen,PSTR("<br>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("Status n&aring;:  "));
	if (ethPacket[pFieldsModules+pStatus]==0)
	{
		plen=fill_tcp_data_p(buf,plen,PSTR("<font color=red> "));
		plen=fill_tcp_data_p(buf,plen,PSTR("Off"));
	}
	else if (ethPacket[pFieldsModules+pStatus]==1)
	{
		plen=fill_tcp_data_p(buf,plen,PSTR("<font color=green> "));
		plen=fill_tcp_data_p(buf,plen,PSTR("On"));
	}
	else if (ethPacket[pFieldsModules+pStatus]==2)
	{
		plen=fill_tcp_data_p(buf,plen,PSTR("<font color=red> "));
		plen=fill_tcp_data_p(buf,plen,PSTR("NC"));
	}
	plen=fill_tcp_data_p(buf,plen,PSTR("</font><br><br>"));
	
	// Change status
	plen=fill_tcp_data_p(buf,plen,PSTR("<fieldset><legend><h4>Undermodul</h4></legend><form action=\"/stat2\" method=get>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<br><input type=\"radio\" name=\"onoff2\" value=1 "));
	if (sm[1].status==1)
		plen=fill_tcp_data_p(buf,plen,PSTR("checked "));
	plen=fill_tcp_data_p(buf,plen,PSTR(">Sl&aring;tt p&aring;<br>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<input type=\"radio\" name=\"onoff2\" value=0 "));
	if (sm[1].status==0)
		plen=fill_tcp_data_p(buf,plen,PSTR("checked "));
	plen=fill_tcp_data_p(buf,plen,PSTR(">Sl&aring;tt av<br>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("Passord:</td><td><input type=password size=8 name=pw><br>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<input type=submit value=\"OK\"></form><br></fieldset>"));
	
	// Set temperatur
	plen=fill_tcp_data_p(buf,plen,PSTR("<br><fieldset><legend><h4>Temp</h4></legend>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<form action=/tmpc2 method=get>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<br>Ny temp: <input type=text size=1 name=ntemp2 value="));
	plen=fill_tcp_data_p(buf,plen,PSTR("><br>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("Passord:<input type=password size=8 name=pw>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("<br><input type=submit value=\"OK\"><br>"));
	plen=fill_tcp_data_p(buf,plen,PSTR("</form><br></fieldset>"));
	
	// end
	plen=fill_tcp_data_p(buf,plen,PSTR("</div></body></html>"));
	
	return(plen);
}