void AgentuinoClass::listen(void) { // if bytes are available in receive buffer // and pointer to a function (delegate function) // isn't null, trigger the function Udp.parsePacket(); if (Udp.available() && _callback != NULL) (*_callback)(); }
void uartToUdp() { bool sendIt = false; if (Serial.available() > 0) { uartBuffer[uartCounter] = Serial.read(); if (uartBuffer[uartCounter] == HDLC_SS_BYTE) { if (hdlcStart)sendIt = true; else { hdlcStart = true; Timer1.start(); } } else if (hdlcStart) { ++uartCounter; } } if (sendIt) { Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); Udp.write(uartBuffer, uartCounter); Udp.endPacket(); resetUartBuffer(); } }
void WakeOnLan::send(byte* mac, byte port, EthernetUDP udp) { byte preamble[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; byte i; udp.beginPacket(_ip, port); udp.write(preamble, sizeof preamble); for (i = 0; i < 16; i++) udp.write(mac, sizeof mac); udp.endPacket(); #else // ARDUINO not defined or ARDUINO < 100 void WakeOnLan::send(byte* mac, byte port) { byte magic_packet[102] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; for (byte i = 6; i < 102; i++) { magic_packet[i] = mac[i%6]; } Udp.sendPacket(magic_packet, 102, _ip, port); #endif // #if defined(ARDUINO) && ARDUINO >= 100 }
SNMP_API_STAT_CODES SNMPClass::begin(const char *getCommName, const char *setCommName, const char *trapCommName, uint16_t port) { //initialize request counter requestCounter = 1; _extra_data_size = 0; _udp_extra_data_packet = false; // set community name set/get sizes _setSize = strlen(setCommName); _getSize = strlen(getCommName); _trapSize = strlen(trapCommName); // // validate get/set community name sizes if ( _setSize > SNMP_MAX_NAME_LEN + 1 || _getSize > SNMP_MAX_NAME_LEN + 1 || _trapSize > SNMP_MAX_NAME_LEN + 1) { return SNMP_API_STAT_NAME_TOO_BIG; } // // set community names _getCommName = getCommName; _setCommName = setCommName; _trapCommName = trapCommName; // validate session port number if ( port == NULL || port == 0 ) port = SNMP_DEFAULT_PORT; // // init UDP socket Udp.stop(); Udp.begin(port); return SNMP_API_STAT_SUCCESS; }
void sendNTPpacket(const byte *address) /** * send an NTP request to the time server at the given address */ { // set all bytes in the buffer to 0 memset(pb, 0, NTP_PACKET_SIZE); // Initialize values needed to form NTP request // (see URL above for details on the packets) pb[0] = 0b11100011; // LI, Version, Mode pb[1] = 0; // Stratum, or type of clock pb[2] = 6; // Polling Interval pb[3] = 0xEC; // Peer Clock Precision // 8 bytes of zero for Root Delay & Root Dispersion pb[12] = 49; pb[13] = 0x4E; pb[14] = 49; pb[15] = 52; // all NTP fields have been given values, now // you can send a packet requesting a timestamp: Udp.beginPacket(address, 123); //NTP requests are to port 123 Udp.write(pb,NTP_PACKET_SIZE); Udp.endPacket(); }
time_t getNtpTime() { while (Udp.parsePacket() > 0) ; // discard any previously received packets Serial.println("Transmit NTP Request"); sendNTPpacket(ntp_server); uint32_t beginWait = millis(); while (millis() - beginWait < 2000) { int size = Udp.parsePacket(); if (size >= NTP_PACKET_SIZE) { Serial.println("Receive NTP Response"); Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer unsigned long secsSince1900; unsigned long fracSecs; // 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]; fracSecs = (unsigned long)packetBuffer[44] << 24; fracSecs |= (unsigned long)packetBuffer[45] << 16; fracSecs |= (unsigned long)packetBuffer[46] << 8; fracSecs |= (unsigned long)packetBuffer[47]; return ((time_t)(secsSince1900 - 2208988800UL)<<32) + fracSecs; } } Serial.println("No NTP Response :-("); return 0; // return 0 if unable to get the time }
int readUdp( char * buf, int size ) { if ( ! active ) return 0 ; int count = Udp.parsePacket() ; if ( count ) Udp.read( buf, size ) ; return count ; }
roveEthernet_Error roveEthernet_SendUdpPacket(roveIP destIP, uint16_t destPort, const uint8_t* msg, size_t msgSize) { udpReceiver.beginPacket(destIP, destPort); udpReceiver.write(msg, msgSize); udpReceiver.endPacket(); return ROVE_ETHERNET_ERROR_SUCCESS; }
// --------------------------------------------------- Ethernet Send Data ----------------------------------------------------------- void UDP_Send_Data() { Udp.beginPacket(ip_rpi, rpi_port); Udp.write(nav.ch, sizeof(nav.data)); Udp.write(cam.ch, sizeof(cam.pose)); Udp.write(imu.ch, sizeof(imu.imu_data)); Udp.endPacket(); }
int sendUdp( char * data, int size ) { if ( ! active ) return 0 ; Udp.beginPacket(*timeServerAddress, serverPort); byte count = Udp.write((const unsigned char *)data,size); Udp.endPacket(); return (int) count ; }
// This function's purpose is to receive data and prepare it for parsing void RobotOpenClass::handleData() { _packetBufferSize = Udp.parsePacket(); // If there's data, read the packet in if (_packetBufferSize > 0) { _remotePort = Udp.remotePort(); _remoteIp = Udp.remoteIP(); Udp.read(_packetBufferAccessor, 256); parsePacket(); // Data is all set, time to parse through it } }
void SNMPClass::writePacket(IPAddress address, uint16_t port, char *extra_data) { Udp.beginPacket(address, port); Udp.write(_packet+_packetPos+1, _packetSize); if(extra_data != NULL){ Udp.write((byte*)extra_data, _extra_data_size); } Udp.endPacket(); }
/* * Restart our ntp handler. This is only done as part of restarting the * network. As such, all we want to do is make sure udp is still working. * We dont need to issue a time request unless one was already outstanding * (not likely) */ void restartNtpTime() { Udp.stop(); Udp.begin(8888); // If we were already waiting for a response, let's re-issue the request if (ntpState == WAITING_FOR_RESPONSE) { sendTimeRequest(); } }
void udpToUart() { int packetSize = Udp.parsePacket(); if(packetSize) { // read the packet into packetBufffer Udp.read(udpBuffer,MAX_HDLC_FRAME_SIZE); Serial.write(HDLC_SS_BYTE); Serial.write(udpBuffer, packetSize); Serial.write(0x12); Serial.write(0x34); Serial.write(HDLC_SS_BYTE); } }
bool gatewayTransportInit() { _w5100_spi_en(true); #if defined(MY_GATEWAY_ESP8266) #if defined(MY_ESP8266_SSID) // Turn off access point WiFi.mode (WIFI_STA); #if defined(MY_ESP8266_HOSTNAME) WiFi.hostname(MY_ESP8266_HOSTNAME); #endif (void)WiFi.begin(MY_ESP8266_SSID, MY_ESP8266_PASSWORD); #ifdef MY_IP_ADDRESS WiFi.config(_ethernetGatewayIP, _gatewayIp, _subnetIp); #endif while (WiFi.status() != WL_CONNECTED) { delay(500); MY_SERIALDEVICE.print("."); yield(); } MY_SERIALDEVICE.print(F("IP: ")); MY_SERIALDEVICE.println(WiFi.localIP()); #endif #else #ifdef MY_IP_ADDRESS Ethernet.begin(_ethernetGatewayMAC, _ethernetGatewayIP); #else // Get IP address from DHCP if (!Ethernet.begin(_ethernetGatewayMAC)) { MY_SERIALDEVICE.print("DHCP FAILURE..."); _w5100_spi_en(false); return false; } #endif MY_SERIALDEVICE.print(F("IP: ")); MY_SERIALDEVICE.println(Ethernet.localIP()); // give the Ethernet interface a second to initialize delay(1000); #endif #ifdef MY_USE_UDP _ethernetServer.begin(_ethernetGatewayPort); #else // we have to use pointers due to the constructor of EthernetServer _ethernetServer.begin(); #endif /* USE_UDP */ _w5100_spi_en(false); return true; }
roveEthernet_Error roveEthernet_GetUdpMsg(roveIP* senderIP, void* buffer, size_t bufferSize) { int packetSize = udpReceiver.parsePacket(); if (packetSize > 0) //if there is a packet available { udpReceiver.read((char*)buffer, bufferSize); *senderIP = udpReceiver.remoteIP(); return ROVE_ETHERNET_ERROR_SUCCESS; } else { return ROVE_ETHERNET_ERROR_WOULD_BLOCK; } }
void SyncTime_setup() { Udp.begin(LOCALPORT); setSyncProvider(getNtpTime); while(timeStatus()== timeNotSet) ; // wait until the time is set by the sync provider }
/* * Initialize our ntp client. * * We need ntp time in order to have a valid, up-to-date time * in order to create timestamps for the data we collect. If we dont have * valid time, then there's no use returning since we cant collect data * without valid time. * * Note, however, that now() will still keep track of time - it will just * be relative to when we booted. */ void initNtpTime() { Udp.begin(8888); setSyncProvider(0); // We will take care of synching time ourselves, // so tell time library there's no external time source sendTimeRequest(); // send an initial time request to kickstart things // Wait until our request is completed. Note that this is initialization, // so if we dont have a good time value, we cant collect data because // we wont be able to create valid timestamps. int retryCount = 500; while (ntpState == WAITING_FOR_RESPONSE) { delay(500); serviceNtpTime(); if (--retryCount <=0) { // We've been trying for a while to get the time. Let's try // restarting in case the network didnt come up right last time. retryCount = 500; startEthernet(); // Let's try restarting the ethernet restartNtpTime(); } } if (bootTime == 0) { bootTime = now(); } }
void setup_ntp(){ // setup NTP sync service Udp.begin(ntp_port); Serial.println(F("Waiting for NTP sync")); setSyncInterval(600); // initial one 10 minutes to take care of most of the drift setSyncProvider(getNtpTime); }
/* * Perform housekeeping needed to keep ntp time up to date. * * This essentially involves occasionally re-syncing our * time with a time server so our internal clock doesnt * drift too far. * */ void serviceNtpTime() { switch (ntpState) { case NTP_IN_SYNC : // Check to see if we need to re-sync if ((ntpLastSyncTime + SYNC_INTERVAL) < now()) { sendTimeRequest(); } break; case WAITING_FOR_RESPONSE : // See if we've received a response to a sync request yet if ( Udp.parsePacket() ) { ntpLastSyncTime = parseNtpPacket(); setTime(ntpLastSyncTime); ntpState = NTP_IN_SYNC; Serial.print("Updated time: "); Serial.println(now()); } else if ((lastNtpRequestTime + MAX_WAIT_INTERVAL) < now()) { // We've been waiting a while now and we're probably not going // to get a response, so let's try the next server in the list Serial.println("No ntp response"); sendTimeRequest(); } break; } }
unsigned long getNtpTime() { // We need to call the begin to reset the socket. // Because localClient.connect() may occupy the same socket. EthernetUDP theUDP; theUDP.begin(localPort); Serial.println("Sending time sync request to NTP server."); sendNTPpacket(timeServer); // send an NTP packet to a time server unsigned long startMillis = millis(); int tryCounter = 1; while( millis() - startMillis < 1000) // wait up to one second for the response { // wait to see if a reply is available if ( theUDP.available() ) { theUDP.readPacket(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 Arduino Time format: // Time starts on Jan 1 1970. In seconds, that's 2208988800: const unsigned long seventyYears = 2208988800UL; // subtract seventy years: unsigned long epoch = secsSince1900 - seventyYears; Serial.println("Time sync successfully."); return epoch; } Serial.print("No date data available. Try counter: ."); Serial.println(tryCounter++); delay(100); } Serial.println("Time sync failed."); return 0; // return 0 if unable to get the time }
void setup() { // start the Ethernet and UDP: Ethernet.begin(mac,ip); Udp.begin(localPort); pinMode(13,OUTPUT); pinMode(12,OUTPUT); pinMode(11,OUTPUT); pinMode(10,OUTPUT); }
void SyslogClass::logger(uint8_t facility, uint8_t severity, const char tag[], const char message[]) { String Pri; Pri="<"; Pri+=(8 * facility + severity); Pri+=">"; char UDPBufferPri[Pri.length()+1]; Pri.toCharArray(UDPBufferPri,Pri.length()+1); SyslogUdp.beginPacket(ip_syslogserver, SYSLOG_DEFAULT_PORT); SyslogUdp.write(UDPBufferPri); SyslogUdp.write(tag); SyslogUdp.write(" "); SyslogUdp.write(message); SyslogUdp.endPacket(); }
void setup() { Serial.begin(9600); // start the Ethernet and UDP: Ethernet.begin(mac,ip); Udp.begin(localPort); Timer1.initialize(TIMER_PERIOD); Timer1.attachInterrupt(TimerLoop); Timer1.stop(); }
void UdpController::Loop() { unsigned long currentMillis = millis(); int packetSize = Udp.parsePacket(); if (packetSize && packetSize == 3) { Udp.read(Buffer, UDP_TX_PACKET_MAX_SIZE); if (Buffer[0] == 0x28 && Buffer[1] == 0x06) { _traffic->SetMode((LightMode)Buffer[2]); _lastUpdateMillis = currentMillis; } } if ((currentMillis - _lastUpdateMillis) > UpdateIntervalMillis) { _traffic->ShowInconclusive(); } }
bool SetupUdpClient ( void ) { bool success = false; #ifdef INTERFACE_ETHERNET #ifdef ETHERNET_UDPCLIENT success = (bool)Udp.begin(localPort); #endif //ETHERNET_UDPCLIENT #endif // INTERFACE_ETHERNET return success; }
boolean SNMPClass::listen(void) { // if bytes are available in receive buffer // and pointer to a function (delegate function) // isn't null, trigger the function if(Udp.parsePacket() > 1024){ _udp_extra_data_packet = true; }else{ _udp_extra_data_packet = false; } if (Udp.available()){ if(_callback != NULL){ (*_callback)(); }else{ return true; } } return false; }
void UdpController::Setup() { if (Ethernet.begin(MacAddress) == 0) { _traffic->ShowError(); for(;;); } Udp.begin(Port); _traffic->ShowInconclusive(); _lastUpdateMillis = millis(); }
unsigned long getNtpTime() { sendNTPpacket(timeServer); // send an NTP packet to a time server // wait to see if a reply is available delay(1000); if ( Udp.parsePacket() ) { // 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; #ifdef LOGGING Serial.print("Seconds since Jan 1 1900 = " ); Serial.println(secsSince1900); #endif // 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: unsigned long epoch = secsSince1900 - seventyYears; // print Unix time: #ifdef LOGGING Serial.print(UNIX_TIME); Serial.println(epoch); #endif return epoch; } return 0; // return 0 if unable to get the time }
/* * Send an NTP request to get the current time. * * This code was lifted from sample code that was part of an * arduino ntp libary. */ time_t parseNtpPacket() { Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); unsigned long ntpTime = highWord << 16 | lowWord; return (ntpTime - 2208988800UL); // NTP time is based on 01-Jan-1900, but // we want Unix time which is seconds // since 01-Jan-1970 }