Wifi disconnects and connects again and again while using esp8266, max30100 and oled display

24 Views Asked by At

This is my code , It basically ususe an esp8266, max30100 and an oled display . The max30100 is used to detect the spo2 and hear beat of the person and shows the output in the oled display and also has an inbuilt local webserver in which the output will be shown. This was working properly before a month but now the esp connects to the wifi and disconnect again and again. What could be the issue here ?

#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>

#define REPORTING_PERIOD_MS 1000
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

float BPM, SpO2;
uint32_t tsLastReport = 0;

PulseOximeter pox;

const char *ssid = "Testing";
const char *password = "zxcvbnm";

AsyncWebServer server(80);

void setup() {
    Serial.begin(115200);

    // Connect to Wi-Fi
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi...");
    }
    Serial.println("Connected to WiFi");
    Serial.print("IP Address: ");
    Serial.println(WiFi.localIP());

    // Initialize OLED display
    if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
        Serial.println(F("SSD1306 allocation failed"));
        for (;;)
            ;
    }
    // Initialize MAX30100 sensor
    Serial.print("Initializing Pulse Oximeter..");
    if (!pox.begin()) {
        Serial.println("FAILED");
        display.clearDisplay();
        display.setTextSize(1);
        display.setTextColor(SSD1306_WHITE);
        display.setCursor(0, 0);
        display.println("FAILED");
        display.display();
        for (;;)
            ;
    } else {
        display.clearDisplay();
        display.setTextSize(1);
        display.setTextColor(SSD1306_WHITE);
        display.setCursor(0, 0);
        display.println("SUCCESS");
        display.display();
        Serial.println("SUCCESS");
    }

    // Web Server Routes
    server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
        // Generate HTML response
        pox.update();
        float bpm = pox.getHeartRate();
        float spo2 = pox.getSpO2();

        // Check if readings are valid
        bool validReading = isValidReading(bpm, spo2);

        String html = "<!DOCTYPE html>
              **HTML CODE GOES HERE**

        if (validReading) {
            html += "<div class='data BPM'><i class='fas fa-heartbeat icon'></i><div class='reading' id='BPM'>" + String(bpm) + "<span class='superscript'>BPM</span></div></div><div class='data SpO2'><i class='fas fa-lungs icon'></i><div class='reading' id='SpO2'>" + String(spo2) + "<span class='superscript'>%</span></div></div>";
        } else {
            html += "<div class='data invalid'><p>Invalid readings. Wear your band properly.</p></div>";
        }
        html += "</body></html>";

        request->send(200, "text/html", html);
    });

    server.on("/data", HTTP_GET, [](AsyncWebServerRequest *request) {
        // Retrieve latest sensor data
        pox.update();
        float bpm = pox.getHeartRate();
        float spo2 = pox.getSpO2();

        // Create a JSON response
        String json = "{\"bpm\":" + String(bpm) + ",\"spo2\":" + String(spo2) + "}";
        request->send(200, "application/json", json);
    });

    server.begin();
}

void loop() {
    // Update sensor data
    pox.update();

    BPM = pox.getHeartRate();
    SpO2 = pox.getSpO2();

    if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
        // Update display
        display.clearDisplay();
        display.setTextSize(1);
        display.setTextColor(SSD1306_WHITE);

        if (isValidReading(BPM, SpO2)) {
            display.setCursor(0, 16);
            display.println("BPM: " + String(BPM) + " BPM");

            display.setCursor(0, 30);
            display.println("SpO2: " + String(SpO2) + "%");
        } else {
            display.setCursor(0, 16);
            display.println("Invalid readings. Wear your band properly");
        }

        display.display();
        tsLastReport = millis();
    }
}

bool isValidReading(float bpm, float spo2) {
    return (bpm > 50 && spo2 > 90 && bpm < 150 && spo2 <= 100);
}

** I've tried a lot of changes to the program and the connection in the Arduino ide too but still i cant sort out the issue. Can someone sort the issue or suggest me any changes in the program.**

0

There are 0 best solutions below