raspberry pi - How to import variable (sensor input) & update it from one python script to another? -
i'm setting sensors raspberry pi in python. total python newbie here.
each sensor has it's own script read sensor , there script drives lcd display , displays imported variable sensor script.
i've gotten far working scripts run sensors , generate output, however, cannot seem import variables (temperature & ph) lcd display script. also, once have imported variables, how instruct lcd script "refresh" , fetch updated variable?
here's trimmed down version of have far, i've omitted sensor , data logging parts of each script. simplicity, script_display lcd driver, , ph_script ph , temp_script temperature.
here's simplified version of scripts:
script_display.py
import sys sys.path.insert(0, '/home/pi/raspberry-pi-sample-code') import ph_script import temp_script ph_script import ph_main temp_script import get_temp import time while true: print ph.ph_main(ph_output) print get_temp(temp) time.sleep(1) temp_script.py
from w1thermsensor import w1thermsensor import time #get temperature def get_temp(): global temp sensor = w1thermsensor(w1thermsensor.therm_sensor_ds18b20, "031683a0a4ff") activate_temp = sensor.get_temperature() temp = str(activate_temp) return temp #read temp frequency def read(): threading.timer(0.5, read).start() get_temp() time.sleep(1) try: while true: read() get_temp() print get_temp() except keyboardinterrupt: print("program ended user") ph_script.py
def ph_main(): lots , lots of code activate ph probe, , other variables, variable ph_output returns string, ph_output
try: while true: global ph_output dev.send_cmd("r") lines = dev.read_lines() in range(len(lines)): print lines[i] if lines[i][0] != '*': print lines[i] ph_output = str(lines[i]) return ph_output time.sleep(delaytime) try: while true: ph_main() except keyboardinterrupt: print("continuous polling stopped") so, again, first question, how pass global variables display script? , two, how instruct display script 'refresh' variables?
the error getting is:
traceback (most recent call last): file "script_display.py", line 8, in <module> print ph.ph_main(ph_output) nameerror: name 'ph_output' not defined looking forward input , time + help!
Comments
Post a Comment