Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 file.write stops while true loop from executing in python3
Hi All,

I am using a python script to control some GPIO pins on a raspberry pi.
Is as follows:

from gpiozero import PWMLED
import time
import os
from decimal import Decimal

led = PWMLED("GPIO12") 				#set the GPIO pin number you are using here. It uses the GPIO method of numbering.
						#details on pin numbering can be found here:
setZero=open("/var/www/html/alpha.txt", "w")

while True:					#a loop that never stops running
	try:					#prevents an bug with the code that would cause it to terminate when writing to alpha.txt file
		initial=open("/var/www/html/alpha.txt", "r") 	#open the alpha.txt file as read only. Declare it as "initial"
		brightness=(	#declare the variable brightness by reading the variable "initial". Convert the string into a number
		x = Decimal(brightness)		#convert the number into a decimal. Store it as the variable X
		int_brightness = round(x,2)	#round the variable x to 2 decimal places. PHP should handle this, but just in-case. Store the rounded variable as int_brightness
		led.value = int_brightness	#set the brightness of the LED to the value of int_brightnesss
		()				#return nothing if code fails
		()				#return nothing if code fails
The trouble is with the following lines:
setZero=open("/var/www/html/alpha.txt", "w")
If I run the script without these 3 lines, the code works fine. As soon as I add it, the LED stops illuminating (I assume the while true loop is not executing). I have confirmed that on startup the script successfully writes a 0 to the file though.

Any ideas on what might be causing this to fail? There is no returned output in the console
You start with brightness 0, which is equal to off. Why would anything happen?
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Brightness starts with off. This changes from a slider in php which writes to the alpha.txt file. The code in the while True loop looks for a change in the value and writes it to the GPIO pin. The reason brightness is set to 0 at the start is so that when the Pi boots up the light starts off.
I would put a print statement in the while loop. Probably a conditional one if the brightness changes, otherwise you'll get a flood of output. You'll have a variable to track the previous value for the conditional. Set that up with a dummy value to make sure it prints the first time through the loop, to confirm that the loop is executing.

My guess is the initial value is messing up the PHP somehow, but I really have no clue. That's why I suggest print statements.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  is it possible to use python to read and write from a jsx file? nadavrock 3 167 Jun-08-2019, 02:59 AM
Last Post: heiner55
  if cond1 or cond2 or ...condN is not True asheru93 4 158 Jun-03-2019, 04:24 PM
Last Post: buran
  write image into string format into text file venkat18 2 199 Jun-01-2019, 06:46 AM
Last Post: venkat18
  Com Error with macro within for loop due to creating new workbook in the loop Lastwizzle 0 73 May-18-2019, 09:29 PM
Last Post: Lastwizzle
  Delete Lines that Contain Words - Loop through files in a folder - Write to new files dj99 3 250 May-18-2019, 06:34 AM
Last Post: heiner55
  MySQL executemany - Failed executing the operation; Could not process parameters josh_marchant 1 119 May-16-2019, 04:22 PM
Last Post: woooee
  How to write to csv file SriMekala 1 80 May-14-2019, 01:58 PM
Last Post: buran
  how can I write my function output on CSV file go127a 13 324 May-05-2019, 01:55 PM
Last Post: go127a
  First for loop stops after first iteration Divanova94 10 448 May-01-2019, 04:27 PM
Last Post: buran
  Error executing Jupyter command 'notebook': [Errno 'jupyter-notebook' not found] 2 Newtopython123 10 10,564 Apr-25-2019, 07:30 AM
Last Post: banu0395

Forum Jump:

Users browsing this thread: 1 Guest(s)