- Mon Jan 25, 2016 9:39 pm
#39709
Tom Cook wrote:Has anyone successfully used an ESP8266 as a GCM endpoint? Or does anyone have pointers on where to start getting it working?
I'd like to control an ESP8266 device from a web page / android app, with something approaching "instant" response. I know about services like pubnub, but I'm failiar with AppEngine and would like to use something that doesn't mean learning a new platform. GCM seems the Google way of doing this, but they unfortunately don't have an ESP8266 or Arduino library.
Any thoughts on how to do this well?
Found a working solution. But I used a web server, the ESP8266 is just calling the web server to send GCM push messages to the Android devices. Anyway, to register Android devices to the GCM service you need a MYSQL (or SQLITE,....) database and a web server. I don't think it can be done easy with a ESP8266 only.
I used the following tutorial to setup GCM on the web server and the Android devices:
http://androidsrc.net/android-push-notification-using-google-cloud-messaging-gcm-part-1/http://androidsrc.net/android-push-notification-using-google-cloud-messaging-gcm-part-2/Then I added another PHP file to the web server to send push messages to all registered Android devices:
device_sendall.php
Code: Select all<?php
if (isset($_GET["message"])) {
$message = $_GET["message"];
include_once './gcm_sendmsg.php';
$gcm = new GCM_SendMsg();
// TODO: Instead of writing the IDs here in the code they should be pulled from the MYSQL database
// Add additional devices into this string, separated by "," and endclosed in "'"
$registatoin_ids = array('===YOUR REGISTERED_ANDROID_ID_HERE_AS===','===YOUR REGISTERED_ANDROID_ID_HERE_AS===');
date_default_timezone_set("Asia/Manila");
$timestamp = date('Y-m-d H:i:s');
$message = array("message"=>$message, "timestamp"=>$timestamp);
$result = $gcm->send_notification($registatoin_ids, $message);
echo $result;
}
?>
On the ESP I run the following code to send a "message" through the web server (device_sendall.php) to the Android devices:
Code: Select all/**
sendToGCM
sends broadcast message to my cgm server
message is broadcasted to registered Android devices
*/
void sendToGCM(String message) {
if (tcpClient.connect(myServerName, 80)) {
// This will send the request to the server
tcpClient.print(String("GET ") + "/device_sendall.php/?message=" + message + " HTTP/1.1\r\n" +
"Host: " + myServerName + "\r\n" +
"Connection: close\r\n\r\n");
tcpClient.stop();
} else {
Serial.println("connection failed");
tcpClient.stop();
connectWiFi();
}
}
Hope this helps to get started.