pleasedontcode

Sensor Publisher rev_02

Aug 4th, 2025
632
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Sensor Publisher
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2025-08-04 13:28:15
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Enviar los datos del sensor DHT22 (temperatura y */
  21.     /* humedad) y MAX30100 (frecuencia cardíaca y SpO2) */
  22.     /* mediante MQTT usando la librería PubSubClient en */
  23.     /* un Arduino Nano ESP32 conectado a los componentes */
  24.     /* listados. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Wire.h>
  30. #include <DHT.h> //https://github.com/adafruit/DHT-sensor-library
  31. #include <MAX30100_PulseOximeter.h> //https://github.com/gabriel-milan/Arduino-MAX30100
  32. #include <WiFi.h>
  33. #include <PubSubClient.h>
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38. void reconnect(void);
  39. void publishSensorData(void);
  40.  
  41. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  42. const uint8_t temphum_DHT22_DOUT_PIN_D2 = 2;
  43. const uint8_t bmp_MAX30100_INT_PIN_D3 = 3;
  44.  
  45. /***** DEFINITION OF I2C PINS *****/
  46. const uint8_t bmp_MAX30100_I2C_PIN_SDA_A4 = A4;
  47. const uint8_t bmp_MAX30100_I2C_PIN_SCL_A5 = A5;
  48.  
  49. /***** MQTT CONFIGURATION *****/
  50. // Replace with your network credentials
  51. const char* ssid = "YOUR_SSID";
  52. const char* password = "YOUR_PASSWORD";
  53. // Replace with your MQTT broker address
  54. const char* mqtt_server = "broker.hivemq.com"; // example public broker
  55.  
  56. // MQTT topics
  57. const char* dht_temperature_topic = "sensor/dht22/temperature";
  58. const char* dht_humidity_topic = "sensor/dht22/humidity";
  59. const char* max30100_hr_topic = "sensor/max30100/heart_rate";
  60. const char* max30100_spo2_topic = "sensor/max30100/spo2";
  61.  
  62. WiFiClient espClient;
  63. PubSubClient client(espClient);
  64.  
  65. /****** LIBRARIES CLASS INSTANCES****/
  66. DHT dht(temphum_DHT22_DOUT_PIN_D2, DHT22);
  67. MAX30100_PulseOximeter max30100;
  68.  
  69. // Variables to hold sensor data
  70. float dht_temperature = 0.0;
  71. float dht_humidity = 0.0;
  72. int max30100_heart_rate = 0;
  73. int max30100_SpO2 = 0;
  74.  
  75. unsigned long lastPublishTime = 0;
  76. const unsigned long publishInterval = 2000; // 2 seconds
  77.  
  78. /********* CALLBACK for MAX30100 *********/
  79. void onBeatDetected()
  80. {
  81.   Serial.println("Beat detected!");
  82. }
  83.  
  84. void setup()
  85. {
  86.   Serial.begin(115200);
  87.  
  88.   // Initialize DHT22
  89.   dht.begin();
  90.  
  91.   // Initialize MAX30100
  92.   pinMode(bmp_MAX30100_INT_PIN_D3, INPUT_PULLUP);
  93.   if (!max30100.begin()) {
  94.     Serial.println("MAX30100 init failed");
  95.   } else {
  96.     Serial.println("MAX30100 initialized");
  97.   }
  98.   max30100.setMode(MAX30100_MODE_SPO2_HR);
  99.   max30100.setLedsCurrent(MAX30100_LED_CURR_50MA, MAX30100_LED_CURR_50MA);
  100.   max30100.setLedsPulseWidth(MAX30100_SPC_PW_1600US_16BITS);
  101.   max30100.setSamplingRate(MAX30100_SAMPRATE_100HZ);
  102.   max30100.setHighresModeEnabled(true);
  103.   max30100.setOnBeatDetectedCallback(onBeatDetected);
  104.  
  105.   // Setup WiFi
  106.   WiFi.begin(ssid, password);
  107.   Serial.print("Connecting to WiFi");
  108.   while (WiFi.status() != WL_CONNECTED) {
  109.     delay(500);
  110.     Serial.print(".");
  111.   }
  112.   Serial.println(" connected");
  113.  
  114.   // Setup MQTT
  115.   client.setServer(mqtt_server, 1883);
  116. }
  117.  
  118. void loop()
  119. {
  120.   if (!client.connected()) {
  121.     reconnect();
  122.   }
  123.   client.loop();
  124.  
  125.   unsigned long currentMillis = millis();
  126.   if (currentMillis - lastPublishTime >= publishInterval)
  127.   {
  128.     // Read DHT22 sensor
  129.     dht_temperature = dht.readTemperature();
  130.     dht_humidity = dht.readHumidity();
  131.  
  132.     // Read MAX30100 sensor
  133.     max30100.update();
  134.     if (max30100.getHeartRate() > 0)
  135.       max30100_heart_rate = max30100.getHeartRate();
  136.     if (max30100.getSpO2() > 0)
  137.       max30100_SpO2 = max30100.getSpO2();
  138.  
  139.     // Publish the data
  140.     publishSensorData();
  141.  
  142.     lastPublishTime = currentMillis;
  143.   }
  144. }
  145.  
  146. void reconnect()
  147. {
  148.   // Loop until we're reconnected
  149.   while (!client.connected()) {
  150.     Serial.print("Attempting MQTT connection...");
  151.     // Create a random client ID
  152.     String clientId = "ESP32Client-";
  153.     clientId += String(random(0xffff), HEX);
  154.     // Attempt to connect
  155.     if (client.connect(clientId.c_str())) {
  156.       Serial.println("connected");
  157.     } else {
  158.       Serial.print("failed, rc=");
  159.       Serial.print(client.state());
  160.       Serial.println(" try again in 5 seconds");
  161.       delay(5000);
  162.     }
  163.   }
  164. }
  165.  
  166. void publishSensorData()
  167. {
  168.   char buffer[10];
  169.  
  170.   // Publish DHT22 temperature
  171.   dtostrf(dht_temperature, 4, 2, buffer);
  172.   client.publish(dht_temperature_topic, buffer);
  173.  
  174.   // Publish DHT22 humidity
  175.   dtostrf(dht_humidity, 4, 2, buffer);
  176.   client.publish(dht_humidity_topic, buffer);
  177.  
  178.   // Publish MAX30100 Heart Rate
  179.   dtostrf((float)max30100_heart_rate, 3, 0, buffer);
  180.   client.publish(max30100_hr_topic, buffer);
  181.  
  182.   // Publish MAX30100 SpO2
  183.   dtostrf((float)max30100_SpO2, 3, 0, buffer);
  184.   client.publish(max30100_spo2_topic, buffer);
  185. }
  186.  
Advertisement
Add Comment
Please, Sign In to add comment