I am using 8 channel relay, with raspberry pi 3 and programming in python. i want to read or sense the pins if they or low every 5 seconds and to print the output in .txt or .csv file. for single pin I have attached the code and it working fine but how i can expand to all 8 channels (relays).
Python code:
import time
from time import sleep # Allows us to call the sleep function to slow down
our loop
import RPi.GPIO as GPIO # Allows us to call our GPIO pins and names it just
GPIO
GPIO.setmode(GPIO.BCM) # Set's GPIO pins to BCM GPIO numbering
INPUT_PIN = 26 # Write GPIO pin number.
GPIO.setup(INPUT_PIN, GPIO.IN) # Set our input pin to be an input
# Start a loop that never ends
while True:
if (GPIO.input(INPUT_PIN) == True):
# load is turned off.
print (time.strftime ("%Y/%m/%d , %H:%M:%S"),"0")
else:
#now 20 watt load is turned ON!.
print(time.strftime ("%Y/%m/%d , %H:%M:%S"),"20")
sleep(10); # Sleep for 10 seconds.
and i want to save all the data in a .csv or .txt file with time stamp at the start and other 8 columns for pins status that are connected with relay.
My output should look like:
06/01/2018,18:54:00,1,0,1,1,1,0,0,0
06/01/2018,18:54:05,1,0,1,1,1,0,0,0
06/01/2018,18:54:10,1,0,1,1,1,0,0,0
06/01/2018,18:54:15,1,0,1,1,1,0,0,0
06/01/2018,18:54:20,0,0,0,0,0,0,0,0
import csv
import time
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
INPUT_PINS = [26, 27, 28, ] # FILL ALL 8 PIN NUMBERS HERE
[GPIO.setup(pin, GPIO.IN) for pin in INPUT_PINS]
while True:
output = []
for pin in INPUT_PINS:
if (GPIO.input(pin) == True):
# load is turned off.
value = 0
else:
#now 20 watt load is turned ON!.
value = 20
output.append(value)
with open('logs.csv', 'wa') as csvfile:
logcsv = csv.writer(csvfile, delimiter=',')
logcsv.writerow([time.strftime ("%Y/%m/%d , %H:%M:%S")] + output)
sleep(10);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.