#include <SoftwareSerial.h>
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#define RX 2
#define TX 3
#define REPORTING_PERIOD_MS 1000
String AP = "xxxxxxx"; // AP NAME
String PASS = "yyyyyyy"; // AP PASSWORD
String API = "zzzzzzzzzzzzzz"; // Write API KEY
String HOST = "api.thingspeak.com";
String PORT = "80";
//String field = "field1";
int countTrueCommand;
int countTimeCommand;
boolean found = false;
int valSensor = 1;
int ADXL345 = 0x53;
int X_out, Y_out, Z_out;
const int sampleWindow = 50;
unsigned int sample;
int counter=0;
SoftwareSerial esp8266(RX,TX);
void setup() {
Serial.begin(9600);
esp8266.begin(9600);
sendCommand("AT",2,"OK");
sendCommand("AT+CWMODE=1",2,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",10,"OK");
Wire.begin();
Wire.beginTransmission(ADXL345);
Wire.write(0x2D);
Wire.write(8);
Wire.endTransmission();
delay(10);
}
void loop() {
Serial.println("Entering Loop");
Wire.beginTransmission(ADXL345);
Wire.write(0x32);
Wire.endTransmission(false);
Wire.requestFrom(ADXL345, 6, true);
X_out = ( Wire.read() | Wire.read() << 8);
Y_out = ( Wire.read() | Wire.read() << 8);
Z_out = ( Wire.read() | Wire.read() << 8);
Serial.print(X_out);
Serial.print(" ");
Serial.print(Y_out);
Serial.print(" ");
Serial.println(Z_out);
//delay(100);
unsigned long startMillis= millis(); // Returns the number of milliseconds passed since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.
unsigned int peakToPeak = 0;
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
while (millis() - startMillis < sampleWindow)
{
sample = analogRead(A0); // Used to measure the voltage between 0 to 5 volts and converts it into a digital value between 0 to 1023. The reason for value 1023 is because each analog channel is a 10-bit ADC(Analog to Digital converter).
if (sample < 1024) // toss out spurious readings
{
if (sample > signalMax)
{
signalMax = sample; // To find the max sample
}
else if (sample < signalMin)
{
signalMin = sample; // To find the min sample
}
}
}
peakToPeak = signalMax - signalMin; // max - min = peak-peak amplitude
double volts = (peakToPeak * 5.0) / 1024; // convert to volts
Serial.print("loudness : ");
Serial.println(volts);
//delay(1000);
int array_[1000];
while(Serial.available())
{
//Serial.println(Serial.read());
int array_[1000];
array_[counter] = Serial.read();
// double heart_rate = array_[0];
// double spo_2 = array_[1];
if(counter==1)break;
counter = counter + 1;
}
double heart_rate = array_[0];
double spo_2 = array_[1];
delay(1000);
//valSensor = getSensorData();
String getData = "GET /update?api_key="+ API +"&"+ "field1" +"="+String(spo_2)+"&"+ "field2" +"="+String(heart_rate)+"&"+ "field3" +"="+String(X_out)+"&"+ "field4" +"="+String(Y_out)+"&"+ "field5" +"="+String(Z_out)+"&"+ "field6" +"="+String(volts);
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 random(1000); // Replace with your own sensor code
//}
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
{
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;
}
My Question:-
sendCommand("AT",2,"OK");
sendCommand("AT+CWMODE=1",2,"OK");
sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",10,"OK");
For some reason these AT commands are continuously running in a loop and no other code is being executed after these 3.
Any suggestions on how to resolve this issue will be very helpful.
Thank You
