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

User avatar
By Diyhouse
#29577 Hi folks,... I am trying to develop/extend and existing program that takes temperature samples from a DS18b20 temperature sensor and then logs the data to thingspeak... running of an ESP8266-07

I have had the basic program working fine,.. even created a *.lc file to save space,.. but unfortunately when I try to add some functionality to toggle an LED on and off every-time a sample is sent,.. and flash another LED when the 8266 starts to talk to thingspeak.

Looking at the code I figured shifting the one function into its own file would fix the space issue,.. and I could then develop the other function with more space to play with.

My thought was to shift out the getTemp() but then I get unsure how lua handles calling/loading functions and passing parameters back to the sendData() function,... I have searched lua forums and found info about modules but not how to call functions with other files.... or how to actually split the functionality up,..

And yes I may well be missing the obvious and I may well be barking up the wrong tree,... I am new to lua,.. can someone point me help me in the right direction..

many thanks

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 = 6         -- Assign pin to DS18b20
led1 = 2        -- Assign Pin to LED
ow.setup(pin)
gpio.mode(led1, gpio.OUTPUT)

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()

gpio.write(led2, HIGH);
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')
-- XDP4IIJT65R8QFGR
--conn:send("GET /update?key=XDP4IIJT65R8QFGR&field1="..t1.."."..string.format("%04d", t2).." HTTP/1.1\r\n")
conn:send("GET /update?key=XDP4IIJT65R8QFGR&field1="..t1..".".." 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)
  gpio.write(led2, LOW);
end
-- *****************************
-- send data every X ms to thing speak
tmr.alarm(0, 60000, 1, function() sendData() end )
User avatar
By Diyhouse
#29643 Well,.. I can't say I have fixed my problem,.. but having done some more digging around I get the feeling I was barking up the wrong tree!!, with my original approach

I have now got my code working driving a couple of LEDs at different stages of the code, the problem I have now is once I load the init.lua file things starting falling apart with where I get out of memory issues, and if I try to compile the ds1820.lua file I get stdin:1: not enough memory,.. even when I have striped out comments and blank lines... and the heap reports 10960,.. or is this expected behaviour,...

I think I can/should be able to compile in the ESplorer environment,.. but again I get the "stdin:1: attempt to call field 'fsinfo' (a nil value)",.. I have looked for causes of the this error message,.. but nothing yet,....
error message.

Does anyone have some thoughts for me....
User avatar
By Diyhouse
#29650 well 2 steps forward, and 1 back,...

Took a flier and reloaded firmware,... even though I thought I was up to date,.. with 0.95,.. but clearly not,...
I can now compile and upload and things work great on that front,...

Its just that temperature on the DS18b20 is always -1C,...
Hummm,.. some more digging required,...
User avatar
By flagtrax
#29655 Interesting for me "dyi", this is one of my future projects. I've used the 18b20's with arduino, but with the capabilities of the 8266, a lot of hardware overhead could be eliminated. I have several areas that i want monitor with a distance that makes wifi the best transmission medium. Thanks for the posts.