Re: Example - measure temperature using 1-wire DS1820
Posted: Sat Jul 04, 2015 6:31 pm
I got it figured out. I was connected to the wrong GPIO. How do I get it to display in fahrenheit?
-->
Open Community Forum for ESP8266, Come share Arduino and IoT (Internet of Things)
https://www.esp8266.com/
Connect the pull-up resistor between VCC and DQ (middle pin) on the DS18B20
Connect pin 1 (GND) and pin 3 (Vdd) on the DS18B20 to common ground.
Connect DQ to GPIO2 on the ESP
Connect TX/RX to your 3.3V serial cable
function sendData(temp)
t1 = temp / 10000
t2 = (temp >= 0 and temp % 10000) or (10000 - temp % 10000)
print("Temp:"..t1 .. "."..string.format("%04d", t2).." C\n")
-- conection to thingspeak.com
print("Sending data to thingspeak.com")
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)
print(payload)
conn:close()
end)
-- api.thingspeak.com 184.106.153.149
conn:connect(80,'184.106.153.149')
str = "GET /update?key=<key>&field1="..t1.."."..string.format("%04d", t2).." HTTP/1.1\r\nHost: api.thingspeak.com\r\n".."Connection: close\r\nAccept: */*\r\n\r\n"
conn:send(str)
end
function work()
sendData(31000)
end
function work1()
t = require("ds18b20")
gpio2 = 4
t.setup(gpio2)
addrs = t.addrs()
if (addrs ~= nil) then
print("Total DS18B20 sensors: "..table.getn(addrs))
end
-- Just read temperature
temp = t.read()
print("Temperature: "..t.read().."°C")
-- Don't forget to release it after use
t = nil
ds18b20 = nil
package.loaded["ds18b20"]=nil
sendData(temp)
end
wifi.setmode(wifi.STATION)
print("******* 5 seconds delay *******");
tmr.alarm(2,5000,0, function()
wifi.sta.config("SSID","password")
wifi.sta.connect()
print("DHCP delay (5 s.)...");
tmr.delay(5000000)
work1()
tmr.alarm(0, 30000, 1, function() work1() end )
end)