Hi All,
I am able to send and receive SMS messages with the named hardware but have an issue. The message obtained by device in receivedmsg contains my phone number, date and time and then the actual message. I cannot figure out an easy way to just get the actual message so I can use in IF statement. The code is listed below. Works great but need to parse out the message only. Other code for other controllers I have do not work with this hardware.
This is what is in receivedMsg --> +myphonenumber",,"25/04/23,15:09:25-28"
Relay1on
Any ideas?
#include <iostream>
#include <sstream>
#include <string>
#include "M5Stack.h"
#include "M5GFX.h"
#include "M5_SIM7080G.h"
M5GFX display;
M5Canvas canvas(&display);
M5_SIM7080G device;
void log(String str) {
Serial.println(str);
canvas.fillSprite(TFT_BLACK); // Clear the canvas
canvas.setCursor(0, 0); // Reset cursor
canvas.print(str); // Print the new message
canvas.pushSprite(0, 0); // Push updated sprite to the display
}
void setup()
{
M5.begin();
display.begin();
if (display.isEPD())
{
display.setEpdMode(epd_mode_t::epd_fastest);
display.invertDisplay(true);
display.clear(TFT_BLACK);
}
if (display.width() < display.height())
{
display.setRotation(display.getRotation() ^ 1);
}
canvas.setColorDepth(1);
canvas.createSprite(display.width(), display.height());
canvas.setTextSize((float)canvas.width() / 160);
canvas.setTextScroll(false);
/* Init SIM7080G */
device.Init(&Serial2, 16, 17);
/* Reboot SIM7080G */
log("Rebooting SIM7080G...");
while (device.send_and_getMsg("AT+CREBOOT\r\n").indexOf("OK") == -1) {
delay(1000);
}
log("Reboot successful!");
/* Set SMS text mode */
log("Setting SMS text mode...");
while (device.send_and_getMsg("AT+CMGF=1\r\n").indexOf("OK") == -1) {
delay(1000);
}
log("SMS mode ready.");
}
void loop()
{
log("Checking for messages");
String smsList = device.send_and_getMsg("AT+CMGL=\"REC UNREAD\"\r\n"); // Fetch unread SMS
if (smsList.indexOf("+CMGL") != -1) {
int msgStart = smsList.indexOf("\",\"");
int msgEnd = smsList.lastIndexOf("\r\n");
if (msgStart != -1 && msgEnd != -1) {
String receivedMsg = smsList.substring(msgStart + 3, msgEnd); // Extract message content
log(receivedMsg); // Display the received message
delay(10000);
/*
if (receivedMsg.equals("desiredMessage")) {
// Do something specific
System.out.println("Action triggered for 'desiredMessage'");
}
*/
/* Respond to the received SMS */
String recipient = "+myphonenumbe";
String response = "Acknowledged: " + receivedMsg;
log("Sending response...");
device.send_and_getMsg("AT+CMGS=\"" + recipient + "\"\r"); // Set recipient
delay(100); // Wait for the prompt
device.send_and_getMsg(response + "\x1A"); // Send response (Ctrl+Z to finalize)
log("Response sent!");
}
/* Delete the processed message */
device.send_and_getMsg("AT+CMGD=1,4\r\n"); // Delete all messages to avoid duplicates
}
delay(1000); // Short delay between operations
}