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 14:11:00
- ********* 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 <WiFi.h>
- #include <PubSubClient.h>
- #include <DHT.h>
- #include <Ultrasonic.h>
- //#include <MAX30100.h> // Uncomment if MAX30100 library is available
- /****** WIFI AND MQTT CONFIGURATION *****/
- const char* ssid = "YOUR_SSID";
- const char* password = "YOUR_PASSWORD";
- const char* mqtt_server = "YOUR_MQTT_BROKER_IP";
- WiFiClient espClient;
- PubSubClient client(espClient);
- /****** SENSOR PINS AND INSTANCES *****/
- const uint8_t temphum_DHT22_DOUT_PIN_D2 = 2;
- const uint8_t Distancia_HC_SR04_Echo_PIN_D5 = 5;
- const uint8_t Distancia_HC_SR04_Trigger_PIN_D4 = 4;
- DHT dht(temphum_DHT22_DOUT_PIN_D2, DHT22);
- // Placeholder variables for MAX30100 sensor data
- float heartRate = 0.0;
- float SpO2 = 0.0;
- /****** Ultrasonic Sensor Instance *****/
- Ultrasonic ultrasonic(Distancia_HC_SR04_Trigger_PIN_D4, Distancia_HC_SR04_Echo_PIN_D5);
- /****** Function Prototypes *****/
- void setup_wifi();
- void reconnect();
- void setup(void);
- void loop(void);
- void publishSensorData();
- void setup() {
- // Initialize serial communication
- Serial.begin(9600);
- // Initialize sensors
- pinMode(Distancia_HC_SR04_Trigger_PIN_D4, OUTPUT);
- pinMode(Distancia_HC_SR04_Echo_PIN_D5, INPUT_PULLUP);
- pinMode(temphum_DHT22_DOUT_PIN_D2, INPUT_PULLUP);
- dht.begin();
- // Connect to WiFi
- setup_wifi();
- // Setup MQTT server
- client.setServer(mqtt_server, 1883);
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- publishSensorData();
- delay(2000); // Delay between readings
- }
- void setup_wifi() {
- delay(10);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("WiFi connected");
- }
- void reconnect() {
- while (!client.connected()) {
- if (client.connect("ESP32Client")) {
- // Connected
- } else {
- delay(5000);
- }
- }
- }
- void publishSensorData() {
- // Read DHT22 temperature and humidity
- float temperature = dht.readTemperature();
- float humidity = dht.readHumidity();
- // Check if reads are valid
- if (isnan(temperature) || isnan(humidity)) {
- Serial.println("Failed to read DHT22 sensor");
- return;
- }
- // Read ultrasonic distance in centimeters
- float distance_cm = ultrasonic.read();
- // Placeholder: Read MAX30100 sensor data
- // Replace with actual MAX30100 library calls
- // e.g., heartRate = max30100.getHeartRate();
- // SpO2 = max30100.getSpO2();
- heartRate = 75.0; // Example value
- SpO2 = 98.0; // Example value
- // Publish DHT22 data
- char tempStr[8];
- dtostrf(temperature, 6, 2, tempStr);
- client.publish("sensor/dht/temperature", tempStr);
- char humStr[8];
- dtostrf(humidity, 6, 2, humStr);
- client.publish("sensor/dht/humidity", humStr);
- // Publish MAX30100 data
- char hrStr[8];
- dtostrf(heartRate, 6, 2, hrStr);
- client.publish("sensor/max30100/heart_rate", hrStr);
- char spo2Str[8];
- dtostrf(SpO2, 6, 2, spo2Str);
- client.publish("sensor/max30100/spo2", spo2Str);
- // Publish ultrasonic distance
- char distStr[8];
- dtostrf(distance_cm, 6, 2, distStr);
- client.publish("sensor/ultrasonic/distance_cm", distStr);
- }
Advertisement
Add Comment
Please, Sign In to add comment