Hello there! I'm Stephanie
Im working on a project with an Arduino Uno. I have connected the following to my Ardunio:
- LED that lights up whenever the BPM < 150 is recorded.
- ESP8266-01 WiFi module.
- Pulse Rate sensor.
I want the values recorded by the Pulse rate sensor, to be displayed on my Node.js local web server through the ESP8266-01 wifi module and have the option to save the displayed data into a CSV. and example view can be the following:
Here are my codes:
Arduino:
#define USE_ARDUINO_INTERRUPTS true
#include <PulseSensorPlayground.h>
#include <WiFiEsp.h>
#include <SoftwareSerial.h>
#define DEBUG true
WiFiEspClient client;
const char ssid[] PROGMEM = "ssid";
const char password[] PROGMEM = "password";
// Replace with your server details
const char IP[] PROGMEM = "serverip";
const String serverURL = "/MyNodeApp/"; // Replace with your actual WebSocket endpoint
PulseSensorPlayground pulseSensor;
SoftwareSerial esp8266(10, 11);
const int LightBulbPin = 12;
const int LED13 = 13;
int Threshold = 550;
int myBPM;
char BPM[10];
int error;
unsigned long previousMillis = 0;
const unsigned long interval = 10000;
void connectWiFi() {
esp8266.println(F("AT+CWMODE=1"));
delay(2000);
char cmd[50];
snprintf_P(cmd, sizeof(cmd), PSTR("AT+CWJAP=\"%s\",\"%s\""), ssid, password);
esp8266.println(cmd);
delay(5000);
if (!esp8266.find("OK")) {
Serial.println("Failed to connect to WiFi");
// Add any error handling or retry logic as needed
}
}
void sendDataToServer() {
String postData = "field1=" + String(BPM);
Serial.println("Sending data to server: " + postData);
client.print("POST ");
client.print(serverURL);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(IP);
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(postData.length());
client.println();
client.print(postData);
}
void setup() {
Serial.begin(9600);
esp8266.begin(115200);
pulseSensor.analogInput(A0);
pulseSensor.blinkOnPulse(LED13);
pulseSensor.setThreshold(Threshold);
pinMode(LightBulbPin, OUTPUT);
if (pulseSensor.begin()) {
Serial.println("Pulse Sensor Initialized");
}
if (esp8266.find("OK")) {
connectWiFi();
}
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
getReadings();
sendDataToServer();
previousMillis = currentMillis;
}
// Check if BPM is less than 150
if (myBPM < 150) {
digitalWrite(LightBulbPin, HIGH); // Turn on the LED
} else {
digitalWrite(LightBulbPin, LOW); // Turn off the LED
}
// Check and handle errors
if (error == 1) {
Serial.println("Error: " + error);
}
}
void getReadings() {
int newBPM = pulseSensor.getBeatsPerMinute();
if (pulseSensor.sawStartOfBeat()) {
myBPM = newBPM;
// Convert integer BPM to string and update the BPM array
snprintf(BPM, sizeof(BPM), "%d", myBPM);
Serial.println(BPM);
}
}
and this is my server.js code:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const PORT = 3000;
// Use body-parser middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Handle a POST request
app.post('/example', (req, res) => {
const formData = req.body; // Access the parsed data
console.log('Received data:', formData);
res.send('Data received successfully');
});
app.listen(PORT, () => {
console.log(`Server running at serverip:${PORT}/`);
});
Please I would really appreciate any help on this!