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! ![]()
#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);
}