Sprint 7: Particle.publish() released! Let's build a cloud-connected motion detector

Just go look at the other discussion(topic) names “Registering a callback”…in this forum. Just go back to the main menu.

thank you

1 Like

Confirmed !! example codes from @Playagood and @bko works just fine ! Thank you very much !

@omarojo So after getting this working what do you think is the easiest way for a beginner to get started using the Spark.publish() features?

Well as you mentioned, you wanted to send and email every time a publish event is sent.
So its not exactly for beginners but my best guest is:

1- Prepare your spark core code to send the publish event Spark.publish(“sendemail”, “something happened !”);
when ever you need it.

2- Install node.js
3- install node.js module/package using npm package manager: "npm install eventsource"
4- Use @Playagood example code. create a .js file then execute it with “node myprogram”

var EventSource = require('eventsource');
var es = new EventSource('https://api.spark.io/v1/events/?access_token={{YOUR_ACCESS_TOKEN}}');

es.addEventListener('sendemail', function(event){

//Send email procedure  with  event.data 
console.log(event.data);

},false);

So… thats it. You need to search how to send emails with Node.js there might be a module/package for that I guess.

1 Like

By way of potential reminder;

It would appear that none of the String class oriented Spark.published functions, which are documented int he main and current documentation page, are in fact present (latest git pull, local build, master)

Documented …

Spark.publish(String eventName);
Spark.publish(String eventName, String data);
Spark.publish(String eventName, String data, int ttl);
Spark.publish(String eventName, String data, int ttl, PRIVATE);

But according to spark_utilities.h, only these publish functions are declared …

static void publish(const char *eventName);
static void publish(const char *eventName, const char *eventData);
static void publish(const char *eventName, const char *eventData, int ttl);
static void publish(const char *eventName, const char *eventData, int ttl, Spark_Event_TypeDef eventType);

They are not implemented in the .cpp counterpart file, either.

Cheers.

Hi @gruvin,

Thanks for the reminder! These are built and were submitted as a pull request to the firmware here: https://github.com/spark/core-firmware/pull/147 – we’re just waiting on review and for them to be merged into the firmware. They should be included in the next firmware push. Sorry about the misleading documentation.

Thanks!
David

Wow, there goes my plans for doing something really cool with Spark.publish() until the next push. :frowning:

I don’t understand why you guys typed them all as constants? At first I was like, nah, @gruvin can’t be right, but sure enough this…

void setup() {
    pinMode(A0, INPUT);
}

void loop() {
    int x = analogRead(A0);
    Spark.publish(x);
} 

…fails to compile. Sighs

Ah well! :smiley:

@timb does .c_str() help?

1 Like

@timb, do you mean you want your event name to be the integer value x ?

Wouldn't you rather do something like:

Spark.publish("pins/A0", String(x).c_str());
1 Like

@dave we took like 30 minutes to think about that the other time with the help of @bko :stuck_out_tongue: :stuck_out_tongue:

But all is temporary till the new push to compile-server2 :smiley:

@Dave Oh yeah, I meant to put name then data. I’m more familiar with C versus C++ so .c_str never occurred to me!

I assume the next update will support basically all data types (byte, long, int, char, string, etc.) correct?

Makes sense :slight_smile: The next release will include those public function overrides for the String class object, but I don’t think that’ll be quite the same as non-string event payloads, especially since with the overrides you can just String(x), etc, etc. Are you wanting overrides for other value types as shortcuts to convert into strings, or are you wanting to publish other value types?

Thanks,
David

The former more than anything, really. Have you thought of adding the Print Class to Spark.publish? It would be nice to have DEC, BIN, HEX, etc. options that come along with it. (So you could take an INT and have it output as an ASCII string representing the hex value.)

1 Like

@timb The String class already supports those “Print” thingies.

So this should work …

Spark.publish("eventname", String(x, HEX).c_str()); 

Oh … except for that other strange bug, meaning you may need this ugliness, for now …

Spark.publish("eventname", String(x, (unsigned char)HEX).c_str());

P.S: Have you seen my PM?

Hmmm, no. When did you send it?

1 Like

@timb I sent ya a PM also about that Sharp 2.7" screen also. Never heard back so maybe PM’s are not working.

PMs are working really :smiley:

@Dave,
as String seems to be a C++ class, would it not be possible to create an overload for the cast operator (char*)?

While still keeping c_str() for compatibility, the cast seems to be more intuitive - at least for CPPeoble :wink:

Spark.publish() was initially sent out with char* C-style strings but there is a pull request making its way into the firmware to overload it for Arduino String objects, so that should remove the need for c_str().

1 Like

Good to hear that Spark.publish() will be able to work with String, but for general use of String (char*) cast would still be useful :smiley:

Edit: Actually it’d need to be a (const char*) cast operator overload

2 Likes