学习 https://blog.csdn.net/column/details/nodemcu.html
1.小灯闪烁
pin=0
flag=1
gpio.mode(pin,gpio.OUTPUT)
gpio.write(pin,gpio.LOW)
function run_led()
if (flag==1) then
gpio.write(pin,gpio.HIGH)
flag=0
else
gpio.write(pin,gpio.LOW)
flag=1
end
end
tmr.alarm(0,1000,1,run_led)
2.连接一个热点
wifi.setmode(wifi.STATION)
wifi.sta.config("OPPO",12345678)
wifi.sta.connect()
tmr.alarm(0,1000,1,function()
if(wifi.sta.getip~=nil) then
print(wifi.sta.getip())
tmr.stop(0)
end
end)
3.创建一个热点
wifi.setmode(wifi.STATIONAP)
table={}
table.ssid="haha"
table.pwd="12345678"
wifi.ap.config(table)
print(wifi.ap.getip())
4.创建一个服务器
wifi.setmode(wifi.STATIONAP)
table={}
table.ssid="haha"
table.pwd="12345678"
wifi.ap.config(table)
print(wifi.ap.getip())
sv=net.createServer(net.TCP, 30)
sv:listen(80,function(c)
c:on("receive", function(c, pl)
print(pl)
end)
c:send("hello world")
end)
5.创建一个客户端
wifi.setmode(wifi.STATIONAP)
table={}
table.ssid="haha"
table.pwd="12345678"
wifi.ap.config(table)
print(wifi.ap.getip())
sk=net.createConnection(net.TCP, 0)
sk:connect(8888,"192.168.4.2")
sk:on("receive", function(sv, c)
print(c)
end )
sk:send("hello world")
- on的用法
event: 取值为:
"connection", 连接成功时
"reconnection",重新连接成功时
"disconnection",连接断开时
"receive",接收到消息时
"sent",发送消息时
function (net.socket, [string]): 回调函数。第一个参数:是 socket.
如果事件是"receive", 第二个参数:则为接收到的字符串。
7.引脚图
image.png
d0 代表 0口 左侧第5 代表 11口 左侧第4 代表12口
image.png
8.远程控制灯
https://blog.csdn.net/huangshangcheng/article/details/80796296?utm_source=blogxgwz1