pleasedontcode

Sensor Publisher rev_03

Aug 4th, 2025
646
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 14:11:00
  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 <WiFi.h>
  30. #include <PubSubClient.h>
  31. #include <DHT.h>
  32. #include <Ultrasonic.h>
  33. //#include <MAX30100.h> // Uncomment if MAX30100 library is available
  34.  
  35. /****** WIFI AND MQTT CONFIGURATION *****/
  36. const char* ssid = "YOUR_SSID";
  37. const char* password = "YOUR_PASSWORD";
  38. const char* mqtt_server = "YOUR_MQTT_BROKER_IP";
  39.  
  40. WiFiClient espClient;
  41. PubSubClient client(espClient);
  42.  
  43. /****** SENSOR PINS AND INSTANCES *****/
  44. const uint8_t temphum_DHT22_DOUT_PIN_D2 = 2;
  45. const uint8_t Distancia_HC_SR04_Echo_PIN_D5 = 5;
  46. const uint8_t Distancia_HC_SR04_Trigger_PIN_D4 = 4;
  47.  
  48. DHT dht(temphum_DHT22_DOUT_PIN_D2, DHT22);
  49.  
  50. // Placeholder variables for MAX30100 sensor data
  51. float heartRate = 0.0;
  52. float SpO2 = 0.0;
  53.  
  54. /****** Ultrasonic Sensor Instance *****/
  55. Ultrasonic ultrasonic(Distancia_HC_SR04_Trigger_PIN_D4, Distancia_HC_SR04_Echo_PIN_D5);
  56.  
  57. /****** Function Prototypes *****/
  58. void setup_wifi();
  59. void reconnect();
  60. void setup(void);
  61. void loop(void);
  62. void publishSensorData();
  63.  
  64. void setup() {
  65.   // Initialize serial communication
  66.   Serial.begin(9600);
  67.  
  68.   // Initialize sensors
  69.   pinMode(Distancia_HC_SR04_Trigger_PIN_D4, OUTPUT);
  70.   pinMode(Distancia_HC_SR04_Echo_PIN_D5, INPUT_PULLUP);
  71.   pinMode(temphum_DHT22_DOUT_PIN_D2, INPUT_PULLUP);
  72.   dht.begin();
  73.  
  74.   // Connect to WiFi
  75.   setup_wifi();
  76.  
  77.   // Setup MQTT server
  78.   client.setServer(mqtt_server, 1883);
  79. }
  80.  
  81. void loop() {
  82.   if (!client.connected()) {
  83.     reconnect();
  84.   }
  85.   client.loop();
  86.  
  87.   publishSensorData();
  88.  
  89.   delay(2000); // Delay between readings
  90. }
  91.  
  92. void setup_wifi() {
  93.   delay(10);
  94.   WiFi.begin(ssid, password);
  95.   while (WiFi.status() != WL_CONNECTED) {
  96.     delay(500);
  97.     Serial.print(".");
  98.   }
  99.   Serial.println("WiFi connected");
  100. }
  101.  
  102. void reconnect() {
  103.   while (!client.connected()) {
  104.     if (client.connect("ESP32Client")) {
  105.       // Connected
  106.     } else {
  107.       delay(5000);
  108.     }
  109.   }
  110. }
  111.  
  112. void publishSensorData() {
  113.   // Read DHT22 temperature and humidity
  114.   float temperature = dht.readTemperature();
  115.   float humidity = dht.readHumidity();
  116.  
  117.   // Check if reads are valid
  118.   if (isnan(temperature) || isnan(humidity)) {
  119.     Serial.println("Failed to read DHT22 sensor");
  120.     return;
  121.   }
  122.  
  123.   // Read ultrasonic distance in centimeters
  124.   float distance_cm = ultrasonic.read();
  125.  
  126.   // Placeholder: Read MAX30100 sensor data
  127.   // Replace with actual MAX30100 library calls
  128.   // e.g., heartRate = max30100.getHeartRate();
  129.   //       SpO2 = max30100.getSpO2();
  130.   heartRate = 75.0; // Example value
  131.   SpO2 = 98.0;      // Example value
  132.  
  133.   // Publish DHT22 data
  134.   char tempStr[8];
  135.   dtostrf(temperature, 6, 2, tempStr);
  136.   client.publish("sensor/dht/temperature", tempStr);
  137.  
  138.   char humStr[8];
  139.   dtostrf(humidity, 6, 2, humStr);
  140.   client.publish("sensor/dht/humidity", humStr);
  141.  
  142.   // Publish MAX30100 data
  143.   char hrStr[8];
  144.   dtostrf(heartRate, 6, 2, hrStr);
  145.   client.publish("sensor/max30100/heart_rate", hrStr);
  146.  
  147.   char spo2Str[8];
  148.   dtostrf(SpO2, 6, 2, spo2Str);
  149.   client.publish("sensor/max30100/spo2", spo2Str);
  150.  
  151.   // Publish ultrasonic distance
  152.   char distStr[8];
  153.   dtostrf(distance_cm, 6, 2, distStr);
  154.   client.publish("sensor/ultrasonic/distance_cm", distStr);
  155. }
  156.  
Advertisement
Add Comment
Please, Sign In to add comment