nodemcu上手体验.docx

上传人:b****8 文档编号:9423258 上传时间:2023-02-04 格式:DOCX 页数:14 大小:18.02KB
下载 相关 举报
nodemcu上手体验.docx_第1页
第1页 / 共14页
nodemcu上手体验.docx_第2页
第2页 / 共14页
nodemcu上手体验.docx_第3页
第3页 / 共14页
nodemcu上手体验.docx_第4页
第4页 / 共14页
nodemcu上手体验.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

nodemcu上手体验.docx

《nodemcu上手体验.docx》由会员分享,可在线阅读,更多相关《nodemcu上手体验.docx(14页珍藏版)》请在冰豆网上搜索。

nodemcu上手体验.docx

nodemcu上手体验

nodemcu上手体验

nodemcu上手体验

byHYH|2017年6月16日下午8:

11

一.固件刷写

官方网站:

参考资料:

二.串口调试

下载串口调试工具(XX),打开连接nodemcu的串口。

就可以像操作终端一样操作nodemcu了。

注意事项:

1.每一句lua语句要以回车或者英文分号结束,否则不会执行。

2.除打印的字符(双引号内的字符)外,其他的均为英文字符,由于博客限制,所有的英文符号在显示时均变为了中文符号,需要手动修改所有的符号。

三.脚本编写。

1.sdk(用于查看api)

https:

//nodemcu.readthedocs.io/en/master/en/support/

2.创建wifi并测试http。

lua脚本(wifi名:

nodemcuwifi密码:

12345678):

wifi.setmode(wifi.SOFTAP)

cfg={}

cfg.ssid=”nodemcu”

cfg.pwd=”12345678″

wifi.ap.config(cfg)

srv=net.createServer(net.TCP)srv:

listen(80,function(conn)conn:

on(“receive”,function(conn,payload)print(payload)conn:

send(“<h1>Hello,NodeMcu.</h1>”)end)end)

测试结果:

连上wifi后:

2.GPIO测试

lua脚本(低电平有效):

wifi.setmode(wifi.SOFTAP)

cfg={}

cfg.ssid=”nodemcu”

cfg.pwd=”12345678″

wifi.ap.config(cfg)

led1=1

led2=2

gpio.mode(led1,gpio.OUTPUT)

gpio.mode(led2,gpio.OUTPUT)

srv=net.createServer(net.TCP)

srv:

listen(80,function(conn)

conn:

on(“receive”,function(client,request)

localbuf=“”;

local_,_,method,path,vars=string.find(request,“([A-Z]+)(.+)?

(.+)

HTTP”);

if(method==nil)then

_,_,method,path=string.find(request,“([A-Z]+)(.+)HTTP”);

end

local_GET={}

if(vars~=nil)then

fork,vinstring.gmatch(vars,“(%w+)=(%w+)&*”)do

_GET[k]=v

end

end

buf=buf..”<center><h1>ESP8266WebControl

GPIO(GPIO1,GPIO2)</h1></center>”;

buf=buf..”<p>D1:

<ahref=\”?

pin1=ON\”>ON</a><a

href=\”?

pin1=OFF\”>OFF</a></p>”;

buf=buf..”<p>D2:

<ahref=\”?

pin2=ON\”>ON</a><a

href=\”?

pin2=OFF\”>OFF</a></p>”;

local_on,_off=“”,””

if(_GET.pin1==“ON”)then

gpio.write(led1,gpio.LOW);

elseif(_GET.pin1==“OFF”)then

gpio.write(led1,gpio.HIGH);

elseif(_GET.pin2==“ON”)then

gpio.write(led2,gpio.LOW);

elseif(_GET.pin2==“OFF”)then

gpio.write(led2,gpio.HIGH);

end

client:

send(buf);

client:

close();

collectgarbage();

end)

end)

 

3.定时器测试

lua代码(每五秒打印字符):

tmr.register(0,5000,tmr.ALARM_AUTO,function(t)

print(“timer0reached”)end)

