Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Sensor Publisher
- - Source Code NOT compiled for: Arduino Nano ESP32
- - Source Code created on: 2025-08-04 13:28:15
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Enviar los datos del sensor DHT22 (temperatura y */
- /* humedad) y MAX30100 (frecuencia cardíaca y SpO2) */
- /* mediante MQTT usando la librería PubSubClient en */
- /* un Arduino Nano ESP32 conectado a los componentes */
- /* listados. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <DHT.h> //https://github.com/adafruit/DHT-sensor-library
- #include <MAX30100_PulseOximeter.h> //https://github.com/gabriel-milan/Arduino-MAX30100
- #include <WiFi.h>
- #include <PubSubClient.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void reconnect(void);
- void publishSensorData(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t temphum_DHT22_DOUT_PIN_D2 = 2;
- const uint8_t bmp_MAX30100_INT_PIN_D3 = 3;
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t bmp_MAX30100_I2C_PIN_SDA_A4 = A4;
- const uint8_t bmp_MAX30100_I2C_PIN_SCL_A5 = A5;
- /***** MQTT CONFIGURATION *****/
- // Replace with your network credentials
- const char* ssid = "YOUR_SSID";
- const char* password = "YOUR_PASSWORD";
- // Replace with your MQTT broker address
- const char* mqtt_server = "broker.hivemq.com"; // example public broker
- // MQTT topics
- const char* dht_temperature_topic = "sensor/dht22/temperature";
- const char* dht_humidity_topic = "sensor/dht22/humidity";
- const char* max30100_hr_topic = "sensor/max30100/heart_rate";
- const char* max30100_spo2_topic = "sensor/max30100/spo2";
- WiFiClient espClient;
- PubSubClient client(espClient);
- /****** LIBRARIES CLASS INSTANCES****/
- DHT dht(temphum_DHT22_DOUT_PIN_D2, DHT22);
- MAX30100_PulseOximeter max30100;
- // Variables to hold sensor data
- float dht_temperature = 0.0;
- float dht_humidity = 0.0;
- int max30100_heart_rate = 0;
- int max30100_SpO2 = 0;
- unsigned long lastPublishTime = 0;
- const unsigned long publishInterval = 2000; // 2 seconds
- /********* CALLBACK for MAX30100 *********/
- void onBeatDetected()
- {
- Serial.println("Beat detected!");
- }
- void setup()
- {
- Serial.begin(115200);
- // Initialize DHT22
- dht.begin();
- // Initialize MAX30100
- pinMode(bmp_MAX30100_INT_PIN_D3, INPUT_PULLUP);
- if (!max30100.begin()) {
- Serial.println("MAX30100 init failed");
- } else {
- Serial.println("MAX30100 initialized");
- }
- max30100.setMode(MAX30100_MODE_SPO2_HR);
- max30100.setLedsCurrent(MAX30100_LED_CURR_50MA, MAX30100_LED_CURR_50MA);
- max30100.setLedsPulseWidth(MAX30100_SPC_PW_1600US_16BITS);
- max30100.setSamplingRate(MAX30100_SAMPRATE_100HZ);
- max30100.setHighresModeEnabled(true);
- max30100.setOnBeatDetectedCallback(onBeatDetected);
- // Setup WiFi
- WiFi.begin(ssid, password);
- Serial.print("Connecting to WiFi");
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println(" connected");
- // Setup MQTT
- client.setServer(mqtt_server, 1883);
- }
- void loop()
- {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- unsigned long currentMillis = millis();
- if (currentMillis - lastPublishTime >= publishInterval)
- {
- // Read DHT22 sensor
- dht_temperature = dht.readTemperature();
- dht_humidity = dht.readHumidity();
- // Read MAX30100 sensor
- max30100.update();
- if (max30100.getHeartRate() > 0)
- max30100_heart_rate = max30100.getHeartRate();
- if (max30100.getSpO2() > 0)
- max30100_SpO2 = max30100.getSpO2();
- // Publish the data
- publishSensorData();
- lastPublishTime = currentMillis;
- }
- }
- void reconnect()
- {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Create a random client ID
- String clientId = "ESP32Client-";
- clientId += String(random(0xffff), HEX);
- // Attempt to connect
- if (client.connect(clientId.c_str())) {
- Serial.println("connected");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- delay(5000);
- }
- }
- }
- void publishSensorData()
- {
- char buffer[10];
- // Publish DHT22 temperature
- dtostrf(dht_temperature, 4, 2, buffer);
- client.publish(dht_temperature_topic, buffer);
- // Publish DHT22 humidity
- dtostrf(dht_humidity, 4, 2, buffer);
- client.publish(dht_humidity_topic, buffer);
- // Publish MAX30100 Heart Rate
- dtostrf((float)max30100_heart_rate, 3, 0, buffer);
- client.publish(max30100_hr_topic, buffer);
- // Publish MAX30100 SpO2
- dtostrf((float)max30100_SpO2, 3, 0, buffer);
- client.publish(max30100_spo2_topic, buffer);
- }
Advertisement
Add Comment
Please, Sign In to add comment