I am new to Raspberry Pi and Google Cloud Messaging. What I want to do is as follows: Raspberry Pi is reading some sensor data and I want to send the data to my Android Phone in real time, i.e. as soon as a sensor reading is received it will be pushed to the phone. I looked into various ways to do this, and think using Google Cloud Messaging will be most suitable for me. So, I did the following:
- Created a GCM project online
- In the project, I generated a server API key with the Raspberry Pi's IP address.
Then ran the following program from RPi. The API in the code is the above API (taken from Google Cloud Messaging HTTP Error 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 issues and questions:
- When I am running the above code from RPi I am getting 'Authorization Error 401'. Why?
- Have I completed all the steps properly? Can I simply run the above code from my RPi without installing any GCM server anywhere?
- I don't know what kind of code I need to write in the GCM project site to receive/send/use this message.
- After this part works, I will write a client program to receive the RPi data in my android phone.
I have already checked the following pages for help:
- Sending notifications to Google Cloud Messaging with php gives me Unauthorized Error 401
- http://fryerblog.com/post/30057483199/implementing-push-notifications-with-gcm
Any help is appreciated! Thanks!