pulse rate enable interrupt caused esp timeout
1 view (last 30 days)
Show older comments
Muhammad Azri Danial Bin Mohamad Zambri
on 18 Apr 2021
Commented: Christopher Stapels
on 19 Apr 2021
#define USE_ARDUINO_INTERRUPTS true
#define DEBUG true
#include "WiFiEsp.h"
#include "secrets.h"
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
#include "ThingSpeak.h" // always include thingspeak header file after other header files and custom macros
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiEspClient client;
// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(10, 11); // RX, TX
#define ESP_BAUDRATE 19200
#else
#define ESP_BAUDRATE 115200
#endif
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
PulseSensorPlayground pulseSensor;
const int PulseWire = A0; // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
const int LED13 = 13; // The on-board Arduino LED, close to PIN 13.
int Threshold = 550;
float myTemp;
int myBPM;
String BPM;
String temp;
int error;
int panic;
int raw_myTemp;
float Voltage;
float tempC;
void setup() {
Serial.begin(115200); //Initialize serial
pulseSensor.analogInput(PulseWire);
pulseSensor.blinkOnPulse(LED13); //blink Arduino's LED with heartbeat.
pulseSensor.setThreshold(Threshold);
// Double-check the "pulseSensor" object was created and "began" seeing a signal.
if (pulseSensor.begin()) {
Serial.println("!!!System Start!!!"); //This prints one time at Arduino power-up, or on Arduino reset.
}
while(!Serial){
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
// initialize serial for ESP module
setEspBaudRate(ESP_BAUDRATE);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
Serial.print("Searching for ESP8266...");
// initialize ESP module
WiFi.init(&Serial1);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
Serial.println("found it!");
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
// Connect or reconnect to WiFi
if(WiFi.status() != WL_CONNECTED){
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while(WiFi.status() != WL_CONNECTED){
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
get_pulseSensor();
get_tempSensor();
delay(5000); // Wait 5 seconds to update the channel again
}
void get_pulseSensor(){
int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int".
// "myBPM" hold this BPM value now.
if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if "a beat happened".
Serial.print("BPM: ");
Serial.println(myBPM); // Print the value inside of myBPM.
} else
{
Serial.println("BPM not detected");
}
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 1.
int x = ThingSpeak.writeField(myChannelNumber, 1, myBPM, myWriteAPIKey);
if(x == 200){
Serial.println("Pulse Sensor update successful.");
}
else{
Serial.println("Problem updating Pulse Sensor. HTTP error code " + String(x));
}
}
void get_tempSensor(){
raw_myTemp = analogRead(A1);
Voltage = (raw_myTemp / 1023.0) * 5000; // 5000 to get millivots.
tempC = Voltage * 0.1;
myTemp = tempC;
String tempMessage = "Temperature: ";
tempMessage += myTemp;
tempMessage += "\" Celcius";
Serial.println(tempMessage);
// Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
// pieces of information in a channel. Here, we write to field 2.
int x = ThingSpeak.writeField(myChannelNumber, 2, myTemp, myWriteAPIKey);
if(x == 200){
Serial.println("Temperature Sensor update successful.");
}
else{
Serial.println("Problem updating Temperature Sensor. HTTP error code " + String(x));
}
}
// This function attempts to set the ESP8266 baudrate. Boards with additional hardware serial ports
// can use 115200, otherwise software serial is limited to 19200.
void setEspBaudRate(unsigned long baudrate){
long rates[6] = {115200,74880,57600,38400,19200,9600};
Serial.print("Setting ESP8266 baudrate to ");
Serial.print(baudrate);
Serial.println("...");
for(int i = 0; i < 6; i++){
Serial1.begin(rates[i]);
delay(100);
Serial1.print("AT+UART_DEF=");
Serial1.print(baudrate);
Serial1.print(",8,1,0,0\r\n");
delay(100);
}
Serial1.begin(baudrate);
}
0 Comments
Answers (1)
Vinod
on 18 Apr 2021
First, unless you have a paid ThingSpeak license, trying to update your channel every 5 seconds is a waste of resources. In fact, continued abuse of the service can result in your channel getting locked out from updates.
Next, if you used arduino interrupts, how do you ensure you're not interrupting the networking stack? Especially a problem if your sensor makes frequent interrupts and your interrupt routine takes long to execute.
Communities
More Answers in the ThingSpeak Community
See Also
Categories
Find more on Run on Target Hardware in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!