#include #define STATUS_IDLE 0 #define STATUS_WAIT_IP 1 #define STATUS_SEND_IP 2 #define STATUS_WAIT_RESP 3 static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x01}; const char ipwebsite[] PROGMEM = "www.icanhazip.com"; const char setipwebsite[] PROGMEM = "www.nomedelsito.it"; char actualIp [16]; byte Ethernet::buffer[700]; static uint32_t getip_timer; byte actual_status; static void ip_callback (byte status, word off, word len) { char* usebuf; int ubuflen; int ipda; int iplen; actualIp[13]=0; actualIp[14]=0; actualIp[15]=0; usebuf = Ethernet::buffer + off; ubuflen = strlen (usebuf); ipda = 486; iplen= ubuflen- ipda -1 ; for(int i = 0; i < iplen ; i++) actualIp[i] = usebuf[ipda+i]; Serial.print ("actualIp="); Serial.println(actualIp); actual_status=STATUS_SEND_IP; } static void setip_callback (byte status, word off, word len) { Serial.println((const char*) Ethernet::buffer + off ); actual_status=STATUS_IDLE; getip_timer = millis()+60000; } void setup () { Serial.begin(57600); actual_status = STATUS_IDLE; if (!ether.begin(sizeof Ethernet::buffer, mymac, 10)) Serial.println( "Failed to access Ethernet controller"); else Serial.println("Ethernet controller initialized"); Serial.println(); if (!ether.dhcpSetup()) Serial.println("Failed to get configuration from DHCP"); else Serial.println("DHCP configuration done"); ether.printIp("IP Address:\t", ether.myip); ether.printIp("Netmask:\t", ether.netmask); ether.printIp("Gateway:\t", ether.gwip); Serial.println(); getip_timer = millis()+5000; } void loop() { ether.packetLoop(ether.packetReceive()); switch(actual_status) { case STATUS_IDLE: getIP(); break; case STATUS_WAIT_IP: waitIP(); break; case STATUS_SEND_IP: sendIP(); break; case STATUS_WAIT_RESP: waitRESP(); break; } } void getIP() { if (millis() > getip_timer) { if (!ether.dnsLookup(ipwebsite)) { Serial.print("Unable to resolve IP for "); SerialPrint_P(ipwebsite); } else { SerialPrint_P(ipwebsite); ether.printIp(" resolved to:\t", ether.hisip); } Serial.println(); ether.packetLoop(ether.packetReceive()); ether.browseUrl(PSTR("/"), "", ipwebsite, ip_callback); actual_status=STATUS_WAIT_IP; } } void waitIP() { } void sendIP() { char pageurl[50]; if (!ether.dnsLookup(setipwebsite)) { Serial.print("Unable to resolve IP for "); SerialPrint_P(setipwebsite); } else { SerialPrint_P(setipwebsite); ether.printIp(" resolved to:\t", ether.hisip); } Serial.println(); pageurl[0]=0; strcat (pageurl, "ipremote.php?newip=") ; strcat (pageurl, actualIp); strcat (pageurl , "&pass=123456"); Serial.print ("pageurl="); Serial.println(pageurl); ether.packetLoop(ether.packetReceive()); ether.browseUrl(PSTR("/"), pageurl , setipwebsite, setip_callback); actual_status=STATUS_WAIT_RESP; } void waitRESP() { } void SerialPrint_P(PGM_P str) { for (uint8_t c; (c = pgm_read_byte(str)); str++) Serial.write(c); }