亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Manufacturing Technology >> 制造工艺

Pi-Arduino Internet Radio

组件和用品

Arduino UNO
× 1
Raspberry Pi 3 B 型
× 1
适用于 Arduino 的 SainSmart LCD 键盘扩展板
× 1
5V 电源
× 1

应用和在线服务

python

关于这个项目


概览

我喜欢在做饭或做饭时听收音机,但我住的地方 FM 接收很糟糕。有了互联网,听世界各地的电台是一种享受,但我不喜欢用我的智能手机听音乐,所以通过大量的谷歌搜索,我找到了几个解决方案。

我的这个项目基于另一个教程: Arduino / Raspberry Pi Internet Radio by akellyirl。代码在树莓派上用 Python 编写,基本上使用 Linux 音乐播放器守护程序 (mpd) 和播放器 (mpd)。有一个库 nanpy 这让 Python 与控制按钮和 LCD 的 Arduino 通信。

我为带有按钮的 Arduino 使用了 LCD/键盘屏蔽:用于 Arduino 的 SainSmart LCD 键盘屏蔽,后来添加了一个 4 按钮条,这是我从坏掉的闹钟上报废的。一开始我使用的是带有 USB WiFi 适配器的 Raspberry Pi 2,但现在我使用的是内置 WIFI 且无需天线的 RP3。

最难的部分是找到我想要的电台的正确 URL。最后我找到了这个程序:URL HELPER,它可以嗅探您所在的站点并显示正在播放的媒体的 URL。对于声音,我使用了一对同样从废料中回收的有源电脑扬声器。

安装说明

RP3 的安装说明(来自 Arduino / Raspberry Pi Internet Radio by Arduino 中的 akellyirl):

1. 将 Raspbian 更新到最新的软件包:

$ sudo apt-get 更新 

2.安装mpd/mpc包:

$ sudo apt-get install mpc mpd 

我们希望能够通过在 Pi 上运行的 Python 与 Arduino 对话。为此,我们需要为 Python 安装 nanpy 库和为 Arduino 安装 nanpy 固件:

1. 安装 Arduino 包以构建 Arduino 的 nanpy 固件:

$ apt-get install arduino 

2.获取Python的nanpy库并从/home/pi中解压:

$ wget http://pypi.python.org/packages/source/n/nanpy/nanpy-v0.7.ta​​r.gz $ tar xvf nanpy-v0.7.ta​​r.gz 

3. 获取 Python 的 setuptools(nanpy 的一个依赖)并提取:

$ wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefe74e $ tar xvf setuptools-0.6cz11.tar /代码> 

4. 获取 Python 的 pyserial(nanpy 的依赖)并提取:

$ wget http://pypi.python.org/packages/source/p/pyserial/pyserial-2.6.tar.gz $ tar xvf pyserial-2.6.tar.gz 

5.进入setuptools目录,安装Python setuptools:

$ cd setuptools-0.6c11 $ sudo python setup.py install 

6.进入nanpy目录,安装Python nanpy库:

$ cd ../nanpy $ sudo python setup.py install 

7.进入串口目录,安装Python串口库:

$ cd ../pyserial-2.6 $ sudo python setup.py install 

现在我们已经有了我们需要的文件,让我们把 nanpy 固件放在 Arduino 上。

固件

nanpy 允许在 Raspberry Pi 上运行的 Python 程序使用传统的 Arduino-Sketch 语法来操作 Arduino,例如从 Arduino 读取模拟输入的 Python 程序可能会读取:

int val =Arduino.analogRead(14) 

Nanpy 有两个组件:

  • 一个 Python 库,允许以熟悉的语法编写 Arduino 命令并与 Arduino 通信;
  • 在 Arduino 上运行的固件,与 Pi 上的 Python 通信。

我们在上一步安装了nanpy的Python部分并在Pi上下载了nanpy的Arduino部分。现在我们需要将 nanpy 固件上传到 Arduino。

将 Arduino 插入 Raspberry Pi 的 USB。

1.进入nanpy固件目录并上传到Arduino:

$ cd ../nanpy/firmware $ export BOARD=uno $ make $ make upload 

如果您收到在 /dev/ttyACM0 上找不到该设备的报告,请将 Arduino 插入另一个 USB 端口。

我创建了一个播放列表脚本和一个 Python 脚本(在代码选项卡中发布)并使 Python 脚本在 Raspberry 启动时自动运行。我还添加了另一个按钮来显示一些菜单选项,例如显示 IP 或选择音频输出。

最后更新:我重新制作:,添加了用于拨号和音量的旋转编码器,并 3d 打印了外壳:


代码

  • piradio
  • radio_plylist.sh
