the Espressif ESP32 family is supported by the Arduino IDE
e.g. a BME280 connected to a Waveshare ESP32-S3 ETH Dev board (referenced in post 4) transmitting results over Ethernet using UDP datagrams
// UDP chat program Waveshare ESP32-S3 ETH Development Board (W5500)
// **** change IP addresses and ports to suit requirements *****
#include <EthernetESP32.h>
#include <EthernetUdp.h> // for UDP
// Define Waveshare ESP32-S3-ETHW5500 pin assignments
#define W5500_CS 14 // Chip Select pin
#define W5500_RST 9 // Reset pin
#define W5500_INT 10 // Interrupt pin
#define W5500_MISO 12 // MISO pin
#define W5500_MOSI 11 // MOSI pin
#define W5500_SCK 13 // Clock pin
// W5500Driver(int8_t cs = SS, int8_t irq = -1, int8_t rst = -1)
W5500Driver driver(W5500_CS, W5500_INT, W5500_RST);
// ***** IP of this machine and remote machine *********
IPAddress localIP(192, 168, 1, 174); // local static localIP address
IPAddress remoteIP(192, 168, 1, 65); // local static localIP address
unsigned int port = 10000; // port to receive/transmit
// Enter a MAC address
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAA };
unsigned int localPort = 10000; // local port to listen on
unsigned int remotePort = 10000; // remote port to transmiit too
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
SPIClass SPI1(SPI);
// BMP280 setup
#include <Wire.h> // include Wire library, required for I2C devices
#include <Adafruit_Sensor.h> // include Adafruit sensor library
#include <Adafruit_BMP280.h> // include adafruit library for BMP280 sensor
// define device I2C address: 0x76 or 0x77 (0x77 is library default address)
#define BMP280_I2C_ADDRESS 0x76
Adafruit_BMP280 bmp280;
#define I2C_SDA 16
#define I2C_SCL 17
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
delay(2000);
Serial.println("\nWaveshare ESP32-S3 ETH Development Board (W5500) Ethernet UDP chat program");
// Check for Ethernet hardware present
// bool begin(int8_t sck = -1, int8_t miso = -1, int8_t mosi = -1, int8_t ss = -1);
SPI1.begin(W5500_SCK, W5500_MISO, W5500_MOSI, W5500_CS);
driver.setSPI(SPI1);
Ethernet.init(driver);
Serial.println("Initialize Ethernet with DHCP:");
if (Ethernet.begin(mac)) {
Serial.print(" DHCP assigned IP ");
Serial.println(Ethernet.localIP());
} else {
Serial.println("Failed to configure Ethernet using DHCP");
Serial.print("\nDefault localIP");
displayIPaddress(localIP, localPort);
Serial.println("Failed to configure Ethernet using DHCP - using static IP");
getIPaddress("local IP address (<return> use default)? ", localIP, localPort);
Ethernet.begin(mac, localIP); // static IP initialize Ethernet shield
}
Serial.print("My localIP address: "); // print local localIP address:
displayIPaddress(Ethernet.localIP(), localPort);
// Initializes the ethernet UDP library and network settings.
if (Udp.begin(port)) Serial.println("UDP begin() OK");
else Serial.println("UDP begin() failed!");
// get remote IP and port
Serial.print("\nDefault remoteIP ");
displayIPaddress(remoteIP, remotePort);
//getIPaddress("remote IP address (<return> use default)? ", remoteIP, remotePort);
Serial.print("\nRemote ");
displayIPaddress(remoteIP, remotePort);
// initialize BMP280
Wire.begin(I2C_SDA, I2C_SCL);
Serial.println(F("Arduino + BMP280"));
if (!bmp280.begin(BMP280_I2C_ADDRESS)) {
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1)
;
}
Serial.println("Found BMP280 sensor!");
}
void loop() {
char text[100] = { 0 };
// get temperature, pressure and altitude from library
float temperature = bmp280.readTemperature(); // get temperature
float pressure = bmp280.readPressure(); // get pressure
float altitude_ = bmp280.readAltitude(1013.25); // get altitude (this should be adjusted to your local forecast)
// print data on the serial monitor software
// 1: print temperature
sprintf(text, "\nBMP280 data\nTemperature = %.2f °C\nPressure = %.2f hPa\n",
temperature, pressure / 100);
Serial.println(text); // start a new line
Udp.beginPacket(remoteIP, remotePort); // transmit datagram
Udp.print(text);
Udp.endPacket();
delay(2000); // wait 2 seconds
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
displayIPaddress(Udp.remoteIP(), Udp.remotePort());
// read the packet into packetBuffer
char packetBuffer[100] = { 0 }; // buffer to hold incoming packet,
Udp.read(packetBuffer, packetSize); //UDP_TX_PACKET_MAX_SIZE); // receive datagram
Serial.print("Contents: ");
Serial.println(packetBuffer);
// if (strcmp(packetBuffer, "OK") != 0) {
// send a reply to the IP address and port that sent us the packet we received
// Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
// Udp.write("OK");
// Udp.endPacket();
//}
}
delay(10);
}
// read IP address and port from keyboard and check it
void getIPaddress(const char* prompt, IPAddress& defaultIP, unsigned int& defaultPort) {
IPAddress ip;
while (1) { // read IP (end with new line)
char text[40] = { 0 };
Serial.print(prompt); // prompt and wait for input
while (Serial.available() == 0) delay(10);
Serial.readBytesUntil('\n', (char*)text, 40);
for (int i = 0; i < 40; i++) // convert IP address
if (text[i] < ' ') text[i] = 0;
if (text[0] == 0) return; // ig just <enter> return defaultIP;
//Serial.println(text);
if (ip.fromString(text)) break; // if IP OK break while
Serial.println("\ninvalid IP try again");
}
char text[40] = { 0 };
// read port number
while (Serial.available()) Serial.read();
defaultIP = ip;
Serial.print("\nport ? "); // prompt and wait for input
while (Serial.available() == 0) delay(10);
Serial.readBytesUntil('\n', (char*)text, 40); // read value
sscanf(text, "%u", &defaultPort); // convert value
}
// print IPAdress and port
void displayIPaddress(const IPAddress address, unsigned int port) {
Serial.print(" IP ");
Serial.print(address);
Serial.print(" port ");
Serial.println(port);
}
UDP datagrams received on a PC using a Java program
F:\Ardunio\Networking\Ethernet\Waveshare ESP32-S3 ETH Development Board\ESP32-S3-ETH-chat>java UDPchat
chat program: IP address BB-DELL2/192.168.1.65 port 10000
UDP datagram length 64 from IP /192.168.1.106 received:
BMP280 data
Temperature = 24.68 ?°C
Pressure = 1016.52 hPa
UDP datagram length 64 from IP /192.168.1.106 received:
BMP280 data
Temperature = 24.72 ?°C
Pressure = 1016.59 hPa
UDP datagram length 64 from IP /192.168.1.106 received:
BMP280 data
Temperature = 24.69 ?°C
Pressure = 1016.51 hPa
UDP datagram length 64 from IP /192.168.1.106 received:
BMP280 data
Temperature = 24.70 ?°C
Pressure = 1016.48 hPa
UDP datagram length 64 from IP /192.168.1.106 received:
BMP280 data
Temperature = 24.68 ?°C
Pressure = 1016.48 hPa
UDP datagram length 64 from IP /192.168.1.106 received:
BMP280 data
Temperature = 24.72 ?°C
Pressure = 1016.58 hPa
UDP datagram length 64 from IP /192.168.1.106 received:
BMP280 data
Temperature = 24.69 ?°C
Pressure = 1016.57 hPa
UDP datagram length 64 from IP /192.168.1.106 received:
BMP280 data
Temperature = 24.71 ?°C
Pressure = 1016.47 hPa
photo showing BME280 connected to a Waveshare ESP32-S3 ETH Development Board powered over Ethernet using a POE injector