ESP8266发送和接收套接字 [英] ESP8266 send and receive sockets

查看:261
本文介绍了ESP8266发送和接收套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ESP8266的NodeMCU编程的新手。我需要将字符串发送到服务器并接收响应字符串。所以我写了一些代码,但是不能正常工作。我的程序会一直运行,然后显示一条消息,指出内存已满。你能帮我吗?

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()


推荐答案

这是我在最新的dev固件中使用的代码。我试图使其适应您的情况。

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

与nodemcu一样,您必须记住它很大程度上基于事件。

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)

这篇关于ESP8266发送和接收套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