Erni
Posts: 4
Joined: Tue Jul 21, 2015 2:32 pm

Webserver

Mon Sep 07, 2015 2:21 pm

I have just got my WiFiMCU board and am trying the various LUA examples.

The GPIO module works without problems.
Also I can make a list of AP's like this:

Code: Select all

function listap(t) if t then for k,v in pairs(t) do print(k.."\t"..v);end else print('no ap') end end
wifi.scan(listap)


So the net module is working OK.
But I cant make a webserver working.
This is the closest I can make

Code: Select all

--net demo
print("------net demo------")
print("------webserver demo------")
cfg={ssid = 'xxx',pwd = 'xxx'}
wifi.startsta(cfg)
cfg=nil
skt = net.new(net.TCP,net.SERVER)
net.on(skt,"accept",function(clt,ip,port)
print("accept ip:"..ip.." port:"..port.." memory:"..mcu.mem())
net.send(clt,"HTTP/1.1 200 OK\r\n")
net.send("Connection: close\r\n\r\n")
net.send("<html><body>")
net.send("<h1>Welcome to WiFiMCU!</h1></body></html>\n")
end)
net.start(skt,80)


If I try to connect via a browser I can make a connection, but the module restart when the connection are made, so nothing show up in the browser.
Has anybody made a working example ?

doit_yp
Posts: 21
Joined: Wed Aug 19, 2015 1:06 am

Re: Webserver

Wed Sep 09, 2015 2:55 am

Hi, Erni

According to the HTTP protocol, your code is not correct.

You can download the webserver.lua file to WiFiMCU directly via WiFiMCU STUDIO, then run it.
https://github.com/SmartArduino/WiFiMCU/blob/master/Document/demos/5%20net/1-1%20webserver.lua



Erni wrote:I have just got my WiFiMCU board and am trying the various LUA examples.

The GPIO module works without problems.
Also I can make a list of AP's like this:

Code: Select all

function listap(t) if t then for k,v in pairs(t) do print(k.."\t"..v);end else print('no ap') end end
wifi.scan(listap)


So the net module is working OK.
But I cant make a webserver working.
This is the closest I can make

Code: Select all

--net demo
print("------net demo------")
print("------webserver demo------")
cfg={ssid = 'xxx',pwd = 'xxx'}
wifi.startsta(cfg)
cfg=nil
skt = net.new(net.TCP,net.SERVER)
net.on(skt,"accept",function(clt,ip,port)
print("accept ip:"..ip.." port:"..port.." memory:"..mcu.mem())
net.send(clt,"HTTP/1.1 200 OK\r\n")
net.send("Connection: close\r\n\r\n")
net.send("<html><body>")
net.send("<h1>Welcome to WiFiMCU!</h1></body></html>\n")
end)
net.start(skt,80)


If I try to connect via a browser I can make a connection, but the module restart when the connection are made, so nothing show up in the browser.
Has anybody made a working example ?
Software engineer from DoIT
yangping@doit.am

Erni
Posts: 4
Joined: Tue Jul 21, 2015 2:32 pm

Re: Webserver

Wed Sep 09, 2015 11:12 am

Thankyou for the answer doit_yp

Unfortunately your code was the first I tried but I get this error:

Code: Select all

stdin:1: nesting of [[...]] is deprecated near '['


According to the HTTP protocol, your code is not correct.


Ok, I changed my code, and now I have a working webserver

Code: Select all

--net demo
print("------net demo------")
print("------webserver demo------")
cfg={ssid = 'xxx',pwd = 'xxx'}
wifi.startsta(cfg)
cfg=nil
print("IP :"..wifi.sta.getip())
skt = net.new(net.TCP,net.SERVER)
net.on(skt,"receive",function(clt,d) print("receive:clt:"..clt.."data:"..d)
net.send(clt,"HTTP/1.1 200 OK")
net.send(clt,"Content-Type: text/html")
net.send(clt,"Connection: close\r\n\r\n")
net.send(clt,"<!DOCTYPE HTML>\r\n")
net.send(clt,"<html><head><meta  content='text/html; charset=utf-8'>\n</head><body>")
net.send(clt,"<h1>Welcome to WiFiMCU!</h1></body></html>\n")
end)
net.start(skt,80)

