I'm running a bell app connected by ENet between two RPI 4. The button end works well, but the server end only works when I sign in to activate it. How do I get it to start at start-up in Crontab?
@reboot python3 /home/pi/Coding/BellButton.py >> /home/pi/Coding/BellButton.log
Log has nothing
#!/usr/bin/env python3
import threading
import pygame.mixer
import automationhat
import datetime
from datetime import date
import time
import socket
class ButtonServer(threading.Thread):
def __init__(self, channel, serverIP, serverPort):
threading.Thread.__init__(self)
self.channel = channel
self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.serv.bind((serverIP,serverPort))
self.serv.listen(5)
self.BellButtonOne = -1
self.BellButtonTwo = -1
self.BellButtonThree = -1
self.deamon = True
self.start()
def run(self):
while True:
conn, addr = self.serv.accept()
from_client = ""
while True:
from_client = ""
data = conn.recv(4096)
if not data:
break
from_client = data.decode()
if from_client == "Input 1 On":
self.BellButtonOne = 1
if from_client == "Input 1 Off":
self.BellButtonOne = 0
if from_client == "Input 2 On":
self.BellButtonTwo = 1
if from_client == "Input 2 Off":
self.BellButtonTwo = 0
if from_client == "Input 3 On":
self.BellButtonThree = 1
if from_client == "Input 3 Off":
self.BellButtonThree = 0
conn.close()
B8 = ButtonServer(36,"123.456.789.123",12345)
pygame.mixer.init(48000, -16, 1, 1024)
pygame.mixer.init()
sndAChr = pygame.mixer.Sound("/home/pi/Public/Active/ChristmasChurchBell.wav")
sndBChr = pygame.mixer.Sound("/home/pi/Public/Active/ChristmasDinnerBell.wav")
sndA = pygame.mixer.Sound("/home/pi/Public/Active/ChurchBell.wav")
sndB = pygame.mixer.Sound("/home/pi/Public/Active/DinnerBell.wav")
sndB1 = pygame.mixer.Sound("/home/pi/Public/Active/DinnerBell2.wav")
sndB2 = pygame.mixer.Sound("/home/pi/Public/Active/DinnerBell3.wav")
sndC = pygame.mixer.Sound("/home/pi/Public/Active/Doing.wav")
snd1 = pygame.mixer.Sound("/home/pi/Public/Active/Doing.wav")
snd15 = pygame.mixer.Sound("/home/pi/Public/Active/Quarter.wav")
snd30 = pygame.mixer.Sound("/home/pi/Public/Active/Half.wav")
snd45 = pygame.mixer.Sound("/home/pi/Public/Active/3Quarter.wav")
snd100 = pygame.mixer.Sound("/home/pi/Public/Active/Hour.wav")
sc1 = pygame.mixer.Channel(1)
sc1.play(snd1)
playDoing = False
DinnerBellNo = 0
while True:
tnow = datetime.datetime.now()
today = date.today()
ChristStartYear = date(today.year,11,19)
ChristEndYear = date(today.year + 1,1,7)
if B8.BellButtonOne == 1:
if pygame.mixer.get_busy():
playDoing = False
pygame.mixer.stop()
time.sleep(0.2)
if B8.BellButtonOne == 0:
if today >= ChristStartYear and today <= ChristEndYear:
sc1.play(sndAChr)
else:
sc1.play(sndA)
time.sleep(0.2)
if B8.BellButtonTwo == 1:
if pygame.mixer.get_busy():
playDoing = False
pygame.mixer.stop()
time.sleep(0.2)
if B8.BellButtonTwo == 0:
if today >= ChristStartYear and today <= ChristEndYear:
sc1.play(sndBChr)
else:
if DinnerBellNo == 2:
DinnerBellNo += 1
sc1.play(sndB2)
if DinnerBellNo == 1:
DinnerBellNo += 1
sc1.play(sndB1)
if DinnerBellNo == 0:
DinnerBellNo += 1
sc1.play(sndB)
if DinnerBellNo == 3:
DinnerBellNo = 0
time.sleep(0.2)
if B8.BellButtonThree == 1:
if pygame.mixer.get_busy():
playDoing = False
pygame.mixer.stop()
time.sleep(0.2)
if B8.BellButtonThree == 0:
sc1.play(sndAChr)
time.sleep(0.2)
#On Board Buttons
#if automationhat.input.one.is_on():
# if pygame.mixer.get_busy():
# playDoing = False
# pygame.mixer.stop()
# time.sleep(0.2)
# if automationhat.input.one.is_off():
# if today >= ChristStartYear and today <= ChristEndYear:
# sc1.play(sndAChr)
# else:
# sc1.play(sndA)
# time.sleep(0.2)
#if automationhat.input.two.is_on():
# if pygame.mixer.get_busy():
# playDoing = False
# pygame.mixer.stop()
# time.sleep(0.2)
# if automationhat.input.two.is_off():
# if today >= ChristStartYear and today <= ChristEndYear:
# sc1.play(sndBChr)
# else:
# if DinnerBellNo == 2:
# DinnerBellNo += 1
# sc1.play(sndB2)
# if DinnerBellNo == 1:
# DinnerBellNo += 1
# sc1.play(sndB1)
# if DinnerBellNo == 0:
# DinnerBellNo += 1
# sc1.play(sndB)
# if DinnerBellNo == 3:
# DinnerBellNo = 0
# time.sleep(0.2)
#if automationhat.input.three.is_on():
# if pygame.mixer.get_busy():
# playDoing = False
# pygame.mixer.stop()
# time.sleep(0.2)
# if automationhat.input.three.is_off():
# sc1.play(sndAChr)
# time.sleep(0.2)
#Chime Clock
if tnow.hour >= 6 and tnow.hour <= 22:
if tnow.hour % 1 == 0 and tnow.minute == 0 and tnow.second == 0:
if not pygame.mixer.get_busy():
sc1.play(snd100)
playDoing = True
time.sleep(0.2)
if tnow.hour >= 6 and tnow.hour <= 21:
if tnow.minute % 15 == 0:
if tnow.minute == 15 and tnow.second == 0:
if not pygame.mixer.get_busy():
sc1.play(snd15)
time.sleep(0.2)
if tnow.minute == 30 and tnow.second == 0:
if not pygame.mixer.get_busy():
sc1.play(snd30)
time.sleep(0.2)
if tnow.minute == 45 and tnow.second == 0:
if not pygame.mixer.get_busy():
sc1.play(snd45)
time.sleep(0.2)
if playDoing == True and not pygame.mixer.get_busy():
h=tnow.hour
if h > 12:
h -= 12
sc1.play(snd1, h-1)
playDoing = False