레이블이 rapsberry pi인 게시물을 표시합니다. 모든 게시물 표시
레이블이 rapsberry pi인 게시물을 표시합니다. 모든 게시물 표시

2015년 2월 2일 월요일

에디슨에서 pushetta를 사용해 스마트폰으로 푸쉬 메시지 보내기

Pushetta documentation에 보면 실제로 메시지를 보내는 간단한 예제들을 찾아볼 수 있다.
여기서는 에디슨에서 바로 사용할 수 있는 python과 node.js를 사용해 메시지를 보내보겠다.

python 의 경우 documentation에 있는 코드를 그대로 사용하면 된다.


import
urllib2 import json data = { "body" : "Hello World", "message_type" : "text/plain", "expire" : "2015-01-01" } req = urllib2.Request('http://api.pushetta.com/api/pushes/my%20%edision%20alert/') req.add_header('Content-Type', 'application/json') req.add_header('Authorization', 'Token c4d......89') response = urllib2.urlopen(req, json.dumps(data))

위의 코드에서 빨간색 볼드체 부분만 자신의 경우에 맞게 수정해주면 된다. 'Hello World' 부분이 자신이 실제로 보내고자 하는 메시지가 되고, my%20edision%20alert에는 자신이 만든 채널 이름을, c4d......07 부분에 자신의 API key (Pushetta 홈페이지의 Dashboard에서 확인할 수 있음)을 넣어주면 된다.

만일 위의 코드를 pushettatest.py라는 이름으로 저장했다면 다음과 같이 실행하면 된다.

# python pushettatest.py
# 

실행한 후 스마트폰을 보면 아래와 같이 메시지가 온걸 확인할 수 있을 것이다.


Node.js에서도 유사한 방법으로 메시지를 보낼 수 있다.

# cat pushettatest.js
var http=require('http');

var opt = {
  host: 'api.pushetta.com',
  path: '/api/pushes/my%20edison%20alert/',
  headers: {'Content-type': 'application/json',
            'Authorization': 'Token c4d....89'},
  method: 'POST'
};

cb = function(resp) {
  var str = '';
  resp.on('data', function(chunk) {
    str += chunk;
  });

  resp.on('end', function() {
    console.log(str);
  });
}

var msg = {'body':'Hello from nodejs'};
msg.message_type='text/plain';
msg.expire='2015-12-31';

var req = http.request(opt,cb);

req.write(JSON.stringify(msg));
req.end();
# node pushetta.js
{"success": true, "error_code": ""}
#



여기서는 에디슨에서 실행을 시켰지만 리눅스를 실행하는 다른 보드(Raspberry pi, Beaglebone Black 등)에서도 동일하게 적용할 수 있다. 또한 documentation을 참고하면 아두이노에 ethernet shield를 붙여서도 메시지를 보낼 수 있다.