nodeMCU dimmer/switch/temp.sensor without arduino

9 years 8 months ago - 9 years 8 months ago #818 by cdj
Ok guys, here is new files updated. But with :

Input: GETPRESSURE
Received pressure request
reading value
PANIC: unprotected error in call to Lua API (not enough memory)
PANIC: unprotected error in call to Lua API (attempt to call a string value)

and

> print("BMP Temp", dofile("bmp085.lua").read(4,3,false,false))
not enough memory
> print("BMP Press", dofile("bmp085.lua").read(4,3,false,true))
not enough memory

i have really no idea to what can i do.

init.lua based on 0.7
print("Initializing...")
wifi.setmode(wifi.STATION)
wifi.sta.config("XXXX","XXXX")
wifi.sta.setip({ip="192.168.0.51",netmask="255.255.255.0",gateway="192.168.0.1"})

srv=net.createServer(net.TCP) 
  srv:listen(43333,function(conn) 
    conn:on("receive",function(conn,payload)
         
          print("Input: "..payload) 
          if string.find(payload,"GETTEMP") then
               print("Received temperature request")
               myval = nil         
               while myval == nil do
                    print("reading value")
                    myval = (dofile("bmp085.lua").read(4,3,false,false))
               end
               print("read "..myval)
               conn:send((myval/10))
       
          elseif string.find(payload,"GETPRESSURE") then
               print("Received pressure request")
               myval = nil         
               while myval == nil do
                    print("reading value")
                    myval = (dofile("bmp085.lua").read(4,3,false,true))
               end
               print("read "..myval)
               conn:send((myval/10))
          end         
               
    end) -- end srv conn:on receive function

    conn:on("sent",function(conn)
        print("Closing connection")
        conn:close()
    end) -- end srv conn:on sent function

  end) -- end srv:listen function

print ("easyIoT Pressure/temperature sensor for nodeMCU")
print ("Based on QuinLED_ESP8266_V0.4")
print ("Version 0.7 (c) 2015 by DennisSc")

