-1

I'm working with the ESP8266 wifi module programming it in LUA using NodeMCU. I was able to connect from an iPhone app to the TCP server created by the wifi module to send 3 floats. I parse the huge string into 3 strings and send them using uart to and Arduino. It works just fine but after about 10 inputs it crashes/freezes. I need the constant flow of data to keep coming but consistently and I'm unable to do it reliably. I've printed after each line to keep track of what is actually happening and even after that i'm still not sure what is going on.

Code `

print("11\n")
wifi.setmode(wifi.STATION)
print("22\n")
wifi.sta.config("WDTS03","Walker14!")
print("33\n")
elWiFi =(wifi.sta.getip())
if elWiFi ~= nil then
    print(wifi.sta.getip())
end
print("44\n")
if srv~=nil then
    print("444\n")
    srv:close()
    print("555 \n")
end

print("Create server \n")
srv=net.createServer(net.TCP) 
print("1\n")
if srv ~= nil then
    print("srv !=nil \n")
    srv:listen(6969,function(conn) 
    print("listening \n")
        if conn ~= nil then
        print("incoming\n")

        conn:on("receive",function(conn,numbers) 
        print("2\n")
        print(#numbers)
        print("chekcing for nil \n")
        if numbers ~= nil then 
            print("3\n")
            p = string.find(numbers, "x=")
            print("4\n")
            q = string.find(numbers, "&y")
            print("5\n")
            if p ~= nill then
                print("6\n")
                if q ~=  nil then
                    print("7\n")
                    x = (string.sub(numbers,p+2, q-1))
                    print("x=" .. x)
                end
            end --p ~= nill
            print("8\n")
            p = string.find(numbers, "y=")
            print("9\n")
            q = string.find(numbers, "&z")
            print("10\n")
            if p ~= nill then
                print("11\n")
                if q ~=  nil then
                 print("12\n")
                    y = (string.sub(numbers,p+2, q-1))
                    print("y=" .. y)
                end
            end --p ~= nill
            print("13\n")
            p = string.find(numbers, "z=")
            print("14\n")
            q = string.find(numbers, " H")
            print("15\n")
            if p ~= nill then
                print("16\n")
                if q ~=  nil then
                    print("17\n")
                    z = (string.sub(numbers,p+2, q-1))
                    print("z=" .. z)
                end
            end-- p ~= nill
            print("18\n")

        end --numbers ~= nil
        print("54\n")

        --conn:send("test\n")

        end)
        print("55 \n")
        end
        print("66 \n")
    end)
    print("77\n")

end
print("666\n")`

and I get the following output

11

22

33

44

Create server 

1

srv !=nil 

77

666

> listening 

incoming

55 

66 

listening 

incoming

55 

66 

listening 

incoming

55 

66 

listening 

incoming

55 

66 

2

338
chekcing for nil 

3

4

5

6

7

x=0.1722259521484375
8

9

10

11

12

y=-0.7733306884765625
13

14

15

16

17

z=-0.5716094970703125
18

54

2

337
chekcing for nil 

3

4

5

6

7

.
.--repeats a few times 
.

y=-0.005340576171875
13

14

15

16

17

z=-0.9838409423828125
18

54

PANIC: unprotected error in call to Lua API (attempt to call a nil value)
�l� �=+���T2n���

NodeMCU 0.9.6 build 20150704  powered by Lua 5.1.4
11

22`

It fails on the "conn:on" argument for "srv:listen" after a few times

Thanks for the help and sorry if formatting is messed up. First time

user3249979
  • 21
  • 1
  • 5

1 Answers1

1

Too long for a comment, sorry.

PANIC: unprotected error in call to Lua API (attempt to call a nil value)

Hard to tell which value was nil/null. Create a Minimal, Complete, and Verifiable Example (MCVE) to reduce the amount of code one needs to analyze.

The following snippet will eventually lead to out-of-memory due to closed upvalues. Each of your callback functions should use its own copy of the passed socket instance rather referencing the one of a wrapping callback function. The third line should NOT re-use the conn variable but define one with a new name. See https://stackoverflow.com/a/37379426/131929 for details.

srv:listen(6969,function(conn)
  if conn ~= nil then
    conn:on("receive",function(conn,numbers)

NodeMCU 0.9.6 build 20150704

Don't use those old 0.9.x binaries, they're outdated and contain lots of bugs. http://nodemcu.readthedocs.io/en/latest/en/#getting-started helps you get started with a new firmware.

Community
  • 1
  • 1
Marcel Stör
  • 22,695
  • 19
  • 92
  • 198