1 使用PIR传感器作为触发器拍摄照片并将其上传到tumblr-德赢Vwin官网 网
×

使用PIR传感器作为触发器拍摄照片并将其上传到tumblr

消耗积分:0 | 格式:zip | 大小:0.53 MB | 2023-06-12

贺服窍

分享资料个

描述

使用 PIR 传感器作为触发器拍摄照片并将其上传到 tumblr。

配置你的云盾

有两种配置 Yùn 的方法。

第一个是上传示例 YunFirstConfig并设置所需的参数。

打开您的 Arduino IDE 并转到example -> Bridge -> YunFirstConfig

如果您使用的是 YunShield,请务必选择它所连接的板的名称,而不是 Yun 板!

上传草图并打开串行监视器。您将在下面的屏幕截图中看到一条消息。

 
poYBAGSBsfGABQU-AABFEuU3qFY203.png
 

按照几个简单的步骤为您的开发板设置名称、密码和网络。现在您可以将 Yùn 连接到互联网了!

配置开发板的第二种方法是通过浏览器进行。

在开始之前确保你的 yun-shield 处于访问模式。在您的计算机上的可用 WiFi 网络中检查云的名称。它应该是这样的

Arduino Yun-90A2DAF200DI

连接到该网络并在浏览器中键入地址

192.168.240.1

现在你应该被重定向到 yun 的配置页面,你可以在这里设置名称、密码和你想要连接的 WiFi 网络。

注意默认密码是arduino

 
pYYBAGSBsfSABWVFAABqag4iI-8604.png
配置页面
 

完毕!现在您的云盾有了名字和密码,并且可以上网了!

拍照

对于这个项目,我们使用了 quickcam communicate stx 这是一个廉价且兼容 linux 的网络摄像头,我们可以通过 USB 连接到我们的 yun-shield。

在进行这项工作之前,我们需要下载一些驱动程序:

打开你的终端并通过 ssh 连接到 yun-shield(确保你的计算机连接到 yun-shield 的同一网络)并下载所需的包

ssh root@name_of_your_yun.local
opkg update
opkg install kmod-video-gspca-zc3xx
opkg install python-openssl
opkg install fswebcam
opkg install mjpg-streamer

请注意,许多网络摄像头都适用于 UVC 驱动程序。在这种情况下使用命令:

opkg install kmod-video-uvc libwebcam libuvc

现在我们可以使用此命令拍照并存储它们

fswebcam test.png

图片将保存为.png 并命名为test。

为了让事情更有条理,我们可以创建一个文件夹来保存照片,只需在终端(通过 ssh 连接到 yun-shield)输入命令:

mkdir photos

现在要将照片保存在该文件夹中,请使用命令

fswebcam -r 640x480 photos/my_photo.jpg

我们刚刚拍了一张 640x480 分辨率的照片,将其命名为 my_photo.jpg并将其保存在文件夹photos 中。

将照片上传到 tumblr

在上传任何照片之前,我们首先需要创建一个连接到我们博客的 tumblr 应用程序。为此,请访问 https://www.tumblr.com/oauth/apps 并注册一个新应用程序。我们需要此应用程序来获取向 tumblr 发出 http POST 请求所需的所有令牌。

在表格中填写应用程序名称、描述和电子邮件联系方式。您可以使用您想要的任何链接填充其余字段,例如默认回调 URL,我们不会将它们用于此项目。

 
poYBAGSBsfaAWtraAACAg0uMSE4070.png
注册您的应用程序
 

现在您应该可以访问您的 OAuth Consumer Key和 OAuth Consumer Secret将这些标记保存在文本文件中,我们将在接下来的步骤中需要它们。 

现在我们还想获得OAuth TokenOAuth Token Secret

转到Tumblr API 控制台并使用您已有的令牌对您的应用程序进行身份验证,然后在右上角按显示密钥按钮。你应该看到类似的东西: 

 
pYYBAGSBsfqAOwk_AADbYRXcnEA895.png
您的凭据
 

现在我们有了令牌,我们可以写下将完成所有工作的 python 代码。您可以复制并粘贴下面的代码或从本教程的软件部分下载它。

请记住用您的凭据填写空白字段。

import glob
import json
import os
import  time
import urllib2
import urlparse
import oauth2
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
 
class APIError(StandardError):
   def __init__(self, msg, response=None):
       StandardError.__init__(self, msg)
 
