connection - Problems with Ethernet shield + arduino -
i trying simple (ready) programs arduino examples regardin ethernet shield. still getting no result. receiving not connected or blank serial monitor. knows why? think connected dhcp since on dhcp list of router
<#include <spi.h> #include <ethernet.h> #include <ethernetudp.h> // enter mac address controller below. // newer ethernet shields have mac address printed on sticker on shield byte mac[] = { 0xde, 0xad, 0xbe, 0xef, 0xfe, 0xed }; unsigned int localport = 8888; // local port listen udp packets ipaddress timeserver(192, 43, 244, 18); // time.nist.gov ntp server const int ntp_packet_size = 48; // ntp time stamp in first 48 bytes of message byte packetbuffer[ ntp_packet_size]; //buffer hold incoming , outgoing packets // udp instance let send , receive packets on udp ethernetudp udp; void setup() { // open serial communications , wait port open: serial.begin(9600); while (!serial) { ; // wait serial port connect. needed leonardo } // start ethernet , udp if (ethernet.begin(mac) == 0) { serial.println("failed configure ethernet using dhcp"); // no point in carrying on, nothing forevermore: (;;) ; } udp.begin(localport); } void loop() { sendntppacket(timeserver); // send ntp packet time server // wait see if reply available delay(1000); if ( udp.parsepacket() ) { // we've received packet, read data udp.read(packetbuffer, ntp_packet_size); // read packet buffer //the timestamp starts @ byte 40 of received packet , 4 bytes, // or 2 words, long. first, esxtract 2 words: unsigned long highword = word(packetbuffer[40], packetbuffer[41]); unsigned long lowword = word(packetbuffer[42], packetbuffer[43]); // combine 4 bytes (two words) long integer // ntp time (seconds since jan 1 1900): unsigned long secssince1900 = highword << 16 | lowword; serial.print("seconds since jan 1 1900 = " ); serial.println(secssince1900); // convert ntp time everyday time: serial.print("unix time = "); // unix time starts on jan 1 1970. in seconds, that's 2208988800: const unsigned long seventyyears = 2208988800ul; // subtract seventy years: unsigned long epoch = secssince1900 - seventyyears; // print unix time: serial.println(epoch); // print hour, minute , second: serial.print("the utc time "); // utc time @ greenwich meridian (gmt) serial.print((epoch % 86400l) / 3600); // print hour (86400 equals secs per day) serial.print(':'); if ( ((epoch % 3600) / 60) < 10 ) { // in first 10 minutes of each hour, we'll want leading '0' serial.print('0'); } serial.print((epoch % 3600) / 60); // print minute (3600 equals secs per minute) serial.print(':'); if ( (epoch % 60) < 10 ) { // in first 10 seconds of each minute, we'll want leading '0' serial.print('0'); } serial.println(epoch % 60); // print second } // wait ten seconds before asking time again delay(10000); } // send ntp request time server @ given address unsigned long sendntppacket(ipaddress& address) { // set bytes in buffer 0 memset(packetbuffer, 0, ntp_packet_size); // initialize values needed form ntp request // (see url above details on packets) packetbuffer[0] = 0b11100011; // li, version, mode packetbuffer[1] = 0; // stratum, or type of clock packetbuffer[2] = 6; // polling interval packetbuffer[3] = 0xec; // peer clock precision // 8 bytes of 0 root delay & root dispersion packetbuffer[12] = 49; packetbuffer[13] = 0x4e; packetbuffer[14] = 49; packetbuffer[15] = 52; // ntp fields have been given values, // can send packet requesting timestamp: udp.beginpacket(address, 123); //ntp requests port 123 udp.write(packetbuffer, ntp_packet_size); udp.endpacket(); }
Comments
Post a Comment