Files
2026-07-29 12:46:54 +02:00

122 lines
3.4 KiB
Arduino

/*
Seeeduino XIAO ESP32-S3 + BMP280 -> WiFi -> UDP sender
--------------------------------------------------------
Reads temperature (°C) and pressure (hPa) from a BMP280 over I2C,
along with the WiFi signal strength (RSSI), and sends them as a
semicolon-delimited string (terminated with a newline) over UDP
to a specified IP address and port 9999.
Payload format: temperature;pressure;rssi\n
Example: 24.53;1013.25;-58
Wiring (I2C):
XIAO ESP32-S3 SDA (D4/GPIO5) -> BMP280 SDA
XIAO ESP32-S3 SCL (D5/GPIO6) -> BMP280 SCL
3V3 -> VCC
GND -> GND
Required libraries (install via Library Manager):
- Adafruit BMP280 Library
- Adafruit Unified Sensor
*/
#include <Wire.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
// ---------------- USER CONFIG ----------------
const char* WIFI_SSID = "control-tower-24";
const char* WIFI_PASSWORD = "expaexpa";
// Destination for UDP packets
const char* UDP_TARGET_IP = "192.168.1.17"; // <-- change to your receiver's IP
const uint16_t UDP_TARGET_PORT = 9999;
// How often to read + send (ms)
const unsigned long SEND_INTERVAL_MS = 500;
// I2C pins for XIAO ESP32-S3 (default Wire pins)
const int I2C_SDA_PIN = 5; // D4
const int I2C_SCL_PIN = 6; // D5
// BMP280 I2C address: 0x76 (SDO to GND) or 0x77 (SDO to VCC)
const uint8_t BMP280_I2C_ADDRESS = 0x76;
// -----------------------------------------------
Adafruit_BMP280 bmp;
WiFiUDP udp;
unsigned long lastSendTime = 0;
void connectWiFi() {
Serial.print("Connecting to WiFi");
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(400);
Serial.print(".");
}
Serial.println();
Serial.print("WiFi connected. IP address: ");
Serial.println(WiFi.localIP());
}
void setup() {
Serial.begin(115200);
delay(1000); // give the serial monitor time to attach
// Init I2C on XIAO ESP32-S3 pins
Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN);
// Init BMP280
if (!bmp.begin(BMP280_I2C_ADDRESS)) {
Serial.println("Could not find a valid BMP280 sensor, check wiring or address!");
while (1) {
delay(1000);
}
}
// Recommended settings for weather monitoring (see Adafruit example)
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,
Adafruit_BMP280::SAMPLING_X2, // temperature
Adafruit_BMP280::SAMPLING_X16, // pressure
Adafruit_BMP280::FILTER_X16,
Adafruit_BMP280::STANDBY_MS_500);
connectWiFi();
}
long counter = 0;
void loop() {
// Reconnect WiFi if dropped
if (WiFi.status() != WL_CONNECTED) {
connectWiFi();
}
unsigned long now = millis();
if (now - lastSendTime >= SEND_INTERVAL_MS) {
lastSendTime = now;
float temperature = bmp.readTemperature(); // °C
float pressure = bmp.readPressure() / 100.0F; // Pa -> hPa
long rssi = WiFi.RSSI(); // dBm
// Build payload: counter;temperature;pressure;rssi\n
char payload[64];
snprintf(payload, sizeof(payload), "%ld;%.2f;%.2f;%ld\n", counter, temperature, pressure, rssi);
counter = counter + 1;
Serial.print("Sending UDP packet: ");
Serial.println(payload);
udp.beginPacket(UDP_TARGET_IP, UDP_TARGET_PORT);
udp.print(payload);
udp.endPacket();
}
}