#include #include #include #include #include #include #include "esp_log.h" static const BaseType_t app_cpu = 1; #define HTTP_PORT 80 // Define the ADC pin #define ADC_PIN 7 #define BUFFER_SIZE 5000 // Create a buffer to hold the analog readings float buffer[BUFFER_SIZE]; const char *WIFI_SSID = "SDNet"; const char *WIFI_PASS = "CapstoneProject"; AsyncWebServer server(HTTP_PORT); AsyncWebSocket ws("/ws"); TaskHandle_t analogDataTask_Handle; //Initialize On Board LED struct Led { // state variables uint8_t pin; bool on; // methods void update() { digitalWrite(pin, on ? HIGH : LOW); } }; Led onboard_led = { LED_BUILTIN, false }; //Initialize SPIFFS void initSPIFFS() { if (!SPIFFS.begin()) { Serial.println("Cannot mount SPIFFS volume..."); while (1) { onboard_led.on = millis() % 200 < 50; onboard_led.update(); } } } //Connect to Wifi void initWiFi() { WiFi.mode(WIFI_STA); WiFi.begin(WIFI_SSID, WIFI_PASS); Serial.printf("Trying to connect [%s] ", WiFi.macAddress().c_str()); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.printf(" %s\n", WiFi.localIP().toString().c_str()); } //Initialize Web Server String processor(const String &var) { return String(var == "STATE" && onboard_led.on ? "on" : "off"); } void onRootRequest(AsyncWebServerRequest *request) { request->send(SPIFFS, "/index.html", "text/html", false, processor); } void initWebServer() { server.on("/", onRootRequest); server.serveStatic("/", SPIFFS, "/"); server.begin(); } //Initialize Web Socket void notifyClients() { const uint8_t size = JSON_OBJECT_SIZE(1); StaticJsonDocument json; json["status"] = onboard_led.on ? "on" : "off"; char buffer[17]; size_t len = serializeJson(json, buffer); ws.textAll(buffer, len); } void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) { AwsFrameInfo *info = (AwsFrameInfo*)arg; if (info->final && info->index == 0 && info->len == len && info->opcode == WS_TEXT) { const uint8_t size = JSON_OBJECT_SIZE(1); StaticJsonDocument json; DeserializationError err = deserializeJson(json, data); if (err) { Serial.print(F("deserializeJson() failed with code ")); Serial.println(err.c_str()); return; } const char *action = json["action"]; if (strcmp(action, "toggle") == 0) { //led.on = !led.on; notifyClients(); } } } void onEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) { switch (type) { case WS_EVT_CONNECT: Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str()); break; case WS_EVT_DISCONNECT: Serial.printf("WebSocket client #%u disconnected\n", client->id()); break; case WS_EVT_DATA: handleWebSocketMessage(arg, data, len); break; case WS_EVT_PONG: case WS_EVT_ERROR: break; } } void initWebSocket() { ws.onEvent(onEvent); server.addHandler(&ws); } bool writeArrayToFile(float arr[], int size) { // Initialize SPIFFS if (!SPIFFS.begin()) { Serial.println("Failed to mount SPIFFS"); return false; } // Serialize array as JSON DynamicJsonDocument doc(160000); JsonArray array = doc.to(); for (int i = 0; i < size; i++) { array.add(arr[i]); } String jsonStr; serializeJson(array, jsonStr); // Write JSON string to file File file = SPIFFS.open("/temp.txt", "w"); if (!file) { Serial.println("Failed to open file for writing"); return false; } file.print(jsonStr); file.close(); return true; } void updateClients(void *parameters){ while(1){ ws.cleanupClients(); onboard_led.on = millis() % 1000 < 50; onboard_led.update(); } } // Define the task that collects analog data void analogDataTask(void *parameter) { int currentIndex = 0; int i; while(1) { // Read the analog value from the ADC uint16_t analogValue = analogRead(ADC_PIN); // Convert the analog value to a voltage float voltage = (analogValue / 4095.0) * 3.3; //Serial.println(voltage); buffer[currentIndex] = voltage; currentIndex++; if (currentIndex == BUFFER_SIZE){ Serial.println("Buffer full"); for(i = 0; i < BUFFER_SIZE; i++){ Serial.print((float)buffer[i]); Serial.print(" - "); Serial.println(i); } bool status = writeArrayToFile(buffer, BUFFER_SIZE); Serial.println("ADC Task has been suspended"); vTaskSuspend(analogDataTask_Handle); Serial.println("ERROR: This line should not be running right now"); vTaskDelay(10000 / portTICK_PERIOD_MS); currentIndex = 0; } // Wait for a short period of time to achieve a sampling frequency of about 44kHz vTaskDelay(1 / portTICK_PERIOD_MS); } } void writeIntToFile(int var){ // Create a DynamicJsonDocument object DynamicJsonDocument doc(1024); doc["value"] = var; // Serialize the DynamicJsonDocument object into a JSON string String jsonString; serializeJson(doc, jsonString); // Write JSON string to file File file = SPIFFS.open("/heartRate.json", "w"); if (!file) { Serial.println("Failed to open file for writing"); } file.print(jsonString); file.close(); } void writeIntToFile2(int var){ // Create a DynamicJsonDocument object DynamicJsonDocument doc(1024); doc["value"] = var; // Serialize the DynamicJsonDocument object into a JSON string String jsonString; serializeJson(doc, jsonString); // Write JSON string to file File file = SPIFFS.open("/SPO2.json", "w"); if (!file) { Serial.println("Failed to open file for writing"); } file.print(jsonString); file.close(); } void setup() { // put your setup code here, to run once: pinMode(onboard_led.pin, OUTPUT); delay(2000); Serial.begin(115200); initSPIFFS(); initWiFi(); initWebSocket(); initWebServer(); writeIntToFile(29); writeIntToFile2(30); xTaskCreatePinnedToCore(analogDataTask, "ECG-ADC", 8000, NULL, 2, &analogDataTask_Handle, app_cpu); xTaskCreatePinnedToCore(updateClients, "Update Wifi", 8000, NULL, 1, NULL, 0); vTaskDelete(NULL); } void loop() { // put your main code here, to run repeatedly: }