bmp085.lua modified for easyiot :
------------------------------------------------------------------------------
-- LAST VERSION SENT BY VLADIMIR
-- BMP085 query module
-- Vladimir Dronnikov <dronnikov@gmail.com>
-- Heavily based on work of Christee <Christee@nodemcu.com>
-- MODIFIED FOR EASYIOT
-- print(dofile("bmp085.lua").read(4, 3))
------------------------------------------------------------------------------
local M
do
  -- cache
  local i2c, tmr = i2c, tmr
  -- helpers
  local r8 = function(reg)
    i2c.start(0)
    i2c.address(0, 0x77, i2c.TRANSMITTER)
    i2c.write(0, reg)
    i2c.stop(0)
    i2c.start(0)
    i2c.address(0, 0x77, i2c.RECEIVER)
    local r = i2c.read(0, 1)
    i2c.stop(0)
    return r:byte(1)
  end
  local w8 = function(reg, val)
    i2c.start(0)
    i2c.address(0, 0x77, i2c.TRANSMITTER)
    i2c.write(0, reg)
    i2c.write(0, val)
    i2c.stop(0)
  end
  local r16u = function(reg)
    return r8(reg) * 256 + r8(reg + 1)
  end
  local r16 = function(reg)
    local r = r16u(reg)
    if r > 32767 then r = r - 65536 end
    return r
  end
  -- calibration data
  local AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MC, MD
  -- read
  local read = function(sda, scl, oss, pression)
    i2c.setup(0, sda, scl, i2c.SLOW)
    -- cache calibration data
    if not AC1 then
      AC1 = r16(0xAA)
      AC2 = r16(0xAC)
      AC3 = r16(0xAE)
      AC4 = r16u(0xB0)
      AC5 = r16u(0xB2)
      AC6 = r16u(0xB4)
      B1  = r16(0xB6)
      B2  = r16(0xB8)
      MC  = r16(0xBC)
      MD  = r16(0xBE)
    end
    if not oss then oss = 0 end
    if oss <= 0 then oss = 0 end
    if oss > 3 then oss = 3 end
    -- get T
    w8(0xF4, 0x2E)
    tmr.delay(5000)
    local t = r16(0xF6)
    local X1 = (t - AC6) * AC5 / 32768
    local X2 = MC * 2048 / (X1 + MD)
    t = (X2 + X1 + 8) / 16
    -- get raw P
    w8(0xF4, 0x34 + 64 * oss)
    tmr.delay((4 + 3 ^ oss) * 1000)
    local p = (r8(0xF6) * 65536 + r8(0xF7) * 256 + r8(0xF8)) / 2 ^ (8 - oss)
    -- normalize P
    local B6 = t * 16 - 8 - 4000
    local X1 = B2 * (B6 * B6 / 4096) / 2048
    local X2 = AC2 * B6 / 2048
    local X3 = X1 + X2
    local B3 = ((AC1 * 4 + X3) * 2 ^ oss + 2) / 4
    X1 = AC3 * B6 / 8192
    X2 = (B1 * (B6 * B6 / 4096)) / 65536
    X3 = (X1 + X2 + 2) / 4
    local B4 = AC4 * (X3 + 32768) / 32768
    local B7 = (p - B3) * (50000 / 2 ^ oss)
    p = B7 / B4 * 2
    X1 = (p / 256) ^ 2
    X1 = (X1 * 3038) / 65536
    X2 = (-7357 * p) / 65536
    p = p + (X1 + X2 + 3791) / 16
    -- Celsius * 10, Hg mm * 10
    p = p * 3 / 40
    -- has floats? divide by 10
    if 1/2 ~= 0 then
      t, p = t / 10, p / 10
    end
    --
    if pression then
      return p
     else   
       return t --, p
    end
    --return t, p
  end
  -- expose
  M = {
    read = read,
  }
end
return M

Dennis, if you are so able to fix this we've also bmp sensor working. i can't do better than this :( sorry :unsure:

The only thing that work is bmp085.lua.
If i upload only this file to esp (without init.lua) and try to query i obtain :

> print("BMP Temp", dofile("bmp085.lua").read(4,3,false,false))
BMP Temp 20.735415942589

> print("BMP Press",
dofile("bmp085.lua").read(4,3,false,true))
BMP Press 765.31947596567

Can i bang my head against the wall ?
Thanks for helping and regards

Please Log in or Create an account to join the conversation.

9 years 8 months ago - 9 years 8 months ago #819 by Dennis
Try this init.lua, with original unmodified bmp085.lua:
print("Initializing...")
               
wifi.setmode(wifi.STATION)
wifi.sta.config("MYSSID","MYPASSWORD")



srv=net.createServer(net.TCP) 
  srv:listen(43333,function(conn) 
    conn:on("receive",function(conn,payload)
         
          print("Input: "..payload) 
          if string.find(payload,"GETTEMP") then
               print("Received temperature request")
               myval = nil         
               while myval == nil do
                    print("reading value")
                    myval, myval2 = dofile("bmp085.lua").read(4,3))
               end
               print("read "..myval)
               conn:send((myval))
 
       
          elseif string.find(payload,"GETPRESSURE") then
               print("Received Pressure request")
               myval2 = nil         
               while myval2 == nil do
                    print("reading value")
                    myval, myval2 = dofile("bmp085.lua").read(4,3))
               end
               print("read "..myval2)
               conn:send((myval2))

          
       
		  end -- end if
     
    end) -- end srv conn:on receive function

    conn:on("sent",function(conn)
        print("Closing connection")
        conn:close()
    end) -- end srv conn:on sent function

  end) -- end srv:listen function

print ("easyIoT bmp180 temperature/pressure sensor for nodeMCU")
print ("Version 0.1b (c) 2015 DennisSc")

And remove all other files from your esp8266.

