예제 #1
0
time_t getNtpTime() {
  while (Udp.parsePacket() > 0)
    ; // discard any previously received packets
  //Serial.println("$CLS,Y0,X0#TX NTP RQ");
  PulseLed(BLUE, 2, 50, 10);
  sendNTPpacket(timeServer);
  uint32_t beginWait = millis();
  uint32_t waited = millis() - beginWait;
  while (waited < 3000)
  {
    int size = Udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      //Serial.println("$CLS,Y0,X0#RX NTP OK");
      Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
      unsigned long secsSince1900;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 = (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      analogWrite(GREEN, 100);
      analogWrite(RED, 0);
      analogWrite(BLUE, 0);
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR + (waited / 1000);
    }
    waited = millis() - beginWait;
  }
  //Serial.println("$CLS,Y0,X0#*** NTP TO ***");
  PulseLed(RED, 4, 50, 10);
  analogWrite(GREEN, 0);
  analogWrite(RED, 100);
  analogWrite(BLUE, 0);
  return 0; // return 0 if unable to get the time
}
unsigned long getNTPTimestamp()
{
  unsigned long ulSecs2000;
  
  udp.begin(ntpPort);
  sendNTPpacket(timeServer); // send an NTP packet to a time server
  delay(1000);    // wait to see if a reply is available
  int cb = udp.parsePacket();

  if(!cb)
  {
    Serial.println("Timeserver not accessible! - No RTC support!"); 
    ulSecs2000=0;
  }
  else
  {
    Serial.print("packet received, length=");
    Serial.println(cb);
    udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
    
    //the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, esxtract the two words:
    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
    
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900):
    ulSecs2000  = highWord << 16 | lowWord;
    ulSecs2000 -= 2208988800UL; // go from 1900 to 1970
    ulSecs2000 -= 946684800UL; // go from 1970 to 2000
  }    
  return(ulSecs2000);
}
예제 #3
0
파일: aj_net.c 프로젝트: durake/core-ajtcl
AJ_Status AJ_Net_RecvFrom(AJ_IOBuffer* buf, uint32_t len, uint32_t timeout)
{
    AJ_InfoPrintf(("AJ_Net_RecvFrom(buf=0x%p, len=%d., timeout=%d.)\n", buf, len, timeout));

    AJ_Status status = AJ_OK;
    int ret;
    uint32_t rx = AJ_IO_BUF_SPACE(buf);
    unsigned long Recv_lastCall = millis();

    AJ_InfoPrintf(("AJ_Net_RecvFrom(): len %d, rx %d, timeout %d\n", len, rx, timeout));

    rx = min(rx, len);

    while ((g_clientUDP.parsePacket() == 0) && (millis() - Recv_lastCall < timeout)) {
        delay(10); // wait for data or timeout
    }

    AJ_InfoPrintf(("AJ_Net_RecvFrom(): millis %d, Last_call %d, timeout %d, Avail %d\n", millis(), Recv_lastCall, timeout, g_clientUDP.available()));
    ret = g_clientUDP.read(buf->writePtr, rx);
    AJ_InfoPrintf(("AJ_Net_RecvFrom(): read() returns %d, rx %d\n", ret, rx));

    if (ret == -1) {
        AJ_InfoPrintf(("AJ_Net_RecvFrom(): read() fails. status=AJ_ERR_READ\n"));
        status = AJ_ERR_READ;
    } else {
        if (ret != -1) {
            AJ_DumpBytes("AJ_Net_RecvFrom", buf->writePtr, ret);
        }
        buf->writePtr += ret;
        AJ_InfoPrintf(("AJ_Net_RecvFrom(): status=AJ_OK\n"));
        status = AJ_OK;
    }
    AJ_InfoPrintf(("AJ_Net_RecvFrom(): status=%s\n", AJ_StatusText(status)));
    return status;
}
예제 #4
0
int SonosEsp::discoverSonos(){
    _numberOfDevices=0;
    WiFiUDP Udp;
    Udp.begin(1900);
    IPAddress sonosIP;
    bool timedOut = false;
    unsigned long timeLimit = 15000;
    unsigned long firstSearch = millis();
    do {
        Serial.println("Sending M-SEARCH multicast");
        Udp.beginPacketMulticast(IPAddress(239, 255, 255, 250), 1900, WiFi.localIP());
        Udp.write("M-SEARCH * HTTP/1.1\r\n"
        "HOST: 239.255.255.250:1900\r\n"
        "MAN: \"ssdp:discover\"\r\n"
        "MX: 1\r\n"
        "ST: urn:schemas-upnp-org:device:ZonePlayer:1\r\n");
        Udp.endPacket();
        unsigned long lastSearch = millis();

        while((millis() - lastSearch) < 5000){
            int packetSize = Udp.parsePacket();
            if(packetSize){
                char packetBuffer[255];
                //Serial.print("Received packet of size ");
                //Serial.println(packetSize);
                //Serial.print("From ");
                sonosIP = Udp.remoteIP();

                //xxx if new IP, it should be put in an array

                addIp(sonosIP);
                //found = true; 
                Serial.print(sonosIP);
                Serial.print(", port ");
                Serial.println(Udp.remotePort());
                
                // read the packet into packetBufffer
                int len = Udp.read(packetBuffer, 255);
                if (len > 0) {
                    packetBuffer[len] = 0;
                }
                //Serial.println("Contents:");
                //Serial.println(packetBuffer);
            }
            delay(50);
        }
    } while((millis()-firstSearch)<timeLimit);
    //if (!found) {
      //sonosIP.fromString("0.0.0.0"); xxx 
    //}
    return _numberOfDevices;
}
예제 #5
0
time_t getNtptime() {
  time_t epoch = 0;

  //get a random server from the pool
  WiFi.hostByName(ntpServerName, timeServerIP);

  sendNTPpacket(timeServerIP); // send an NTP packet to a time server
  // wait to see if a reply is available
  delay(500);

  int cb = udp.parsePacket();
  if (!cb) {
    //Serial.println("no packet!?");
    wsSend("NTP Error");
  }
  else {
    // Serial.print("packet received, length=");
    // Serial.println(cb);
    // We've received a packet, read the data from it
    udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

    //the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, esxtract the two words:

    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900):
    unsigned long secsSince1900 = highWord << 16 | lowWord;
    // Serial.print("Seconds since Jan 1 1900 = " );
    // Serial.println(secsSince1900);

    // now convert NTP time into 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:
    epoch = secsSince1900 - seventyYears;

    epoch = epoch - (60 * 60 * ntpOffset); // take off 4 hrs for EDT offset
    sprintf(str, "NTP epoch=%d", epoch);
    wsSend(str);



  }
  return epoch;
}
예제 #6
0
void Protocol::loop()
{
  _packetSize = Udp.parsePacket();

  if (_packetSize) {
    now = millis();

    char _packetBuffer[PACKET_SIZE] = {}; // UDP_TX_PACKET_MAX_SIZE is too large: 8192

    Udp.read(_packetBuffer, _packetSize);

    #ifdef MODULE_CAN_DEBUG
    _remoteIP    = Udp.remoteIP();
    _remotePort  = Udp.remotePort();

    Serial.print("New packet received from: ");
    Serial.print(_remoteIP);
    Serial.print(":");
    Serial.println(_remotePort);
    Serial.print("Message: ");
    Serial.println(_packetBuffer);
    #endif

    _lastTalkTime = now;

    if (strcmp(_packetBuffer, "hi") == 0) {
      _isConnected = true;

      _onConnectedCb();
    } else if (strcmp(_packetBuffer, "bye") == 0) {
      _isConnected = false;

      _onDisconnectedCb();
    } else if (strcmp(_packetBuffer, "ping") == 0) {
      send("ping");
    } else {
      _onMessageCb(String(_packetBuffer));
    }
  } else if (_isConnected) {
    now = millis();

    if(now - _lastTalkTime > TIMEOUT) {
      _isConnected = false;

      _onDisconnectedCb();
    }
  }
}
예제 #7
0
void radioTask(const void *arg) {

//while(true){

unsigned long tv;

  char getChar;
  char count = 0;

  char receiverBuffer2[RECEIVER_BUFFER_SIZE];

    while (true){

    if (Udp.read(receiverBuffer, sizeof(receiverBuffer)) > 0) {
	    
		for(int i=0;i<sizeof(receiverBuffer);i++){
			getChar=receiverBuffer[i];
			if (getChar == '@') {
						memset(receiverBuffer2, '\0', sizeof(receiverBuffer2));
						receiverBuffer2[0] = getChar;
						count = 1;
					} else if ((getChar == '#') && (receiverBuffer2[0] == '@')
							&& (count < sizeof(receiverBuffer2))) {
						receiverBuffer2[count] = getChar;
						processRadioMessages( receiverBuffer2, strlen(receiverBuffer2));
						memset(receiverBuffer2, '\0', sizeof(receiverBuffer2));
						count = 0;
					} else {
						if (receiverBuffer2[0] == '@' && (count < sizeof(receiverBuffer2))) {
							receiverBuffer2[count] = getChar;
							count++;
						} else {
							memset(receiverBuffer2, '\0', sizeof(receiverBuffer2));
							count = 0;
						}
					}
		}

      memset(receiverBuffer,'\0',sizeof(receiverBuffer));
	   memset(receiverBuffer2,'\0',sizeof(receiverBuffer2));
    }
	os_thread_yield();
	delay(1);
  }

}
예제 #8
0
time_t getNtpTime()
{
  while (Udp.parsePacket() > 0) ; // discard any previously received packets
  println_dbg("Transmit NTP Request");
  sendNTPpacket(timeServer);
  uint32_t beginWait = millis();
  while (millis() - beginWait < 1500) {
    int size = Udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      println_dbg("Receive NTP Response");
      Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
      unsigned long secsSince1900;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  println_dbg("No NTP Response :-(");
  return 0; // return 0 if unable to get the time
}
예제 #9
0
// http://www.arduino.cc/en/Tutorial/WiFiRTC
unsigned long readLinuxEpochUsingNTP()
{
  Udp.begin(localPort);
  sendNTPpacket(timeServer); // send an NTP packet to a time server
  
  // wait to see if a reply is available
  delay(1000);

  if ( Udp.parsePacket() ) {
    Serial.println("NTP time received");
    // We've received a packet, read the data from it
    Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

    //the timestamp starts at byte 40 of the received packet and is four bytes,
    // or two words, long. First, esxtract the two words:

    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
    // combine the four bytes (two words) into a long integer
    // this is NTP time (seconds since Jan 1 1900):
    unsigned long secsSince1900 = highWord << 16 | lowWord;

    // now convert NTP time into everyday time:
    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
    const unsigned long seventyYears = 2208988800UL;
    // subtract seventy years:

    Udp.stop();
    return (secsSince1900 - seventyYears + timeZone * SECS_PER_HOUR);
  }

  else {
    Udp.stop();
    return 0;
  }
}
예제 #10
0
// Check UDP for incoming packets
void check_incoming() {
  int packetSize;
  int i;
  int empty = 0;
  int found = -1;

  packetSize = udp.parsePacket();
  if (packetSize) {
    IPAddress remote = udp.remoteIP();
    // read the packet into packetBufffer
    udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    #ifdef DEBUG
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    for (i = 0; i < 4; i++) {
      Serial.print(remote[i], DEC);
      if (i < 3) {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(udp.remotePort());
    Serial.print("Dump: ");
    for (i = 0; i < packetSize / sizeof(uint16_t); i++) {
      Serial.print(*((uint16_t *) (packetBuffer + i * sizeof(uint16_t))));
      Serial.print(" ");
    }
    Serial.println();
    #endif

    // Master sends all packets to its clients and updates cache
    if (my_node_type == MSGMULTI_MASTER) {
      found = -1;
      empty = 0;
      send_packet(packetBuffer, packetSize, remote);
      for (i = 0; i < MSGMULTI_MAXCLIENTS; i++) {
        if (clients[i].client == remote) {
          found = i;
          break;
        }
        if (clients[i].expire <= 0)
          empty = i;
      }
      if (found >= 0)
        clients[found].expire = 1024;
      else {
        clients[empty].client = remote;
        clients[empty].expire = 1024;
      }
    }

    // Process incoming statuses
    for (i = 0; i < *((uint16_t *) packetBuffer); i++) {
      receive_status((struct msgrecord *) (packetBuffer + i * sizeof(struct msgrecord) + sizeof(uint16_t)));
    }

    // Repeat incoming check to process all packets in queue
    check_incoming();
  }

  // Check if we have to repeat last sent statuses
  resend_status();
} // void check_incoming()
예제 #11
0
파일: rrtime.cpp 프로젝트: slevon/EspLogger
//This gets the time from the server and sets the system Time.
//This function is also used as syncProvider:
time_t RRTime::getTime(){
   unsigned int localPort = 8888;      // local port to listen for UDP packets
  
  /* Don't hardwire the IP address or we won't get the benefits of the pool.
   *  Lookup the IP address for the host name instead */
  //IPAddress timeServerIP(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov
  IPAddress timeServerIP; // time.nist.gov NTP server address
  const char* ntpServerName = "europe.pool.ntp.org";
  
  static const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
  
  byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packet
  const int timeZone = 1;     // Central European Time
  WiFiUDP udp; // A UDP instance to let us send and receive packets over UDP
  udp.begin(localPort);
  DEBUGPRINT.print("Local port: ");
  DEBUGPRINT.println(udp.localPort());
  DEBUGPRINT.println("waiting for sync");

     //get a random server from the pool
  if(!WiFi.hostByName(ntpServerName, timeServerIP)){
    DEBUGPRINT.print("ERROR; Could not resolve IP using ");
    IPAddress fallBack(132, 163, 4, 102);
    timeServerIP = fallBack;
    DEBUGPRINT.println(timeServerIP);
    } 
  DEBUGPRINT.print("Timesever IP:");
  DEBUGPRINT.println(timeServerIP);
  while (udp.parsePacket() > 0) ; // discard any previously received packets
  DEBUGPRINT.println("Transmit NTP Request");
  // set all bytes in the buffer to 0
  /////////////////////////////////////////////////////////////////////////
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  // (see URL above for details on the 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 zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:                 
  udp.beginPacket(timeServerIP, 123); //NTP requests are to port 123
  udp.write(packetBuffer, NTP_PACKET_SIZE);
  udp.endPacket();
  /////////////////////////////////////////////////////////////////////////
  uint32_t beginWait = millis();
  while (millis() - beginWait < 3000) {
    int size = udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      DEBUGPRINT.println("Receive NTP Response");
      udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
      unsigned long secsSince1900;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      //store last sync:
      lastSync = millis();
      
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  DEBUGPRINT.println("No NTP Response :-(");
  return 0; // return 0 if unable to get the time
}
예제 #12
0
bool doNTPupdate() {
    bool ntpValid;
    
    //get a random server from the pool
    WiFi.hostByName(ntpServerName, timeServerIP);

    sendNTPpacket(timeServerIP); // send an NTP packet to a time server
    // wait to see if a reply is available
    delay(1000);

    int cb = udp.parsePacket();
    if (!cb) {
        Serial.println("no packet yet");
        ntpValid = false;
    } else {
        lastNTP.updateTimeMillis = millis(); //track time of update, so we can estimate time between updates

        Serial.print("packet received, length=");
        Serial.println(cb);
        // We've received a packet, read the data from it
        udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

        //the timestamp starts at byte 40 of the received packet and is four bytes,
        // or two words, long. First, esxtract the two words:

        unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
        unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
        // combine the four bytes (two words) into a long integer
        // this is NTP time (seconds since Jan 1 1900):
        unsigned long secsSince1900 = highWord << 16 | lowWord;
        //Serial.print("Seconds since Jan 1 1900 = " );
        //Serial.println(secsSince1900);

        // now convert NTP time into 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);

        int8_t h_temp = (uint8_t) ((epoch % 86400L) / 3600);
        h_temp -= 8; //PST offset
        if (h_temp < 0) {
            h_temp += 24;
        }


        lastNTP.hours = (uint8_t) h_temp;
        ;
        lastNTP.minutes = (uint8_t) ((epoch % 3600) / 60);
        lastNTP.seconds = (uint8_t) (epoch % 60);
        ntpValid = true;

        // print the hour, minute and second:
        Serial.print("hours = ");
        Serial.println(lastNTP.hours); // print the hour (86400 equals secs per day)
        Serial.print("mins = ");
        Serial.println(lastNTP.minutes);
        Serial.print("secs = ");
        Serial.println(lastNTP.seconds);
    }

    return ntpValid;
}