class TumblrAPIv2:
   def __init__(self, consumer_key, consumer_secret, oauth_token, oauth_token_secret):
       self.consumer = oauth2.Consumer(consumer_key, consumer_secret)
       self.token = oauth2.Token(oauth_token, oauth_token_secret)
       self.url = "http://api.tumblr.com"
 
   def parse_response(self, result):
       content = json.loads(result)
       if 400 <= int(content["meta"]["status"]) <= 600:
           raise APIError(content["meta"]["msg"], result)
       return content["response"]
 
   def createPhotoPost(self, id, post):
       url = self.url + "/v2/blog/%s/post" %id
 
       img_file = post['data']
       del(post['data'])
       req = oauth2.Request.from_consumer_and_token(self.consumer,
                                                token=self.token,
                                                http_method="POST",
                                                http_url=url,
                                                parameters=post)
       req.sign_request(oauth2.SignatureMethod_HMAC_SHA1(), self.consumer, self.token)
       compiled_postdata = req.to_postdata()
       all_upload_params = urlparse.parse_qs(compiled_postdata, keep_blank_values=True)
 
       for key, val in all_upload_params.iteritems():
           all_upload_params[key] = val[0]
 
       all_upload_params['data'] = open(img_file, 'rb')
       datagen, headers = multipart_encode(all_upload_params)
       request = urllib2.Request(url, datagen, headers)
 
       try:
           respdata = urllib2.urlopen(request).read()
       except urllib2.HTTPError, ex:
           return 'Received error code: ', ex.code
 
       return self.parse_response(respdata)
 
register_openers()
 
#Insert here your tokens
CONSUMER_KEY = '****'
CONSUMER_SECRET = '****'
OAUTH_TOKEN = '****'
OAUTH_TOKEN_SECRET = '****'
 
