STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1/**
2 * Multi-Board Comms Testing - SECONDARY
3 *
4 * Listens on port 5006 for SENSOR_DATA (or any) packets from the primary.
5 * On receive: sends a reply SENSOR_DATA packet back to the primary at
6 * remoteIP():5005.
7 *
8 * Uses: Arduino Ethernet, EthernetUDP, DAQv2-Comms (sensor packet format).
9 */
10
11#include <Arduino.h>
12#include <Ethernet.h>
13#include <EthernetUdp.h>
14#include <SPI.h>
15#include <daq-protocol.h>
16#include <esp_mac.h>
17
18#include <cstring>
19#include <vector>
20
21#include "sense_board_pins.h"
22
23// ---------------------------------------------------------------------------
24// Network: secondary = 192.168.2.101, listen 5006; reply to primary at
25// primaryIP:5005
26// ---------------------------------------------------------------------------
27byte mac[6];
28IPAddress staticIP(192, 168, 2, 101);
29IPAddress gateway(192, 168, 2, 1);
30IPAddress subnet(255, 255, 255, 0);
31IPAddress dns(192, 168, 2, 1);
32
33const int localPort = 5006;
34const int primaryListenPort = 5005;
35
36EthernetUDP udp;
39
40// ---------------------------------------------------------------------------
41// Build and send reply SENSOR_DATA to primary (remoteIP:5005)
42// ---------------------------------------------------------------------------
43void sendReplyTo(IPAddress primaryIP) {
44 unsigned long ts = millis();
45 const uint8_t numSensors = 1;
47 float ack = 42.0f; // simple "ACK" value
48 uint32_t ackBits;
49 memcpy(&ackBits, &ack, sizeof(float));
50 chunk.add_datapoint(0, ackBits);
51
52 std::vector<daq::SensorDataChunkCollection> chunks;
53 chunks.push_back(chunk);
54
55 size_t n = daq::create_sensor_data_packet(chunks, numSensors, millis(),
56 replyBuffer, sizeof(replyBuffer));
57 if (n == 0)
58 return;
59
60 udp.beginPacket(primaryIP, primaryListenPort);
61 udp.write(replyBuffer, n);
62 udp.endPacket();
63
64 Serial.print("REPLY: [SENSOR_DATA] ");
65 Serial.print(n);
66 Serial.print(" bytes -> ");
67 Serial.print(primaryIP);
68 Serial.print(":");
70}
71
72// ---------------------------------------------------------------------------
73// Setup & loop
74// ---------------------------------------------------------------------------
75void setup() {
76 Serial.begin(115200);
77 while (!Serial)
78 delay(10);
79
80 Serial.println("Multi-Board Comms Testing – SECONDARY");
81 Serial.println(" Listens on :5006 for packets from primary");
82 Serial.println(" Replies with SENSOR_DATA -> <primaryIP>:5005");
83 Serial.println("----------------------------------------");
84
85 ESP_ERROR_CHECK(esp_read_mac(mac, ESP_MAC_ETH));
86 Serial.print("MAC: ");
87 for (int i = 0; i < 6; i++) {
88 if (i)
89 Serial.print(":");
90 if (mac[i] < 0x10)
91 Serial.print("0");
92 Serial.print(mac[i], HEX);
93 }
94 Serial.println();
95
100 delay(500);
102 delay(500);
104 delay(500);
105
106 udp.begin(localPort);
107
108 Serial.print("IP: ");
109 Serial.println(Ethernet.localIP());
110 Serial.print("Link: ");
111 Serial.println(Ethernet.linkStatus() == LinkON ? "ON" : "OFF");
112 Serial.println();
113}
114
115void loop() {
116 int n = udp.parsePacket();
117 if (n > 0) {
118 int len = udp.read(packetBuffer, sizeof(packetBuffer));
119 if (len > 0) {
120 IPAddress from = udp.remoteIP();
121 Serial.print("RECV: ");
122 Serial.print(len);
123 Serial.print(" bytes from ");
124 Serial.print(from);
125 Serial.print(":");
126 Serial.println(udp.remotePort());
127
128 sendReplyTo(from);
129 }
130 }
131
132 delay(10);
133}
static SerialStub Serial
Definition Arduino.h:128
unsigned long millis()
Definition Arduino.h:29
#define HEX
Definition Arduino.h:24
static EthernetClass Ethernet
Definition Ethernet.h:116
#define MAX_PACKET_SIZE
Definition main.h:6
void delay(int ms)
static uint8_t chunk[64]
Definition board_uart9.c:12
void setup()
Definition main.cpp:142
void loop()
Definition main.cpp:201
std::vector< daq::SensorDataChunkCollection > chunks
Definition main.cpp:55
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)
static constexpr int ETH_CS
Ball Valve Hotfire — motorized ball valve prototype board.
Definition main.cpp:53
static constexpr int ETH_SCLK
Definition main.cpp:55
static constexpr int ETH_MISO
Definition main.cpp:56
static constexpr int ETH_MOSI
Definition main.cpp:54
uint8_t replyBuffer[MAX_PACKET_SIZE]
Definition main.cpp:38
const int primaryListenPort
Definition main.cpp:34
void sendReplyTo(IPAddress primaryIP)
Definition main.cpp:43
size_t create_sensor_data_packet(const std::vector< SensorDataChunkCollection > &chunks, const uint8_t num_sensors, uint32_t timestamp_ms, uint8_t *buffer, size_t buffer_size)
Creates a complete Sensor Data packet in the provided buffer.
*brief user defined string literal for JSON values *sa std::size_t n
Definition json.hpp:24474
constexpr Layout PT_Board
Represents a single data chunk with timestamp and sensor datapoints.
static const uint8_t ack[]