Hi All,
I'm exploring and working on an arduino IOT-based project that reads values from sensors (pulse rate sensor and LM35DZ) then send the values to Thingspeak channel then get the values from Thingspeak and post these values to Google Sheet using IFTTT
The idea behind this project is as follow:
- Connect both sensors to Arduino Uno R3
- Get readings from pulse rate and LM35DZ sensor
- Establish a wireless connection using ESP8266 ESP-01S
- Connect to Thingspeak channel using write API key then send sensors' readings to Thingspeak channnel every 30 seconds
- Write sensors' readings from Thingspeak Channel to Google Sheet using IFTTT
As of now, everything is working as expected, except the STEP 4 and consequently STEP 5.
Observing the serial monitor, the sensors do get the values but sending them to Thingspeak channel is a hit or miss.
Snippet from serial monitor:
19:00:11.683 -> Temp is:
19:00:11.716 -> 25.42
19:00:11.716 -> BPM is:
19:00:11.716 -> 204
19:00:12.708 -> AT+CIPSTART="TCP","184.106.153.149",80
19:00:15.718 -> AT+CIPSEND=59
19:00:17.739 -> GET /update?key=E4GF8L6XWGZ947Q3&field1=204.0&field2=25.4
19:00:41.793 -> Temp is:
19:00:41.793 -> 19.06
19:00:41.793 -> BPM is:
19:00:41.793 -> 138
19:00:42.806 -> AT+CIPSTART="TCP","184.106.153.149",80
19:00:45.826 -> AT+CIPSEND=59
19:00:48.823 -> AT+CIPCLOSE
19:01:12.845 -> Temp is:
19:01:12.845 -> 16.62
19:01:12.887 -> BPM is:
19:01:12.887 -> 100
19:01:13.864 -> AT+CIPSTART="TCP","184.106.153.149",80
19:01:16.863 -> AT+CIPSEND=59
19:01:19.886 -> AT+CIPCLOSE
19:01:43.927 -> Temp is:
19:01:43.927 -> 23.95
19:01:43.927 -> BPM is:
19:01:43.927 -> 217
19:01:44.954 -> AT+CIPSTART="TCP","184.106.153.149",80
19:01:47.949 -> AT+CIPSEND=59
19:01:49.944 -> GET /update?key=E4GF8L6XWGZ947Q3&field1=217.0&field2=23.9
19:02:13.977 -> Temp is:
19:02:14.013 -> 23.95
19:02:14.013 -> BPM is:
19:02:14.013 -> 168
19:02:15.032 -> AT+CIPSTART="TCP","184.106.153.149",80
19:02:18.035 -> AT+CIPSEND=59
19:02:21.055 -> AT+CIPCLOSE
19:02:45.062 -> Temp is:
19:02:45.062 -> 16.62
19:02:45.098 -> BPM is:
19:02:45.098 -> 211
19:02:46.107 -> AT+CIPSTART="TCP","184.106.153.149",80
19:02:49.079 -> AT+CIPSEND=59
19:02:52.085 -> AT+CIPCLOSE
Green means the data was successfully captured, sent to Thingspeak, and posted on Google Sheet using IFTTT
Red Means the data was successfully captured but wasn't sent to thinkspeak, and consequently did not get posted on Google Sheet.
Below is a diagram of Arduino connection:
This is my code:
#define USE_ARDUINO_INTERRUPTS true
#define DEBUG true
#define SSID "XXXXXXXX" // "SSID-WiFiname"
#define PASS "XXXXXXXX" // "password"
#define IP "184.106.153.149" // thingspeak.com ip
#include <SoftwareSerial.h>
#include "Timer.h"
#include <PulseSensorPlayground.h> // Includes the PulseSensorPlayground Library.
Timer t;
PulseSensorPlayground pulseSensor;
String msg = "GET /update?key=XXXXXXXXXXXX";
SoftwareSerial esp8266(10, 11);
//Variables
const int PulseWire = A0; // PulseSensor PURPLE WIRE connected to ANALOG PIN 0
int Threshold = 550; //for heart rate sensor
float myTemp;
int myBPM;
String BPM;
String temp;
int error;
int raw_myTemp;
float Voltage;
float tempC;
void setup()
{
Serial.begin(9600);
esp8266.begin(115200);
pulseSensor.analogInput(PulseWire);
pulseSensor.setThreshold(Threshold);
// Double-check the "pulseSensor" object was created and "began" seeing a signal.
if (pulseSensor.begin()) {
Serial.println("We created a pulseSensor Object !"); //This prints one time at Arduino power-up, or on Arduino reset.
}
Serial.println("AT");
esp8266.println("AT");
delay(3000);
if (esp8266.find("OK"))
{
connectWiFi();
}
t.every(30000, getReadings);
t.every(30000, updateInfo);
}
void loop()
{
start: //label
error = 0;
t.update();
//Resend if transmission is not completed
if (error == 1)
{
goto start; //go to label "start"
}
delay(4000);
}
void updateInfo()
{
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
delay(1000); //added
Serial.println(cmd);
esp8266.println(cmd);
delay(2000);
if (esp8266.find("Error"))
{
return;
}
cmd = msg ;
cmd += "&field1="; //field 1 for BPM
cmd += BPM;
cmd += "&field2="; //field 2 for temperature
cmd += temp;
cmd += "\r\n";
Serial.print("AT+CIPSEND=");
esp8266.print("AT+CIPSEND=");
delay(1000); //added
Serial.println(cmd.length());
esp8266.println(cmd.length());
if (esp8266.find(">"))
{
delay(1000); //added
Serial.print(cmd);
esp8266.print(cmd);
}
else
{
delay(1000); //added
Serial.println("AT+CIPCLOSE");
esp8266.println("AT+CIPCLOSE");
//Resend...
error = 1;
}
}
boolean connectWiFi()
{
Serial.println("AT+CWMODE=1");
esp8266.println("AT+CWMODE=1");
delay(2000);
String cmd = "AT+CWJAP=\"";
cmd += SSID;
cmd += "\",\"";
cmd += PASS;
cmd += "\"";
delay(1000); //added
Serial.println(cmd);
esp8266.println(cmd);
delay(5000);
if (esp8266.find("OK"))
{
return true;
}
else
{
return false;
}
}
void getReadings() {
raw_myTemp = analogRead(A1);
Voltage = (raw_myTemp / 1023.0) * 5000; // 5000 to get millivots.
tempC = Voltage * 0.1;
myTemp = tempC;
Serial.println("Temp is: ");
Serial.println(myTemp);
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.println("BPM is: ");
Serial.println(myBPM); // Print the value inside of myBPM.
}
delay(20);
char buffer1[10];
char buffer2[10];
BPM = dtostrf(myBPM, 4, 1, buffer1);
temp = dtostrf(myTemp, 4, 1, buffer2);
}
My question is, why are my data are randomly sent to thingspeak ?
If there is a major error in what i'm doing, it should not send ANYTHING at all to Thingspeak, instead i'm getting unreliable results.