ESP8266发送和接收套接字

时间:2015-11-19 15:15:38

标签: esp8266 nodemcu

我是ESP8266的NodeMCU编程新手。我需要将字符串发送到服务器并接收响应字符串。所以我写了一些代码,但它无法正常工作。我的程序通过时间工作,然后显示内存已满的消息。你能救我吗?

---------init funсtion-----------------
wifi.setmode(wifi.STATION)
wifi.sta.config("TP-LINK_ROBOT","63793246")
wifi.sta.connect()
---------------------------------------------

function hello (sck,c)
   print (c)
   sk:close()
   if c == "Thank you" then
   print("Great!")
   end 
end

function test()
sk=net.createConnection(net.TCP, 0)
sk:on("receive", hello)
sk:on("sent", function(sck)  end)
sk:connect(9999,"192.168.0.100")
sk:send("HELLO")
print("sent to server")
end

test()

1 个答案:

答案 0 :(得分:3)

这是我使用最新开发固件的代码。我试着让它适应你的情况。它应该按原样运作。

与nodemcu一样,你总是要记住它是基于事件的。

sk=net.createConnection(net.TCP, 0)
sk:on("receive", function(sck, c)
    print(c)
    if c == "Thank you" then
      print("Great!")
    end 
end )
sk:connect(9999,"192.168.0.100")
sk:on("connection", function(sck,c)
  -- Wait for connection before sending.
  sk:send("HELLO")
end)
相关问题