完整脚本代码请在资源下载区下载:https://download.csdn.net/download/qq_45467083/12124392
使用和风天气API用来获取天气数据:https://www.heweather.com
1 注册、测试API
之后使用HTTP协议来先测试一下这个API(和风天气API链接:https://dev.heweather.com/docs/api/weather),我使用的是谷歌浏览器。输入如下:
2 树莓派使用网络API
我们使用curl工具(树莓派自带)来调用远程API,再使用jq工具来解析返回的天气信息。
#安装jq工具
sudo apt-get install jq
相关指令:
参考博客:https://www.cnblogs.com/yjt1993/p/10028089.html
shell编程学习之使用jq对json数据进行提取:https://www.cnblogs.com/nul1/p/12228785.html
获取API的bash脚本:
3 发送邮件
安装sendmail:
sudo apt-get install sendemail
参考博客:
如何使用sendEmail发送邮件:https://yq.aliyun.com/articles/514587
Unable to locate package perl(Net: & IO::Socket:):https://askubuntu.com/questions/604346/unable-to-locate-package-perlnet-iosocket
在send_email.sh后加入以下代码:
SERVER="邮箱服务器"
#qq邮箱参考
#https://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=371
#https://service.mail.qq.com/cgi-bin/help?id=28&no=167&subtype=1
FROM="发件人"
TO="收件人"
SUBJECT="今日天气"
MESSAGE="${SUGGESTIONS}"
CHARSET="utf-8"
USERNAME="发件人账号"
PASSWORD="邮箱密码"
sendmail \
-f ${FROM} \
-t ${TO} \
-u ${SUBJECT} \
-s ${SERVER} \
-m ${MESSAGE} \
-xu ${USERNAME} \
-xp ${PASSWORD} \
-v -O message-charset=${CHARSET}
比较坑的是我使用smtp.163.com服务器无法发送,最后我用的是iPhone邮箱的服务器:sendemail -f 邮箱地址 -t 邮箱地址 -s applesmtp.163.com -u "我是邮件主题" -o message-content-type=html -o message-charset=utf8 -xu 邮箱地址 -xp 邮箱授权码(注意不是邮箱密码) -m "我是邮件"
,发送成功了。
4 定时任务
https://blog.csdn.net/qq_45467083/article/details/104056367
参考书籍:树莓派开始,玩转Linux