I prefer using Spark.publish(), since itās pushed. You just set up a listener on a webpage, and act on it when a publish comes in. In combination with the JavaScript library, this shouldnāt be too hard.
Give it a shot, and let us know if you need any assistance.
Thanks Moors7,
Iām looking at this example from Zach regarding publishing (http://blog.particle.io/2014/03/11/spark-publish/) and think maybe Iāll modify it to publish on a button instead of the PIR.
Do you know of any similar examples I can work from (for both the publish code and listen on HTML) and Iāmm mash them together?
Thank you!
Here's an example for the subscribing: (You'll have to adjust that for HTML/javascript. The example as-is is for node.js)
This is for playing your audio, in combination with your own link:
Swapping out the PIR sensor for a button should be trivial. Just make sure you debounce the button to make sure if doesn't trigger repeatedly, or you might cross the publish threshold.
Thank you.
Iāve taken the advice and written the following for the core (using info from this blog).
The switch is on D3 and is debounced - publishing to āsound1ā
int last;
bool ready;
void setup() {
pinMode(D3, INPUT);
last = millis();
ready = true;
}
void loop() {
if (millis() - last > 200) {
if (digitalRead(D3)) {
// button pressed
if (ready) {
ready = false;
Spark.publish("sound1");
last = millis();
}
} else {
// button released
ready = true;
}
}
}
Where I get lost in on the HTML/Javascript side, especially how to cause the mp3 to play.
For instance am I on the right track on modifying this post. Iām so sorry I wasnāt sure how to integrate your above āspark/sparkjs/blob/master/examples/node/get-event-stream-forever.jsā link to my HTML code.
Try the technique at the bottom of this page using the HTML5 audio player:
Instead of an onclick action, you would call the player (playSound() in the example) in the event subscription handler when you get an event. You could have different event data play different sounds quite easily too.
Oh Hey @bko !! Iāve read heaps of your posts, really nice to meet you.
Iāve taken that code, modified it and itās now live here (back to the future pic + random horse noise).
Could you help me with the āyou would call the player (playSound() in the example) in the event subscription handler when you get an eventā part, I just donāt know how to accomplish this. Thanks!
I still canāt get it to play the mp3. Maybe a quick check is to get the page to show āonā or āoffā depending on the switch state, how do I do that?
Iām getting very close now. @bko@Moors7 thank you so much for your help so far. Just curious if you know how to activate the MP3 to play (or another series of actions that will activate a sound).
I believe the core is getting publishing correctly (I hope so, any easy way to check?)
Hereās the core code:
int last;
bool ready;
int led1 = D0;
void setup() {
pinMode(led1, OUTPUT);
digitalWrite(led1, LOW);
pinMode(D3, INPUT);
last = millis();
ready = true;
}
void loop() {
if (millis() - last > 200) {
if (digitalRead(D3)) {
// button pressed
if (ready) {
ready = false;
Spark.publish("sound1");
digitalWrite(led1,HIGH);
last = millis();
}
} else {
// button released
ready = true;
digitalWrite(led1,LOW);
}
}
}
Hereās the HTML:
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function playSound(el,soundfile) {
if (el.mp3) {
if(el.mp3.paused) el.mp3.play();
else el.mp3.pause();
} else {
el.mp3 = new Audio(soundfile);
el.mp3.play();
}
}
</script>
</head>
<body>
<script type="text/javascript">
function start() {
document.getElementById("sound1").innerHTML = "Waiting for data...";
var deviceID = "48ff68065067555026472287";
var accessToken = "91f68364396f0217f77ef56e7fdbd62cc75d6284";
var eventSource = new EventSource("https://api.spark.io/v1/devices/" + deviceID + "/events/?access_token=" + accessToken);
eventSource.addEventListener('sound1', function(e) {
playSound(this, 'http://www.w3schools.com/html/horse.mp3');
}, false);
}
</script>
<span id="dummy" onclick="playSound(this, 'http://movie-sounds.org/quotes/back-future/1-21-gigawatts.mp3');">
<img src="https://upload.wikimedia.org/wikipedia/en/d/d2/Back_to_the_Future.jpg" name="Bottom-1" width="240" height="374" border="0" id="Bottom-1"/>
</span>
</body>
</html>
Glad to see youāve gotten this far already. For the playing part, there are some things you can try. You can try the (supposedly) native tags, and control those. You can also use javascript directly.
This is for the tags: http://www.w3schools.com/tags/tag_audio.asp
And this is how you can control them: http://www.w3schools.com/tags/av_met_play.asp (switch video things for corresponding audio)
Mind you that youāll probably need to support a couple of audio formats, since not all browsers support the same things.
Give it a try, and let us know how/if that worked for you :)!