Raspberry et SMS

raspberry sms nadhat garatronic gammu

Depuis de nombreuses années, les avantages de la plateforme Raspberry ne sont plus à démontrer. Utilisé à la fois sur les chaînes de production en usine, au bureau ou dans le monde académique.

Un des avantages est la richesse des équipements que l’on peut lui ajouter pour enrichir ses capacités de communication, en >particulier ls SMS. Pour cela nous pouvons utiliser une carte SIM Free à 2 euros et le module Nadhat. Nous avons testé aussi une >carte SIM Bouygtel avec succès.

Testée depuis plusieurs mois, la carte Nadhat de Garatronic nous donne pleinement satisfaction. Attention, elle est dédié au réseau 2G, réseau qui fonctionne toujours en région parisienne en 2020, mais dans certains pays (la Suisse semble être dans ce cas) , celui ci est en cours de disparition. On pourra aussi s'intéresser à la possibilité de monter un lien réseau point à point PPP, très utile pour la maintenance d'un système distant déconnecté du réseau Ethernet. Cela nous rappelle l'époque des modems sur ligne de cuivre téléphonique des années 1990-2000 !


Première étape: copie de l'image raspbian sur une carte SD ( attention /dev/sdx doit être adapté à votre système afin de ne pas écraser votre propre partition ! )

# dd if=2019-04-08-raspbian-stretch-full.img of=/dev/sdx bs=4096 conv=noerror,sync status=progress
5398847488 bytes (5,4 GB, 5,0 GiB) copied, 325,004 s, 16,6 MB/s
1318912+0 records in
1318912+0 records out
5402263552 bytes (5,4 GB, 5,0 GiB) copied, 325,221 s, 16,6 MB/s
# sync

Puis démarrage du Raspberry équipé de la carte GSM de Garatronic

Installons le logiciel nécessaire au démarrage de la carte Garatronic:
# mkdir garatronic
# cd garatronic/
# git clone http://github.com/fpie/nadhat
Cloning into 'nadhat'...
remote: Enumerating objects: 74, done.
remote: Total 74 (delta 0), reused 0 (delta 0), pack-reused 74
Unpacking objects: 100% (74/74), done.
Checking out files: 100% (60/60), done.
root@sviwallboard:/home/pi/garatronic# ls
nadhat
# ls nadhat/
README.md  datasheet  documentation  hardware  mechanic  software

Le logiciel utile se trouve dans software/ et permet d'activer la carte Nadhat:

# /home/pi/garatronic/nadhat/software/nadpwr.sh 
pulse low PWRKEY pin on GPIO26 for 1s to startup SIM800...
end

Afin d'automatiser cette activation au démarrage on ajoute au fichier /home/pi/.config/lxsession/LXDE-pi/autostart

/home/pi/garatronic/nadhat/software/nadpwr.sh

Ensuite on install le package gammu:

apt-get install gammu gammu-smsd

on identifie la puce GSM utilisée par Garatronic: SIMCOM et les informations sur la carte SIM (IMEI). Le NadHAT GSM fonctionne en utilisant un port série (appelé aussi UART) disponible sur les GPIO du Raspberry Pi (/dev/ttyAMA0, 115200 bauds, 8N1). On utilisera l'excellent programme screen ou minicom pour discuter avec la carte Nadhat si besoin. Mais Gammu le fait pour nous afin de cacher la complexité des commandes AT de commande.

# gammu identify
No response in specified timeout. Probably phone not connected.

Si vous avez l'erreur précédente, essayez d'activer la carte Nadhat à nouveau:

# /home/pi/garatronic/nadhat/software/nadpwr.sh 
pulse low PWRKEY pin on GPIO26 for 1s to startup SIM800...
end
# gammu identify
Device               : /dev/ttyAMA0
Manufacturer         : SIMCOM_Ltd
Model                : unknown (SIMCOM_SIM800C)
Firmware             : Revision:1418B05SIM800C24
IMEI                 : 865209030372222
SIM IMSI             : 208201007705244

La carte Nadhat utilise 2 broches GPIO pour le port série ( TxD et RxD). Il est pratique de récupérer le package wiringPI donnant les informations sur ces broches (BCM14 et BCM15)

Editez le fichier /boot/config.txt, vous devez y trouver :

enable_uart=1

Ajouter cette ligne :

dtoverlay=pi3-miniuart-bt

Modifier le fichier /boot/cmdline.txt et supprimer la ligne

console=serial0,115200

lien : https://pinout.xyz/pinout/pin10_gpio15

# git clone git://git.drogon.net/wiringPi
# gpio -v
gpio version: 2.46
Copyright (c) 2012-2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty

Raspberry Pi Details:
  Type: Model B+, Revision: 02, Memory: 512MB, Maker: Sony 
  * Device tree is enabled.
  *--> Raspberry Pi Model B Plus Rev 1.2
  * This Raspberry Pi supports user-level GPIO access.
root@sviwallboard:/home/pi/garatronic/wiringPi# gpio readall
 +-----+-----+---------+------+---+---Pi B+--+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5v      |     |     |
 |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 1 | ALT0 | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 0 | ALT0 | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi B+--+---+------+---------+-----+-----+

