sparkwlf
Published © GPL3+

Moer Broadcast for Home Education

This is an auto audio player that's controlled by a customized schedule for children ages 5-10.

AdvancedProtipOver 12 days499
Moer Broadcast for Home Education

Things used in this project

Hardware components

CM108 USB Voice card
×1
Speaker: 3W, 4 ohms
Speaker: 3W, 4 ohms
×2
5V 3W power amplifier board
×1
3W Light shield
×1
10mm LED
×1
Raspberry Pi Zero Wireless
Raspberry Pi Zero Wireless
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian
Raspbian Stretch Lite

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Custom parts and enclosures

3D Sound Box file

Code

Untitled file

Python
#coding=utf8
#! /usr/bin/env python3
#设置定时任务
import os
import sys
import string
import signal
import shutil
import socket
import configparser
import re
import socket
import urllib.request
import subprocess
from crontab import CronTab

def getconf(conffile):
    try:
        conf= configparser.ConfigParser()
        conf.read(conffile)
        return conf.get("mytime","taskRate"),conf.get("mytime","downRate"),conf.get("mytime","configRate"),conf.get("mytime","radionameRate"),conf.get("mytime","albumRate")
    except:
        print("error is happened!")
        return '1','2','10','3','5'

def getftp(ftpfile):
    try:
        conf= configparser.ConfigParser()
        conf.read(ftpfile)
        return conf.get("myftp","add"),conf.get("myftp","port")
    except:
        print("error is happened!")
        return "sparkofworld.51vip.biz","8008"

#HTTP下载配置文件
def urldown(url,filename):
    try:
        f = urllib.request.urlopen(url)
        with open(filename, "wb") as code:
            code.write(f.read())
        return True
    except:
        return False

if __name__ == '__main__':
    myhost=socket.gethostname()
    taskfile='/home/pi/radioConfig/'+myhost+'.task'
    configfile='/home/pi/radioConfig/'+myhost+'.conf'
    taskRate='2'
    downRate='3'
    configRate='10'
    radionameRate='3'
    albumRate='5'

    ftpfile='/var/myftp.ini'	
    if os.path.exists(ftpfile):	
       ftpadd,ftpport=getftp(ftpfile)
       ftpport=ftpport.rstrip('\n')
    
    if os.path.exists(configfile):
        taskRate,downRate,configRate,radionameRate,albumRate=getconf(configfile)
    
    os.system('sudo cp /etc/crontab /etc/crontab.bak') 
    try:
        print("start write task info")
        my_cron=CronTab(user='pi')
        my_cron.remove_all()
        #alwaysjob1=my_cron.new(command='sudo python3 /home/pi/FtpUploadFiles.py')
        #alwaysjob1.setall('0 '+uploadtime+' * * '+uploadday)
        alwaysjob2=my_cron.new(command='sudo python3 /home/pi/DownConfig.py')
        #alwaysjob2.setall('0 */'+ratehour+' * * *')     #每隔几小时执行下载任务
    
        alwaysjob2.setall('*/'+taskRate+' * * * *')     #每隔分钟执行下载任务
        alwaysjob3=my_cron.new(command='sudo python3 /home/pi/DownRadiofiles.py')
        alwaysjob3.setall('*/'+downRate+' * * * *')     #每隔分钟执行下载任务
        alwaysjob4=my_cron.new(command='sudo python3 /home/pi/MonitorSensor.py')
        alwaysjob4.setall('*/6 * * * *')     #每隔6分钟检查传感器程序是否启动
        alwaysjob5=my_cron.new(command='sudo python3 /home/pi/DownAlbumofXmly.py')
        alwaysjob5.setall('*/'+albumRate+' * * * *')     #每隔5分钟执行专辑下载
        alwaysjob6=my_cron.new(command='sudo python3 /home/pi/DownSongbyName.py')
        alwaysjob6.setall('*/'+radionameRate+' * * * *')     #每隔3分钟执行搜索歌曲下载
        my_cron.write()
        print("Finish write task info")
        if os.path.exists(taskfile)==True:
            with open(taskfile,'r') as f:
                lines=f.readlines()
                for line in lines:
                    line=line.rstrip('\n')
                    taskStr=line.split('|')
                    playParam=taskStr[0]+";"+taskStr[4]+";"+taskStr[5]+";"+taskStr[6]
                    playjob=my_cron.new(command="sudo python3 /home/pi/PlayRadio.py '"+playParam+"'")
                    playtime=taskStr[1]+' '+taskStr[2]+' * * '+taskStr[3]
                    playjob.setall(playtime)
                    playjob.set_comment(taskStr[0])
                    if playjob.is_valid()==True:
                        my_cron.write()
                    else:
                        print("job1 is failed")
                print('写入crontab任务成功')   
    except Exception as e:
        os.system('sudo cp /etc/crontab.bak /etc/crontab')    #恢复备份的crontab文件
        print('写入crontab任务失败')
    
    exit(0)

Credits

sparkwlf

sparkwlf

2 projects • 0 followers
上善若水,厚德载物。

Comments