1

我正在尝试使用 gammu 在短信中发送变量。当我从手机向树莓派发送消息时,我使用 gammu smsd runonreceive 运行 python 脚本。这就是脚本的样子。

#!/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")

该脚本的作用是运行一个 shell 脚本,该脚本从我的 gps 模块中获取纬度和经度,并将它们放入一个文本文件中。然后它从文本文件中获取值并将纬度放入 lat 变量中,将经度放入 long 变量中。我可以验证这是否有效,因为当我打印变量时,我可以看到纬度和经度,它们与文本文件中的值相同。

现在我遇到的问题是将值发送到我的手机。如果我按照当前的方式运行 python 脚本,那么我会在手机上收到一条消息,上面写着 lat。我想要的是发送纬度和经度的实际值,我不知道如何将变量放入 gammu 注入文本行。

4

2 回答 2

0

您在手机中收到“lat”,因为在 os.system echo 调用中解析 python var“lat”并不容易。

将 python 变量发送到 shell 是一个有点奇怪的故事。

在类似情况下对我有用的一种解决方案是这样的:

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

cmd="echo "+lat+" | sudo gammu-smsd-inject TEXT 07xxxxxxxxx"
os.system(cmd)
于 2017-04-23T22:41:44.500 回答
0

更好地使用你自己的 gammu 库,Python-gammu允许你轻松直接访问手机,并更好地处理错误。python-gammu 源代码的 examples/ 目录中提供了许多示例。

在 Ubuntu 上,建议使用发行版存储库。所以安装python-gammu应该是每个 apt manager:

apt-get install python-gammu 

下面是一个脚本示例:发送消息

#!/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)
于 2019-05-17T14:28:42.623 回答