I know this has been asked around but everytime the example is different or more complex than what I would like to do so here goes.
First.py
value = 10 #This variable should go to the Second script
Second.py
newdata = value #This variable should come from the First script, 10
So all I want to do it pass a SINGLE variable to another python that is running independently. Please I do not want to pass all the variables from the First script to the next or call the entire second script as I have seen some examples. I am running these two scripts on the Raspberry Pi and the first script is reading some sensor data while the second scripts takes that data and further computes it. Lots of variables are used from the first script so I really don't want to pass everything, only the ones I want.
Also I would like v2.7 if that makes any difference.
Thanks
Here is my code: {The variable that I want to pass to Second.py is temp}
First.py
# External module imports
import time
import os
import datetime
import MySQLdb
os.system('sudo modprobe w1-gpio')
os.system('sudo modprobe w1-therm')
# Connect to mysql
db=MySQLdb.connect("localhost","zikmir","gforce","temp_database")
cursor=db.cursor()
while True:
# Initialization
sensor= "/sys/bus/w1/devices/28-011620ee98ee/w1_slave"
# Open the file for sensor
file = open(sensor)
# Read all of the text in the file.
text = file.read()
# Close the file now that the text has been read.
file.close()
# Split the text with new lines (\n) and select the second line.
second_line = text.split("\n")[1]
# Split the line into words, referring to the spaces, and select the 10th word (counting from 0).
temp_data = second_line.split(" ")[9]
# The first two characters are "t=", so get rid of those and convert the temperature from a string to a number.
temp = float(temp_data[2:])
# Put the decimal point in the right place and display it.
temp = temp / 1000
# Display time
t= datetime.datetime.now()
print t,temp
# Push data into mySQL
sql = "INSERT INTO time_temp VALUES(now(),%s)"
cursor.execute (sql,(temp,))
db.commit()
# Wait 5 seconds
import seven_segment
seven_segment.getdata(temp)
time.sleep(5)
Second.py
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
# GPIO ports for the 7seg pins
segments = (11,2,23,8,7,10,18,25)
# 7seg_segment_pins (11,7,4,2,1,10,5,3) + 100R inline
for segment in segments:
GPIO.setup(segment, GPIO.OUT)
GPIO.output(segment, 0)
# GPIO ports for the digit 0-3 pins
digits = (22,27,17)
# 7seg_digit_pins (12,9,8) digits 0-3 respectively
for digit in digits:
GPIO.setup(digit, GPIO.OUT)
GPIO.output(digit, 1)
num = {' ':(0,0,0,0,0,0,0),
'0':(1,1,1,1,1,1,0),
'1':(0,1,1,0,0,0,0),
'2':(1,1,0,1,1,0,1),
'3':(1,1,1,1,0,0,1),
'4':(0,1,1,0,0,1,1),
'5':(1,0,1,1,0,1,1),
'6':(1,0,1,1,1,1,1),
'7':(1,1,1,0,0,0,0),
'8':(1,1,1,1,1,1,1),
'9':(1,1,1,1,0,1,1)}
try:
while True:
def getdata(temp):
n = temp
s = str(n).rjust(3)
for digit in range(3):
for loop in range(0,7):
GPIO.output(segments[loop], num[s[digit]][loop])
if (int(time.ctime()[18:19])%2 == 0) and (digit == 1):
GPIO.output(25, 1)
else:
GPIO.output(25, 0)
GPIO.output(digits[digit], 0)
time.sleep(0.001)
GPIO.output(digits[digit], 1)
finally:
GPIO.cleanup()
I have it working on a test code but not in my main code for some reason, Here is what I tried and I was successfully able to pass the data:
firsttest.py
value = 1000
def main():
print("abc")
if __name__ == "__main__":
main()
secondtest.py
from firsttest import value
recieved = value
print recieved
The out put is indeed 1000, and I only have to run the secondtest.py and the firsttest.py gets executed automatically. But when I run my original code, the first.py does execute but I'm not having any output from the second.py I don't know if this is raspberry pi related since I'm using multiple GPIO's or if this is a programming error.
Any help would be amazing. My worse solution would be to make the first.py output to a text file and have the second.py read it from there, hopefully that wont also contradict since both scripts will be trying to use it at the same time?