As I could not find any tutorial or example on the Internet how to achieve this, I started to write the code by myself.
The result is not (yet) a library that you can just link to your ESP8266 code, it is more a collection of functions that you can copy into your code to use the ESP8266 to send push messages via GCM.
This code is written for the Arduino IDE, it is not usable for LUA or other coding solutions for the ESP8266 modules. But of course it can be used as a base to write something similar.
This code requires an additional library to handle JSON objects. I used ArduinoJSON, a nicely written and well working library to deal with JSON objects, JSON arrays, encoding and decoding. All credits for the ArduinoJson library go to Benoit Blanchon. You can get it from:
Blog on Good Code Smell – Github sources
Install the library as explained in the ArduinoJson Wiki: Using the library with Arduino
I used an Adafruit HUZZAH ESP8266 module to test this code. This breakout is a little bit more expensive than other available ESP8266 breakouts, but I like it because I can power it direct with 5V and it has a 5V compatible serial connection. But the functions will work on any ESP8266 module as long as you program it with the Arduino IDE.
For testing I connected the module to the local WiFi and my Android phone and tablet were connected to the same WiFi while doing the registration process. As my local internet provider I doesn’t allow to forward port 80 to a server on my local lan, I could not test it with an public domain name. But it should be working the same.
Android Studio (or Eclipse) is required to build the test application.
At least one Android phone or tablet to test the functions.
The small application I used to test the functions is a slightly changed version of the application provided by Android Push Notification using Google Cloud Messaging (GCM) – Part 2
Limitations of this article
I will not explain
- how to setup the IDEs for Arduino, ESP8266 or Android.
- how to install the ESP8266 board in the Arduino IDE
- how to program the ESP8266
- how to add libraries to the Arduino IDE or Android studio
- how to get the necessary API needed for GCM.
There are a lot of blogs/articles/tutorials and examples on the Internet about these basic requirements.
But I strongly recommend to read the articles Android Push Notification using Google Cloud Messaging (GCM) – Part 1 and Part 2. They are a very good explanation and tutorial how GCM works. Even in this article a external web server with PHP scripts is used, the explanations about GCM, how to get a API key and how to setup the Android test app is very easy to understand and helpful.
Enough basics, lets go into the function details on the next page.