Server Web

Hi,

I would like create a little server web with one page.

Can you give me an example ? I just want show a variable on a page.

Thank you in advance,

Sébastien

Hi,
which module are you using? what exactly is your requirement? could you please elaborate a little more?

Thanks,
Alex

Hi,

I work on a modem LS300.
I created a LUA program that alllows to get back data on the seial port.

I just want to display this data on a web page ?

Thank in advance for your answer,

Sébastien

Hi,

We do not officially provide a web server in AAF (thus the lack of documentation). However there is some code that does provide this service in AAF already. The better way to get documented is to go through the Lua sources of that module ‘web’ (most accurate doc is in there :smiley: )
Here is a code snippet that display the epoch time, dynamically (time updates everytime you update the page)

require 'sched'
require 'web.compile'


local page =[[
<html>
  <head>
    <title>Some dynamic page example</title>
  </head>
  <body>
    <h1>Number of second since epoch is...</h1>
    <p><%=os.time()%></p>
  </body>
</html>
]]

local function main()
    web.site['time'] = web.compile(page)

    web.start("*", 8080)
end

sched.run(main)
sched.loop()

Note: In the code snippet above the server listen on “*” (all interfaces). You may select another interface. Also please note that on the WAN interface all ports are closed by default, so you would need to configure ALEOS in order to open/redirect a port on localhost:8080, for instance. If you access the device from local interface (USBNet or Ethernet) then you do not need to open a port.

Hi,

Thank you very much for your example.

It works good but I can’t display on my page a dynamic variable on my script LUA.

For example, I would like to display my variable “sync_Buffer”, how can do that ?

My code :

function receivefromuart(uarthandle)

	print("inside synchronize")
	local err
	local Pointeur_Write_Buffer 
	
	while true do  --  permanently read on the UART
	uarthandle:settimeout (1)
	
	local read1_buffer, err = uarthandle:read("*a") --lecture des caractères arrivant sur l'UART
	

	if read1_buffer ~= nil then 
			
			synch_buffer = synch_buffer .. read1_buffer
			print("_________________ received data is: __________________")		
			tohexandprint(synch_buffer) 
			print("______________________________________________________")
			Pointeur_Write_Buffer  = string.len(synch_buffer)
			--print ("Longeur Buffer :",Pointeur_Write_Buffer)
				if Pointeur_Write_Buffer <= 24 then 
				else
					checkintegrity(synch_buffer)
					synch_buffer = ""
				end
		
	end 
	end 

end

Thank you in advance for your answer,

Sebastien

The Webpage template that contains Lua code can only access ‘global’ variables or functions. in your case you need to make sure your variable is in the global scope.

See for example the variables dots bellow, it will be appended an additional dot every seconds:

require 'sched'
require 'web.compile'


dots = "."

function event()
    while true do
        dots = dots .. "."
        sched.wait(1)
    end
end

page =[[
<html>
  <head>
    <title>Some dynamic page example</title>
  </head>
  <body>
    <h1>Number of second since epoch is...</h1>
    <p><%=dots%></p>
  </body>
</html>
]]

function main()
    web.site['time'] = web.compile(page)

    web.start("*", 8080)
end

sched.run(main)
sched.run(event)

sched.loop()

I tried this code and got this error:

