NodeMcu出现死循环,程序刷不进去,remove init.lua文件也不行

晚上撸NodeMcu的Http模块,一直与局域网下的服务器连不上,写着写着出来了个死循环,程序怎么也刷不进去,当时就想买块新的板子了,幸好后来灵机一动:

重新刷入了一个新的固件,缺少init.lua程序内所需的固件模块就行。

还有上面的问题哪位聚聚知道点拨小弟一番。

 

令人窒息的操作,数据上传成功,附上代码

wifi.sta.sethostname("Node-MCU")
wifi.setmode(wifi.STATION)
print(wifi.sta.gethostname(),"\n")

--自动连接wifi
station_cfg={}
station_cfg.ssid="520"
station_cfg.pwd="z520520520"
station_cfg.save=false
wifi.sta.config(station_cfg)
wifi.sta.connect()

url = "http://192.168.1.105:8080/SmartHomeDemo/SensorData_servlet"
sensorDataJson = nil
state = true

--启动定时器发送传感器数据
tmr.alarm(1, 1000, tmr.ALARM_AUTO, function()
    if wifi.sta.getip() == nil then
        print('Waiting for connectting IP ...')
    else if state and getSensorData() then
        print('IP is ' .. wifi.sta.getip())
        print(sensorDataJson)
        state = false
        sendData(url, sensorDataJson)
        --tmr.stop(1)
    end
    end
end)

-- 获取传感器数据
function getSensorData()
pin = 5
status, temp, humi, temp_dec, humi_dec = dht.read(pin)
if status == dht.OK then
    -- Integer firmware using this example
    --print(string.format("DHT Temperature:%d.%03d;Humidity:%d.%03d\r\n",
    --      math.floor(temp),
    --      temp_dec,
    --      math.floor(humi),
    --      humi_dec
    --))

    data = {}
    data.id = "12345678"
    data.ip = wifi.sta.getip()
    data.temp = temp
    data.humi = humi
    sensorDataJson = sjson.encode(data)
    
    return true
else
    print('sensor data read failed\r\n')
    return false
end
end

-- 发送http链接
function sendData(url, st)
http.post(url,
  'Content-Type: application/json\r\n',
  st,
  function(code, data)
    state = true
    if (code < 0) then
      print("HTTP request failed\r\n")
    else
      print(code, data, '\r\n')
    end
  end)
end

-- 监听80端口
sv = net.createServer(net.TCP, 30)

function receiver(sck, data)
  print(data)
  changeLight()
  sck:close()
end

if sv then
  sv:listen(80, function(conn)
    conn:on("receive", receiver)
    conn:send("hello world")
  end)
end

function changeLight()
    gpio.mode(4, gpio.OUTPUT)
    light = gpio.read(4)
    if (light == 1) then
        gpio.write(4, gpio.LOW)
    else
        gpio.write(4, gpio.HIGH)
    end
end

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值