1

I have the following script which is meant to write/read a json file. The problem is it will only write/read the file from the command line and not when using a remote via LIRC.

import RPi.GPIO as GPIO
from time import sleep
import json

GPIO.setmode(GPIO.BOARD)

GPIO.setup(22,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(19,GPIO.OUT)
GPIO.setup(21,GPIO.OUT)
GPIO.setup(23,GPIO.OUT)

abort = False
counter = 0

# read config
try:
    with open('config.json') as readfile:
        data = json.load(readfile)
    if data['lastrun'] == 'up':
        print('blind is already up')
        abort = True
except:
    pass

data = {'lastrun': 'up'}

if abort:
    quit()

# write config
with open('config.json', 'w') as outfile:
    json.dump(data, outfile, sort_keys=True, indent=4)

print('blind up')
GPIO.output(19,GPIO.HIGH)
GPIO.output(21,GPIO.LOW)
GPIO.output(23,GPIO.HIGH)

while True:
        input_state = GPIO.input(22)
        if input_state == False:
                counter = counter + 1
                print counter
                if counter == 13:
                        GPIO.output(23,GPIO.LOW)
                        exit()
                        GPIO.cleanup()  
                sleep (0.3)

This is the entry in the lircrc file

begin
    button = KEY_4
    prog = irexec
    config = sudo python /home/pi/up.py
end
  • @jsotola - thanks. I have now edited the question with details of the lircrc entry whcih calls the script – Nick C Jan 6 at 22:27
  • If your using sudo - it will be run as root user, so I would suggest adding the full path to the config.json file ie '/home/pi/config.json' – CoderMike Jan 6 at 22:37
  • @CoderMike You should make your comment an answer so Nick can accept it. – Ingo Jan 6 at 23:54
2

Question was answered by @CoderMike in comments:

If you're using sudo - it will be run as root user, so I would suggest adding the full path to the config.json file ie '/home/pi/config.json'

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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