【ESP8266之LUA开发】六、建立TCP客户端,实现ADC测量,单点接地的小知识
【摘要】
8266ADC测电压
init.lua
gpio.mode(4,gpio.OUTPUT)
gpio.mode(2,gpio.OUTPUT)
gpio.write(4,1)
if adc.force...
8266
ADC测电压
init.lua
gpio.mode(4,gpio.OUTPUT)
gpio.mode(2,gpio.OUTPUT)
gpio.write(4,1)
if adc.force_init_mode(adc.INIT_ADC) then
node.restart()
return
end
tmr.alarm(0, 1000, 1, function()
gpio.write(4,1-gpio.read(4))
end)
tmr.alarm(1, 3000, 0, function()
dofile("wifi.lua")
end)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
wifi.lua
wifi.setmode(wifi.STATIONAP)
cfg={}
cfg.ssid="Hellow8266"
cfg.pwd="11223344"
wifi.ap.config(cfg)
apcfg={}
apcfg.ssid="qqqqq"
apcfg.pwd="11223344"
wifi.sta.config(apcfg)
wifi.sta.autoconnect(1)
ClientConnectedFlage = 0
TcpConnect = nil
tmr.alarm(1, 1000, 1, function()
if ClientConnectedFlage == 0 then
Client = net.createConnection(net.TCP, 0)
Client:connect(8080,"192.168.1.103")
Client:on("receive", function(Client, data)
uart.write(0,data)
ReadAd(data)
end)
Client:on("connection", function(sck, c)
ClientConnectedFlage = 1
TcpConnect = Client
print("Link OK")
tmr.stop(1)
Client:on("disconnection", function(sck, c)
ClientConnectedFlage = 0
TcpConnect = nil
tmr.start(1)
end)
end)
if ClientConnectedFlage == 0 then
print("Link Error")
end
end
end)
function ReadAd(data)
if data == "++MD9" then
ad = adc.read(0)
if TcpConnect ~= nil then
TcpConnect:send("++MDAD="..ad)
end
end
end
uart.on("data",0,function(data)
if TcpConnect ~= nil then
TcpConnect:send(data)
end
end, 0)
printip = 0
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
printip = 0
end)
wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
if printip == 0 then
print("+IP"..T.IP)
end
printip = 1
end)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
ADC
对应引脚
对应板子的位置引脚
命令以及回复这样规定
API文档说明
adc.INIT_ADC
- 引脚输入
adc.INIT_VDD33
- 测内部
我们使用的是引脚输入,然后readadc(0)
就行。
在init.lua
里面加入
if adc.force_init_mode(adc.INIT_ADC) then
node.restart() --重启生效
return
end
- 1
- 2
- 3
- 4
然后wifi.lua
中加入
function ReadAD(data)
if data=="++MD9" then
if TcpClient~=nil then
ad = adc.read(0)
TcpClient:send(ad)
end
end
end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
并在Client:on("receive", function(Client, data)
事件监听中进行函数的调用。
对了测试的电压是0-1V
,分辨率是 1024
当悬空的时候,数据是飘忽的,正常现象。
当接到3V3
上面的时候,传回来的数据是1024
接到GND
的时候的数据
竟然是14,竟然不是0,相关具体解释可参考<这里>
文章来源: recclay.blog.csdn.net,作者:ReCclay,版权归原作者所有,如需转载,请联系作者。
原文链接:recclay.blog.csdn.net/article/details/81844012
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)