Hello,
I am asking for help on a school project that I am rushing for. It is a project for heartbeat monitoring system on IoT platform, Thingspeak.
The code and program works well without the ESP8266 WiFi module.
However, after I added the code and module on my board, the original code wouldn't work and the LCD would just freeze on the "Heart Beat Sensor" output.
Have tried many ways. Would appreciate alot if someone can help me. Thank you very much
Attached is the hardware wiring that I followed. (https://community.createlabz.com/knowledgebase/heart-rate-monitoring-system-using-heart-rate-pulse-sensor-with-i2c-1602-lcd/)
with ESP8266 connected according to its pins.
My Code thus far:
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <millisDelay.h>
#include <Wire.h>
#include <SPI.h>
#define RX 12
#define TX 11
String AP = "xxxxxxx"; // CHANGE ME
String PASS = "xxxxxx"; // CHANGE ME
String API = "xxxxxxxx"; // CHANGE ME
String HOST = "api.thingspeak.com";
String PORT = "80";
String field = "field1";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
//boolean pulseFound = false;
int valSensor = 1;
//int finalbpm = 0;
SoftwareSerial esp8266(RX,TX);
millisDelay timerDelay;
int pulsesensor = 0;
int counter = 0;
int bpm = 0;
int test = 0;
int rate = 0;
int r1,r2,r3,r4,r5 = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void sendCommand(String command, int maxTime, char readReplay[]) {
Serial.print(countTrueCommand);
Serial.print(". at command => ");
Serial.print(command);
Serial.print(" ");
while(countTimeCommand < (maxTime*1))
{
esp8266.println(command);//at+cipsend
if(esp8266.find(readReplay))//ok
{
Serial.print("true");
found = true;
break;
}
countTimeCommand++;
}
if(found == true)
{
Serial.println("OYI");
countTrueCommand++;
countTimeCommand = 0;
}
if(found == false)
{
Serial.println("Fail");
countTrueCommand = 0;
countTimeCommand = 0;
}
found = false;
}
void pulse(){
if (pulsesensor < 70){
r1 = 1;
r2 = 1;
r3 = 1;
r4 = 1;
r5 = 1;
lcd.setCursor(0, 0);
lcd.print("Heart Rate:");
lcd.setCursor(2, 1);
lcd.print("Please Wait");
delay(6000);
test = 1;
timerDelay.start(15000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Heart Rate:");
lcd.setCursor(2, 1);
lcd.print("Scanning");
}
if (test == 1) {
if (pulsesensor == 540 && r1 == 1)
{
r2 = 0;
r3 = 0;
r4 = 0;
r5 = 0;
counter++;
delay(500);
Serial.print("counter:");
Serial.println(counter);
}
else if (pulsesensor == 530 && r2 == 1)
{
counter++;
delay(500);
Serial.print("counter:");
Serial.println(counter);
r1 = 0;
r3 = 0;
r4 = 0;
r5 = 0;
}
else if (pulsesensor == 520 && r3 == 1)
{
counter++;
delay(500);
Serial.print("counter:");
Serial.println(counter);
r2 = 0;
r1 = 0;
r4 = 0;
r5 = 0;
}
else if (pulsesensor == 512 && r4 == 1)
{
counter++;
delay(500);
Serial.print("counter:");
Serial.println(counter);
r2 = 0;
r3 = 0;
r1 = 0;
r5 = 0;
}
else if (pulsesensor == 500 && r5 == 1)
{
counter++;
delay(500);
Serial.print("counter:");
Serial.println(counter);
r2 = 0;
r3 = 0;
r4 = 0;
r1 = 0;
}
if (timerDelay.justFinished())
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Heart Rate:");
lcd.setCursor(2, 1);
lcd.print("Remove");
bpm = counter * 8;
rate = 1;
test = 0;
counter = 0;
Serial.println("bpm:");
Serial.println(bpm);
//finalbpm = bpm;
delay(5000);
lcd.clear();
}
}
if (rate == 1)
{
if (bpm > 60 && bpm < 100 || bpm == 60 || bpm == 100)
{
normal();
rate = 0;
delay(5000);
//pulseFound = true;
bpm = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Heart Rate:");
}
else if (bpm < 60)
{
slow();
rate = 0;
delay(5000);
//pulseFound = true;
bpm = 0;
lcd.setCursor(0, 0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Heart Rate:");
}
else if (bpm > 100)
{
fast();
rate = 0;
delay(5000);
//pulseFound = true;
bpm = 0;
lcd.setCursor(0, 0);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Heart Rate:");
}
}
}
void slow()
{
lcd.setCursor(0, 0);
lcd.print("Heart Rate:");
lcd.setCursor(2, 1);
lcd.print("Slow");
lcd.setCursor(9, 1);
lcd.print("BPM:");
lcd.setCursor(13, 1);
lcd.print(bpm);
}
void normal()
{
lcd.setCursor(0, 0);
lcd.print("Heart Rate:");
lcd.setCursor(2, 1);
lcd.print("normal");
lcd.setCursor(9, 1);
lcd.print("BPM:");
lcd.setCursor(13, 1);
lcd.print(bpm);
}
void fast()
{
lcd.setCursor(0, 0);
lcd.print("Heart Rate:");
lcd.setCursor(2, 1);
lcd.print("Fast");
lcd.setCursor(9, 1);
lcd.print("BPM:");
lcd.setCursor(13, 1);
lcd.print(bpm);
}
void wifi(){
valSensor = getSensorData();
String getData = "GET /update?api_key="+ API +"&"+ field +"="+String(valSensor);
sendCommand("AT+CIPMUX=1",5,"OK");
sendCommand("AT+CIPSTART=0,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK");
sendCommand("AT+CIPSEND=0," +String(getData.length()+4),4,">");
esp8266.println(getData);delay(1500);countTrueCommand++;
sendCommand("AT+CIPCLOSE=0",5,"OK");
}
int getSensorData(){
return bpm;
}
void setup() {
Serial.begin(9600);
lcd.begin();
lcd.setCursor(0, 0);
lcd.print("Heart Beat");
lcd.setCursor(2, 1);
lcd.print("Sensor");
lcd.backlight();
delay(40);
esp8266.begin(115200);
sendCommand("AT",5,"OK");
sendCommand("AT+CWMODE=1",5,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK");
}
void loop() {
pulsesensor = analogRead(A0);
pulse();
Serial.println(pulsesensor);
delay(1000);
wifi();
delay(100);
}