If this does not work, then it is no memory problem.
edit - I removed the "divide by 10" in the conn:send call!

regards

Please Log in or Create an account to join the conversation.

9 years 8 months ago #820 by cdj
Ehm pheraps there's a ")" more in line :

myval, myval2 = dofile("bmp085.lua").read(4,3)) ?
if i remove last ")" in two places and it starts correctly but when i ask for temp or pressure, node sadly reset.

What firmware are you using?
What bmp085.lua are you using... original, but what version? the one sent me by Vladimir ?

If i leave the one sent by Vladimir and do this manual command :

> print(dofile("bmp085.lua").read(4, 3))
it respond me this strange value :

184 7655

that i suppose is 18.4° and 76,55 mb

but if i put init.lua when i send this manual command node reset.

Thanks for patience

Dario

Please Log in or Create an account to join the conversation.

9 years 8 months ago #821 by cdj
Nothing, tried also myval, myval2 = (dofile("bmp085.lua").read(4,3)) that is correct but node reset.

if init.lua is not present bmp085.lua works fine.all versions, original,modified, sent by vladimir, ALL

if init.lua is present and try to call bmp085.lua with dofile node rrespond with a good reset ! :evil:

if i try to replace myval = nil with myval=1234 all the rest work... easyiot ask for GETPRESSURE AND GETTEMP and node respond with 1234 and the other test number and assign correctly to virtual node

tried with 0.9.4 unsuccessfully
i can't believe that i'm doing this from this morning and it is so difficult.

Oh, and now if i send manually request query respond me with number without comma like "rounded"... temp 178 press 7596 that is in reality 17.8 and 759.6. same bmp085.lua file !!!

it depends from call to bmp085.lua. singles files work well... when you combine together it die.
really need help. My skill finished here.

Thanks in advice
regards
Dario

Please Log in or Create an account to join the conversation.

9 years 8 months ago #822 by Dennis
No idea what goes wrong (besides the () issue you discovered), it sounds like the library from vladimir doesn't like being called from within init.lua for whatever reason.

Maybe you should try something else, i found another bmp085 lib for nodemcu, developed by the nodemcu team: github.com/nodemcu/nodemcu-firmware/tree...r/lua_modules/bmp085

here is my proposed init.lua (I have no bmp180 sensor, sorry)
print("Initializing...")
bmp085 = require("bmp085")
sda = 4
scl = 3
bmp085.init(sda, scl)
	       
               
wifi.setmode(wifi.STATION)
wifi.sta.config("MYSSID","MYPASSWORD")



srv=net.createServer(net.TCP) 
  srv:listen(43333,function(conn) 
    conn:on("receive",function(conn,payload)
         
          print("Input: "..payload) 
          if string.find(payload,"GETTEMP") then
               print("Received temperature request")
               myval = bmp085.getUT(true)
	       print("read "..myval)
               conn:send(myval)
	       
       
          elseif string.find(payload,"GETPRESSURE") then
               print("Received Pressure request")
	       myval = bmp085.getUP(1)
               print("read "..myval)
               conn:send(myval)

          
       
		  end -- end if
     
    end) -- end srv conn:on receive function

    conn:on("sent",function(conn)
        print("Closing connection")
        conn:close()
    end) -- end srv conn:on sent function

  end) -- end srv:listen function

print ("easyIoT bmp180 temperature/pressure sensor for nodeMCU")
print ("Version 0.1b2 (c) 2015 DennisSc")

regards
The following user(s) said Thank You: cdj

Please Log in or Create an account to join the conversation.

9 years 8 months ago #823 by cdj
Dennis,
NodeMCU 0.9.5 build 20150213 powered by Lua 5.1.4
Initializing...
lua: not enough memory

It's crazy.

Try to ask Vladimir why his version can't be called....
Thanks
Dario

Please Log in or Create an account to join the conversation.

Time to create page: 0.310 seconds

Forum latest

  • No posts to display.