STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include <Arduino.h>
2#include <Ethernet.h>
3#include <EthernetUdp.h>
4#include <SPI.h>
5
6// Network configuration for the RECEIVER PCB
7byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE}; // Unique MAC address
8IPAddress staticIP(192, 168, 2, 101); // Unique IP for the receiver
9IPAddress gateway(192, 168, 2, 1);
10IPAddress subnet(255, 255, 255, 0);
11IPAddress dns(192, 168, 2, 1);
12
13unsigned int localPort =
14 5006; // Port to listen on (must match sender's receiverPort)
15char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // Buffer to hold incoming packets
16
17EthernetUDP udp;
18
19// Define the Chip Select (CS) pin as a constant variable
20const int ETH_CS_PIN = 6;
21
22void setup() {
23 Serial.begin(115200);
24 while (!Serial)
25 ; // Wait for Serial Monitor to open
26
27 Serial.println("Minimal UDP Receiver");
28
29 // Initialize Ethernet using the variable
30 Ethernet.init(ETH_CS_PIN);
31 delay(1000);
32
33 // Start the Ethernet connection and the UDP library
35 udp.begin(localPort);
36
37 Serial.print("Receiver IP: ");
38 Serial.println(Ethernet.localIP());
39 Serial.print("Listening on port ");
40 Serial.println(localPort);
41}
42
43void loop() {
44 // Check for incoming UDP packets
45 int packetSize = udp.parsePacket();
46 if (packetSize) {
47 Serial.print("Received packet of size ");
48 Serial.println(packetSize);
49 Serial.print("From ");
50 IPAddress remoteIp = udp.remoteIP();
51 Serial.print(remoteIp);
52 Serial.print(", port ");
53 Serial.println(udp.remotePort());
54
55 // Read the packet into the buffer
56 int len = udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
57 if (len > 0) {
58 packetBuffer[len] = '\0'; // Null-terminate the string
59 }
60
61 Serial.println("Contents:");
62 Serial.println(packetBuffer);
63 }
64}
static SerialStub Serial
Definition Arduino.h:128
static EthernetClass Ethernet
Definition Ethernet.h:116
void delay(int ms)
void setup()
Definition main.cpp:142
void loop()
Definition main.cpp:201
IPAddress dns(192, 168, 2, 1)
byte mac[6]
Definition main.cpp:31
IPAddress subnet(255, 255, 255, 0)
IPAddress staticIP(192, 168, 2, BOARD_ID_DEFAULT)
EthernetUDP udp
Definition main.cpp:40
IPAddress gateway(0, 0, 0, 0)
static IPAddress subnet(255, 255, 255, 0)
static IPAddress gateway(192, 168, 2, 1)
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]
Definition main.cpp:15
IPAddress dns(192, 168, 2, 1)
unsigned int localPort
Definition main.cpp:13
IPAddress staticIP(192, 168, 2, 101)
#define ETH_CS_PIN
Definition main.cpp:11