tmr.start(0)

停止定时器:

tmr.stop(0)

4.串口操作(uart)

直接写串口(使用默认串口配置,如需改变使用uart.config()):

uart.write(0,”helloworld\n”)

–写字符串

uart.write(0,102)

–写数字

使用回调处理串口(会使调试串口异常,断电或输入quit即可恢复):

—when‘\r’isreceived.

uart.on(“data”,“\r”,

function(data)

print(“receivefromuart:

”,data)

ifdata==”quit\r”then

uart.on(“data”)—unregistercallbackfunction

end

end,0)

 

Linux下,USB转串口的设备通常是/dev/ttyUSBN(N为整数)。

使用cat命令和echo命令即可做到简单的使用。

首先打开两个终端(一个用于读,另一个用于写):

读终端:

cat设备

写终端(echo的内容用单(双)引号括起来):

echo发送到串口的内容>设备

也可以在一个终端上用,只需要在cat命令后加一个&,使其在后台运行即可,只是这样Shell的输出将和串口输出混在一起。

为了分清,可在串口输出结束后按enter使提示符出现。

5.adc(模数转换)

注意事项:

1.基准电压是3.3V而不是5V,引脚是A0

2.固件必须有adc这个模块(本文中2016没有这个模块)。

打印adc转换结果:

print(adc.read(0));

网页显示结果脚本:

wifi.setmode(wifi.SOFTAP)

cfg={}

cfg.ssid=”nodemcu”

cfg.pwd=”12345678″

wifi.ap.config(cfg)

led1=1

led2=2

gpio.mode(led1,gpio.OUTPUT)

gpio.mode(led2,gpio.OUTPUT)

srv=net.createServer(net.TCP)

srv:

listen(80,function(conn)

conn:

on(“receive”,function(client,request)

localbuf=“”;

local_,_,method,path,vars=string.find(request,“([A-Z]+)(.+)?

(.+)

HTTP”);

if(method==nil)then

_,_,method,path=string.find(request,“([A-Z]+)(.+)HTTP”);

end

local_GET={}

if(vars~=nil)then

fork,vinstring.gmatch(vars,“(%w+)=(%w+)&*”)do

_GET[k]=v

end

end

A0=string.format(“<p>A0=%d</p>”,adc.read(0))

buf=buf..”<center><h1>ESP8266WebControl

GPIO(GPIO1,GPIO2)</h1></center>”;

buf=buf..”<p>D1:

<ahref=\”?

pin1=ON\”>ON</a><ahref=\”?

pin1=OFF\”>OFF</a></p>”;

buf=buf..”<p>D2:

<ahref=\”?

pin2=ON\”>ON</a><a

href=\”?

pin2=OFF\”>OFF</a></p>”;

buf=buf..A0

local_on,_off=“”,””

if(_GET.pin1==“ON”)then

gpio.write(led1,gpio.LOW);

elseif(_GET.pin1==“OFF”)then

gpio.write(led1,gpio.HIGH);

elseif(_GET.pin2==“ON”)then

gpio.write(led2,gpio.LOW);

elseif(_GET.pin2==“OFF”)then

gpio.write(led2,gpio.HIGH);

end

client:

send(buf);

client:

close();

collectgarbage();

end)

end)6.nodemcu实现telnet服务器(实现远程控制)

1)注意事项

由于固件限制,不能多开tcp服务器(即使用telnet服务时不能使用http服务)。

文件名保存为init.lua时才会自动加载,否则需要手动启动(dofile(“文件名”))。

原官方脚本兼容linux下的telnet,如果本脚本要在linux下使用,命令输入完成后,需要多按一下Enter。

2)代码:

wifi.setmode(wifi.SOFTAP)

cfg={}

cfg.ssid=”nodemcu”

cfg.pwd=”12345678″

wifi.ap.config(cfg)

s=net.createServer(net.TCP,180)

