/* Projekt: MQTT Wifi DSP310 Sensor Beschreibung: Dieses Sketch kann zur Nutzung auf einem Arduino UNO R4 Wifi mit dem Sensor DSP310 genutzt werden Autor: Joachim Hummel Datum: 08.02.2024 Erstellung Arduino-Board: Arduino Uno R4 WiFi Bibliotheken: - [Liste der verwendeten Bibliotheken, inklusive Versionen wenn möglich] Lizenz: Apache 2.0 Hinweise: - Die Nutzung des Code beinhaltet keinerlei Garantie oder GewÃhrliestung. Die Nutzung erfolgt auf eigenes Risiko ohne Rechtsanspruch Credits: - Danke fÃr die Bereitstellung von Codeteilen aus der Arduino IDE Weitere Informationen findest du in der README.md-Datei oder auf GitHub. */ #include #include #include #include #include "arduino_secrets.h" Adafruit_DPS310 dps; Adafruit_Sensor *dps_temp = dps.getTemperatureSensor(); Adafruit_Sensor *dps_pressure = dps.getPressureSensor(); // Please enter your sensitive data in the Secret tab/arduino_secrets.h char ssid[] = SECRET_SSID; // Your network SSID (name) char pass[] = SECRET_PASS; // Your network password (use for WPA, or use as key for WEP) const char* mqtt_server = "192.168.10.7"; // IP of your MQTT Broker WiFiClient espClient; PubSubClient client(espClient); unsigned long lastMsg = 0; #define MSG_BUFFER_SIZE (50) char msg[MSG_BUFFER_SIZE]; int value = 0; void setup_wifi() { delay(100); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void callback(char* topic, byte* payload, unsigned int length) { // Handle incoming MQTT messages if needed } void reconnect() { while (!client.connected()) { Serial.print("Attempting MQTT connection..."); String clientId = "ESP8266Client-"; clientId += String(random(0xffff), HEX); if (client.connect(clientId.c_str())) { Serial.println("connected"); client.publish("outTopic", "Sensor Online!"); client.subscribe("inTopic"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } void setup() { Serial.begin(115200); while (!Serial) delay(10); Serial.println("DPS310"); if (! dps.begin_I2C()) { Serial.println("Failed to find DPS"); while (1) yield(); } Serial.println("DPS OK!"); // Setup highest precision dps.configurePressure(DPS310_64HZ, DPS310_64SAMPLES); dps.configureTemperature(DPS310_64HZ, DPS310_64SAMPLES); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); StaticJsonDocument<32> doc; char output[55]; sensors_event_t temp_event, pressure_event; if (dps.temperatureAvailable() && dps.pressureAvailable()) { dps_temp->getEvent(&temp_event); dps_pressure->getEvent(&pressure_event); Serial.print(F("Temperature = ")); Serial.print(temp_event.temperature); Serial.println(" *C"); Serial.print(F("Pressure = ")); Serial.print(pressure_event.pressure); Serial.println(" hPa"); Serial.println(); doc["temperature"] = temp_event.temperature; doc["pressure"] = pressure_event.pressure; serializeJson(doc, output); client.publish("/home/sensors", output); delay(5000); } }