Error in the log formating! (bad argument #2 to ‘?’ (number expected, got string)) - Fallback to raw printing:
module=(WEB), severity=(INFO), format=(“Starting web server on port %d”), args=(1:[*])
Tue Dec 8 07:16:11 2015 SCHED-ERROR: In thread: 0x44a10: /root/aleos/usr/readyagent/lua/socket.lua:37: bad argument #2 to ‘bind’ (number expected, got string) stack traceback:
[C]: in function ‘bind’
/root/aleos/usr/readyagent/lua/socket.lua:37: in function ‘bind’
/root/aleos/usr/readyagent/lua/web/server.lua:37: in function ‘start’
main.lua:22: in function main.lua:19

Hello billygbarnes,

I can’t find an explanation to your problem with just those traces.
Can you please give your device type and ALEOS version please?

Regards,

Hi,

This is very likely you have the problem because you are using a (very?) old version of ALEOS.
The web server start API has changed: in old releases it used to take only port parameter so you may want to try changing
web.start("*", 8080)
to
web.start(8080)

However it is strongly advised to update to a recent version of ALEOS because it contains several fixes.
Then you should be able to use the code sample without modifications.

Let me know if this resolves your problem.

Regards,

Ok I will try that.

I have the LS300 with ALEOS 4.3.6

web.start(8080) works fine.

Thank you

Hello,

Showing a variable on the webpage works well but is there a way to call a lua function from the webpage ?

For example :

[...]
<div id="boutonReboot">
      <p><input class ="rond" type="button" OnClick="reboot()" value="Reboot"></p>
</div>
[...]

[...]
function reboot()
	system.reboot()
end
[...]

Also, when I launch the page “myIPaddress:8080” I land on :

Then

And I have to click on “Supervision_Enless” (my webpage) to display it.

Is there a way to land directly on that page whithout typing “myIpaddress:8080/Supervision_Enless” but just “myIpaddress:8080” ?

Thanks,
Dylan

Tu display a page on the ‘default’ / empty URL, you have to register your page with an empty string in the web.site table. Doing something like web.site[""]=yourpage should work.
It is indeed possible to call a Lua function in your page.

There are two ways to do that, for different purpose:
Calling a Lua function while your page is generated/served. This is useful if you want to dynamically generate the page when the use browse it. It is similar to what you are already doing: displaying a variable in the page. To do this you need to use template/compiled Lua page. You can embed Lua code whithin <% %>, including calling a function.

However from your comment it seems that what you want to do it to actually call a a Lua function when the user click on a button! Do do this you could use AJAX, but this is not well documented, you would have to dig into the source code to understand how to use it. Another alternative, simpler, would be to create another page, whit a dedicated url (for instance “reboot”), and make you button to redirect/link to that page.
The implementation of that page would call the reboot function (remember, a page ca be a function).

I attached a sample UI application that illustrate what I have mentioned above. Untar the file to see the Lua source code.
Once installed the UI is visible on 192.168.13.31:8081
updatepackage.tar (1.08 MB)

web.site[""]=yourpage works well thanks.

I will try to create an other page as you explained.

The sample is now attached to the previous post :slight_smile:

Hello,

Can I install your project on the RV50 ? I tried to create a lua project with your files in the src folder and export a local appliction with no result.

If it’s possible I’m really interrested beacause I have only .lua file in my project with html code in it. Your architecture is way better with .html .css and others in separated files.

Yes the app should work alright on any kind of devices supporting AAF. So it should work on RV50.

As I mentioned in another post, you cannot build this kind of package with the IDE (it ignores anything else than lua files). You have to manually generate the tar file. The easiest way is to first generate a template app with the IDE, get the tar file, extract it into a directory, modify/add files to the directory and regenerate a tar file.

Once you have done that you can install the modified tar file (without the IDE). To do this:

  1. Copy the tarfile on the device:
    scp myapp.tar uasuser@192.168.13.31:/tmp/RA

  2. Ask AAF to install the App
    ssh uasuser@192.168.13.31

Once logged in, you can access lua shell:

telnet localhost 2000

You should see a Lua shell prompt, type

=agent.update.localupdate("/tmp/RA/updatepackage.tar", true)

The result should be “OK”, and your app is installed.

Okay got it !

As I need to be able to install remote application, I use the AirVantage platform to install it.

But it works the same way as you explained.
When I export my project to an AirVantage Application Package, I need to untar the file to add all non .lua files in it and then re tar it to upload the project to AirVantage platform.

And the app runs well on the RV50.
I will study the code to see if I’m still able to show variable from my lua file to my html file. If yes, I will use that structure in the future.

Thanks you helped a lot !