General area when it fits no where else

Moderator: Mmiscool

User avatar
By cicciocb
#45722 HI all,
please find here another example, extended from the previous one.
This example shows how it's possible to set variables inside the ESP from the browser and also how GET variables from the ESP to the browser. The principle is still the same, AJAX and basic.
This example shows a slider and one textbox; the slider controls the PWM as in the previous example but the textbox receive a variable (a simple random number) at 1 second interval from the ESP.
Developing this example, you can make a web page where the information changes dynamically in function of ESP stimulus (switches, temperature, ...).
As you can see, all this with only few lines of code .....
Code: Select allmemclear
cls

' this is the default value for pwm out
pdef = 512

' this is the pin defined as pwm output
pwo 13 pdef

' this is the slider
wprint |<input type="range" id="dimmer" oninput="setPWM()" onclick="setPWM()" min="0" max="1023" value="| & pdef & |"/>|
'this is the textbox
wprint |<br><input type="text" id="myText" value="textbox_ajax">|

'this is the javascript "ajax" code
fun$ =        |function setPWM() {|
fun$ = fun$ & |lum=document.getElementById("dimmer").value;|
fun$ = fun$ & |window.event.returnValue = false;|
fun$ = fun$ & |var xmlHttp = new XMLHttpRequest();|
fun$ = fun$ & |xmlHttp.open("GET", "msg?pwm=" + lum, false);|
fun$ = fun$ & |xmlHttp.send(null);|
fun$ = fun$ & |}|
'fun$ = fun$ & |return xmlHttp.responseText;}|

' this is where the code is inserted into the html
wprint "<script>" & fun$ & "</script>"

'this is another function called by the timer 1 time/ sec
fun$ =        |function myTimer() {|
fun$ = fun$ & |var d = new Date();|
fun$ = fun$ & |var xmlHttp = new XMLHttpRequest();|
fun$ = fun$ & |xmlHttp.open("GET", "msg?req=OK", false);|
fun$ = fun$ & |xmlHttp.send(null);|
fun$ = fun$ & |var r = xmlHttp.responseText;|
fun$ = fun$ & |document.getElementById("myText").value= r;|
fun$ = fun$ & |}|
fun$ = fun$ & |var myVar = setInterval(myTimer, 1000);|

wprint "<script>" & fun$ & "</script>"

'this is where the prog will jump on slider change
'and also at the javascript timer interval (1000 msec)
msgbranch [message]
wait

[message]
msgget "pwm" mess
if mess <> "" then goto [PWM.MSG]
msgget "req" mess
'uncomment this line to see the value received
'serialprintln mess
'this is just a random number; can be a pin input, temp sensor, ....
ret = rnd(9999)
msgreturn ret
wait

[PWM.MSG]
'uncomment this line to see the value received
'serialprintln mess
pwo 13 mess
wait



CiccioCB

p.s. : I can warranty that this code is genuine :D
User avatar
By forlotto
#45755 Verify that it is good to go working code as stated I have little doubt but uggghhhh I always prefer to check stuff out myself everyone wings through stuff from time to time missing tid bits but often they are important bits ... And as much as basic has got a makeover some stuff works better other things do not work the same which is not the end of the universe but ... I'm sure you get the drift.

Words and choice of words can make for interesting conversation and good entertainment were you not entertained?

Ahhh yes thx for the other example as well these are examples that should go into documentation I have placed it in my documentation and mentioned it was your doing ;)

:P

-forlotto
User avatar
By forlotto
#49009 @cicciocb

I finally had the time to test this code and it does not work ...

Initial Value is set but onclick is now working on the slider there is no update no matter where I click on the slider...

I am using v2 alpha 24 with the following code:

Code: Select allmemclear
cls

' this is the default value for pwm out
pdef = 830

' this is the pin defined as pwm output
pwo 14 pdef

' this is the slider
wprint |<input type="range" id="dimmer" oninput="setPWM()" onclick="setPWM()" min="300" max="837" value="| & pdef & |"/>|

'this is the javascript "ajax" code
fun$ =        |function setPWM() {|
fun$ = fun$ & |lum=document.getElementById("dimmer").value;|
fun$ = fun$ & |window.event.returnValue = false;|
fun$ = fun$ & |var xmlHttp = new XMLHttpRequest();|
fun$ = fun$ & |xmlHttp.open("GET", "msg?pwm=" + lum, false);|
fun$ = fun$ & |xmlHttp.send(null);|
fun$ = fun$ & |return xmlHttp.responseText;}|

' this is where the code is inserted into the html
wprint "<script>" & fun$ & "</script>"

'this is where the prog will jump on slider change
msgbranch [message]
wait

[message]
msgget "pwm" mess
'uncomment this line to see the value received
'serialprintln mess
pwo 14 mess
wait


Only thing I changed from your code was the pin I am using. Trying to get this working so hopefully I can upload my latest dimmer that is now flicker free as a project. I hope folks will enjoy this...

In the code itself I guess from what I can tell is you are building an xml request and then preforming the request without actually returning the page to the browser however if I manually make the request http://192.168.x.x/msg?pwm=800 lets say I get a page that says no message branch defined however it does change the pwm I am assuming no message branch defined is the standard message when there is no msgreturn defined...

I love the example I wish it would work as is but it does not still stuck on the solution for a real time slider.

Maybe if you get time you could hold my hand and give me a few pointers hate bugging you folks for too much help I know how it is to be busy but I am sure it is something likely small I am missing.

I will note the only change I have made to your code is the pin number I am unaware if I am supposed to add my ip to the xmlrequest your are building I have tried to add it and it does not seem to work.

Maybe there is some missing element I need to configure with my IP or something here but to me it seems fairly straight forward.

I am using firefox browser works better than chrome for most anything else so IDK maybe I'll give chrome a shot while I await your response.
User avatar
By forlotto
#49018 Odd does not work on firefox this is a first I wonder why I googled briefly I see others have had issues with ajax sliders on firefox I am going to have to research a fix.... Chrome does not support stuff I use daily that the FF browser does I cannot see transitioning to this browser as my main browser will have to see how well MIT app inventor works with this as well...

I wonder if the onclick event is stopped dead or if it is the initial request with no return that is blocked by some security feature or if simply XML or Javascript or AJAX code just don't play nice oddly I've seen other ajax websites that worked fine chat room etc..

So for now chrome fixes the problem but it would be nice to come up with a solution that fits firefox as well possibly I could have the on click load to a 1x1 pix iframe doing the request for the iframe as it appears the loaded request works just dandy.

I really don't know but I think I'll aim for something a bit more universal as a solution I hate being tied to just chrome its a great browser don't get me wrong but I think its important to support some of the other browser alternatives or else we will wind up in a situation where there is no choice and there is not competition to improve browsers. FF has been my go to browser for years this is the first instance of basic where it has failed me and I prefer the way it runs my code vs chrome no offense to chrome fans its just a personal preference.