SIM800L and ESP32

Hello! I am doing a project which sends data upon request by sms. I have done everything else, the measurements are fine, sending sms is fine / When I send the command through Serial, but what I want is to filter the sms body and then accordingly send an sms with the data.
I can't get to receive the sms in the type I want it to be.
I tried several examples and they don't work on my system.

I am posting the code for you to look at and give advice - thanks in advance! :slight_smile:

#include <Sim800l.h>
#include <SoftwareSerial.h>

SoftwareSerial mySerial(25,26);
Sim800l simService;

// defines pins numbers
const int trigPin = 2;
const int echoPin = 4;

// defines variables
long duration;
float distance;


// Variables about container properties
int containerLength = 17;
int containerWidth = 8;
int emptyContainerHeight = 8;
int measuredContainerHeight;

// Variables about container volume
int emptyContainerVolume;
int measuredFreeSpace;

// Final Powder weight measured
int powderDensity = 917;
int measuredPowderWeight;
int fullContainerWeight;


int Content;
String StringForComparison;
char receivedChar;
String textSms,numberSms;

String stringToBeSent;
bool smsconfig = false;
String amarettoString = "amaretto";
String phoneNumber = "+359-----------"; //it's hidden for the forum post

// SIM800L Config Strings
String at = "AT";
String atconfig = "AT+CMGS=\"" + phoneNumber + "\"";
String atcnmi = "AT+CNMI=1,2,0,0,0";
String atcmgf = "AT+CMGF=1";

/* Errors possible:
 *  
 *  Error 123 - Distance read from ultrasonic sensor is more 
 *  than default distance between container's lid and container's bottom.
 *  
 */

char* text;
char* number;
bool error; //to catch the response of sendSms

void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  
  Serial.begin(9600);
  mySerial.begin(9600);
  
  simService.begin();
  simService.delAllSms();
  
  mySerial.println(at); //Once the handshake test of SIM800L is successful, it will return 'OK'
  updateSerial();
  mySerial.println(atcnmi); // Decides how newly arrived SMS messages should be handled
  updateSerial();
  mySerial.println(atcmgf); // Configuring TEXT mode
  updateSerial();
   
  Serial.println("Initialization successful! \nYou can write your request now");
}
    
void loop() {
  delay(500);
  updateSerial();

   String messags = simService.read();
   Serial.println(messags);
   
  while (Serial.available() == 0) {
        delay(1000);     
  }
  
  while (Serial.available() > 0) {
  receivedChar = Serial.read();
  StringForComparison += String(receivedChar);
  }
  
  StringForComparison.trim();
  Serial.println("Your request is: " + StringForComparison);  
  StringForComparison.toLowerCase();
  
  if (StringForComparison.equals(amarettoString)) {
    DistanceMeasurement();
    MeasurementsToVolumeCalculator();
    StringForComparison = "";
  } else {
    Serial.println("Command is not allowed! \n \nTry another one!");;
    receivedChar = '\0';
    StringForComparison = "";
  }
}

void updateSerial() {
  delay(500);
  while (Serial.available()) 
  {
    mySerial.write(Serial.read());
  }
  
  while(mySerial.available()) 
  {
    Serial.write(mySerial.read());
  }
  
}

void DistanceMeasurement() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  
  // Calculating the distance
  distance = ((duration * 0.034 / 2) - 1);
  
}

void MeasurementsToVolumeCalculator() {
//  SMSValidityCheck = -1;

  emptyContainerVolume = (containerLength * containerWidth * emptyContainerHeight); // Calculating the volume of the empty container in cm2
  measuredFreeSpace = (containerLength * containerWidth * distance); // Calculating the volume of the container after putting substance in

  fullContainerWeight = (emptyContainerVolume * powderDensity) / 1000;

  Serial.println("\nInfo for your request for \"" + StringForComparison + "\": \n" + "The Distance is " + String(distance) + "cm");

  if (distance >= 0  && distance < 9) {
    measuredPowderWeight = (measuredFreeSpace * powderDensity) / 1000;
    
    Content = ((fullContainerWeight - measuredPowderWeight));
    
    stringToBeSent = "The content of " + StringForComparison + " in the container is: " + String(Content) + " gr \n" ;
    Serial.println(stringToBeSent);
    
//    SendSMS(stringToBeSent);

    Serial.println("SMS Sent Successfully!");
  } else if (distance > 1190 || distance < 0) {
    Serial.println("The container is full" + String(Content) + " gr");
  }  else if (distance > 8 && distance < 1190) {
    Serial.println("Error 123 - Container lid not placed correctly!" );
  }
  Serial.println("Write your request");
}

void ReceiveSMS() {

}

void SendSMS(String smstext) {
  mySerial.println(atconfig);
  updateSerial();
  mySerial.print(smstext);
  updateSerial();
  mySerial.write(26);
}

@valkata752, your topic has been moved to a more suitable location on the forum.

Does Serial manage to command any SMS sending?

What is filtering suppose to do? How?.... Lots to explain.

I can't get to receive the sms in the type I want it to be.
I tried several examples and they don't work on my system.

Ask a friend to dry up Your tears. No help possible reading those words.

Hello and thanks for answering!

I can do it to send an sms with info from the serial and i can make it send an sms with specific data /or measurement in my case/ on command from serial. But my goal is to receive an sms with a command and then according to the content of the sms to send an sms back with the measurement.
So when i send an sms to sim800l it receives it and writes it in the Serial as CMT:.... The phone number, date and then the body.
What I tried is to filter out the body only through several sim800l libraries, but they either don't do anything or i receive it in its full form again. How can i get the body of the sms only?

And how do i need to 'watch' for a received sms because sim800l.available() doesnt exist in the library i used

I my opinion the delays You use ruins the sketch. I fear that buffers wull overflow and data be lost.
I see if I fund the point where commands arrive to the controller....

The function receiveSMS is empty......

The function is empty because im trying to make it work in the loop method and then i will move it to ReceiveSMS. Inside the loop i created while myserial.available() > 0 {
String randomname = sim800l.read(); and then for the serial to print it. But it doesnt do anything when a new sms comes in until I update the serial with writing something in the serial monitor.

Okey. You are a bit on the way to the final project.
I find it difficult to follow the main idea. You use serial monitor, some serial stuff via myserial, and also SMS.
Could You draw a block diagram of the chain, the flow of info?
Again, wy those huge delays? To me they would ruin any communication.

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