SMS Using M5Core + U137 SIM7080G CAT-M/NB-IoT+GNSS Unit
-
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"
Relay1onAny 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
}
-
@lenswerks what is exact string you getting in receivedMsg?
you could split string to sub messages based on (") or (,)
-
The string when sending Relay1on is
+myphonenumber",,"25/04/23,15:09:25-28"
Relay1onNote that Relay1on is on next line.
I want to stack a 4RELAY module and be able to control it with SMS messages.
Thanks. -
That was painful but figured it out.
Must be better ways but this works where lastWord will be the exact SMS message sent.log(receivedMsg); // Display the received message
receivedMsg.trim(); // Remove any trailing or leading whitespace, including newlines // Find the last newline character or any other delimiter expected) int lastNewlineIndex = receivedMsg.lastIndexOf('"'); // Extract the substring after the last newline character String lastWord = receivedMsg.substring(lastNewlineIndex + 1); lastWord.replace("\n", ""); lastWord.replace("OK", ""); lastWord.replace("\r", ""); // Removes all carriage return characters