On peut ouvrir une console sur le port UART pour connecter directement à la carte Nadhat avec minicom ou screen :

minicom -b 115000 -o -D /dev/ttyAMA0     

screen /dev/ttyAMA0 115200 ( sortie par CTRL-a :exit ) 

Note: Il est plus simple de désactiver le code PIN de sécurité, tout simplement en utilisant un téléphone GSM. Toutefois, il existe une >option dans le fichier /etc/gammurc permettant de renseigner le code PIN.

Liste des commandes AT fournis par le constructeur de la puce SIM800:

  • at+cgmi : nom du fabricant

  • at+cgmm : nom de la carte

  • at+cnetscan : détection des bornes GSM autour du bureau (à Saint Cloud)

    AT+cnetscan
    Operator:"BOUYGUES TELECOM",MCC:208,MNC:20,Rxlev:31,Cellid:79DD,Arfcn:1017
    Operator:"SFR",MCC:208,MNC:10,Rxlev:30,Cellid:2C19,Arfcn:85
    Operator:"SFR",MCC:208,MNC:10,Rxlev:32,Cellid:3417,Arfcn:118
    Operator:"SFR",MCC:208,MNC:10,Rxlev:30,Cellid:23FA,Arfcn:76
    Operator:"SFR",MCC:208,MNC:10,Rxlev:24,Cellid:33B6,Arfcn:121

    OK

  • at+csq : qualité du signal 2G

    AT+csq
    +CSQ: 18,0
    
    OK
  • at+csca? : numéro du service SMS opérateur (Bouygtel ici)

    AT
    OK
    AT+csca?
    +CSCA: "2B3333363630303033303030",145
  • at+cmgf=1 : envoi des sms en texte

  • at+cmgs="+33612345678" : numéro de téléphone destinataire

message tapé au clavier ici ......

CTRL+Z => pour finir et valider le message at+cmgf=0 Test SMS.

Commande d'arrêt de la carte Nadhat:

at+cpowd=1

    AT
    OK
    AT+cpowd=1
    NORMAL POWER DOWN

Ci dessous un extrait des commandes envoyées par gammu :

2019-06-12 11:37:51,7, GSM1: Command is sent, waiting for the answer
2019-06-12 11:37:51,7, GSM1: <- OK
2019-06-12 11:37:51,6, GSM1: Checking memory size
2019-06-12 11:37:51,7, GSM1: -> AT+CPMS?
2019-06-12 11:37:51,7, GSM1: Command is sent, waiting for the answer
2019-06-12 11:37:51,7, GSM1: <- +CPMS: "SM_P",0,40,"SM_P",0,40,"SM_P",0,40 OK
2019-06-12 11:37:51,6, GSM1: Used memory is 0 of 40
2019-06-12 11:37:51,6, GSM1: No SMS received
2019-06-12 11:37:52,6, GSM1: I have to send 1 short message for /var/spool/sms/checked/mon_sms_de_test
2019-06-12 11:37:52,6, GSM1: Sending SMS from  to 336226XXX53
2019-06-12 11:37:52,6, GSM1: Checking if modem is ready
2019-06-12 11:37:52,7, GSM1: -> AT
2019-06-12 11:37:52,7, GSM1: Command is sent, waiting for the answer
2019-06-12 11:37:52,7, GSM1: <- OK
2019-06-12 11:37:52,6, GSM1: Pre-initializing modem
2019-06-12 11:37:52,7, GSM1: -> ATE0+CMEE=1;+CREG=2
2019-06-12 11:37:52,7, GSM1: Command is sent, waiting for the answer
2019-06-12 11:37:52,7, GSM1: <- OK
2019-06-12 11:37:53,7, GSM1: -> AT+CSQ
2019-06-12 11:37:53,7, GSM1: Command is sent, waiting for the answer
2019-06-12 11:37:53,7, GSM1: <- +CSQ: 17,0 OK
2019-06-12 11:37:53,6, GSM1: Signal Strength Indicator: (17,0) -79 dBm (Good), Bit Error Rate: less than 0.2 %
2019-06-12 11:37:53,6, GSM1: Checking if Modem is registered to the network
2019-06-12 11:37:53,7, GSM1: -> AT+CREG?
2019-06-12 11:37:53,7, GSM1: Command is sent, waiting for the answer
2019-06-12 11:37:53,7, GSM1: <- +CREG: 2,1,"000A","79DD" OK
2019-06-12 11:37:53,6, GSM1: Modem is registered to the network
2019-06-12 11:37:53,6, GSM1: Selecting PDU mode
2019-06-12 11:37:54,7, GSM1: -> AT+CMGF=0
2019-06-12 11:37:54,7, GSM1: Command is sent, waiting for the answer
2019-06-12 11:37:54,7, GSM1: <- OK
2019-06-12 11:37:54,7, GSM1: -> AT+CMGS=44
2019-06-12 11:37:54,7, GSM1: Command is sent, waiting for the answer
2019-06-12 11:37:54,7, GSM1: <- >
2019-06-12 11:37:54,7, GSM1: -> 0011000B913326622953F30000FF22F4F29C0EA281C865783D3D0789DFF5FC995E6683C2F6F218E40E93D1613A
2019-06-12 11:37:55,7, GSM1: Command is sent, waiting for the answer
2019-06-12 11:37:57,7, GSM1: <- +CMGS: 254 OK
2019-06-12 11:37:57,5, GSM1: SMS sent, Message_id: 254, To: 3362269XXX53, sending time 5 sec.
2019-06-12 11:37:57,6, GSM1: Moved file /var/spool/sms/checked/mon_sms_de_test to /var/spool/sms/sent/mon_sms_de_test
2019-06-12 11:37:57,6, GSM1: Checking device for incoming SMS
2019-06-12 11:37:57,6, GSM1: Checking memory size
2019-06-12 11:37:58,7, GSM1: -> AT+CPMS?
2019-06-12 11:37:58,7, GSM1: Command is sent, waiting for the answer
2019-06-12 11:37:58,7, GSM1: <- +CPMS: "SM_P",0,40,"SM_P",0,40,"SM_P",0,40 OK
2019-06-12 11:37:58,6, GSM1: Used memory is 0 of 40
2019-06-12 11:37:58,6, GSM1: No SMS received
2019-06-12 11:37:58,6, GSM1: Checking device for incoming SMS
2019-06-12 11:37:58,6, GSM1: Checking memory size
2019-06-12 11:37:58,7, GSM1: -> AT+CPMS?
2019-06-12 11:37:58,7, GSM1: Command is sent, waiting for the answer
2019-06-12 11:37:59,7, GSM1: <- +CPMS: "SM_P",0,40,"SM_P",0,40,"SM_P",0,40 OK
2019-06-12 11:37:59,6, GSM1: Used memory is 0 of 40

