Need help with AT commands using Esp8266-01S and Arduino Uno

#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

test it without all the other code

My guess (and it is only a guess because you give so little information, please read the forum guide) is that this is a power supply issue. As soon as the esp attempts to connect to wi-fi, the current it consumes will increase and this is causing the supply voltage to drop and the Arduino to reset.

It works alone but isn't working with the other code

Thanks for the reply.
So are you saying that if I connect my esp8266-01 to a constant external power supply of 3.3 V it should work?
But if that is the case then why does the esp8266-01 when tested alone works but the issue occurs when I connect the 3 other sensors with it.(Accelerometer- ADXL345, Microphone - Max4466, GY-Max30100)

That might fix the problem, but it is not what I said.

I don't know.

Did you follow my suggestion yet? (Rhetorical question)

The command you send is never receiving a response, that is 1 issue.
You do not include the line ending that should be 'both LF & CR', so change (within sendCommand() )

esp8266.println(command);//at+cipsend

to

esp8266.print(command);
esp8266.print("\n\r");

That should at least send the command properly.
Now you also don't give the ESP time to respond, in fact you don't even wait until the command has been fully send, so before you start test for a response add

  esp8266.flush();
  delay(10); // 10 ms should be enough time for a response to be returned
 if(esp8266.find(readReplay))//ok
    {
      found = true;
      break;
    }  
    countTimeCommand++;
  }

after all this, you should clear the input buffer

while(esp8266.available()) char c = esp8266.read();

Only then should you start with the whole i2C thing.

As the ESP-01 is not actually getting properly formatted commands, this is not what is happening, but it may in the future once you are sending proper commands.

It shouldn't work with the code you posted, The program may run, because there is not conflict on the interrupts. swSerial has not completed it's process. The I2c communication is using some resource that is also used by swSerial. I am not to familiar with I2C, i've never used it, but there appears to be some conflict.

while(esp8266.available()) char c = esp8266.read();
Could you please clarify where this should be written?

Just FYI as I didn't mention it earlier
I am using 2 arduino uno's
One is connected to the pulse oximeter sensor and the other arduino is connected to the other 2 sensors and the esp8266-01
The arduino uno connected to the pulse oximeter transmits to the arduino connected to the rest of the sensors.

Two I2C devices and one using an analogue input.
Wouldn't it have made more sense to use a WeMos D1 mini or NodeMCU
instead of a kludge with an Uno and an ESP-01.
Leo..

I would put it here

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++;
  }
  while(esp8266.available()) char c = esp8266.read();

You do not send anything to, nor test the response thereafter, so time to clear the buffer.
If the buffer is full, new data is simply discarded.

Why?

Just - why? :crazy_face:

Because the Max30100 has a pox.update() function which needs to be run every 10 ms. So it doesn't work if I connect all the sensors to a single arduino.

Okay. Thanks I'll try this out.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.