- Fri Mar 11, 2016 12:30 am
#42877
Hey There!
I was getting the negative temperature reading as well. I solved it by
1. going to
http://nodemcu-build.com/, getting a custom build (up to date firmware as well). I added all of the modules, just to be sure. You might only need the OW ("One Wire") and a few others.
2. Using the following code
Code: Select all--init.lua
print("Connecting to wifi")
wifi.setmode(wifi.STATION)
--modify according your wireless router settings
wifi.sta.config("YOUR ROUTERr","PASSWORD")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("waiting for ip")
else
tmr.stop(1)
print("Got ip "..wifi.sta.getip())
dofile("ds1820.lua")
end
end)
Code: Select all-- ds1820.lua
-- Measure temperature and post data to thingspeak.com
-- 2014 OK1CDJ
--- Tem sensor DS18B20 is conntected to GPIO0
--- 2015.01.21 sza2 temperature value concatenation bug correction
--- 2016.01.06 ahanktcd edits
print("P4/GPIO2\n")
pin = 4
ow.setup(pin)
function bxor(a,b)
local r = 0
for i = 0, 31 do
if ( a % 2 + b % 2 == 1 ) then
r = r + 2^i
end
a = a / 2
b = b / 2
end
return r
end
--- Get temp and send data to thingspeak.com
function sendData()
ow.reset(pin)
ow.write(pin,0xCC, 1)
ow.write(pin, 0x44, 1)
tmr.delay(1000000)
ow.reset(pin)
ow.write(pin,0xCC, 1)
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
t = (data:byte(1) + data:byte(2) * 256)
data = nil
if (t > 32768) then
t = (bxor(t, 0xffff)) + 1
t = (-1) * t
end
t = t * 625
t1 = t / 10000
t2 = (t >= 0 and t % 10000) or (10000 - t % 10000)
t = nil
print("Temp:"..t1 .. "."..string.format("%04d", t2).." C\n")
-- conection to thingspeak.com
print("Sending to thingspeak")
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) end)
-- api.thingspeak.com 184.106.153.149
conn:connect(80,'184.106.153.149')
conn:send("GET /update?key=YOUR_FORM_API_KEY&field1="..t1.."."..string.format("%04d", t2).." 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("success")
end)
t1 = nil
t2 = nil
end
-- send data every X ms to thing speak
tmr.alarm(0, 20000, 1, function() sendData() end )
Even if you don't use it, someone else down the line might be able to.
I struggled with the -1.7 temp for a few hours. It appears the be inconsistencies in the firmware. Also, the chips are not foolproof, so don't be afraid to try 2/3 times to make sure you catch it during the right moment of bootup.
To communicate and read files I used Slovik Dudes
-------------------------------------------------
http://esp8266.ru/esplorer/ - Esplorer
To write files I used Hari Wigunas
-------------------------------------------------https://www.youtube.com/watch?v=ty-sUlYM9M4 - LuaUploader
"saving" files directly to the ESP8266
Good Luck, Good Flashing