【发布时间】:2020-05-11 05:18:45
【问题描述】:
您好,我在树莓派上构建了一个 GPS 解决方案。
python 代码运行良好,直到我将所有内容都打包到一个 bash 脚本中……然后奇怪的事情发生了……
所以我有: 1. publish.py(此文件将 GPS 数据发布到 MQTT 主题) 2. listener.py(这个文件监听MQTT主题,用于测试目的) 3.job.sh(bash脚本运行一些命令publish.py文件) 4. crontab(我做了一个crontab,每10分钟运行一次来运行job.sh脚本)
发布.py
from gps import *
import time
from sense_hat import SenseHat
import json
import paho.mqtt.client as mqtt
import nacl.secret
import nacl.utils
import device
#initiate the device object
device = device.Device()
sense = SenseHat()
sense.clear()
timestamp = 0
latitude = 0
longitude = 0
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("topic")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("mqtt.eclipse.org", 1883, 60)
# put the symmetricKey into box
file = open("/home/pi/Project/symmetricKey", "rb")
symmetricKey = file.readline()
box = nacl.secret.SecretBox(symmetricKey)
def getPositionData(gps):
nx = gpsd.next()
if nx['class'] == 'TPV':
global timestamp
global latitude
global longitude
print("##############")
timestamp = getattr(nx,'time', "Unknown")
latitude = getattr(nx,'lat', "Unknown")
longitude = getattr(nx,'lon', "Unknown")
#print "##timestamp: ", timestamp, "##Your position: lon = ", str(longitude), ", lat = ", str(latitude)
return timestamp, latitude, longitude
def getSenseHatData():
pressure = sense.get_pressure()
temp = sense.get_temperature()
humidity = sense.get_humidity()
#print "pressure: ", pressure, "temp: ", temp, "humidity: ", humidity
return pressure, temp, humidity
def encrptyMessage(body):
encrpted = box.encrypt(body)
return encrpted
gpsd = gps(mode=WATCH_ENABLE|WATCH_NEWSTYLE)
def run():
try:
print ("Application started!")
while True:
gpsData = getPositionData(gpsd)
senseHatData = getSenseHatData()
dictionary = {
"timestamp": gpsData[0],
"latitude": gpsData[1],
"longitude": gpsData[2],
"pressure": senseHatData[0],
"temp": senseHatData[1],
"humidity": senseHatData[2]
}
body = json.dumps(dictionary)
encrptedBody = encrptyMessage(body)
print (dictionary)
try:
client.publish('topic', payload=encrptedBody)
except:
print("message is not successfully sent...")
time.sleep(1.0)
except (KeyboardInterrupt):
running = False
print ("Applications closed!")
return body
run()
job.sh
#!/bin/bash
sudo systemctl start gpsd.socket & #this is to start GPS socket
sleep 5
python /home/pi/Project/publish.py & #this is to run my publish.py
sleep 10
sudo systemctl stop gpsd.socket & #this is to stop the socket service
crontab
*/10 * * * * bash /home/pi/job.sh
奇怪的是
当我直接运行publish.py时,一切正常......但是当我运行job.sh时,代码会抛出错误
Traceback (most recent call last):
File "/home/pi/Project/publishTelemetry.py", line 123, in <module>
run()
File "/home/pi/Project/publishTelemetry.py", line 97, in run
gpsData = getPositionData(gpsd)
File "/home/pi/Project/publishTelemetry.py", line 45, in getPositionData
nx = gpsd.next()
File "/usr/lib/python2.7/dist-packages/gps/gps.py", line 287, in next
return self.__next__()
File "/usr/lib/python2.7/dist-packages/gps/gps.py", line 279, in __next__
raise StopIteration
你知道如果 python 和 bash 有这种奇怪的行为吗???
感谢您的帮助!!!
【问题讨论】:
-
我认为,您需要保持您的 publish.py 运行正常吗? (在您的代码中有一个
while True),所以不要直接从 shell 脚本调用它,如python file.py,您必须使用类似屏幕的东西并将其分离,以便脚本继续在后台运行
标签: python bash cron raspberry-pi gpsd