🤖Have you ever tried Chat.M5Stack.com before asking??😎
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Register
  • Login
M5Stack Community
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Register
  • Login

SMS Using M5Core + U137 SIM7080G CAT-M/NB-IoT+GNSS Unit

General
2
4
275
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L
    lenswerks
    last edited by 17 days ago

    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
    

    }

    R L 2 Replies Last reply 17 days ago Reply Quote 0
    • R
      robski @lenswerks
      last edited by 17 days ago

      @lenswerks what is exact string you getting in receivedMsg?

      you could split string to sub messages based on (") or (,)

      M5StickC, M5StickCPlus, M5StickCplus2,M5GO, M5Core, M5Tough, M5Core2, M5 Demo Board, M5Dial, M5Paper, M5Atom, M5Cardputer, M5StampS3, CoreMP135, StamPLC, AirQ

      1 Reply Last reply Reply Quote 0
      • L
        lenswerks @lenswerks
        last edited by 17 days ago

        The string when sending Relay1on is
        +myphonenumber",,"25/04/23,15:09:25-28"
        Relay1on

        Note that Relay1on is on next line.

        I want to stack a 4RELAY module and be able to control it with SMS messages.
        Thanks.

        L 1 Reply Last reply 16 days ago Reply Quote 0
        • L
          lenswerks @lenswerks
          last edited by 16 days ago

          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
          
          1 Reply Last reply Reply Quote 0
          4 out of 4
          • First post
            4/4
            Last post