#include <WiFi.h>
#include "ThingSpeak.h"
#include <LiquidCrystal_I2C.h>
#define LDR_PIN 2
// esp32 wifi parameters
const char* WIFI_NAME = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
const int myChannelNumber = 2306904; //thingspeak channel ID
const char* myApiKey = "BHFBH6PI4TX0NGTE"; //write API key from ThingSpeak
const char* server = "api.thingspeak.com";
const int photoresistorPin = A0;
//Simulate HR and SPO2 parameters
const float calibrationFactor = 100.0; // Your calibration factor (adjust as needed)
WiFiClient client;
LiquidCrystal_I2C lcd(0x27,20,4); //setting LCD address
void setup() {
// put your setup code here, to run once:
pinMode(LDR_PIN, INPUT);
lcd.init();
lcd.backlight();
//esp32 module code
WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED){
delay(1000);
Serial.println("Wifi not connected");
}
Serial.println("Wifi connected !");
Serial.println("Local IP: " + String(WiFi.localIP()));
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop() {
// put your main code here, to run repeatedly:
//Code to simulate HR and SPO2 using lightlevel and lux of photoresistor
int lightLevel = analogRead(photoresistorPin);
// Convert the analog value to lux using your calibration factor
float lux = lightLevel / calibrationFactor;
// Convert lux to simulate HR and SPO2
int spo2 = lux * 2.8;
int hr = lux*2.2;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SPO2:" + String(spo2) + "%"); //Display lux as spo2
lcd.setCursor(0, 1);
lcd.print("HR: " + String(hr)); // Display lux as hr
delay(1000);
//Data sent to thingspeak
ThingSpeak.setField(1, hr);
ThingSpeak.setField(2, spo2);
int x = ThingSpeak.writeFields(myChannelNumber, myApiKey);
Serial.println(String(hr, 1));
if(x == 200){
Serial.println("Data pushed successfull");
}else{
Serial.println("Push error" + String(x));
}
Serial.println(String(spo2, 2));
if(x == 200){
Serial.println("Data pushed successfull");
}else{
Serial.println("Push error" + String(x));
}
Serial.println("---");
delay(10000);
}