ESP8266 send and receive sockets

…衆ロ難τιáo~ 提交于 2019-12-14 03:48:56

问题


I'm new to NodeMCU programming for ESP8266. I need to send strings to the server and receive response strings. So I wrote some code, but it doesn't work properly. My program works through time, and then displays a message that the memory is full. Can you help me?

---------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:


This is the code I'm using with latest dev firmware. I tried to adapt it to your case. It should work as is.

As usual with nodemcu, you always have to remember that it's heavily event-based.

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)


来源:https://stackoverflow.com/questions/33807693/esp8266-send-and-receive-sockets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!