简体   繁体   中英

Can't read value from python script output

I've got 2 raspberrypi devices. First is measuring temperature, second one is displaying it. I've wrote some python scripts to manage sending temperature results.

Server script:

import RPi.GPIO as GPIO
import dht11
import time
import datetime
import sys
# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()

# read data using pin 17
instance = dht11.DHT11(pin=17)

result = instance.read()

out = str(result.temperature) + " " + str(result.humidity)

sys.stdout.write(out)
sys.stdout.flush()
sys.exit(0)

Client script

#!/usr/bin/python
import math
import time
import os
import Adafruit_CharLCD as LCD
# Initialize the LCD using the pins 
lcd = LCD.Adafruit_CharLCDPlate()

lcd.set_color(1.0, 1.0, 1.0)
lcd.clear()
ws_dht11 = ""
while True:
        ws_dht11 = str(os.system('ssh pi@weatherstation.local \'python ~/Apps/DHT11_Python/dht11_ssh_read.py\''))
        if ws_dht11<>'0 0':
                break;
print 'F'
print ws_dht11

The problem is with ws_dht11 variable. It seems it doesn't receive any value, instead of this server script is printing results. How can I repair it?

os.system returns the exit code of the process you ran, which is going to be 0 in this case (I assume ssh even passes the return code back, which is not necessarily the case). I think what you are looking for is os.popen() which executes a command and allows you to read its output to stdout.
This is a very superficial answer, I suspect this is a very weird way of achieving communication, but I'm just answering your specific question w/o thinking.

You are simply running a process which then returns said process exit code.

A better solution would be to have a listening socket to communicate between the hosts, or use some sort of HTTP API, such as REST or XML, to fetch the value from host A and host B.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM