完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
当我添加代码以使用定时器打开和关闭 LED 时,我得到以下错误输出(间歇性的,这很烦人):
代码:全选 PANIC: unprotected error in call to Lua API (attempt to call a number value) 我似乎已将其缩小到 tmr.alarm 函数。是否有解决方法,或者有更好的方法让 LED 亮起一秒钟? 下面的代码运行得很好,没有问题。 代码:全选-- dht22_tcp_panic print(wifi.sta.getip()) dhtpin = 2 reading = nil ledpin = 7 gpio.mode(ledpin,gpio.OUTPUT) srv=net.createServer(net.TCP,5) srv:listen(8080,function(conn) conn:on(\"receive\", function(client,payload) print(\"Connection received\") -- Turn on LED gpio.write(ledpin,gpio.HIGH) -- If the payload is correct, read the DHT sensor if (payload == \"gettemp\") then local status,temp,humi,temp_decimal,humi_decimal = dht.read(dhtpin) if( status == dht.OK ) then -- Integer firmware using this example reading = string.format( \"%d.%03d, %d.%03d\", math.floor(temp), temp_decimal, math.floor(humi), humi_decimal ) readingT = string.format(\"%d.%03d\",math.floor(temp),temp_decimal) readingH = string.format(\"%d.%03d\",math.floor(humi),humi_decimal) elseif( status == dht.ERROR_CHECKSUM ) then reading = \"DHT Checksum error.\"; readingT = nil; readingH = nil; elseif( status == dht.ERROR_TIMEOUT ) then reading = \"DHT Time out.\"; readingT = nil; readingH = nil; end else reading = \"Wrong payload\"; end print(reading) function sendreading () print(client); client:send(reading); end -- Try to send the reading if pcall(sendreading) then print(\"reading sent\") else print(\"error sending reading\") end -- Try to close the connection if pcall(function() client:close() end) then print(\"client closed\") else print(\"error closing client\") end gpio.write(ledpin,gpio.LOW) end) -- commented out, not sure if I need this -- conn:on(\"sent\", function(conn) conn:close() end) end) 这是导致错误的完整代码(强调了更改): 代码:全选-- dht22_tcp print(wifi.sta.getip()) dhtpin = 2 reading = nil ledpin = 7; gpio.mode(ledpin,gpio.OUTPUT); srv=net.createServer(net.TCP,5) srv:listen(8080,function(conn) conn:on(\"receive\", function(client,payload) print(\"Connection received\") -- Turn on LED and set a timer to turn it off after 1 second -- ------------ BEGIN ADDED CODE ------------------- gpio.write(ledpin,gpio.HIGH) tmr.alarm(1,1000,0,function() gpio.write(ledpin,gpio.LOW) end) -- -------- END ADDED CODE -------------------- -- If the payload is correct, read the DHT sensor if (payload == \"gettemp\") then local status,temp,humi,temp_decimal,humi_decimal = dht.read(dhtpin) if( status == dht.OK ) then -- Integer firmware using this example reading = string.format( \"%d.%03d, %d.%03d\", math.floor(temp), temp_decimal, math.floor(humi), humi_decimal ) readingT = string.format(\"%d.%03d\",math.floor(temp),temp_decimal) readingH = string.format(\"%d.%03d\",math.floor(humi),humi_decimal) elseif( status == dht.ERROR_CHECKSUM ) then reading = \"DHT Checksum error.\"; readingT = nil; readingH = nil; elseif( status == dht.ERROR_TIMEOUT ) then reading = \"DHT Time out.\"; readingT = nil; readingH = nil; end else reading = \"Wrong payload\"; end print(reading) function sendreading () print(client); client:send(reading); end -- Try to send the reading if pcall(sendreading) then print(\"reading sent\") else print(\"error sending reading\") end -- Try to close the connection if pcall(function() client:close() end) then print(\"client closed\") else print(\"error closing client\") end end) -- conn:on(\"sent\", function(conn) conn:close() end) end) 当连接发生时: 代码:全选 Connection received 21.700, 42.900 userdata: 3fff3bb8 reading sent client closed PANIC: unprotected error in call to Lua API (attempt to call a number value) |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
920 浏览 1 评论
552浏览 6评论
461浏览 5评论
有没有办法在不使用混杂模式的情况下实现Wifi驱动程序接收缓冲区访问中断呢?
446浏览 5评论
447浏览 4评论
417浏览 4评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-25 21:02 , Processed in 0.766156 second(s), Total 76, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号