#include <Arduino.h> #include <EtherCard.h> //#include <IPAddress.h> #include <MemoryFree.h> // ethernet interface mac address, must be unique on the LAN #define VERSION "WOWlanCloud 20140102-1805" #define URL "/index.php?action=get&id=zorglub" #define WEBSITE "wolpobox.logsm.net" #define PERIODEMS 5000 #define WOLLISTENPORT 9 #define WOLTARGETPORT 9 #define WOLOUTPORT 100 #define true 1 #define false 0 #define DEBUG false // true for debugging #define TRACE true //some progree info. #define CLOUD true #define SERVER true #define PROXY true #define LBUF 600 char website[] PROGMEM = WEBSITE; char url[] PROGMEM = URL; static byte mymac[] = { 0x00,0x16,0xA5,0x76,0x19,0x3A }; static byte lowip= 255; // left part by Dhcp 255 if no! static uint32_t timer; static byte broadcast[4] = {255,255,255,255} ; //{255,255,255,255}; static int n=0; // count http requests static int ok=true; // global abandon static int lenquoi; static int termine; // 1 when found static int eq; static int iii; static int pv; static int ipv; // byte mac[6] ; byte key[10]; char magic[102]; char alphabet[23] /*PROGMEM*/ ="0123456789ABCDEFabcdef"; char hexamac[100]=""; byte Ethernet::buffer[LBUF]; //?????? BufferFiller bfill; //??????? static int udpon=0; void setup () { // pinMode(13, OUTPUT); Serial.begin(9600); Serial.println("andiamo "); Serial.println(VERSION); Serial.print("Options: Debug, Trace, Cloud, Server, Proxy:"); Serial.print (DEBUG); Serial.print (TRACE); Serial.print (CLOUD); Serial.print (SERVER); Serial.println (PROXY); Serial.print("freeMemory()="); Serial.println(freeMemory()); Serial.print("Ethernet Controller: "); if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0) { ok=false; Serial.println( "Access Failed."); } else Serial.println("Ok."); // ether.staticSetup(myip); if (ok) { Serial.print("Dhcp: "); if (!ether.dhcpSetup()) { Serial.println("failed."); ok=false; } else Serial.println("Ok."); } if (ok){ if (lowip<255) ether.myip[3]=lowip; ether.printIp("IP: ", ether.myip); ether.printIp("GW: ", ether.gwip); ether.printIp("DNS: ", ether.dnsip); if (CLOUD){ if (!ether.dnsLookup(website)) { Serial.println("DNS failed"); ok=false; } if (ok) { ether.printIp("SRV: ", ether.hisip); } } } //register udpSerialPrint() to port 1337 if (ok && PROXY) { ether.udpServerListenOnPort(&udpReceivedCallback, WOLLISTENPORT); Serial.print("listen on port: "); Serial.println(WOLLISTENPORT); } } void getvalue(word off,word len,char retour[],int lenret,char pin[]){ lenquoi=strlen(pin); // search for pin: termine=false; // 1 when found pin eq; iii=-1; while (!termine) { eq=0; iii=iii+1; if (iii==len) termine=1; while ((eq<lenquoi) && (Ethernet::buffer [off+iii+eq]==pin[eq])){ if (eq==lenquoi-1 ) termine=true; eq=eq+1; } } // Serial.println(""); if (termine) { pv=false; ipv=0; //Serial.print("reponse="); while (!pv && iii+eq+1+ipv<len && ipv<lenret ) { retour[ipv]=Ethernet::buffer [off+iii+eq+ipv]; if (retour[ipv]==';') { retour[ipv]=0; pv=true; } ipv=ipv+1; } } if (!termine || !pv) { Serial.println ("no colon found"); ok=false; } } // called when the client request is complete static void my_callback (byte status, word off, word len) { if (DEBUG) Serial.println(">>>"); Ethernet::buffer[off+len] = 0; if (DEBUG) for (int j=0;j<len;j=j+1) Serial.print((char ) Ethernet::buffer[off+j]); // check print full packet getvalue(off,len,hexamac,100,"mac:"); if (strlen(hexamac)==0) return; getvalue(off,len,hexamac,100,"ip:"); getvalue(off,len,hexamac,100,"id:"); getvalue(off,len,hexamac,100,"token:"); getvalue(off,len,hexamac,100,"mac:"); if (TRACE) { Serial.print("Cloud: mac:"); Serial.println(hexamac); } buildmagic(); sendmagic(magic,102); } //callback that echo received packet (magic!) void udpReceivedCallback(word port, byte ip[4], const char *data, word len) { if (!udpon) { // protect again re-entrance' udpon=true; if (DEBUG){ Serial.println(" Raw: "); Serial.println(data); } if (TRACE) Serial.println ("udp packet forwarded as broadcast"); sendmagic((char *) data,len); if (DEBUG) Serial.println(" done"); udpon=false; } } static word hP() { n=n+1; // compte les appels à homePage long t = millis() / 1000; word h = t / 3600; byte m = (t / 60) % 60; byte s = t % 60; bfill = ether.tcpOffset(); bfill.emit_p(PSTR( //??????? "HTTP/1.0 200 OK\r\n" "Content-Type: text/html\r\n" "Pragma: no-cache\r\n" "\r\n" // "<meta http-equiv='refresh' content='1'/>" "<title>Po server</title>" "<h1>$D$D:$D$D:$D$D</h1> n= $D"), h/10, h%10, m/10, m%10, s/10, s%10,n); //??????? return bfill.position(); //??????? } int h2d(char h){ char* ih= strchr(alphabet,h); char* i0= strchr(alphabet,'0'); int ici=ih-i0; if (ici>15) ici=ici-6; return ici; } void buildmagic (){ if (DEBUG) Serial.print("build magic mac:"); for (int repe=0;repe<6;repe=repe+1) magic[repe]=255; for (int p=0;p<6;p=p+1){ byte g=h2d(hexamac[2*p]); byte d=h2d(hexamac[2*p+1]); int h2=g*16+d; magic[6+p]=h2; for (int repe=1;repe<16;repe=repe+1){ magic[6+6*repe+p]=magic[6+p]; } } } void sendmagic(char* magic,int len){ if (DEBUG) Serial.println("sendmagic"); if (DEBUG) Serial.println(magic); ether.sendUdp(magic, len, WOLOUTPORT, broadcast, WOLTARGETPORT); } void loop () { if (ok==1 && PROXY) { // nothing } if (CLOUD) ether.packetLoop(ether.packetReceive()); if (ok==1 && CLOUD) { if (millis() > timer) { timer = millis() + PERIODEMS; ether.browseUrl(PSTR(URL) , "", website, my_callback); } // timer } // end cloud if (ok==1 && SERVER){ word len = ether.packetReceive(); word pos = ether.packetLoop(len); if (pos>0) {// check if valid tcp data is received getvalue(pos,len-pos,hexamac,100,"mac:"); ether.httpServerReply(hP()); // send web page data if (hexamac != "") { if (TRACE) { Serial.print("Http: "); Serial.println(hexamac); } buildmagic(); sendmagic(magic,102); } } } }