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:11:08
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Send the MAX30100 sensor's SpO2 and heart rate */
- /* variables via MQTT to a specified broker, ensuring */
- /* data is transmitted only when new readings are */
- /* available, using the MAX30100_milan library */
- /* functions. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <MAX30100_milan.h> // Using the MAX30100_milan library
- #include <WiFi.h>
- #include <PubSubClient.h> // MQTT library
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void reconnect(); // MQTT reconnect function
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t BMP_MAX30100_INT_PIN_D2 = 2;
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t BMP_MAX30100_I2C_PIN_SDA_A4 = A4;
- const uint8_t BMP_MAX30100_I2C_PIN_SCL_A5 = A5;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- MAX30100_milan max30100Sensor; // Instantiate MAX30100 sensor object
- // MQTT broker details
- const char* mqtt_server = "your.mqtt.broker.address"; // Replace with your broker address
- const int mqtt_port = 1883; // Default MQTT port
- const char* mqtt_user = "your_username"; // If needed
- const char* mqtt_password = "your_password"; // If needed
- WiFiClient espClient;
- PubSubClient client(espClient);
- // Variables to hold sensor data
- volatile bool newDataAvailable = false;
- uint16_t lastSpO2 = 0;
- uint16_t lastHeartRate = 0;
- // Callback when a beat is detected
- void onBeatDetected() {
- Serial.println("Beat detected");
- }
- // Connect to WiFi network
- void setup_wifi() {
- delay(10);
- Serial.println();
- Serial.print("Connecting to WiFi...");
- WiFi.begin("your_SSID", "your_PASSWORD"); // Replace with your WiFi credentials
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("WiFi connected");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- }
- // Reconnect to MQTT broker
- void reconnect() {
- // Loop until reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
- Serial.println("connected");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- delay(5000);
- }
- }
- }
- void setup() {
- Serial.begin(115200);
- pinMode(BMP_MAX30100_INT_PIN_D2, INPUT_PULLUP);
- // Initialize WiFi
- setup_wifi();
- // Initialize MQTT
- client.setServer(mqtt_server, mqtt_port);
- // Initialize MAX30100 sensor
- if (!max30100Sensor.begin()) {
- Serial.println("Failed to initialize MAX30100 sensor");
- while (1);
- }
- max30100Sensor.setMode(MAX30100_MODE_SPO2_HR);
- max30100Sensor.setLedsCurrent(MAX30100_LED_CURR_50MA, MAX30100_LED_CURR_50MA);
- max30100Sensor.setOnBeatDetectedCallback(onBeatDetected);
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- max30100Sensor.update();
- uint16_t ir, red;
- if (max30100Sensor.getRawValues(&ir, &red)) {
- // Read SpO2 and Heart Rate
- uint16_t currentSpO2 = max30100Sensor.getSpO2();
- uint16_t currentHR = max30100Sensor.getHeartRate();
- // Check if new data is different from last sent data
- if (currentSpO2 != lastSpO2 || currentHR != lastHeartRate) {
- lastSpO2 = currentSpO2;
- lastHeartRate = currentHR;
- // Prepare JSON payload
- String payload = "{";
- payload += "\"SpO2\": " + String(currentSpO2) + ",";
- payload += "\"HeartRate\": " + String(currentHR);
- payload += "}";
- // Publish to MQTT topic
- if (client.publish("sensor/heart_rate_spo2", payload.c_str())) {
- Serial.println("Data sent: " + payload);
- } else {
- Serial.println("Failed to send data");
- }
- }
- }
- delay(100); // Adjust delay as needed
- }
Advertisement
Add Comment
Please, Sign In to add comment