Ask and you shall receive:
I’m usually quite tentative sharing code, since I’m a bit of a hack. However, I wouldn’t have gotten anywhere without code examples shared on this forum and others. So, with significant trepidation, I’ll offer the following example of how to use publish(), subscribe(), webhooks and PHP to allow a single code base to be utilized across multiple devices to allow them to get individualized configuration information.
This example would allow two devices to get specific configuration information to allow them to subsequently publish data to a Grovestreams account. As my device population grew along with the info I wanted to send, this approach became too cumbersome, so I re-wrote it to utilize a MySQL database. That approach is far more flexible and elegant. However, I hope this suffices to show how to utilize publish() and subscribe() in a fashion other than device to device communications as I alluded to in my earlier comment.
The following is hacked together from working code as an example, but has not been tested per se.
There are three chunks of code in the example: Particle device code, Particle webhook code and PHP code. You’ll need a web server somewhere to host the PHP code.
In a nutshell, each device calls the PHP script with its device ID as the “key” to its configuration information. The PHP script replies with the device specific Grovestreams configuration info ALONG with the device ID, so that the receiving devices know which messages to process and which to ignore. They should only process responses that include their device ID.
================= Start Particle code
//you'll need these libraries for the following example
#include "SparkJson.h"
#include "string.h"
//declare some globals for all for all of this to work
char gPublishString[200]; //declare string for multiple uses
char gDeviceID[25]; //holds device ID
bool gHasCredentials = false; //indicates if credentials have been received
char gGSAPIkey[50]; //GroveStreams API key
char gComponentID[20]; //GroveStreams component ID
char gPeriod[10]; //holds the string version of the loop period in seconds
int gPeriodi = 60001; //holds the integer version of the loop period in milliseconds
void setup() {
//all your other setup() code goes in here
Particle.subscribe("hook-response/getGSconfig",getGSConfigHandler,MY_DEVICES); //directs the response from the PHP script
String myID = System.deviceID();
myID.toCharArray(gDeviceID,25);
sprintf(gPublishString,"{\"deviceID\":\"%s\"}",gDeviceID);
Particle.publish("getGSConfig",gPublishString,60,PRIVATE); //sends the configuration request via the webhook "getGSConfig"
}
void loop() {
//all your main loop code here
//I also use Particle.publish() to write data to GroveStreams via another webhook on a periodic basis in the main loop,
//but that's another example if desired!
}
void getGSconfigHandler(const char *name, const char *data){
char _marvin[200];
strncpy(_marvin,data,199);
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(_marvin);
const char* deviceID = root["deviceID"];
//check to see if the device ID in the message is the same as this device. otherwise ignore the configuration
if (strcmp(deviceID,gDeviceID)==0) {
gHasCredentials = true;
const char* period = root["period"];
strcpy(gPeriod,period);
gPeriodi = atoi(gPeriod)*1000;
Serial.print("The period in milliseconds: ");
Serial.println(gPeriodi);
const char* gsAPIkey = root["gsAPIkey"];
strcpy(gGSAPIkey,gsAPIkey);
const char* componentID = root["componentID"];
strcpy(gComponentID,componentID);
//Blink the LED to indicate we've gotten credentials
int led7 = D7;
pinMode(led7, OUTPUT);
digitalWrite(led7, HIGH);
delay(100);
digitalWrite(led7, LOW);
delay(100);
digitalWrite(led7, HIGH);
delay(100);
digitalWrite(led7, LOW);
}
return;
}
================= End Particle code
================= Start Webhook code for webhook "getGSConfig"
{
"event": "getGSconfig",
"url": "http://www.yourwebsite.com/particlestuff/photonconfig.php",
"requestType": "GET",
"query" : {
"deviceID" : "{{deviceID}}"
},
"mydevices": true,
"noDefaults": true
}
================= End Webhook code
================= Start PHP code stored on your webserver in the file "photonconfig.php"
<?php
//particlegsconfig --> get GroveStreams configuration for Particle Photon/Electron
$deviceID = $_GET["deviceID"];
$foundID = "no";
//This version simply uses IF statements to select which response is appropriate and works for a small number of devices
//A more elegant solution is to utilize a MySQL database to hold the configuration data that should be sent back to the device
//This is a somewhat of a bare bones example
//All the devices
//
//Photon1
if ($deviceID == "123456789012345678901234"){
echo "{\"deviceID\":\"123456789012345678901234\",\"gsAPIkey\":\"12345678-1234-1234-1234-123456789012\",\"componentID\":\"photon1\",\"period\":\"300\"}";
$foundID = "yes";}
//
//Photon2
if ($deviceID == "432109876543210987654321"){
echo "{\"deviceID\":\"432109876543210987654321\",\"gsAPIkey\":\"12345678-1234-1234-1234-123456789012\",\"componentID\":\"photon2\",\"period\":\"60\"}";
$foundID = "yes";}
//
if($foundID=="no") {
echo "deviceID not found";}
?>
================= End Webhook code