#include <WiFi.h>
#include <HTTPClient.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
// Configurações de Wi-Fi
const char* ssid = "WIFI";
const char* password = "SENHA";
// URL do servidor para onde os dados de ECG serão enviados
const char* serverUrl = "http://";
// Pinagem do sensor de ECG
const int ecgPin = 34; // Pino analógico para leitura do ECG
// Função de conexão ao Wi-Fi
void connectToWiFi() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Conectando ao WiFi...");
}
Serial.println("Conectado ao WiFi");
}
// Função para ler dados do ECG (simulada com leitura analógica)
int readECG() {
int ecgValue = analogRead(ecgPin);
return ecgValue;
}
// Tarefa para ler o ECG
void taskReadECG(void* pvParameters) {
while (true) {
int ecgData = readECG();
Serial.print("ECG Data: ");
Serial.println(ecgData);
vTaskDelay(334 / portTICK_PERIOD_MS); // Lê o dado a cada meio 1 segundo
}
}
// Tarefa para enviar os dados do ECG para o servidor
void taskSendECG(void* pvParameters) {
while (true) {
int ecgData = readECG();
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(serverUrl);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Formato dos dados a serem enviados
String httpRequestData = "ecgValue=" + String(ecgData);
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
String response = http.getString(); // Resposta do servidor
Serial.println("Resposta do Servidor: " + response);
} else {
Serial.println("Erro na requisição: " + String(httpResponseCode));
}
http.end();
}
vTaskDelay(333 / portTICK_PERIOD_MS); // Aproximadamente 3 requisições por segundo
}
}
void setup() {
Serial.begin(115200);
// Conectar ao Wi-Fi
connectToWiFi();
// Criar as tarefas do FreeRTOS
xTaskCreate(taskReadECG, "Ler ECG", 1000, NULL, 1, NULL);
xTaskCreate(taskSendECG, "Enviar ECG", 10000, NULL, 1, NULL);
}
void loop() {
// O FreeRTOS gerencia as tarefas, não é necessário código no loop principal
}