Hi everyone,
yesterday i recieved my Arduino Ethernet shield, just to find out it doesnt use the original chip, but the ENC28J60.
So after at least 40.000 hours on the internet i found this post.
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1275614308/15
By using this library http://blog.thiseldo.co.uk/?p=329 and some code by Perfo i was able to turn on and off a few LEDS. But i wasnt able to add more buttons, because it appears the Arduino only sopports very little HTML code.
So everything you do, is to write en additonal HTML you can run on the computer or a server to open the arduino pages in an invisible window in the browser.
So first the Arduino code (of corse you have to change the IP)
// EtherShield gate controller
#include "etherShield.h"
#include <IRremote.h>
#include <IRremoteInt.h>
// Random mac address
// IP that shouldn't clash with anything else on the LAN
static uint8_t mymac[6] = {
0x54,0xA5,0x58,0x1B ,0x00,0x24};
static uint8_t myip[4] = {
192,168,0,16};
#define DEBUG
#define MYWWWPORT 80
#define BUFFER_SIZE 750
static uint8_t buf[BUFFER_SIZE+1];
int LED1=3;
int LED2=4;
int LED3=5;
int LED1status = 0;
int LED2status = 0;
int LED3status = 0;
IRsend irsend;
// The ethernet shield
EtherShield es=EtherShield();
uint16_t http200ok(void)
{
return(es.ES_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")));
}
// prepare the webpage by writing the data to the tcp send buffer
uint16_t print_webpage(uint8_t *buf)
{
uint16_t plen;
plen=http200ok();
return(plen);
}
void setup(){
// initialize enc28j60
Serial.begin(9600);
es.ES_enc28j60Init(mymac);
// init the ethernet/ip layer:
es.ES_init_ip_arp_udp_tcp(mymac,myip, MYWWWPORT);
pinMode (LED1, OUTPUT);
pinMode (LED2, OUTPUT);
pinMode (LED3, OUTPUT);//set open pin to ouptut
//on first run set both the open and close outputs to logic zero to prevent unknown states
digitalWrite(LED2,LOW);
digitalWrite(LED1,LOW);
digitalWrite(LED3,LOW);//set the open output to logic zero
}
void loop(){
uint16_t plen, dat_p;
while(1) {
// read packet, handle ping and wait for a tcp packet:
dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_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=es.ES_fill_tcp_data_p(buf,dat_p,PSTR("<h1>200 OK</h1>"));
goto SENDTCP;
}
// three web page in the "root directory" of the web server
if (strncmp("/ard.html ",(char *)&(buf[dat_p+4]),8)==0){
dat_p=print_webpage(buf);
goto SENDTCP;
}
else if (strncmp("/ard2.html ",(char *)&(buf[dat_p+4]),8)==0){
if(LED1status==1)
{
irsend.sendNEC(0xFFF807,32);
LED1status=0;
Serial.println(LED1status);
}
else
{
LED1status=1;
irsend.sendNEC(0xFFB04F,32);
Serial.println(LED1status);
}
dat_p=print_webpage(buf);
goto SENDTCP;
}
//RED
else if (strncmp("/ard3.html ",(char *)&(buf[dat_p+4]),8)==0){
irsend.sendNEC(0xFF9867,32);
dat_p=print_webpage(buf);
goto SENDTCP;
}
//GREEN
else if (strncmp("/ard4.html ",(char *)&(buf[dat_p+4]),8)==0){
irsend.sendNEC(0xFFD827,32);
dat_p=print_webpage(buf);
goto SENDTCP;
}
//BLUE
else if (strncmp("/ard5.html ",(char *)&(buf[dat_p+4]),8)==0){
irsend.sendNEC(0xFF8877,32);//set open output to logic one
dat_p=print_webpage(buf);
goto SENDTCP;
}
//WHITE
else if (strncmp("/ard6.html ",(char *)&(buf[dat_p+4]),8)==0){
irsend.sendNEC(0xFFA857,32);//set open output to logic one
dat_p=print_webpage(buf);
goto SENDTCP;
}
//STROBE
else if (strncmp("/ard7.html ",(char *)&(buf[dat_p+4]),8)==0){
irsend.sendNEC(0xFF00FF,32);//set open output to logic one
dat_p=print_webpage(buf);
goto SENDTCP;
}
else{
dat_p=es.ES_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:
es.ES_www_server_reply(buf,dat_p); // send web page data
// tcp port 80 end
}
}
My Code now sends IR signals but you can just exchang this for LEDS or whater you want
expl. irsend.sendNEC(0xFF00FF,32); => digitalWrite(LED1,HIGH);
and this is the htmlcode you can just write in an editor and save it as code.html (again change the IP)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>AWESOME CONTROLE</title>
</head>
<body>
<h1>Fernsteuerung</h1>
<iframe src="startseite.htm" name="Fensterlein" marginheight="10" marginwidth="10" align="right" height="0" width="0">
&amp;amp;amp;amp;lt;p&amp;amp;amp;amp;gt;Ihr Browser kann
leider keine
eingebetteten
Frames
anzeigen&amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;gt;
</iframe>
<center>
<p><a href="http://192.168.0.16/ard2.html" target="Fensterlein"><b>RGB ON/OFF</b></a><b>
<a href="http://192.168.0.16/ard3.html" target="Fensterlein"><b>RED
</b></a><b><b><a href="http://192.168.0.16/ard4.html" target="Fensterlein"><b>GREEN</b></a><b>
<a href="http://192.168.0.16/ard5.html" target="Fensterlein"><b>BLUE</b></a><b>
<a href="http://192.168.0.16/ard6.html" target="Fensterlein"><b>WHITE</b></a><b>
<a href="http://192.168.0.16/ard7.html" target="Fensterlein"><b>STROBE</b></a><b>
</center>
<p></p>
<b><b><b><b>
</b></b>
</b></b>
</body>
</html>
you can the that for example the button RGB ON/OFF opens in the invisble window Fensterlein the Arduinopage http://192.168.0.16/ard2.html and in the Arduinocode you can the what happens in that case
//RED
else if (strncmp("/ard3.html ",(char *)&(buf[dat_p+4]),8)==0){
irsend.sendNEC(0xFF9867,32);
dat_p=print_webpage(buf);
goto SENDTCP;
}
In this case it sends the IR signal irsend.sendNEC(0xFF9867,32); but once again you can put in whatever you want.
The only problem by now, is that it only runs in your local network. I heard of dyndns but can anyone maybe post a tuturial? im using a D-Link di-524.
So i hoped many can use this, to create theire one proyects, and someone helps me with connecting the arduino to the real internet