數據源程序庫可行性替換研究
720
2025-04-03
8266ADC測電壓
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
當懸空的時候,數據是飄忽的,正?,F象。
當接到3V3上面的時候,傳回來的數據是1024
接到GND的時候的數據
竟然是14,竟然不是0,相關具體解釋可參考<這里>
Lua TCP/IP
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。