#include <WiFi.h>
#include <WiFiUdp.h>
const int LOCAL_PORT = 8888;
//const int NTP_PACKET_SIZE = 48;
//byte packetBuffer[NTP_PACKET_SIZE];
WiFiUDP UDP;
uint8_t dynData[47];
int count = 0;
int direction = 1;
//IPAddress ip;
/*
run the UDP server on PeteyPi and pipe into TCP port 5001 on fluffy
knelf@RedRat:~ $ nc -lu 192.168.0.121 5002 | nc fluffy.spacetechnology.net 5001
can't seem to get UDP forwarding to fluffy despite adding it into fanqiang.sh
maybe SSH needs special instructions for UDP forwarding?
Found this... seems you have to convert UDP to TCP to fit it through the tunnel
and then convert back again at the other end - if need be
"but had to change the UDP packets into TCP packets and then forward them
into the ssh tunnel and then convert them from TCP packets back into UDP
packets to the listener UDP port of the application."
Also seems to be mega slow... like 5fps?
Doesn't seem to be an issue for me to stream UDP data at PeteyPi from Fluffy
so think speed issue may be at Wokwi end. Could potentially try TCP in case
any better...
*/
int initInternet()
{
Serial.print("\nConnecting to Wifi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED)
{
delay(250);
Serial.print(".");
}
Serial.println(" Connected\n");
UDP.begin(LOCAL_PORT);
return 1;
}
void sendPacketNTP(const char* 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;
*/
/*
packetBuffer[0] = 'H'; // LI, Version, Mode
packetBuffer[1] = 'e'; // Stratum, or type of clock
packetBuffer[2] = 'l'; // Polling Interval
packetBuffer[3] = 'l';
packetBuffer[4] = 'o';
packetBuffer[5] = '\n';
*/
/*
uint8_t size = 24; // 23 for no newline
if (count > 9)
size += 11;
if (count > 99)
size += 11;
//const char* data = "[0,1,2,3,4,180,6,7,8,10,11]\n";
sprintf(dynData, "[%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d]\n", count,
count, count, count, count, count,
count, count, count, count, count);
*/
// array construction
for (int i = 1; i < 12; i++)
{
dynData[i] = count;
}
// data adjustment
count += direction;
if (count == 180 || count == 0)
direction *= -1;
// uncomment for debugging
//Serial.print(dynData);
// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
UDP.beginPacket(address, 5002); // NTP requests are to port 123
UDP.write(dynData, 13);
UDP.endPacket();
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
initInternet();
// set start and stop bytes
dynData[0] = 200; // start byte
dynData[12] = 255; // stop byte
}
void loop()
{
// put your main code here, to run repeatedly:
//Serial.println("Entering Loop");
// *** IMPORTANT - ENSURE IP IS CORRECT/CURRENT ***
//sendPacketNTP("fluffy.spacetechnology.net");
sendPacketNTP("95.146.56.31");
delay(38); // this speeds up the simulation
}