Mon fichier de configuration /etc/gammu-smsdrc

root@rpi3RDC:/home/pi# cat /etc/gammu-smsdrc
# Configuration file for Gammu SMS Daemon

# Gammu library configuration, see gammurc(5)
[gammu]
# Please configure this!
port = /dev/ttyAMA0
connection = at
# Debugging
#logformat = textall

# SMSD configuration, see gammu-smsdrc(5)
[smsd]
service = files
logfile = syslog
# Increase for debugging information
debuglevel = 0

# Paths where messages are stored
inboxpath = /var/spool/gammu/inbox/
outboxpath = /var/spool/gammu/outbox/
sentsmspath = /var/spool/gammu/sent/
errorsmspath = /var/spool/gammu/error/

#PIN=1234
CheckSecurity=0
CommTimeout=5

Pour envoyer un SMS en ligne de commande:

# echo "Envoie d'un sms avec gammu" | gammu --sendsms TEXT 062269XX53
Si vous désirez interrompre, appuyez sur Ctrl + C...
Envois du SMS 1 1 / 1 2.... Attente d'une réponse réseau..OK, message de référence=4 1

On peut lire les SMS reçus:

# gammu getallsms

0 1 partie de SMS 0 2 suite SMS
root@NadHAT:/home/pi# gammu getallsms 1
Location 1, folder "Boîte de réception", SIM memory, Inbox folder
SMS message
SMSC number          : "+33689004000"
Envoyé              : lun. 07 oct. 2019 21:04:08  +0200
Coding               : Default GSM alphabet (no compression)
Remote number        : "+3362269XX53"
État                : UnRead

21h04

Entrée de la Crontab pour envoyer un SMS avec diverses infos sur le système dont la température à 10h00, 14h00 et 18h00 en appelant /usr/bin/gammu :

0 10,14,18 1-6 echo "hostname date uptime vcgencmd measure_temp" | /usr/bin/gammu sendsms TEXT 0622692353

Dans les zones urbaines, nous sommes à l'heure des smartphones connectés en permanence au réseau 4G, et les SMS sont remplacés par des alertes utilisant le réseau Data. Mais dans le monde industriel (usines en province, chantiers BTP itinérants) ou dans votre résidence secondaire en Normandie, nous sommes confrontés à des zones ravitaillées par les corbeaux. Soit le réseau GSM n'offre pas mieux que la 2G, soit les téléphones des ouvriers ne supportent pas la 4G et/ou la 3G. J'espère que si cette problématique d'envoi de SMS se pose, vous penserez à cette carte made in France. Il faudra un peu de patience et d'expérience pour durcir le logiciel du raspberry.

Informations sur la plateforme RPI utilisée:

# cat /proc/cpuinfo
processor   : 0
model name  : ARMv6-compatible processor rev 7 (v6l)
BogoMIPS    : 697.95
Features    : half thumb fastmult vfp edsp java tls 
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part    : 0xb76
CPU revision    : 7

Hardware    : BCM2835
Revision    : 0010
Serial      : 00000000bf29af79

# cat /proc/device-tree/model

Raspberry Pi Model B Plus Rev 1.2

# cat /etc/*rel*
/usr/lib/arm-linux-gnueabihf/libarmmem.so
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

Previous Post Next Post