2024-11-20 17:35:59 +03:00

123 lines
3.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "EthernetMaket.h" // Предполагается, что этот файл подключает Ethernet3.h
// Настройки для W5500
#define W5500_CS_PIN PA2 // Пин для CS
#define SOCKET_NUMBER 0 // Используем сокет 0
#define SERVER_PORT 1337 // Порт на сервере
#define CLIENT_PORT 1337 // Порт клиента
#define ServerMac {0xDA, 0x7A, 0xFF, 0x00, 0x00, 0x00}
#define ServerIP {192, 168, 254, 254}
#define IniMac {0xDA, 0x7A, 0xF0, 0x00, 0x00, 0x00}
#define IniIP {192, 168, 254, 253}
uint8_t gateway[] = {192, 168, 0, 1}; // Шлюз
uint8_t subnet[] = {255, 255, 0, 0}; // Маска подсети
uint8_t mac[] = IniMac; // MAC-адрес
uint8_t localIP[] = IniIP; // Локальный IP-адрес
uint8_t serverMac[] = ServerMac; // MAC-адрес сервера
uint8_t serverIP[] = ServerIP; // IP-адрес сервера
uint8_t buf[512]; // Буфер для передачи данных
uint32_t previousMillis = 0; // Для хранения времени последней отправки
const long interval = 3750; // Интервал отправки данных (1.75 секунды)
uint32_t ttt;
EthernetServer server(SERVER_PORT);
EthernetMaketClient client;
uint32_t packetCounter = 0;
void onSend(){
Serial.println("Sending Successfully");
}
#define TEST_1
// #define TEST_2
void setup()
{
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
Ethernet.setCsPin(W5500_CS_PIN);
Ethernet.init();
// Настройка Ethernet с указанием статических параметров
Ethernet.begin(mac, localIP, subnet, gateway, gateway);
// Ethernet.setRtTimeOut(1000 * 10);
// Ethernet.setRtCount(3);
// client.setNoDelayedACK(true); // Не ждать ответа от сервера
client.setOnSendSuccess(onSend);
}
#ifdef TEST_2
//* for many packets
bool dataReady = false;
#endif
void loop()
{
EthernetMaketClient::tick();
uint32_t currentMillis = millis();
if (currentMillis - ttt > 25)
{
digitalToggle(PC13);
ttt = currentMillis; // Обновляем время
}
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
Serial.println("connectNonBlock");
client.startConnection(serverIP, SERVER_PORT);
#ifdef TEST_1
//* for single packets
client.dataWrite((uint8_t *)&packetCounter, sizeof(packetCounter));
Serial.print("Sending packet: ");
for (size_t j = 0; j < sizeof(packetCounter); j++)
{
Serial.print(((uint8_t *)&packetCounter)[j], HEX);
Serial.print(" ");
}
Serial.println();
packetCounter++;
#endif
#ifdef TEST_2
//* for many packets
dataReady = true;
#endif
}
#ifdef TEST_2
//* for many packets
if(dataReady && client.isConnected()) //* check connection
{
for (size_t i = 0; i < 3; i++)
{
client.dataWrite((uint8_t *)&packetCounter, sizeof(packetCounter));
// Print the packet being sent
Serial.print("Sending packet: ");
for (size_t j = 0; j < sizeof(packetCounter); j++)
{
Serial.print(((uint8_t *)&packetCounter)[j], HEX);
Serial.print(" ");
}
Serial.println();
packetCounter++;
}
dataReady = false;
}
#endif
}