-->
Page 1 of 1

make a sound via tone() function

PostPosted: Sun Apr 10, 2016 9:32 am
by espandi
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

Re: make a sound via tone() function

PostPosted: Sun Apr 10, 2016 3:14 pm
by Mmiscool
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.

Re: make a sound via tone() function

PostPosted: Tue Oct 04, 2016 3:38 pm
by rodrigocirilo
Hello, Her platform wonderful !!

It would be possible, when the ESP detects a change in any one pin (IO) he send a tone to the browser? beep warning?

Re: make a sound via tone() function

PostPosted: Wed Oct 05, 2016 1:11 am
by livetv
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.