我参考了楼主的代码,基本上差不多,但是我的esp8266-01就是发不出数据,接收没有问题。socket:send("hi")不起作用。郁闷死了,楼主帮我看看?
----------------
host = "192.168.27.111"
port = 7030
connected = false
statusOn = "parlor_light|on"
statusOff = "parlor_light|off"
pin = 3
gpio.mode(pin, gpio.OUTPUT)
status = gpio.read(pin)
print("first status of pin3:"..status)
socket=net.createConnection(net.TCP, 0)
socketn("connection", function(sck, response)
print("on connection")
socket:send("hi")
if status==1 then
socket:send(statusOn)
print("send status on")
else
socket:send(statusOff)
print("send status off")
end
connected = true
end)
socketn("disconnection", function(sck, response)
print("disconected from server.")
connected = false
end)
socketn("receive", function(sck, response)
print("device received:"..response)
socket:send("hi")
if response=="open" then
gpio.write(pin, gpio.HIGH)
status = gpio.read(pin)
if status==1 then
socket:send(statusOn)
else
socket:send(statusOff)
end
end
if response=="close" then
gpio.write(pin, gpio.LOW)
status = gpio.read(pin)
if status==1 then
socket:send(statusOn)
else
socket:send(statusOff)
end
end
end)
socket:connect(port, host)
----------------------
输出如下:
set up wifi mode
> IP unavaiable, Waiting...
IP unavaiable, Waiting...
IP unavaiable, Waiting...
Config done, IP is 192.168.27.100
first status of pin3:0
on connection
send status off
device received:hi client. |