DIR = 'photos/'
FILE_MASK = '*.jpg'
BLOG = '****' # put here the name of your blog i.e. arduino.tumblr.com
 
 
api = TumblrAPIv2(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
 
for img in glob.glob( os.path.join(DIR, FILE_MASK) ):
 
   date  = time.gmtime(os.path.getmtime(img))
   post = {
       'type' : 'photo',
       'date' : time.strftime ("%Y-%m-%d %H:%M:%S", date),
       'data' : img,
       'tags' : "I am a tag",
       'caption' : "I am a caption"
   }
 
   try:
       response = api.createPhotoPost(BLOG,post)
       if 'id' in response:
           print response['id']
       else:
           print response
           break
 
   except APIError:
       print "Error"
       break
 
print "Done!"
 

为了在 Yun_Shield 上上传这个脚本,我们首先需要知道我们开发板的 IP 地址。通过 ssh 和数字连接:

ifconfig

 
poYBAGSBsfyAAWf_AABmLQu9rm4889.png
标题(可选)
 

现在我们有了 IP,我们可以继续上传脚本了。您可以使用此命令将your_path替换为文件夹的路径以访问 python 脚本。

scp /your_path/YunTumblr.py root@your_ip_address:

请记住还要上传您可以在软件部分找到的 auth.py 文件: 

scp /your_path/auth.py root@your_ip_address:

最后一步是下载 python 库。通过 ssh 连接到 yun shield 并使用这些命令

ssh root@name_of_your_yun.local
opkg update
opkg install python-pip
pip install oauth2
pip install poster

我们完了!!!

要测试它,你可以数字:

fswebcam -r 640x480 photos/my_photo.jpg
python YunTumblr.py

将它们上传到 tumblr 可能需要几分钟时间,但如果一切正常,您应该会收到照片的 ID 号作为回复。

 
pYYBAGSBsf-ACI_SAAAicbnbLrU000.png
 

接线

通常所有 PIR 传感器都带有三根电线。黑色的用于接地,红色的用于 5V,彩色的用于数据。

请注意,有时电线颜色可能会因传感器而异!

 
poYBAGSBsgKAdz0DAALhZJpDa8Q584.png
 

上传云盾草图

Yun Shield 可以插入每个 Arduino/Genuino 开发板。

插入扩展板后,您必须从 Arduino IDE 中选择扩展板连接到的板,而不是 Arduino Yun 例如,如果将 Yun Shield 插入 Arduino Leonardo,则必须在 IDE 中选择 

工具/板/Arduino Leonardo


声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表德赢Vwin官网 网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

评论(0)
发评论

下载排行榜

全部0条评论

快来发表一下你的评论吧 !

'+ '

'+ '

'+ ''+ '
'+ ''+ ''+ '
'+ ''+ '' ); $.get('/article/vipdownload/aid/'+webid,function(data){ if(data.code ==5){ $(pop_this).attr('href',"//www.hzfubeitong.com/m/login/index.html"); return false } if(data.code == 2){ //跳转到VIP升级页面 window.location.href="https://m.elecfans.com/vip/index?aid=" + webid return false } //是会员 if (data.code > 0) { $('body').append(htmlSetNormalDownload); var getWidth=$("#poplayer").width(); $("#poplayer").css("margin-left","-"+getWidth/2+"px"); $('#tips').html(data.msg) $('.download_confirm').click(function(){ $('#dialog').remove(); }) } else { var down_url = $('#vipdownload').attr('data-url'); isBindAnalysisForm(pop_this, down_url, 1) } }); }); //是否开通VIP $.get('/article/vipdownload/aid/'+webid,function(data){ if(data.code == 2 || data.code ==5){ //跳转到VIP升级页面 $('#vipdownload>span').text("开通VIP 免费下载") return false }else{ // 待续费 if(data.code == 3) { vipExpiredInfo.ifVipExpired = true vipExpiredInfo.vipExpiredDate = data.data.endoftime } $('#vipdownload .icon-vip-tips').remove() $('#vipdownload>span').text("VIP免积分下载") } }); }).on("click",".download_cancel",function(){ $('#dialog').remove(); }) var setWeixinShare={};//定义默认的微信分享信息,页面如果要自定义分享,直接更改此变量即可 if(window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == 'micromessenger'){ var d={ title:'使用PIR传感器作为触发器拍摄照片并将其上传到tumblr',//标题 desc:$('[name=description]').attr("content"), //描述 imgUrl:'https://'+location.host+'/static/images/ele-logo.png',// 分享图标,默认是logo link:'',//链接 type:'',// 分享类型,music、video或link,不填默认为link dataUrl:'',//如果type是music或video,则要提供数据链接,默认为空 success:'', // 用户确认分享后执行的回调函数 cancel:''// 用户取消分享后执行的回调函数 } setWeixinShare=$.extend(d,setWeixinShare); $.ajax({ url:"//www.hzfubeitong.com/app/wechat/index.php?s=Home/ShareConfig/index", data:"share_url="+encodeURIComponent(location.href)+"&format=jsonp&domain=m", type:'get', dataType:'jsonp', success:function(res){ if(res.status!="successed"){ return false; } $.getScript('https://res.wx.qq.com/open/js/jweixin-1.0.0.js',function(result,status){ if(status!="success"){ return false; } var getWxCfg=res.data; wx.config({ //debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId:getWxCfg.appId, // 必填,公众号的唯一标识 timestamp:getWxCfg.timestamp, // 必填,生成签名的时间戳 nonceStr:getWxCfg.nonceStr, // 必填,生成签名的随机串 signature:getWxCfg.signature,// 必填,签名,见附录1 jsApiList:['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 }); wx.ready(function(){ //获取“分享到朋友圈”按钮点击状态及自定义分享内容接口 wx.onMenuShareTimeline({ title: setWeixinShare.title, // 分享标题 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享给朋友”按钮点击状态及自定义分享内容接口 wx.onMenuShareAppMessage({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 type: setWeixinShare.type, // 分享类型,music、video或link,不填默认为link dataUrl: setWeixinShare.dataUrl, // 如果type是music或video,则要提供数据链接,默认为空 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享到QQ”按钮点击状态及自定义分享内容接口 wx.onMenuShareQQ({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享到腾讯微博”按钮点击状态及自定义分享内容接口 wx.onMenuShareWeibo({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); //获取“分享到QQ空间”按钮点击状态及自定义分享内容接口 wx.onMenuShareQZone({ title: setWeixinShare.title, // 分享标题 desc: setWeixinShare.desc, // 分享描述 link: setWeixinShare.link, // 分享链接 imgUrl: setWeixinShare.imgUrl, // 分享图标 success: function () { setWeixinShare.success; // 用户确认分享后执行的回调函数 }, cancel: function () { setWeixinShare.cancel; // 用户取消分享后执行的回调函数 } }); }); }); } }); } function openX_ad(posterid, htmlid, width, height) { if ($(htmlid).length > 0) { var randomnumber = Math.random(); var now_url = encodeURIComponent(window.location.href); var ga = document.createElement('iframe'); ga.src = 'https://www1.elecfans.com/www/delivery/myafr.php?target=_blank&cb=' + randomnumber + '&zoneid=' + posterid+'&prefer='+now_url; ga.width = width; ga.height = height; ga.frameBorder = 0; ga.scrolling = 'no'; var s = $(htmlid).append(ga); } } openX_ad(828, '#berry-300', 300, 250);