As the title says... Chat on...

User avatar
By joefly888
#18031 NodeMCU 0.9.5 build 20150214
Lua 5.1.4
esp-12
2 programs file list
init.lua 788
ds18b20.lua 2804

i originally I tried to run 3 programs. ALL individually work fine, I then ran init.lua and voltage.lua(a simple module voltage reporting to thingspeak) both ran fine also. But when I tried to add ds18b20.lua it complained of not enough memory. DS18b20 obviously runs fine on its own. my init.lua
Code: Select allbutton = 1
led = 2

gpio.mode(led, gpio.OUTPUT)
gpio.write(led, gpio.LOW)
gpio.mode(button, gpio.INT)

gpio.trig(button, "both", function(level)
  if level==1 then
    gpio.write(led, gpio.HIGH)
    print ('level is '..level)
    print ('Turned on')
  else
    print ('level is '..level)
    gpio.write(led, gpio.LOW)
    print ('Turned off')
  end
end)

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
  conn:on("receive",function(conn,payload)
    --print(payload)
    conn:send("<h1> Hello, NodeMcu.</h1>")   
    print(node.heap())
    door="open"
    if gpio.read(button)==1 then
       door="OPEN";
    else door="CLOSED";
    end   
    conn:send( "The door is " .. door .."\r\n")
  end)
  conn:on("sent",function(conn) conn:close() end)
end)

dofile("ds18b20.lua")


and standard ds18b20
Code: Select all-- 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

pin = 3
ow.setup(pin)

counter=0
lasttemp=-999

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 temperature from DS18B20
function getTemp()
      addr = ow.reset_search(pin)
      repeat
        tmr.wdclr()
     
      if (addr ~= nil) then
        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)
                tmr.delay(1000000)
                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)
         if (t > 32768) then
                    t = (bxor(t, 0xffff)) + 1
                    t = (-1) * t
                   end
         t = t * 625
                   lasttemp = t
         print("Last temp: " .. lasttemp)
                end                   
                tmr.wdclr()
          end
        end
      end
      addr = ow.search(pin)
      until(addr == nil)
end

--- Get temp and send data to thingspeak.com
function sendData()
getTemp()
t1 = lasttemp / 10000
t2 = (lasttemp >= 0 and lasttemp % 10000) or (10000 - lasttemp % 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) end)
-- api.thingspeak.com 184.106.153.149
conn:connect(80,'184.106.153.149')
conn:send("GET /update?key=8U4LFX973INKQ54D &field2="..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("Got disconnection...")
  end)
end

-- send data every X ms to thing speak
tmr.alarm(0, 60000, 1, function() sendData() end )


I am using esp-12 and with all these IO pins, i am only using three of them. I thought these ESP modules have more ram than arduinos, it seems strange that I am out of memory and I have just started doing examples.. Is this normal given my code, and I assume it is my ram that is limited, is there a way to reduce ram usage? What is causing so much ram usage on the ds18b20 program, and is there a way to reduce the ram footprint? Advice?
User avatar
By trackerj
#18035
joefly888 wrote:NodeMCU 0.9.5 build 20150214
Lua 5.1.4
esp-12
2 programs file list
init.lua 788
ds18b20.lua 2804

i originally I tried to run 3 programs. ALL individually work fine, I then ran init.lua and voltage.lua(a simple module voltage reporting to thingspeak) both ran fine also. But when I tried to add ds18b20.lua it complained of not enough memory. DS18b20 obviously runs fine on its own. my init.lua
Code: Select allbutton = 1
led = 2

gpio.mode(led, gpio.OUTPUT)
gpio.write(led, gpio.LOW)
gpio.mode(button, gpio.INT)

gpio.trig(button, "both", function(level)
  if level==1 then
    gpio.write(led, gpio.HIGH)
    print ('level is '..level)
    print ('Turned on')
  else
    print ('level is '..level)
    gpio.write(led, gpio.LOW)
    print ('Turned off')
  end
end)

srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
  conn:on("receive",function(conn,payload)
    --print(payload)
    conn:send("<h1> Hello, NodeMcu.</h1>")   
    print(node.heap())
    door="open"
    if gpio.read(button)==1 then
       door="OPEN";
    else door="CLOSED";
    end   
    conn:send( "The door is " .. door .."\r\n")
  end)
  conn:on("sent",function(conn) conn:close() end)
end)

dofile("ds18b20.lua")


and standard ds18b20
Code: Select all-- 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

pin = 3
ow.setup(pin)

counter=0
lasttemp=-999

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 temperature from DS18B20
function getTemp()
      addr = ow.reset_search(pin)
      repeat
        tmr.wdclr()
     
      if (addr ~= nil) then
        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)
                tmr.delay(1000000)
                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)
         if (t > 32768) then
                    t = (bxor(t, 0xffff)) + 1
                    t = (-1) * t
                   end
         t = t * 625
                   lasttemp = t
         print("Last temp: " .. lasttemp)
                end                   
                tmr.wdclr()
          end
        end
      end
      addr = ow.search(pin)
      until(addr == nil)
