Commit 1edbfd81 authored by Harald Klein's avatar Harald Klein

initial import of some lua snippets

parents
id=0
sda=2
scl=1
i2c_addr=0x77
i2c.setup(id, sda, scl, i2c.SLOW)
caldata = {}
function send_command(dev_addr, command)
i2c.start(id)
i2c.address(id, dev_addr, i2c.TRANSMITTER)
i2c.write(id,command)
i2c.stop(id)
end
function send_reset(dev_addr)
send_command(dev_addr, 0x1e)
tmr.delay(300000)
end
function read_data(dev_addr, command, numdelay, numbyte, writezero)
i2c.start(id)
i2c.address(id, dev_addr, i2c.TRANSMITTER)
i2c.write(id,command)
i2c.stop(id)
tmr.delay(numdelay)
if writezero == 1 then
i2c.start(id)
i2c.address(id, dev_addr, i2c.TRANSMITTER)
i2c.write(id, 0)
i2c.stop(id)
end
i2c.start(id)
i2c.address(id, dev_addr, i2c.RECEIVER)
data = i2c.read(id, numbyte)
i2c.stop(id)
return data
end
function read_eprom(dev_addr)
send_reset(dev_addr)
for i = 1, 6, 1 do
data = read_data(dev_addr, 0xa0 + i*2, 1, 2, 0)
caldata[i] = bit.lshift(data:byte(1),8)+data:byte(2)
print("reading", 0xa0 + i*2, caldata[i])
end
end
function read_temp(dev_addr)
temp = read_data(dev_addr, 0x50, 1, 3, 1)
D2 = bit.lshift(temp:byte(1),16)+bit.lshift(temp:byte(2),8)+temp:byte(3)
dT = D2 - caldata[5]*2^8
TEMP = 2000 + dT * caldata[6] / 2 ^ 23
return TEMP / 100
end
function read_pressure(dev_addr)
pressure = read_data(dev_addr, 0x48, 9000, 3, 1)
temp = read_data(dev_addr, 0x50, 1, 3, 1)
D2 = bit.lshift(temp:byte(1),16)+bit.lshift(temp:byte(2),8)+temp:byte(3)
D1 = bit.lshift(pressure:byte(1),16)+bit.lshift(pressure:byte(2),8)+pressure:byte(3)
dT = D2 - caldata[5]*2^8
TEMP = 2000 + dT * caldata[6] / 2 ^ 23
OFF = caldata[2] * 2 ^ 16 + (caldata[4] * dT)/2^7
SENS = caldata[1] * 2 ^ 15 + (caldata[3] * dT)/2^8
P = (D1 * SENS / 2^21 - OFF)/2^15
return P / 100
end
read_eprom(i2c_addr)
\ No newline at end of file
uart.setup(0,115200,8,0,1,0)
print("*** You've got 3 sec to stop timer 0 ***")
tmr.alarm(0, 3000, 0, function()
dofile("mqtt.lua")
end)
tmr.alarm(1, 10000, 0, function()
print("timer1 trigger dsleep")
node.dsleep(15*1000*1000)
end)
\ No newline at end of file
BROKER = "192.168.80.2"
BRPORT = 1883
BRUSER = "user"
BRPWD = "pwd"
CLIENTID = "ESP8266-" .. node.chipid()
dofile("baro.lc")
print "Connecting to MQTT broker. Please wait..."
m = mqtt.Client( CLIENTID, 120, BRUSER, BRPWD)
m:connect( BROKER , BRPORT, 0, function(conn)
print("Connected to MQTT:" .. BROKER .. ":" .. BRPORT .." as " .. CLIENTID )
m:publish("sensors/".. CLIENTID .. "/temperature",read_temp(i2c_addr),0,0, function(conn)
print ("temp published")
tmr.delay(10000)
m:publish("sensors/".. CLIENTID .. "/pressure",read_pressure(i2c_addr),0,0, function(conn)
print ("pressure published")
node.dsleep(60*1000000)
end)
end)
end)
dofile("baro.lua")
read_eprom(0x77)
pressure = read_pressure(0x77)
temperature = read_temp(0x77)
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload) print(payload) node.dsleep(300*1000000) end )
conn:connect(80,"184.106.153.149")
conn:send("GET /update?key=UNARTL1R51ES4MYA".."&field1="..pressure.."&field2="..temperature.." 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")
\ No newline at end of file
dofile("baro.lua")
baro_addr = 0x77
read_eprom(baro_addr)
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(conn,payload)
print(payload)
temperature = read_temp(baro_addr)
pressure = read_pressure(baro_addr)
json = '{"temperature":' .. temperature .. ',"pressure":' .. pressure .. '}'
conn:send(json)
end)
conn:on("sent",function(conn) conn:close() end)
end)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment