As the title says... Chat on...

User avatar
By Palloquin
#32311 Hey all,

So, I'm testing and playing and get stuck on some strange behavior I can not solve.

- I'm using NodeMCU 0.9.5 build 20150318
- I use LuaLoader to program my Huzzah

I'm trying to build a simple program that will allow users to connect to the the device through a web interface, pick a network and supply a password, post that to the device and have it save the settings to a file. pretty straight forward.

If I copy the code below and use LuaLoader's "paste text" option to excute it on the device it works fine
If I save it to a file on the device and then load it using dofile() the device crashes and restarts without any apparent error output.

What am I missing?

Code: Select all   print('starting web server')
   srv=net.createServer(net.TCP)

   srv:listen(80,function(conn)
      conn:on("receive",function(conn,request)
         -- OUTPUT FULL REQUEST
         print(request)

         -- CHECK IF SSID WAS POSTED
         if(string.match(request,'SSID=')) then
         
            -- ADD CODE HERE TO SAVE WIFI SETTINGS TO FILE
   
            conn:send("<h1>Rebooting....</h1>")
          conn:close();
            collectgarbage();
            tmr.alarm(2, 1000, 0, function()
               -- WAIT A SECOND, THEN REBOOT
               print("Restarting...")
               node.restart() 
            end)
            
         else
            -- SHOW LIST OFF SSID'S
            conn:send("<h1>Scanning Access Points:</h1>")

            wifi.sta.getap(function(table)
               if table then
                  print('Wifi scan done')
                conn:send('<form method="post">')
                  conn:send('network:<BR><select name="SSID">')
                  for k,v in pairs(table) do
                     network = trim(k)
                   conn:send('<option value="' .. network .. '">|' .. network .. '|</option>')
                  end
                  conn:send('</select><BR>')
                conn:send('password:<BR><input type="text" name="password" value=""><BR>')
                conn:send('<input type="submit" value="connect"><BR>')
                conn:send('</form>')
               else
                  print('Wifi scan done, nothing found')
                  conn:send("<h1>No networks found:</h1>")
               end
         
             conn:close();
               collectgarbage();
            end)

         end
      end)
   end)

User avatar
By Palloquin
#32316 OK, can someone explain if I'm going mad, doing something wrong or what...

I've tried commenting out random lines of code in the example above. sometimes this will end up in code that is accepted, sometimes it crashes...

I have an example here that runs and one that doesn't. only difference is that I moved some code to a function:

this ends in a crash
Code: Select allfunction makeOptions(t)
   o = ''
   for k,v in pairs(t) do
   network = trim(k)
      o = o .. '<option value="' .. network .. '">' ..network .. '</option>'
   end
   return o
end


   srv=net.createServer(net.TCP)
   srv:listen(80,function(conn)
      conn:on("receive",function(conn,request)
   
         -- OUTPUT FULL REQUEST
         print(request)

         -- CHECK IF SSID WAS POSTED
         if(string.match(request,'SSID=')) then
         
            -- ADD CODE HERE TO SAVE WIFI SETTINGS TO FILE
   
            conn:send("<h1>Rebooting....</h1>")
          conn:close();
            collectgarbage();
            tmr.alarm(2, 1000, 0, function()
               -- WAIT A SECOND, THEN REBOOT
               print("Restarting...")
               node.restart() 
            end)
            
         else

            wifi.sta.getap(function(table)

               if table then
                  o = ''
                  for k,v in pairs(table) do
                  network = trim(k)
                     o = o .. '<option value="' .. network .. '">' ..network .. '</option>'
                  end

                  print(o)
                conn:send('<form method="post">')
                  conn:send('network:<BR><select name="SSID">')
                  conn:send(o)
                  conn:send('</select><BR>')
                conn:send('password:<BR><input type="text" name="password" value=""><BR>')
                conn:send('<input type="submit" value="connect"><BR>')
                conn:send('</form>')
               else
                  print('Wifi scan done, nothing found')
                  conn:send("<h1>No networks found:</h1>")
               end

             conn:close();

            end)

         end
   
      end)
   end)


This runs fine
Code: Select allfunction makeOptions(t)
   o = ''
   for k,v in pairs(t) do
   network = trim(k)
      o = o .. '<option value="' .. network .. '">' ..network .. '</option>'
   end
   return o
end


   srv=net.createServer(net.TCP)
   srv:listen(80,function(conn)
      conn:on("receive",function(conn,request)
   
         -- OUTPUT FULL REQUEST
         print(request)

         -- CHECK IF SSID WAS POSTED
         if(string.match(request,'SSID=')) then
         
            -- ADD CODE HERE TO SAVE WIFI SETTINGS TO FILE
   
            conn:send("<h1>Rebooting....</h1>")
          conn:close();
            collectgarbage();
            tmr.alarm(2, 1000, 0, function()
               -- WAIT A SECOND, THEN REBOOT
               print("Restarting...")
               node.restart() 
            end)
            
         else

            wifi.sta.getap(function(table)

               if table then
                  o = makeOptions(table)
                conn:send('<form method="post">')
                  conn:send('network:<BR><select name="SSID">')
                  conn:send(o)
                  conn:send('</select><BR>')
                conn:send('password:<BR><input type="text" name="password" value=""><BR>')
                conn:send('<input type="submit" value="connect"><BR>')
                conn:send('</form>')
               else
                  print('Wifi scan done, nothing found')
                  conn:send("<h1>No networks found:</h1>")
               end

             conn:close();
               collectgarbage();

            end)

         end
   
      end)
   end)


For completeness: I use a function trim that is declared earlier:
Code: Select allfunction trim(s)
  return (s:gsub("^%s*(.-)%s*$", "%1"))
end