--init.lua print("Setting up WIFI...") wifi.setmode(wifi.STATION) --modify according your wireless router settings wifi.sta.config("YOUR_SSID","YOUR_PASSWD") wifi.sta.connect() tmr.alarm(1, 1000, 1, function() if wifi.sta.getip()== nil then print("IP unavaiable, Waiting...") else tmr.stop(1) print("Config done, IP is "..wifi.sta.getip()) dofile("all.lua") end end)
pin = 6 t = 0 function read(addr, unit) ow.setup(pin) result = nil flag = false if(addr == nil) then ow.reset_search(pin) count = 0 repeat count = count + 1 addr = ow.search(pin) tmr.wdclr() until((addr ~= nil) or (count > 100)) ow.reset_search(pin) end if(addr == nil) then return result end crc = ow.crc8(string.sub(addr,1,7)) if (crc == addr:byte(8)) then if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then ow.reset(pin) ow.select(pin, addr) ow.write(pin, 0x44, 1) present = ow.reset(pin) ow.select(pin, addr) ow.write(pin,0xBE,1) data = nil data = string.char(ow.read(pin)) for i = 1, 8 do data = data .. string.char(ow.read(pin)) end crc = ow.crc8(string.sub(data,1,8)) if (crc == data:byte(9)) then t = (data:byte(1) + data:byte(2) * 256) * 625 t = t / 10000 return t end tmr.wdclr() else end else end return result end function sendt(t,co,key) print("Temp:"..t.." C\n") conn=net.createConnection(net.TCP, 0) conn:on("receive", function(conn, payload) print(payload) end) conn:connect(80,'184.106.153.149') conn:send("GET /update?key="..key.."&field1="..t.."&field2="..co.." HTTP/1.1\r\n") conn:send("Host: api.thingspeak.com\r\n") conn:send("Accept: */*\r\n") conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n") conn:send("\r\n") conn:on("sent",function(conn) print("Closing connection") conn:close() end) conn:on("disconnection", function(conn) print("Got disconnection...") end) end uart.setup(0,9600,8,0,1,0) uart.on("data", 0, function(data) --print("receive from uart:", data) if string.len(data) == 7 then result = string.byte(data,4)*256 + string.byte(data,5) print (result) sendt(t,result,"YOUR_KEY") end end, 0) function sendData() t = read() uart.write(0,0xFE,0x04,0x00,0x03,0x00,0x01,0xD5,0xC5) end tmr.alarm(0, 60000, 1, function() sendData() end )
Source: https://habr.com/ru/post/258137/
All Articles