SMS server setup
Daftar isi
Overview
This document describes the steps required to configure a Rasberry Pi computer as an SMS server, connected via the internet to the iSIKHNAS database server. A USB GSM modem is attached to the Pi using a powered USB hub. It assumes that configuration is being done using a Windows computer.
Hardware
Requirements are:
- Raspberry Pi
- Powered USB hub
- GSM USB modem (this example uses a Huawei E173)
- Micro USB cable
- Standard USB cable
- Ethernet cable
- 4Gb SD card (or micro SD card with adapter)
Installing the operating system (Windows)
- Download the latest Raspbian disk image (fastest via BitTorrent if you have a client) and save it to your hard disk
- Create a new directory and unzip the image file into the new directory
- Download Win32DiskImager and save it to your hard disk
- Unzip the program files and save them to your new directory
- Put a blank 4Gb SD card into the SD card slot of your computer (or an attached USB card reader)
- Start Win32DiskImager by double clicking
- Select the Raspbian image (e.g. 2013-02-09-wheezy-raspbian.img) as the file, and your card as the destination. Be careful to get the right drive. If you select your hard disk it will be overwritten and destroyed.
- Click write and wait for the image to be written
- Close the software and remove the SD card
Installing the operating system - Mac
- Download the latest Raspbian disk image (fastest via BitTorrent if you have a client) and save it to your hard disk
- Create a new directory and unzip the image file into the new directory
- The next bit is quickest using the Terminal window (look in /Applications/Utilities/Terminal) - it only takes half a dozen lines, but requires the password of the Admin user to be enabled - if you don't know what this means you might be best to ask for help.
- First we need to find out the internal name of the SD card to be written. In the terminal window, on a new line, type:
df -h
- The lines that read /dev/disk0s2 refer to a physical disk (ie disk0), and a partition on the disk (s2). Put a blank 4Gb or greater SD card into the SD card slot of your computer (or an attached USB card reader) and when it appears on the desktop again type:
df -h
- You should see a new disk has appeared - note the name of it - for example /dev/disk1s1. Using the name you noted, unmount (this is not the same as eject!) the disk from the operating system:
sudo diskutil unmount /dev/disk1s1
- The SD card is now available for overwriting, but you need to put an r in front of the disk name and drop the s1 part (for example /dev/sdisk1s1 becomes /dev/rdisk1).
- If you get the above bit wrong, it will either not work at all (disappointing), or overwrite the operating system and all the data on your computer (also disappointing!).
- Now use dd to copy the new image (input file) onto the SD card (output file) using:
sudo dd bs=1m if=~/Downloads/2013-02-09-wheezy-raspbian.img of=/dev/rdisk
- x <- where the x the value you have noted above!
- This does take some time (9 minutes?) - you can use SIGINFO (Ctrl-T) to see where it is up to.
- When finished, eject the card:
sudo diskutil eject /dev/rdisk1
- You can now remove the SD card. To close the terminal window type:
exit
Connecting and starting the Pi
Connecting the hardware
- Insert the SD card into the slot in the Pi (face down)
- Plug in the power to the USB Hub, and insert a USB GSM modem with a SIM card installed. Make sure the SIM card has had PIN protection disabled
- Connect the hub to the Pi via a USB cable
- Connect an ethernet cable to the Pi and the network (probably an ethernet router)
- Connect the micro USB power cable to a USB power adapter and to the Pi - the lights will start flashing
Getting access
You should be able to access the Pi via SSH to continue the configuration. Use PuTTY or a similar client for this.
- Connect to your router or modem and look at the DHCP table to see what address has been assigned to the Pi
- In PuTTY, create a connection using SSH to the IP address assigned to the Pi. Start the connection
- When prompted enter the user name pi and password raspberry
Expanding the partition
After installation, Raspbian occupies only 2 Gb. You can expand the partition to use the entire space available, using the raspi-config script.
sudo raspi-config
A menu will be shown. Tab or down-arrow to select the expand_rootfs option. This will immediately resize the partition without asking. You need to reboot when asked for the changes to take effect.
Configuring the software
Updating Aptitude
Make sure you have the latest software list
# sudo apt-get update # sudo apt-get upgrade
Install required software
Install USB Mode Switch, which handles modems that also have a simulated CD drive
# sudo apt-get install usb-modeswitch
Install the PostgreSQL client to connect to the server
# sudo apt-get install postgresql-client
Install SMS Tools to handle communications
# sudo apt-get install smstools
Create a user directory
SMS tools runs under user smsd. To make life easier connecting to the database server, we'll create a home directory for smsd. make a home directory for smsd user
First we need to give smsd bash access
# sudo chsh -s /bin/bash smsd
Then create a home directory
# sudo mkdir /home/smsd
Assign appropriate permissions
# sudo chown smsd:smsd /home/smsd
Create the password file for PostgreSQL at /home/smsd/.pgpass
# sudo nano /home/smsd/.pgpass
Enter just the first line to use the central database, or the second if creating a development machine to use a local database
sms.isikhnas.asia:5432:isikhnas:smsd:password localhost:5432:isikhnas:smsd:password
(replacing password with the real password - but we're not going to record that here, are we)
Lock down the permissions
# sudo chown smsd:dialout /home/smsd/.pgpass # sudo chmod 600 /home/smsd/.pgpass
Check where the modem is connected
Disconnect the modem and enter
# ls /dev
Connect the modem and enter
# ls /dev
Compare the two results. There should be some new entries when the modem is connected, an it is probable on the first of these entries (normally ttyUSB0)
Add configuration files
Edit smsd.conf to look something like this:
# sudo nano /etc/smsd.conf
# # /etc/smsd.conf # # Description: Main configuration file for the smsd # devices = GSM1 outgoing = /var/spool/sms/outgoing checked = /var/spool/sms/checked incoming = /var/spool/sms/incoming logfile = /var/log/smstools/smsd.log infofile = /var/run/smstools/smsd.working pidfile = /var/run/smstools/smsd.pid outgoing = /var/spool/sms/outgoing checked = /var/spool/sms/checked failed = /var/spool/sms/failed incoming = /var/spool/sms/incoming sent = /var/spool/sms/sent stats = /var/log/smstools/smsd_stats loglevel = 5 #delaytime = 10 #errorsleeptime = 10 receive_before_send = no # autosplit 0=no 1=yes 2=with text numbers 3=concatenated autosplit = 3 # store_received_pdu 0=no, 1=unsupported, 2=unsupported and 8bit, 3=all store_received_pdu = 3 store_sent_pdu = 3 eventhandler = /home/smsd/sms_in.sh [GSM1] # Huawei E173 init = AT+CNMI=2,1,2,2,0 # Huawei E1682 # init = AT^CURC=0 # init2 = AT+CPMS=\"ME\",\"ME\",\"ME\" device = /dev/ttyUSB0 incoming = yes
Note: the initialisation strings are different for each type of modem and are a little problematic. Just Google your model and see what comes up, then use trial and error. Sometimes it appears to be OK, but then won't receive messages so you need to try again.
Create the incoming message handler script
# sudo nano /home/smsd/sms_in.sh
#!/bin/bash MSGID=$$`date +%s` MSG=`cat $2 | sed \"s/'/''/g\"` psql -q -t -h sms.isikhnas.asia isikhnas smsd << EOF select sms.sms_parse('$MSG','$1','$MSGID'); EOF /home/smsd/sms_out.sh $MSGID
If using a local development server that has the SMS and PostgreSQL server on the same machine, you can change the fourth line to:
psql -q -t isikhnas smsd << EOF
Create the outgoing message handler script
# sudo nano /home/smsd/sms_out.sh
#!/bin/bash psql \\ -h sms.isikhnas.asia\\ -qtAc \"select phone, message from sms.sms_outbox where msgid = $1\"\\ --field-separator '|' \\ isikhnas | while IFS='|' read -ra Record ; do NUM=${Record[0]} MSG=${Record[1]} TMPFILE=`mktemp /var/spool/sms/outgoing/sms.XXXXX` || exit 1 echo -e \"To: $NUM\ \ $MSG\" > $TMPFILE done
If using a local development server that has the SMS and PostgreSQL server on the same machine, you can change the fourth line to:
-h localhost\\
Make both scripts executable
# cd /home/smsd # sudo chown smsd:dialout *.sh # sudo chmod 744 *.sh
Test the system
Test the modem
Open a new PuTTY terminal and keep an eye on the smsd.log
# sudo tail -f /var/log/smstools/smsd.log
In the original terminal, restart the smsd services
# sudo service smstools restart
Check for errors. The output should be something like this:
2013-04-04 20:19:35,2, smsd: Smsd v3.1.14 started. 2013-04-04 20:19:35,2, smsd: Running as smsd:dialout. 2013-04-04 20:19:35,4, smsd: File mode creation mask: 022 (0644, rw-r--r--). 2013-04-04 20:19:35,5, smsd: Outgoing file checker has started. PID: 2591. 2013-04-04 20:19:35,5, GSM1: Modem handler 0 has started. PID: 2592. 2013-04-04 20:19:35,5, GSM1: Using check_memory_method 1: CPMS is used. 2013-04-04 20:19:36,3, GSM1: Unexpected input: OK 2013-04-04 20:19:40,5, GSM1: CGSN: 860872012247822 2013-04-04 20:19:40,5, GSM1: IMSI: 208103691590779
Test database connectivity
Try to access the database server using psql
# sudo su smsd # psql -h sms.isikhnas.asia isikhnas
You should get a response something like this:
psql (9.1.8, server 9.2.3) WARNING: psql version 9.1, server version 9.2. Some psql features might not work. SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256) Type \"help\" for help. isikhnas=>
To exit from psql, type:
isikhnas=> \\q
Lock down the system
Add some security so others can't undo all our good work.
Changing the user password
To change the password for the pi use, you can use the raspi-config script.
# sudo raspi-config
then select the change_pass option.