kajarigd
kajarigd

Reputation: 1309

Issues+Clarification: Sending message from Raspberry Pi to Android Phone using Google cloud Messaging

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:

  1. Created a GCM project online
  2. In the project, I generated a server API key with the Raspberry Pi's IP address.
  3. 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", "[email protected]", "24", "10-09-2014", "VAL")
    

My issues and questions:

  1. When I am running the above code from RPi I am getting 'Authorization Error 401'. Why?
  2. Have I completed all the steps properly? Can I simply run the above code from my RPi without installing any GCM server anywhere?
  3. I don't know what kind of code I need to write in the GCM project site to receive/send/use this message.
  4. 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:

  1. Sending notifications to Google Cloud Messaging with php gives me Unauthorized Error 401
  2. http://fryerblog.com/post/30057483199/implementing-push-notifications-with-gcm

Any help is appreciated! Thanks!

Upvotes: 3

Views: 1480

Answers (1)

kajarigd
kajarigd

Reputation: 1309

I could not solve the above error. But I could 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 the data to Android phone using SNS and GCM. It worked without any trouble.

I used web services to send data to EC2 instance. But Amazon Kinesis is a more efficient way to stream data to Amazon cloud in real time.

AWS gives free membership to try out many of their cloud utilities. Kinesis is not free, hence I didn't try it out. But I know people using it.

You can download entire Android project source code from Amazon SNS official tutorial site and use it after a little bit of personalization.

Upvotes: 0

Related Questions