KevinA
Posts: 13
Joined: Tue Nov 03, 2015 7:33 pm

Re: Webserver

Sat Nov 14, 2015 5:38 pm

Trying to use the demo files while I followed the example I kept getting
-->Upload file: 'webserver.lua' successful<--
dofile('webserver.lua')
webserver.lua:1: unexpected symbol
All the demo files in the WiFiMCU-master\WiFiMCU-master\Document\demos have Unix line termination (LF Line Feed) when I replace LF with CR+LF (DOS) the files would upload to WiFiMCU Studio and have the correct size.

Hint: The Size(Bytes) showed 0 for the webserver.lua until I replaced the line termination then it was 455 bytes
How did the CR/LF get switched to LF?

doit_yp
Posts: 21
Joined: Wed Aug 19, 2015 1:06 am

Re: Webserver

Wed Nov 18, 2015 1:00 am

Use notepad++ may solve the problem
Software engineer from DoIT
yangping@doit.am

keithr
Posts: 5
Joined: Wed Nov 04, 2015 4:33 am

Re: Webserver

Thu Nov 19, 2015 6:31 am

All the demo files in the WiFiMCU-master\WiFiMCU-master\Document\demos have Unix line termination (LF Line Feed) when I replace LF with CR+LF (DOS) the files would upload to WiFiMCU Studio and have the correct size.


Simple solution in Wifimcu-studio in main.cs at line 572
Remove:

Code: Select all

stringFileData = content.Split(new string[] { "\r\n" }, StringSplitOptions.None);

Add:

Code: Select all

content = content.Replace("\r", "");
stringFileData = content.Split(new string[] { "\n" }, StringSplitOptions.None);

The code should then work with both Unix style new lines and Windows ones.

doit_yp
Posts: 21
Joined: Wed Aug 19, 2015 1:06 am

Re: Webserver

Fri Nov 20, 2015 8:45 am

Thanks for your work!
Software engineer from DoIT
yangping@doit.am

Asturias
Posts: 11
Joined: Tue Jan 12, 2016 7:52 pm
Location: France

Re: Webserver

Sun Jan 24, 2016 6:43 pm

Hi,
Erni, I am trying to make your server example working but I don't get anything on firefox ... I am connected but " Welcome to WiFiMCU" is not displayed ... Which browser do you use ?
Thank you
Salutations

AVAKS
Posts: 2
Joined: Tue Feb 02, 2016 9:56 am

Re: Webserver

Tue Feb 02, 2016 10:00 am

Try this

Code: Select all

skt = net.new(net.TCP,net.SERVER)
net.on(skt,"receive",function(clt,data)
local kdesi={}
kdesi=string.find(data,"pwmi=")
if kdesi~=nil then
   if string.sub(data, kdesi+5)=="Toggle" then
   gpio.mode(17,gpio.OUTPUT);
   gpio.toggle(17)
   end
end
net.send(clt,'HTTP/1.1 200 OK\n\n')
net.send(clt,'<!DOCTYPE HTML>\n')
net.send(clt,'<html>\n')
net.send(clt,'<head><meta content="text/html; charset=utf-8">\n')
net.send(clt,'<title>WIFIMCU</title></head>\n')
net.send(clt,'<body><h1>Sample GPIO 17 output control</h1>\n')
net.send(clt,'<form action="" method="POST">\n')
net.send(clt,'<input type="submit" name="pwmi" value="Toggle"> "GPIO 17"<br>\n')
net.send(clt,'</form>\n')
net.send(clt,'</body></html>\n')
net.on(skt,"sent",function(skt)  net.close(skt) end)
end)
net.start(skt,80)

Asturias
Posts: 11
Joined: Tue Jan 12, 2016 7:52 pm
Location: France

Re: Webserver

Wed Feb 03, 2016 8:52 am

HI,
Thnak you very much AVAKS


Return to “WiFiMCU”

Who is online

Users browsing this forum: No registered users and 2 guests