Issues + Clarification: Send Message from Raspberry Pi to Android Phone Using Google Cloud Messaging
I am new to Raspberry Pi and Google Cloud Messaging. I want to do the following: "Raspberry Pi" reads some sensor data, and I want to send data to my Android phone in real time, that is, as soon as the sensor read is received, it will be pushed to the phone. I have looked at various ways to do this and I think using Google Cloud Messaging would be the most suitable for me. So, I did the following:
- Created a GCM project
- In the project, I created a server API key with the Raspberry Pi IP address.
-
Then the following program was launched from RPi. The API in code is the above API (taken from Google HTTP Error Messages 400: Bad Request )
import json
import urllib import urllib2 class RemoteAlert: def sendGCM(self, regid, email, entry_id, date_modified, kind): url = "https://android.googleapis.com/gcm/send' apiKey = MY_API myKey = "key=" + apiKey json_data = { "registration_id": regid, "data" : { "entry_id" : entry_id, "email": email, "date_modified": date_modified, "kind": kind, "reg_id": regid, }, } headers = {'Content-Type': 'application/json', 'Authorization': myKey} data = urllib.urlencode(json_data) req = urllib2.Request(url, data) req.add_header("Authorization", myKey) f = urllib2.urlopen(req) response = f.read() f.close() print "DONE" obj = RemoteAlert() print obj.sendGCM("1234", "xxxxxx@gmail.com", "24", "10-09-2014", "VAL")
My questions and questions:
- When I run the above code from RPi I get "401 Authorization Error". Why?
- Did I follow all the steps correctly? Can I just run the above code from my RPi without installing any GCM server somewhere?
- I don't know what code I need to write on the GCM project site in order to receive / send / use this message.
- After this piece of work, I will write a client program to get RPi data on my Android phone.
I've already checked the following pages for reference:
- Sending notifications to Google Cloud Messaging using php is giving me an unauthorized 401 error
- http://fryerblog.com/post/30057483199/implementing-push-notifications-with-gcm
Any help is appreciated! Thank!
source to share
I couldn't solve the above error. But I was able to successfully send push notification to Android phone using Amazon SNS with GCM!
From my IoT device, I sent sensor data to my web service hosted on Amazon EC2. From EC2, I pushed data to android phone using SNS and GCM. It worked without much problem.
I have used web services to send data to an EC2 instance. But Amazon Kinesis is a more efficient way to transfer data to the Amazon cloud in real time.
AWS provides a free membership to try out many of its cloud services. Kinesis is not free, so I haven't tried it. But I know people are using it.
You can download the entire Android project source code from the official Amazon SNS training site and use it after a little personalization.
source to share