Sending and receiving SMS from a software

Sending and receiving SMS from a software

We have just added a new feature to the YoctoHub-GSM. If you update your firmware, you will be able to use it to send and receive SMS. This is might not look very useful to access a Yoctopuce sensor, but it opens the door to new use-cases for the YoctoHub-GSM itself. For instance, you can now implement two factor authentication.




Nowadays, a simple password is not enough to identify a user with a reasonable degree of certainty. Therefore, two factor authentication is generally used, which means that a second, independent communication channel is used to check with the users who are connecting themselves that they are truly who they say they are. As an example of second communication channel, you could use a mobile phone number. Hence the idea of exchanging SMS...

Naturally, there are gateways between SMS and mail, or to the web, that exist and that you could use to send and receive SMS. But if your application is important, you most likely don't want to depend on a third party service on which you have no control at all. We are going to show you today how to do so with Yoctopuce modules.

To reproduce this example, you therefore need a YoctoHub-GSM-2G, YoctoHub-GSM-3G-EU, or YoctoHub-GSM-3G-NA. You may have to update it with the latest firmware, as we have just added the SMS feature in the YoctoHub-GSM. You also need to download the latest version of the Yoctopuce library for the programming language of your choice to have the SMS sending and receiving functions.

Sending an SMS containing only text requires calling a single method: sendTextMessage(). You can even use our command line library to do it from a shell script. Assuming that you have connected the YoctoHub-GSM by USB, the following command is all it takes to send a Hello World to the +123456789 phone number.

YMessageBox any sendTextMessage "+123456789" "Hello world!"



You canalso send a Flash SMS, also known as Class 0 SMS, which is shown directly on the recipient mobile phone and is now saved on the SIM card by default. For this, use the method sendFlashMessage():

YMessageBox any sendFlashMessage "+123456789" "Your SMS code is 1234"



To have more control over the distinct parameters of the message that you are sending, or to add special unicode characters like emoticons, you have to use a YSms intermediary object. Here is a PHP example:

$mbox = YMessageBox::FirstMessageBox();
$sms = $mbox->newMessage('+123456789');
$sms->addText("Server room is too warm ");
$sms->addUnicodeData(Array(0x1F630)); // sweating face
$sms->send();


If the content of your message takes up more than 160 characters in the GSM alphabet or more that 70 characters if it contains any unicode character, it is automatically sent as several SMS messages and reassembled by the receiver.

If you opt for a two factor authentication protocol where the users must themselves send you a message, you can check the received messages and authorize sessions accordingly. Here is a Node.js (EcmaScript) example:

var mbox = YMessageBox.FirstMessageBox();
var messages = mbox.get_messages();
for(var i = 0; i < messages.length; i++) {à
    var msg = messages[i];
    console.log(msg.get_sender() + ': ' + msg.get_textData());
}



Last potential hurdle: if you cannot connect the YoctoHub-GSM directly to your web server, for instance if you use third party hosting, the most efficient way is to connect the YoctoHub-GSM on a YoctoHub-Ethernet. Indeed, nothing prevents you from using a YoctoHub-GSM like a simple device to be used for sending messages. When you connect it on a YoctoHub-Ethernet, you can configure the Ethernet hub to make HTTP callbacks and provide remote control on your YoctoHub-GSM without incurring any GSM connection expenses. If your web host is based on Java or Node.js, you can use a WebSocket callback, and thus keep your YoctoHub-GSM permanently connected to your web server. If you work with PHP, you must configure your HTTP callback to connect itself every few seconds to your web site to send the pending SMS of the past few seconds. It's somewhat less elegant, but it works as well!

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.