piradioPython
我不是 Python 程序员(这主要是从我在 google 中找到的比特中复制/过去的。
到目前为止它有效:)
这个脚本应该在 pi 启动时加载,我添加到 crontab task
from datetime import datetimefrom subprocess import *from time import sleep, strftimefrom Queue import Queuefrom threading import Threadimport osfrom nanpy import Arduino, Lcdimport socketArduino.pinMode(14, input) lcd =Lcd([8,9,4,5,6,7],[16,2]) # 为 Sainsmart Shieldlcd.printString("Ruben's RadioZ",0,0)lcd.printString 设置 LCD 引脚("Loading" + "."*3,0,1)sleep(5)max_trax =10x =1loop_menu =1loop_radio =1stations=[]#添加按钮按下时显示电台名称stations.append("Ecco 99 ") station.append("Galgalaz ")stations.append("Galaz ")stations.append("88 FM")stations.append("Gimel")stations.append("Radios 100fm ")stations.append("Kol Hamusica" )stations.append("WQXR New York")stations.append("WPR Winsconsin")stations.append("BBC world servic")stations.append("LINN jazz")stations.append("LINN classic")#。 .....def get_local_ip_address(target):ipaddr ='' try:s =socket.socke t(socket.AF_INET, socket.SOCK_DGRAM) s.connect((target, 8000)) ipaddr =s.getsockname()[0] s.close() 除了:pass return ipaddrprint "Raspberry Pi - Local IP Address"print( get_local_ip_address('10.0.1.1'))print(get_local_ip_address('google.com'))#显示ip地址def display_ipaddr():show_wlan0 ="ip addr show wlan0 |剪切 -d/ -f1 | awk '/inet/ {printf \"w%15.15s\", $2}'" show_eth0 ="ip addr show eth0 |剪切 -d/ -f1 | awk '/inet/ {printf \"e%15.15s\", $2}'" ipaddr ='' #ipaddr =run_cmd(show_eth0) ipaddr =get_local_ip_address('10.0.1.1') #if ipaddr =="":# ipaddr =run_cmd(show_wlan0) lcd.printString('IP Address:',0,0) lcd.printString(ipaddr,0,1) sleep(2)#menu 在第五个按钮 def displaymenu():if x==1 :lcd.printString("1. 显示",0,0) lcd.printString(" IP 地址",0,1) elif x==2:lcd.printString("2. 音频输出",0,0) lcd .printString(" to hdmi Port ",0,1) elif x==3:lcd.printString("3. Audio Output ",0,0) lcd.printString(" to Analog port",0,1) elif x ==4:lcd.printString("4. Audio Output ",0,0) lcd.printString(" Auto Sel. Port",0,1) elif x==5:lcd.printString("5.重新加载" ,0,0) lcd.printString("播放列表",0,1) elif x==6:lcd.printString("6.关机",0,0) lcd.printString("系统",0,1) else:lcd.printString("7. Exit to ",0,0) lcd.printString(" Main Menu ",0,1) def load_playlist():output =run_cmd("mpc clear") output =run_cmd("/家/pi/radio/radio_playlist.sh") def ru n_cmd(cmd):p =Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT) output =p.communicate()[0] return output def getKey():val =Arduino.analogRead(14) if val ==1023:返回“NONE” elif val <100:返回“RIGHT” elif val <150:返回“UP” elif val <330:返回“DOWN” elif val <510:返回“LEFT” elif val <750:返回"SEL" else:return "KBD_FAULT" load_playlist()def getTrack():L=[S.strip('\n') for S in os.popen('mpc').readlines()] # 获取曲目信息从mpc命令的stdout output =run_cmd("mpc current") station =output [0:16] # 挑出Station和Track info track =output [-17:-1] lcd.printString(station + " " *(16 - len(station)), 0, 0) lcd.printString(track + " "*(16 - len(track)), 0, 1) track_num =1 # 从轨道号 1os.system(" sudo service mpd restart") os.system("mpc play "+str(track_num)) # 告诉操作系统在 loop_radio ==1 时播放它:getTrack() loop_menu =1 x =1 key =getKey() if key =="UP":track_num +=1 如果 track_num> max_trax:trac k_num =1 lcd.printString(stations[track_num-1],0,0) os.system("sudo service mpd restart") os.system("mpc play" + str(track_num)) getTrack() elif key =="DOWN":track_num -=1 if track_num <1:track_num =max_trax lcd.printString(stations[track_num-1],0,0) os.system("sudo service mpd restart") os.system("mpc play" + str(track_num)) getTrack() elif key =="LEFT":os.system("mpc volume +10") lcd.printString(16*" ", 0, 0) lcd.printString(16*" ", 0, 1) output =run_cmd("mpc volume") lcd.printString("VOLUME UP:", 0, 0) lcd.printString(output, 5, 1) sleep(.25) lcd.printString(16*" " ,0,0) lcd.printString(16*" ",0,1) elif key =="RIGHT":os.system("mpc volume -10") lcd.printString(16*" ",0,0) lcd.printString(16*" ", 0,1) output =run_cmd("mpc volume") lcd.printString("VOLUME DOWN:", 0, 0) lcd.printString(output, 5, 1) sleep(.25 ) lcd.printString(16*" ",0,0) lcd.printString(16*" ",0,1) elif key =="SEL":while loop_menu ==1:displaymenu() key =getKey() if键 =="右":os.system("mpc volume +10") lcd.printString(16*" ", 0, 0) lcd.printString(16*" ", 0, 1) output =run_cmd("mpc volume") lcd.printString("VOLUME UP:", 0, 0) lcd.printString(output, 5, 1) sleep(.25) lcd.printString(16*" ",0,0) lcd.printString(16*" ",0,1) elif key =="左":os.system("mpc volume -10") lcd.printString(16*" ",0,0) lcd.printString(16*" ", 0,1) output =run_cmd("mpc volume") lcd .printString("VOLUME DOWN:", 0, 0) lcd.printString(output, 5, 1) sleep(.25) lcd.printString(16*" ",0,0) lcd.printString(16*" ", 0,1) elif key =="UP":if x <=1:x =7 else:x =x - 1 elif key =="DOWN":if x>=7:x =1 else:x =x + 1 elif key =="SEL":if x ==1:display_ipaddr() #get_local_ip_address() sleep(1) elif x ==2:output =run_cmd("amixer -q cset numid=3 2") lcd。 printString("Audio OUT-->HDMI", 0, 0) lcd.printString("output", 0, 1) sleep(.5) elif x ==3:output =run_cmd("amixer -q cset numid=3 1") lcd.printString("Audio OUT->Analog", 0, 0) lcd.printString("output ", 0, 1) sleep(.5) elif x ==4:output =run_cmd("amixer -q cset numid=3 0") lcd.printString("Audio OUT-> Auto", 0, 0) lcd.printString("output", 0, 1) sleep(.5) elif x ==5:load_playlist() os.system('mpc play 1') elif x ==6:lcd.printString("Good Bye ", 0, 0) lcd.printString("Have a Nice Day ", 0, 1) output =run_cmd("mpc stop") # =run_cmd("sudo shutdown now") quit() elif x ==7:loop_menu =0 getTrack() break elif key =="RIGHT":os.system("mpc volume +2") lcd.printString(16*" ",0,0) lcd.printString(16*" ", 0,1) output =run_cmd("mpc volume") lcd.printString("VOLUME UP:", 0 , 0) lcd.printString(output, 5, 1) sleep(.25) lcd.printString(16*" ",0,0) lcd.printString(16*" ",0,1) elif key =="LEFT ":os.system("mpc volume -2") lcd.printString(16*" ", 0, 0) lcd.printString(16*" ", 0, 1) output =run_cmd("mpc volume") lcd. printString("VOLUME DOWN:", 0, 0) lcd.printString(output, 5, 1) sleep(.25) lcd.printString(16*" ",0,0) lcd.printString(16*" ",0) ,1)
radio_plylist.shSH
这是我的以色列电台播放列表
#! /bin/shmpc 清除 1>/dev/null#1 ecco99mpc 添加 http://99.livecdn.biz/99fm_aac#2 galgalazmpc 添加 http://glglzwizzlv.bynetcdn.com/glglz_mp3#3 galazmpc 添加 http://glzwizzlv .bynetcdn.com/glz_mp3#4 88 fmmpc 添加 http://ibala.vidnt.com:8000/iba_radio-88fmM#5 gimelmpc 添加 http://ibala.vidnt.com:8000/iba_radio-gimelM#6 无线电 100 fmmpc添加 http://100fm.streamgates.net/Radios100Fm#7 kol hamusicampc 添加 http://ibala.vidnt.com:8000/iba_radio-kolmusicaM#8 wqxrmpc 添加 http://stream.wqxr.org/wqxr#9 npr currentmpc 添加 http://current.stream.publicradio.org/kcmp.mp3#10 bbcmpc 添加 http://bbcwssc.ic.llnwd.net/stream/bbcwssc_mp1_ws-eieuk#11 linn jazzmpc 添加 http://radio.linnrecords .com:8003/autodj#12 linn classicmpc 添加 http://radio.linnrecords.com:8004/autodj

示意图


制造工艺

  1. Raspberry Pi Universal Remote
  2. 一片树莓派
  3. Cycle Chaser
  4. Raspberry Pi Thief Detector
  5. Raspberry Pi 温度传感器
  6. RASPBERRY PI 家庭自动化
  7. MrRobot – 支持 Ubuntu 移动应用的机器人(涉及 Raspberry Pi 和 arduino)
  8. Raspberry Pi CD Box Robot
  9. 通过蓝牙控制的树莓派机器人
  10. 使用 Arduino 和 Raspberry Pi 构建您的 Internet 控制的视频流机器人
  11. RaspiRobot Board V2
  12. Rover