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

User avatar
By snice
#13328 Hi

I have written a small lua file (52 lines of code) that gives me an error when I run it:

dofile('tempLogger.lua')
error loading module 'ds18b20' from file 'ds18b20.lua': not enough memory

Of course, I've tried to reboot many times the device, and also tried a format but without any success

Could you please help?

Thank you
User avatar
By snice
#13379 hello,

here is the full script:

Code: Select allrequire('ds18b20')

gpio0 = 1     
ds18b20.setup(gpio0)

-- function that read the current temperature and send it to Web server
function readTemperature()     
     sensors = ds18b20.addrs()
     if (sensors ~= nil) then
       print("Total DS18B20 sensors: "..table.getn(sensors))
       for i = 1, #sensors do       
          temp = ds18b20.read(sensors[i])
          print("Sensor: "..sensors[i].." Temperature: "..temp.."C")
          sendData(sensors[i], temp)
       end
     end       
end

-- function to send data to server using an HTTP POST request
function sendData(sensorAddress, temperature)
    postdata="{\"battlevel\":\"2.925\", \"sensorAddress\":\""..sensorAddress.."\", \"temperature\":\""..temperature.."\", \"date\":\"2015-04-02 19:40:21\"}"
    postLength=string.len(postdata)
    sck=net.createConnection(net.TCP, 0)
    sck:on("receive", function(s, payload)
                            print("received")
                            print(payload)
                            s:close()
                       end)
       
    sck:on("sent",function(s)
                    print("sent")                     
                  end)
                     
    sck:on("disconnection", function(s)
                                print("disconnection...")
                            end)
   
   
    sck:on("connection",function(s)
                            print("connection")               
                            s:send("POST /WebAdminDev/api/Measure HTTP/1.1\r\nHost: 192.168.1.20\r\n"
                            .."Connection: keep-alive\r\n"
                            .."Cache-Control: no-cache\r\nContent-Type: application/json"
                            .."\r\nContent-Length: "..postLength.."\r\nAccept: */*\r\n\r\n"
                            ..postdata)
                        end)     
         
    sck:connect(80,'192.168.1.20')
end

-- read twice because after reset the fist read always get 85°C
ds18b20.read()
ds18b20.read()

-- send temperature each minute
tmr.alarm(0, 60000, 1, function() readTemperature() end)
User avatar
By snice
#13381 I precise that if I put the sendData in a separated file and call it with static data (e.g. sendData('XXXX','15') ) it works without any problem... And the same for the readTemperature, I can read the temperature from the sensor