Pārlūkot izejas kodu

store counter in fs

Innocenty Enikeew 7 gadi atpakaļ
vecāks
revīzija
ec8ff90817
3 mainītis faili ar 38 papildinājumiem un 39 dzēšanām
  1. 34 35
      application.lua
  2. 3 3
      credentials.lua
  3. 1 1
      init.lua

+ 34 - 35
application.lua

@@ -12,31 +12,23 @@ m = mqtt.Client("esp", 120)
 m:on("connect", function(client) print ("connected") end)
 m:on("connect", function(client) print ("connected") end)
 m:on("offline", function(client) print ("offline") end)
 m:on("offline", function(client) print ("offline") end)
 
 
--- -- on publish message receive event
--- m:on("message", function(client, topic, data)
---   print(topic .. ":" )
---   if data ~= nil then
---     print(data)
---   end
--- end)
-
 -- Ice-boil calibration
 -- Ice-boil calibration
-function calibrate(temp)
-   local ReferenceLow = 0.01
-   local ReferenceHigh = 100
-   local ReferenceRange = ReferenceHigh - ReferenceLow
-   local RawLow = TEMP_ICE
-   local RawHigh = TEMP_BOIL
-   local RawRange = RawHigh - RawLow
-   local RawValue = temp
-   local CorrectedValue = (((RawValue - RawLow) * ReferenceRange) / RawRange) + ReferenceLow
+-- function calibrate(temp)
+--    local ReferenceLow = 0.01
+--    local ReferenceHigh = 100
+--    local ReferenceRange = ReferenceHigh - ReferenceLow
+--    local RawLow = TEMP_ICE
+--    local RawHigh = TEMP_BOIL
+--    local RawRange = RawHigh - RawLow
+--    local RawValue = temp
+--    local CorrectedValue = (((RawValue - RawLow) * ReferenceRange) / RawRange) + ReferenceLow
 
 
-   if RawLow == nil or RawHigh == nil then
-      return temp
-   end
+--    if RawLow == nil or RawHigh == nil then
+--       return temp
+--    end
 
 
-   return CorrectedValue
-end
+--    return CorrectedValue
+-- end
 
 
 ds_read_counter = 0
 ds_read_counter = 0
 
 
@@ -44,28 +36,37 @@ function ds_start_read()
    ds18b20.read(
    ds18b20.read(
       function(ind,rom,res,raw_temp,tdec,par)
       function(ind,rom,res,raw_temp,tdec,par)
          local mac = string.format("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",string.match(rom,"(%d+):(%d+):(%d+):(%d+):(%d+):(%d+):(%d+):(%d+)"))
          local mac = string.format("%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",string.match(rom,"(%d+):(%d+):(%d+):(%d+):(%d+):(%d+):(%d+):(%d+)"))
-         local temp = calibrate(raw_temp)
+         --local temp = calibrate(raw_temp)
+         local temp = raw_temp
          print(ind,mac,res,temp,raw_temp)
          print(ind,mac,res,temp,raw_temp)
          ok, json = pcall(sjson.encode, {rom=mac, res=res, temp=temp, raw=raw_temp, cnt=ds_read_counter})
          ok, json = pcall(sjson.encode, {rom=mac, res=res, temp=temp, raw=raw_temp, cnt=ds_read_counter})
          if ok then
          if ok then
-            m:publish(MQTT_TOPIC, json, 0, 0, function(client) print("sent "..json.." to "..MQTT_TOPIC) end)
+            m:publish(MQTT_TOPIC, json, 0, 0, function(client)
+                         print("sent "..json.." to "..MQTT_TOPIC)
+                         ds_read_counter = ds_read_counter + 1
+                         if file.open("counter.txt", "w") then
+                            file.write(ds_read_counter)
+                            file.close()
+                         end
+            end)
          else
          else
             print("failed to encode!")
             print("failed to encode!")
          end
          end
-         ds_read_counter = ds_read_counter + 1
     end,{});
     end,{});
 end
 end
 
 
 function handle_mqtt_connected(client)
 function handle_mqtt_connected(client)
-  print("mqtt connected")
-  -- subscribe topic with qos = 0
-  -- client:subscribe(MQTT_TOPIC, 0, function(client) print("subscribe success") end)
-  -- publish a message with data = hello, QoS = 0, retain = 0
-  -- client:publish("/topic", "hello", 0, 0, function(client) print("sent") end)
+   print("mqtt connected")
+   -- read last counter
+   if file.open("counter.txt", "r") then
+      ds_read_counter = file.read()
+      file.close()
+   end
 
 
-  -- start temp reading
-  ds_start_read()
-  tmr.create():alarm(TEMP_RATE, tmr.ALARM_AUTO, function(timer) ds_start_read() end)
+   -- start temp reading
+   ds18b20.setup(TEMP_PIN)
+   ds_start_read()
+   tmr.create():alarm(TEMP_RATE, tmr.ALARM_AUTO, function(timer) ds_start_read() end)
 end
 end
 
 
 function handle_mqtt_error(client, reason)
 function handle_mqtt_error(client, reason)
@@ -75,6 +76,4 @@ end
 function do_mqtt_connect()
 function do_mqtt_connect()
   m:connect(MQTT_SERVER, handle_mqtt_connected, handle_mqtt_error)
   m:connect(MQTT_SERVER, handle_mqtt_connected, handle_mqtt_error)
 end
 end
-
-ds18b20.setup(TEMP_PIN)
 do_mqtt_connect()
 do_mqtt_connect()

+ 3 - 3
credentials.lua

@@ -4,6 +4,6 @@ STATIC_IP = {ip = "192.168.1.41", netmask = "255.255.255.0", gateway = "192.168.
 MQTT_SERVER = "192.168.1.11"
 MQTT_SERVER = "192.168.1.11"
 MQTT_TOPIC = "sensors/esp/temp/balcony"
 MQTT_TOPIC = "sensors/esp/temp/balcony"
 TEMP_PIN = 3
 TEMP_PIN = 3
-TEMP_RATE = 20 * 1000
-TEMP_BOIL = 100
-TEMP_ICE = 0.01
+TEMP_RATE = 10 * 1000
+--TEMP_BOIL = 100
+--TEMP_ICE = 0.01

+ 1 - 1
init.lua

@@ -25,7 +25,7 @@ wifi_got_ip_event = function(T)
   print("Wifi connection is ready! IP address is: "..T.IP)
   print("Wifi connection is ready! IP address is: "..T.IP)
   print("Startup will resume momentarily, you have 3 seconds to abort.")
   print("Startup will resume momentarily, you have 3 seconds to abort.")
   print("Waiting...")
   print("Waiting...")
-  tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup)
+  tmr.create():alarm(1000, tmr.ALARM_SINGLE, startup)
 end
 end
 
 
 wifi_disconnect_event = function(T)
 wifi_disconnect_event = function(T)