web analytics
August 5, 2015

Sim908 – Text Messages

This page covers how to send and receive text messages using the library, as well explaining how messages are stored on the SIM.

Sending a message

Sending a text message is very straightforward: specify the phone number and message text and you are done:

#include "Phoenard.h"

void setup() {
  // Initialize the SIM and enter the sim pin if so required
  sim.begin();
  sim.enterPin("0000");

  // Send a text message to a mobile number
  sim.sendMessage("+0612345678", "Hello there!");
}

void loop() {
}

 

Receiving a new message

Receiving a text message is done by routinely checking if a message is received, reading it when we do. The SIM communication is done in the background for you. The following example waits for a message to be received, reads it and prints the information to the Serial console:

#include "Phoenard.h"

void setup() {
  // Initialize the SIM and enter the sim pin if so required
  sim.begin();
  sim.enterPin("0000");

  // Initialize Serial at 9600 baud
  Serial.begin(9600);
  Serial.println("Now waiting for messages");
}

void loop() {
  // Check if a message is received
  if (sim.hasNewMessage()) {
    // Read the message
    SimMessage message = sim.getNewMessage();

    // Show information of the message sender
    Serial.print("New message from ");
    Serial.print(message.sender.name);
    Serial.print(" (");
    Serial.print(message.sender.number);
    Serial.println("):");

    // Show the message contents itself
    Serial.println(message.text);
  }
}

 

Automatic generated replies

Now, the most awesome projects will have your device responding to people sending it a text message. Imagine sending it a message asking about the current temperature in your house, with it replying instantly. To do this, you simply combine sending and receiving with a tiny bit of Arduino String basics. The following example tells you the analog readout of A0 when you send it the ‘read’ text message:

#include "Phoenard.h"

void setup() {
  // Initialize the SIM and enter the sim pin if so required
  sim.begin();
  sim.enterPin("0000");
}

void loop() {
  // Check if a message is received
  if (sim.hasNewMessage()) {
    // Read the message
    SimMessage message = sim.getNewMessage();
    
    // Turn the message text into an Arduino String
    // This makes it easier to compare or parse it
    String text = message.text;
    if (text == "read") {
      // This our token! Generate the response.
      int value = analogRead(A0);
      String response;
      response += "The current value is ";
      response += value;
      response += " (10-bit ADC)";

      // Send out the response to the sender of the message
      // Note that we need to use c_str since the function
      // expects char[].
      sim.sendMessage(message.sender.number, response.c_str());
    }
  }
}

 

Reading the message inbox

The inbox is a beast of its own. It is not nicely sorted and can even contain empty entries. This means that, while going through the inbox, we need to check for every entry that it actually contains a valid message. The sim can tell you how many messages could be stored and how many valid entries there are, but nothing more. The following simple example reads the inbox one message at a time and shows it in the Serial console:

#include "Phoenard.h"

void setup() {
  // Initialize the SIM and enter the sim pin if so required
  sim.begin();
  sim.enterPin("0000");

  // Initialize Serial at 9600 baud
  Serial.begin(9600);

  // Read the inbox memory limits
  int count = getMessageCount();
  int limit = getMessageLimit();
  Serial.print("Messages: ");
  Serial.print(count);
  Serial.print(" / ");
  Serial.println(limit);
  Serial.println();

  // Read all the messages until they are all read
  // Make sure to stay within the limit of the memory
  int messageNr = 1;
  for (int i = 0; i < limit; i++) {
    // Read the message and check that it is not empty
    SimMessage message = sim.getMessage(i);
    if (message.valid) {
      // Show current message 'nr'
      Serial.print("Message ");
      Serial.print(messageNr);
      Serial.print(" / ");
      Serial.print(count);
      Serial.println(":");
      messageNr++;

      // Show message sender
      Serial.print("Sender: ");
      Serial.print(message.sender.name);
      Serial.print(" (");
      Serial.print(message.sender.number);
      Serial.println(")");

      // Show message text
      Serial.print("Text: ");
      Serial.println(message.text);
    }

    // Stop if already read all messages
    if (messageNr > count) break;
  }
  
  // Done!
  Serial.println("All messages read.");
}

void loop() {
}