Tell me what you want, What you really, really want.

Moderator: Mmiscool

User avatar
By espandi
#45226 Having a command to let a buzzer or speaker beep with different frequencies and duration
would be cool.
Just like arduinos tone (pin number,freq,duration) command.
Regards Andi
User avatar
By Mmiscool
#45249 It looks like in the next version there will be a command added to give capability of controlling pwm frequency. This should make a tone possible.
User avatar
By livetv
#56088 Make a sound through the browser triggered by a pin change? WOW! Tall order, pal.

Here's the code I'd start with:

Code: Select allpinstat = 0
meter pinstat, 0, 1
wprint "<br><br><audio controls id='player'> <source src='/uploads/sound.mp3' type='audio/mpeg'>You need HTML5.</audio>"
wprint "<br><br><input type='submit' value='Activate' onclick='setup()'>"

wprint "<script> function setup() { "
wprint "var m=document.getElementByName('0');
wprint "m.onchange=function() { make_noise(); }"
wprint "make_noise(); } "
wprint "function make_noise() { var obj=document.getElementById('player'); obj.play(); } "
wprint "</script>"

interrupt 12, [change]
wait

[change]
pinstat =  io(laststat,12)
wait


It is not working but I don't have time to debug it. Here's the (pretty solid) theory:

I set up a meter to monitor a variable set to the status of the pin I want to monitor and set that pin on an interrupt. That's the easy part, first and last parts of the code. Next I send an audio player tag to the browser set up to play a sound that I've uploaded to the ESP8266 (this is one of the two parts that don't work). In theory, that should play the sound but the audio object isn't recognizing the file or perhaps its mime type.

Now to connect the meter to the player, I have a button set up to run a setup function in javascript. This function finds the meter (its name is "0") and creates a function as an onchange event handler. That function calls the make_noise() function which simply plays the sound file.

This should do the job except that besides the audio handler glitch, the setup() function is not finding the meter. Probably something simple but I just haven't gotten to it and have to get on with other things.