end

--- Get temp and send data to thingspeak.com
function sendData()
getTemp()
t1 = lasttemp / 10000
t2 = (lasttemp >= 0 and lasttemp % 10000) or (10000 - lasttemp % 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) end)
-- api.thingspeak.com 184.106.153.149
conn:connect(80,'184.106.153.149')
conn:send("GET /update?key=8U4LFX973INKQ54D &field2="..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("Got disconnection...")
  end)
end

-- send data every X ms to thing speak
tmr.alarm(0, 60000, 1, function() sendData() end )


I am using esp-12 and with all these IO pins, i am only using three of them. I thought these ESP modules have more ram than arduinos, it seems strange that I am out of memory and I have just started doing examples.. Is this normal given my code, and I assume it is my ram that is limited, is there a way to reduce ram usage? What is causing so much ram usage on the ds18b20 program, and is there a way to reduce the ram footprint? Advice?


Use first node.heap() to see how much free mem you have after loading each of them

Also use node.compile() directive to free some extra.
see NodeMCU API : link : https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en#nodecompile

PS: I will also try to reorganise that code in a more efficent manner.
User avatar
By joefly888
#18080 TrackerJ: Your suggestion somehow helped, I am now not only running above 2 programs but also an additional one which measures the onboard voltage and sends data to thingspeak. So amazing your suggestion. But I am not sure what really happened that help

First I print(node.heap()) as you suggested
Nothing uploaded: 22622
only DS18b20 uploaded: 14343
only init.lua uploaded: 18712

I ran the compile on the ds18b20.lua and got both programs working and then I added the third with the voltage measurement, and voila, all three are working concurrently. Thanks for your help.

in the process of learning so I was wondering if I can ask some related question:

Memory limitation: I assume there is memory that is used to store files, and separate memory allocated for variables and computation. Is the later what I am bumping up against, and is this what node.heap indicates, the remaining space available to allocated memory? Or is node.heap indication of total memory remaining?

What does the compile do? I assume it compiles it to machine code to make it run more efficiently, but how does it relate to memory? or is the compile eliminate the need to use memory to interpret on the fly? when I compile example.lua, I get the example.lc file. I dofile("example.lc"), do I still need to keep example.lua onboard or can I delete it? And is there a way I can save the compiled version to my computer?

with all three programs uploaded and running, I am getting a node.heap of 7976 what does this mean? how close am I to the limit?

Thanks for your help on this.
User avatar
By trackerj
#18092
joefly888 wrote:TrackerJ: Your suggestion somehow helped, I am now not only running above 2 programs but also an additional one which measures the onboard voltage and sends data to thingspeak. So amazing your suggestion. But I am not sure what really happened that help

First I print(node.heap()) as you suggested
Nothing uploaded: 22622
only DS18b20 uploaded: 14343
only init.lua uploaded: 18712

I ran the compile on the ds18b20.lua and got both programs working and then I added the third with the voltage measurement, and voila, all three are working concurrently. Thanks for your help.

in the process of learning so I was wondering if I can ask some related question:

Memory limitation: I assume there is memory that is used to store files, and separate memory allocated for variables and computation. Is the later what I am bumping up against, and is this what node.heap indicates, the remaining space available to allocated memory? Or is node.heap indication of total memory remaining?

What does the compile do? I assume it compiles it to machine code to make it run more efficiently, but how does it relate to memory? or is the compile eliminate the need to use memory to interpret on the fly? when I compile example.lua, I get the example.lc file. I dofile("example.lc"), do I still need to keep example.lua onboard or can I delete it? And is there a way I can save the compiled version to my computer?

with all three programs uploaded and running, I am getting a node.heap of 7976 what does this mean? how close am I to the limit?

Thanks for your help on this.


First thing that you need to understand is that you have, in terms of available "memory", 2 different things:
- 150k byte Flash for files -> here is the space where you save your files
- 21k Ram to run it -> this is the total available memory for running programs

hode.heap() - > returns the remain RAM size in bytes, from your small ammount of RAM from above. From my experience, depending on how is organised your program, functions, variables allocation, etc, under 7k free RAM you might get in trouble ( freeze, reboot, etc). So your 7976 it's pretty close to the limit.

node.compile -> compiles lua text file into lua bytecode file, and save it as .lc file. After you have the .lc file you can delete the .lua file but will not help you to free any RAM, as files are stored in Flash (see explication above about memory size and types)

Going thru the NodeMCU API doc and around might help you to have a better picture about.


Unfortunatelly any public available documentation related with ESP8266 is in a very experimental stage, to not say a mess, full of gaps and misteries. Analog (ADC and PWM if any) part is the funniest thing I ever seen in a datasheet for a chip.

By the way, if any of the readers here have a new, fresh updated datasheet, please share it also with us, mortals: ) Thank you.