/* (depreciado com nova versão do wokwi)
Hardware do projeto Defensive Health,
que visa medir situação de pulso e oxigenação de uma pessoa,
armazenar esses dados e enviar avisos caso essa pessoa esteja
passando por uma situação de risco
pdf explicativo:
https://docs.google.com/document/d/1LpQFHBEM2rwWdxiTHo5UR7B46MQKLkaL/edit?usp=sharing&ouid=108401900231564622152&rtpof=true&sd=true
video explicativo:
https://drive.google.com/file/d/1HFZbuYIrJh8qL5WVE-frcDzJkAHkrSAo/view?usp=drive_link
*/
// blynk
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL2R1Gu2FYT"
#define BLYNK_TEMPLATE_NAME "teste"
#define BLYNK_AUTH_TOKEN "6CBwu3r4OyKA64oMdi1b0ehVNEOrWVpq"
/////////////////
byte bpmLed = 17;
byte tempoParaEnvioDeDados = 100;
/////////////////////////////////
#include <U8g2lib.h>
#include <Wire.h>
#include "MPU6050.h"
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// oled
#define fontName u8g2_font_7x13_mf
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE, 10, 1);
int16_t gyroX, gyroY, gyroZ;
// oxímetro de pulso (simulado pelo sensor MPU6050)
MPU6050 mpu;
byte bpm;
byte ox;
// wifi
WiFiClient cliente;
String apiKey = "ZBDNBC3I54OWW3DP";
String request_str;
unsigned long tempo = 0;
//led
const byte led = 3; // led simulando batimentos cardíacos
void setup()
{
Serial.begin(115200);
while (!Serial)
;
Serial.println("Serial inicializado.");
// led (simulando visualmente a pulsação do coração)
pinMode(3, OUTPUT);
// iniciando wire lib
if (!Wire.begin((int)10, 1))
{
Serial.println("Falha ao inicializar a biblioteca Wire");
}
Serial.println("Wire inicializado.");
// iniciando oled
if (!u8g2.begin())
{
Serial.println("Falha ao conectar-se ao display");
}
u8g2.setFont(fontName);
Serial.println("Display inicializado.");
// iniciando oxímetro do pulso (simulado pelo MPU)
mpu.initialize();
if (!mpu.testConnection()) {
Serial.println("Falha ao conectar-se ao MPU6050");
}
Serial.println("Sensor inicializado.");
// iniciando a conexão wifi
WiFi.disconnect();
delay(3000);
Serial.println("Conectando Esp à internet");
WiFi.begin("Wokwi-GUEST", "", 6);
while ((!(WiFi.status() == WL_CONNECTED)))
{
delay(500);
Serial.print(".");
}
Serial.println("Conectado");
// iniciando a biblioteca Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, "Wokwi-GUEST", "");
Serial.println("Blynk Conectado");
}
void loop()
{
sensor();
display();
thingSpeak();
blynk();
pulso();
delay(1);
tempo++;
}
// função responsável por atualizar o display
void display()
{
u8g2.firstPage();
do
{
u8g2.drawStr(/*x*/ 38, /*y*/ 20, /*str*/ String(bpm).c_str());
u8g2.drawStr(/*x*/ 68, /*y*/ 20, /*str*/ "BPM");
u8g2.drawStr(/*x*/ 35, /*y*/ 45, /*str*/ (String(ox) + "%").c_str());
u8g2.drawStr(/*x*/ 65, /*y*/ 45, /*str*/ "SpO2");
} while (u8g2.nextPage());
u8g2.setContrast(255);
}
// função que recebe os valores do sensor (simulado pelo MPU)
void sensor()
{
mpu.getRotation(&gyroX, &gyroY, &gyroZ);
bpm = map(gyroX, -32750, 32750, 30, 170); // valores arbitrários de bpm
ox = map(gyroY, -32750, 32750, 0, 100); // valores arbitrários de SpO2
}
// função que envia os valores ao servidor do ThingSpeak
void thingSpeak()
{
if (tempo % tempoParaEnvioDeDados == 0) // em tempo real, o thingspeak
{
if (cliente.connect("api.thingspeak.com", 80))
{
request_str = apiKey;
request_str += "&field1=";
request_str += bpm;
request_str += "&field2=";
request_str += ox;
cliente.print("POST /update HTTP/1.1\n");
cliente.print("Host: api.thingspeak.com\n");
cliente.print("Connection: close\n");
cliente.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
cliente.print("Content-Type: application/x-www-form-urlencoded\n");
cliente.print("Content-Length: ");
cliente.print(request_str.length());
cliente.print("\n\n");
cliente.print(request_str);
Serial.println("Dados enviados com sucesso");
}
else
{
Serial.println("Falha ao enviar dados.");
delay(500);
tempo += 15999;
}
}
}
// função que simula pulsação com um led
void pulso() // função que simula pulsação
{
if (tempo % (bpmLed - (bpm - 20) / 10) == 0)
{
digitalWrite(led, HIGH);
}
else if (tempo % (bpmLed - (bpm - 20) / 10) == 1)
{
digitalWrite(led, LOW);
}
}
// função que envia os dados ao Blynk
void blynk()
{
Blynk.run();
Blynk.virtualWrite(V5, bpm);
Blynk.virtualWrite(V0, ox);
}
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1