0

I am trying to run 2 scripts sequentially. The first one gets the data and the second one enters the data into a table (which is already created).

Script 1

#!/usr/bin/env python

import RPi.GPIO as GPIO
import MySQLdb as mdb
import datetime
import time

#adjust for where your switch is connected
buttonPin = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(buttonPin,GPIO.IN)

#import time
#initialise a previous input variable to 0 (assume button not pressed last)
prev_input = 0
while True:
  #take a reading
  input = GPIO.input(18)
  #if the last reading was low and this one high, print
  if ((not prev_input) and input):
    print ("Button Pressed")
  #update previous input
    prev_input = input
  #slight pause to debounce
    time.sleep(0.025)

Script 2

conn = mdb.connect (host='localhost', user='root', passwd='xxxxxxxxx',db='paintuse')
cursor = conn.cursor()
sql = ("""INSERT INTO gunuse (`id`, `switchOnLog`) VALUES (666, GPIO.input(18))""")
try:
    cursor.execute(sql)
    cursor.commit()
except:
    conn.rollback()  
    cursor.close()

The first script runs to the time.sleep line and doesn't go any further.

5
  • How are you calling the second script? Why do you have 2 scripts to read a value and add it to a database? This could all be handled in a single script. You say it doesn't go any further. It is rerunning the infinite while loop. What do you expect to happen and what is actually happening? – Steve Robillard Jan 3 '15 at 19:49
  • I rolled back your edit because code should be formatted using the editor not as a picture. A picture can't be cut and pasted for reference in an answer, nor is it as accessible. – Steve Robillard Jan 3 '15 at 19:51
  • 1
    When is script 2 run and how? I assume the import lines are also present there? – Ghanima Jan 3 '15 at 20:00
  • From the comments above i think the most approporiate ans. would be putting the python call executing shell command, which executes the 2nd script. – thekiwi5000 Jan 3 '15 at 20:34
  • What's the reason you split the logic into two scripts? From my point of view you should just combine both scripts into one script. – framp Jan 3 '15 at 21:45
2

The first script is never ending because you have a while True, but you are not breaking it or stopping it in any way. Putting both scripts in a bash shell and calling it will not resolve that. Are you trying to run the first script endlessly as a "daemon"?

If so, first test your setup by running both scripts manually in separate terminal sessions. Once you have it working, you can install script 1 as an actual daemon/system service (google "how to install python script as a daemon" for your specific plaform).

You will also have to design some form of communication between the two scripts (using a database, a pipe, something) if you want script 2 to use information provided by script 1.

I also notice that in script 2 you are not using anything produced by script 1, in your INSERT you are reading GPIO 18 again, not trying to use the "input" variable (that wouldn't work anyway), so how is it that you expect both script to communicate?

0

Create a shell file with the following content:

#!/bin/bash

python script1.py
python script2.py

And instead of launching the scripts, you'll launch the shell file.

But why do you split the script in two files?

1
  • Because I can't figure out how to combine them. Basically I am trying to take the output of Script1 and insert it to a database (instead of screen Print). The 'switchOnLog' column is a datetime field. Put it this way. I have a conveyor belt where bottles go by and trip a micro switch. I am trying to put the output (counter) into a 'datetime' field in the database. The above script gives an error: File "script.py", line 3 sudo python script1.py ^ SyntaxError: invalid syntax – steveark Jan 4 '15 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.