void radioTransceiverTask(const void *arg) { unsigned long tv; static unsigned long radio_last_tv2 = 0; while (true){ #if 1 #if 0 //debug tv=millis(); Serial.println(tv-radio_last_tv2); radio_last_tv2=tv; #endif memset(transceiverBuffer, '\0', sizeof(transceiverBuffer)); sprintf(transceiverBuffer, "@%3.2f:%3.2f:%3.2f:%3.2f:%3.2f:%3.2f:%3.2f:%3.2f:%3.2f:%d:%d:%d:%d:%d#", getRoll(), getPitch(), getYaw(), getPidSp(&rollAttitudePidSettings), getPidSp(&pitchAttitudePidSettings), getYawCenterPoint() + getPidSp(&yawAttitudePidSettings), getRollGyro(), getPitchGyro(), getYawGyro(), getThrottlePowerLevel(), getMotorPowerLevelCCW1(), getMotorPowerLevelCW1(), getMotorPowerLevelCCW2(), getMotorPowerLevelCW2()); Udp.beginPacket(broadcastIP, localPort); Udp.write(transceiverBuffer,strlen(transceiverBuffer)); Udp.endPacket(); increasePacketAccCounter(); os_thread_yield(); delay(TRANSMIT_TIMER); #endif } }
void WiFi_forward_to_xcsoar() { int bearing = CalcBearing(fo.latitude, fo.longtitude, LATITUDE, LONGTITUDE); int alt_diff = fo.altitude - ALTITUDE; char *csum_ptr; unsigned char cs = 0; //clear any old checksum Udp.beginPacket(ARGUS_HOSTNAME, XCSOAR_PORT); snprintf(UDPpacketBuffer, sizeof(UDPpacketBuffer), "$PFLAU,0,1,1,1,%d,2,%d,%u,%X*", \ bearing, alt_diff, (int) fo.distance, fo.addr ); //calculate the checksum for (unsigned int n = 1; n < strlen(UDPpacketBuffer) - 1; n++) { cs ^= UDPpacketBuffer[n]; //calculates the checksum } csum_ptr = UDPpacketBuffer + strlen(UDPpacketBuffer); snprintf(csum_ptr, sizeof(UDPpacketBuffer) - strlen(UDPpacketBuffer), "%02X\n", cs); #ifdef SERIAL_VERBOSE Serial.println(UDPpacketBuffer); #endif Udp.write(UDPpacketBuffer, strlen(UDPpacketBuffer)); Udp.endPacket(); Udp.beginPacket(XCSOAR_HOSTNAME, XCSOAR_PORT); snprintf(UDPpacketBuffer, sizeof(UDPpacketBuffer), "$PFLAA,2,%d,%d,%d,2,%X,,,,,1*", \ (int) (fo.distance * cos(dtor(bearing))), (int) (fo.distance * sin(dtor(bearing))), \ alt_diff, fo.addr ); cs = 0; //clear any old checksum for (unsigned int n = 1; n < strlen(UDPpacketBuffer) - 1; n++) { cs ^= UDPpacketBuffer[n]; //calculates the checksum } csum_ptr = UDPpacketBuffer + strlen(UDPpacketBuffer); snprintf(csum_ptr, sizeof(UDPpacketBuffer) - strlen(UDPpacketBuffer), "%02X\n", cs); #ifdef SERIAL_VERBOSE Serial.println(UDPpacketBuffer); #endif Udp.write(UDPpacketBuffer, strlen(UDPpacketBuffer)); Udp.endPacket(); }
void Protocol::send(const char* message) { #ifdef MODULE_CAN_DEBUG Serial.print("Send message: "); Serial.println(message); #endif Udp.beginPacket(_ip, _port); Udp.write(message); Udp.endPacket(); }
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; }
// Send status packet int send_packet(char *packet, int size, IPAddress exclude) { int i; if (my_node_type == MSGMULTI_MASTER) { for (i = 0; i < MSGMULTI_MAXCLIENTS; i++) { if ((clients[i].expire > 0) && (clients[i].client != exclude)) { #ifdef DEBUG Serial.print("Sending packet to "); Serial.println(clients[i].client); #endif clients[i].expire--; udp.beginPacket(clients[i].client, STATE_UDP_PORT); udp.write(packet, size); return udp.endPacket(); } } } else { udp.beginPacket(WiFi.gatewayIP(), STATE_UDP_PORT); udp.write(packet, size); return udp.endPacket(); } } // void send_packet(IPaddress dest, char *packet, int size)
void CAIPSendData(CAEndpoint_t *endpoint, const void *data, uint32_t dataLength, bool isMulticast) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL_VOID(data, TAG, "data"); VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint"); OIC_LOG_V(DEBUG, TAG, "remoteip: %s", endpoint->addr); OIC_LOG_V(DEBUG, TAG, "port: %d", endpoint->port); uint8_t ip[4] = {0}; uint16_t parsedPort = 0; CAResult_t res = CAParseIPv4AddressInternal(endpoint->addr, ip, sizeof(ip), &parsedPort); if (res != CA_STATUS_OK) { OIC_LOG_V(ERROR, TAG, "Remote adrs parse fail %d", res); return; } IPAddress remoteIp(ip); Udp.beginPacket(remoteIp, endpoint->port); uint32_t bytesWritten = 0; while (bytesWritten < dataLength) { // get remaining bytes size_t writeCount = dataLength - bytesWritten; // write upto max ARDUINO_WIFI_BUFFERSIZE bytes writeCount = Udp.write((uint8_t *)data + bytesWritten, (writeCount > ARDUINO_IP_BUFFERSIZE ? ARDUINO_IP_BUFFERSIZE:writeCount)); if(writeCount == 0) { // write failed OIC_LOG_V(ERROR, TAG, "Failed after %u", bytesWritten); break; } bytesWritten += writeCount; } if (Udp.endPacket() == 0) { OIC_LOG(ERROR, TAG, "Failed to send"); return; } OIC_LOG(DEBUG, TAG, "OUT"); return; }
AJ_Status AJ_Net_SendTo(AJ_IOBuffer* buf) { int ret; uint32_t tx = AJ_IO_BUF_AVAIL(buf); AJ_InfoPrintf(("AJ_Net_SendTo(buf=0x%p)\n", buf)); if (tx > 0) { // send to subnet-directed broadcast address #ifdef WIFI_UDP_WORKING IPAddress subnet = WiFi.subnetMask(); IPAddress localIp = WiFi.localIP(); #else IPAddress subnet = Ethernet.subnetMask(); IPAddress localIp = Ethernet.localIP(); #endif uint32_t directedBcastAddr = (uint32_t(subnet) & uint32_t(localIp)) | (~uint32_t(subnet)); IPAddress a(directedBcastAddr); ret = g_clientUDP.beginPacket(IPAddress(directedBcastAddr), AJ_UDP_PORT); AJ_InfoPrintf(("AJ_Net_SendTo(): beginPacket to %d.%d.%d.%d, result = %d\n", a[0], a[1], a[2], a[3], ret)); if (ret == 0) { AJ_InfoPrintf(("AJ_Net_SendTo(): no sender\n")); } ret = g_clientUDP.write(buf->readPtr, tx); AJ_InfoPrintf(("AJ_Net_SendTo(): SendTo write %d\n", ret)); if (ret == 0) { AJ_ErrPrintf(("AJ_Net_Sendto(): no bytes. status=AJ_ERR_WRITE\n")); return AJ_ERR_WRITE; } buf->readPtr += ret; ret = g_clientUDP.endPacket(); if (ret == 0) { AJ_ErrPrintf(("AJ_Net_Sendto(): endPacket() error. status=AJ_ERR_WRITE\n")); return AJ_ERR_WRITE; } } AJ_IO_BUF_RESET(buf); AJ_InfoPrintf(("AJ_Net_SendTo(): status=AJ_OK\n")); return AJ_OK; }
// send an NTP request to the time server at the given address void sendNTPpacket(IPAddress &address) { // 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(address, 123); // NTP requests are to port 123 Udp.write(packetBuffer, NTP_PACKET_SIZE); Udp.endPacket(); }
void WiFi_forward_to_argus() { char str_lat[16]; char str_lon[16]; dtostrf(fo.latitude, 8, 4, str_lat); dtostrf(fo.longtitude, 8, 4, str_lon); Udp.beginPacket(ARGUS_HOSTNAME, ARGUS_PORT); snprintf(UDPpacketBuffer, sizeof(UDPpacketBuffer), "%u %X %s %s %d %s %s %u", \ fo.timestamp * 1000, fo.addr, str_lat, str_lon, fo.altitude, "0" , "0" , TypeADSB ); #ifdef SERIAL_VERBOSE Serial.println(UDPpacketBuffer); #endif Udp.write(UDPpacketBuffer, strlen(UDPpacketBuffer)); Udp.endPacket(); }
//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 }