【ESP8266之LUA开发】六、建立TCP客户端,实现ADC测量,单点接地的小知识

网友投稿 575 2022-05-30

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

【ESP8266之LUA开发】六、建立TCP客户端,实现ADC测量,单点接地的小知识

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,相关具体解释可参考<这里>

Lua TCP/IP

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:四、Matlab 之 常见函数的使用
下一篇:关于 CloudFoundry 云平台服务 quota 管理的一些错误消息和解决方案
相关文章