cmd=””

s:

listen(23,function(c)

functions_output(str)

if(c~=nil)

thenc:

send(str)

end

end

node.output(s_output,0)

—re-directoutputtofunctions_ouput.

c:

on(“receive”,function(c,l)

cmd=cmd..l

if(l==’;’orl==”\r\n”)

then

node.input(cmd)

cmd=””

end

–likepcall(loadstring(l)),supportmultipleseparatelines

end)

c:

on(“disconnection”,function(c)

node.output(nil)

–unregistredirectoutputfunction,outputgoestoserial

end)

print(“WelcometoNodeMcuworld.”)

end)

文件下载:

inittelnet

3)测试

连上nodemcu的wifi热点后,在windows的cmd窗口中输入(使用本页所使用的固件的命令)

telnet192.168.4.1

然后按回车即可开始输入命令(不具有退格功能,输入只能重输。

出现>即可输下一条命令)

 

4)telnet客户端C源码(用于二次开发)

编译命令(MSYS2):

gcc-staticlibtelnet.ctelnet.c-otelnet

文件简介:

libtelnet.clibtelnet.h:

libtelnet库

telnet.c:

主程序所在文件

telnet

使用方法:

telnet程序路径ip地址端口

 

7.I2C使用

使用的是nodemcu-flasher自带的固件,下载nodemcu-flasher直接点flash烧录即可。

1)简单配置

使用I2C需要先配置I2c的gpio口(可自定义变量名,不过需要同时修改后面的指令)

id=0;(一般是0);

sda=1;(可根据实际情况修改)

scl=2;(可根据实际情况修改)

初始化i2c

i2c.setup(id,sda,scl,i2c.SLOW);

测试写(以PCF8591的DA为例,A0=A1=A2=0):

i2c.start(id);//发起起始信号

i2c.address(id,地址,

i2c.TRANSMITTER);//写设备地址(注意是高7位,比如PCF8591本来是0x90/0x91,但这里要写0x48)

i2c.write(id,值);//写第一个值,按pcf8591用法,此次写的是片内控制寄存器

i2c.write(id,值);//写第二个值,此时才是DA转换的数值

i2c.stop(id);//停止信号

测试读(以PCF8591的AD为例,A0=A1=A2=0):

i2c.start(id);//开始信号

i2c.address(id,地址,

i2c.TRANSMITTER);//设置地址

i2c.write(id,值);//写控制寄存器

i2c.stop(id);//停止信号

i2c.start(id);//开始信号

i2c.address(id,地址,

i2c.RECEIVER);//设置地址

c=i2c.read(id,1);//读一个字节并保存到C这个变量

i2c.stop(id);//停止信号

print(string.byte(c));//打印C的值

 

下面是一个用网页控制pcf8591的示例。

由init.lua与prf8591.lua组成,需要同时写到板子上。

prf8591封有3个函数。

分别是

init()

read()

write()

参数中addr为地址,con为控制寄存器内容,val为输出值。

prf8591

 

8.利用redis模块实现远程指令推送

1)条件

有一台redis数据库服务器,且有客户端使用redis的publish指令。

下载redis模块并下到nodemcu里:

2)lua编写

连接wifi

wifi.setmode(wifi.STATION)

wifi.sta.config(“SSID”,”密码”)

while(wifi.sta.getip()==nil)

do

end

print(wifi.sta.getip())//打印信息,可删除

连接redis

localredis=dofile(“redis.lua”).connect(‘服务器ip地址’,端口)

注册subscribe响应函数:

redis:

subscribe(“频道”,

function(channel,msg)

print(channel,msg)//打印信息,可删除

node.input(msg)//将获取到的信息执行

end)

redis

3)使用

启动nodemcu确认已连上热点(可连接到服务器)

登入redis:

publish频道‘推送的命令’

 

redis测试版:

redis

forwindowsx64

SourceURL:

?

2017何亚红的博客unlessotherwise

noted.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1