1
votes

Im trying to send a variable in an sms using gammu. Im using the gammu smsd runonreceive to run a python script when I send a message to my raspberry pi from my phone. This is what the script looks like.

#!/usr/bin/python
import os

os.system("sh .webgps.sh > coordinates.text")

file = "/home/pi/coordinates.text"
with open(file) as f:
(lat, long) = f.read().splitlines()

os.system("echo lat | sudo gammu-smsd-inject TEXT 07xxxxxxxxx")

What this script does is it runs a shell script which gets the latitude and longitude from my gps module and puts them in a text file. Then it gets the values from the text file and puts the latitude in the lat variable and the longitude in the long variable. I can verify that this works because when I print the variables I can see latitude and longitude and they are the same values as the ones in the text file.

Now the bit that im having problems with is sending the values to my phone. If i run the python script how it currently is, then i get a message on my phone which says lat. What I want is to be sent the actual values for latitude and longitude and I dont know how to put the variables into the gammu inject text line.

2

2 Answers

0
votes

You receive "lat" in your phone, because the python var "lat" is not parsed that easy in the os.system echo call.

Sending a python variable to the shell is a bit weird story.

One solution that has worked for me in a similar situation is one like this:

with open(file) as f:
  (lat, long) = f.read().splitlines()

cmd="echo "+lat+" | sudo gammu-smsd-inject TEXT 07xxxxxxxxx"
os.system(cmd)
0
votes

Better use your own library of gammu, Python-gammu allows you to easily straight forward access the phone, and better error handling. Many examples are available in examples/ directory in the python-gammu sources.

On Ubuntu it is advised to use the distributions repository. So installing python-gammu should be per apt manager:

apt-get install python-gammu 

Here is an example of the script: Sending a message

#!/usr/bin/env python
# Sample script to show how to send SMS

from __future__ import print_function
import gammu
import sys

# Create object for talking with phone
state_machine = gammu.StateMachine()

# Optionally load config file as defined by first parameter
if len(sys.argv) > 2:
    # Read the configuration from given file
    state_machine.ReadConfig(Filename=sys.argv[1])
    # Remove file name from args list
    del sys.argv[1]
else:
    # Read the configuration (~/.gammurc)
    state_machine.ReadConfig()

# Check parameters
if len(sys.argv) != 2:
    print('Usage: sendsms.py [configfile] RECIPIENT_NUMBER')
    sys.exit(1)

# Connect to the phone
state_machine.Init()

# Prepare message data
# We tell that we want to use first SMSC number stored in phone
message = {
    'Text': 'python-gammu testing message',
    'SMSC': {'Location': 1},
    'Number': sys.argv[1],
}

# Actually send the message
state_machine.SendSMS(message)