pleasedontcode

Sensor Publisher rev_01

Aug 4th, 2025
619
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:11:08
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Send the MAX30100 sensor's SpO2 and heart rate */
  21.     /* variables via MQTT to a specified broker, ensuring */
  22.     /* data is transmitted only when new readings are */
  23.     /* available, using the MAX30100_milan library */
  24.     /* functions. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Wire.h>
  30. #include <MAX30100_milan.h>       // Using the MAX30100_milan library
  31. #include <WiFi.h>
  32. #include <PubSubClient.h>         // MQTT library
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37. void reconnect(); // MQTT reconnect function
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t BMP_MAX30100_INT_PIN_D2 = 2;
  41.  
  42. /***** DEFINITION OF I2C PINS *****/
  43. const uint8_t BMP_MAX30100_I2C_PIN_SDA_A4 = A4;
  44. const uint8_t BMP_MAX30100_I2C_PIN_SCL_A5 = A5;
  45.  
  46. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  47. MAX30100_milan max30100Sensor;  // Instantiate MAX30100 sensor object
  48.  
  49. // MQTT broker details
  50. const char* mqtt_server = "your.mqtt.broker.address"; // Replace with your broker address
  51. const int mqtt_port = 1883; // Default MQTT port
  52. const char* mqtt_user = "your_username"; // If needed
  53. const char* mqtt_password = "your_password"; // If needed
  54.  
  55. WiFiClient espClient;
  56. PubSubClient client(espClient);
  57.  
  58. // Variables to hold sensor data
  59. volatile bool newDataAvailable = false;
  60. uint16_t lastSpO2 = 0;
  61. uint16_t lastHeartRate = 0;
  62.  
  63. // Callback when a beat is detected
  64. void onBeatDetected() {
  65.   Serial.println("Beat detected");
  66. }
  67.  
  68. // Connect to WiFi network
  69. void setup_wifi() {
  70.   delay(10);
  71.   Serial.println();
  72.   Serial.print("Connecting to WiFi...");
  73.   WiFi.begin("your_SSID", "your_PASSWORD"); // Replace with your WiFi credentials
  74.  
  75.   while (WiFi.status() != WL_CONNECTED) {
  76.     delay(500);
  77.     Serial.print(".");
  78.   }
  79.  
  80.   Serial.println("WiFi connected");
  81.   Serial.print("IP address: ");
  82.   Serial.println(WiFi.localIP());
  83. }
  84.  
  85. // Reconnect to MQTT broker
  86. void reconnect() {
  87.   // Loop until reconnected
  88.   while (!client.connected()) {
  89.     Serial.print("Attempting MQTT connection...");
  90.     // Attempt to connect
  91.     if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
  92.       Serial.println("connected");
  93.     } else {
  94.       Serial.print("failed, rc=");
  95.       Serial.print(client.state());
  96.       Serial.println(" try again in 5 seconds");
  97.       delay(5000);
  98.     }
  99.   }
  100. }
  101.  
  102. void setup() {
  103.   Serial.begin(115200);
  104.   pinMode(BMP_MAX30100_INT_PIN_D2, INPUT_PULLUP);
  105.  
  106.   // Initialize WiFi
  107.   setup_wifi();
  108.  
  109.   // Initialize MQTT
  110.   client.setServer(mqtt_server, mqtt_port);
  111.  
  112.   // Initialize MAX30100 sensor
  113.   if (!max30100Sensor.begin()) {
  114.     Serial.println("Failed to initialize MAX30100 sensor");
  115.     while (1);
  116.   }
  117.   max30100Sensor.setMode(MAX30100_MODE_SPO2_HR);
  118.   max30100Sensor.setLedsCurrent(MAX30100_LED_CURR_50MA, MAX30100_LED_CURR_50MA);
  119.   max30100Sensor.setOnBeatDetectedCallback(onBeatDetected);
  120. }
  121.  
  122. void loop() {
  123.   if (!client.connected()) {
  124.     reconnect();
  125.   }
  126.   client.loop();
  127.  
  128.   max30100Sensor.update();
  129.  
  130.   uint16_t ir, red;
  131.   if (max30100Sensor.getRawValues(&ir, &red)) {
  132.     // Read SpO2 and Heart Rate
  133.     uint16_t currentSpO2 = max30100Sensor.getSpO2();
  134.     uint16_t currentHR = max30100Sensor.getHeartRate();
  135.  
  136.     // Check if new data is different from last sent data
  137.     if (currentSpO2 != lastSpO2 || currentHR != lastHeartRate) {
  138.       lastSpO2 = currentSpO2;
  139.       lastHeartRate = currentHR;
  140.  
  141.       // Prepare JSON payload
  142.       String payload = "{";
  143.       payload += "\"SpO2\": " + String(currentSpO2) + ",";
  144.       payload += "\"HeartRate\": " + String(currentHR);
  145.       payload += "}";
  146.  
  147.       // Publish to MQTT topic
  148.       if (client.publish("sensor/heart_rate_spo2", payload.c_str())) {
  149.         Serial.println("Data sent: " + payload);
  150.       } else {
  151.         Serial.println("Failed to send data");
  152.       }
  153.     }
  154.   }
  155.  
  156.   delay(100); // Adjust delay as needed
  157. }
  158.  
Advertisement
Add Comment
Please, Sign In to add comment