Arduino with SIM 900a- How can I store all incoming messages into a text file?

503 views Asked by At

I have connected my Arduino Uno with SIM 900a GSM module. I want to store all my text messages that I receive on the SIM inside the GSM module to a text file continuously.

I can send SMS through the code shown below but I cannot receive and save my messages to a file. What is the correct way to do this?

#include <SoftwareSerial.h>

SoftwareSerial mySerial(9, 10);

void setup() {
  mySerial.begin(9600);   // Setting the baud rate of GSM Module  
  Serial.begin(9600);    // Setting the baud rate of Serial Monitor (Arduino)
  delay(100);
}

void loop() {
  if (Serial.available()>0)
    switch(Serial.read()) {
      case 's':
        SendMessage();
        break;
      case 'r':
        RecieveMessage();
        break;
    }
  if (mySerial.available()>0)
    Serial.write(mySerial.read());
}


void SendMessage() {
  mySerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  mySerial.println("AT+CMGS=\"+9779813546162\"\r"); // Replace x with mobile number
  delay(1000);
  mySerial.println("I am SMS from GSM Module");// The SMS text you want to send
  delay(100);
  mySerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
}

void RecieveMessage() {
  mySerial.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
  delay(1000);
}
1

There are 1 answers

0
ti7 On

The Arduino cannot naturally create files on your host system, so you will need to

  • run an independent program on the host system which watches the serial connection and logs the messages produced by the Arduino (my recommendation)
  • attach some storage to the Arduino (such as an SD card shield)
  • have the Arduino pretend to be a keyboard and output as if it were being typed upon

Independent Program

This is the route I would recommend

  • easy to use and test (just check your file has the contents you want)
  • will not "mess up" your Arduino
  • can do other work and tests on system
  • do not need to deal with storage problems

This simple Python script may work for your needs: Serial data logging with python

This post suggests you can do it with a 1-liner under both Linux (may be the same for Mac) and Windows, but you may run into trouble with the Serial Port's baud rate. If this answer is dated or only gets some of the output (ie. a single line and then exits), you could run it in a loop or search further. You'll need to pick the right serial port as there may be a few (or just one with a different name).

Attached Storage

Many vendors will sell you a Shield for this

Beware that Flash Storage can be annoying to deal with

  • need to eject (perhaps swap out) the card and look at it periodically to both see the results and if the results are correct
  • filesystems hoopla (should I use FAT, exFAT, ext2..)
  • ensuring the Arduino can write the filesystem (though modern shields probably do this for you, at least the Adafruit one suggested above does)

Keyboard Emulation

To start, I do not recommend doing this for the following reasons, though it's pretty neat and doesn't require any more hardware than you have.

  • BEWARE may make your Arduino unusable without some start gate such as waiting for a switch to be enabled (haywire inputs to computer: cannot program it)
  • requires total access to the computer while running (computer cannot be otherwise used)
  • not easier to configure than an independent logger (annoying trial and error / waiting / haywire inputs)

Official docs: https://www.arduino.cc/reference/en/language/functions/usb/keyboard/

They have the same warning I would give

A word of caution on using the Mouse and Keyboard libraries: if the Mouse or Keyboard library is constantly running, it will be difficult to program your board. Functions such as Mouse.move() and Keyboard.print() will move your cursor or send keystrokes to a connected computer and should only be called when you are ready to handle them. It is recommended to use a control system to turn this functionality on, like a physical switch or only responding to specific input you can control. Refer to the Mouse and Keyboard examples for some ways to handle this.