Innocenty Enikeew 7 vuotta sitten
vanhempi
commit
0e54c9123a
1 muutettua tiedostoa jossa 4 lisäystä ja 3 poistoa
  1. 4 3
      application.lua

+ 4 - 3
application.lua

@@ -38,8 +38,7 @@ function calibrate(temp)
    return CorrectedValue
 end
 
-local ow_pin = TEMP_PIN
-ds18b20.setup(ow_pin)
+ds_read_counter = 0
 
 function ds_start_read()
    ds18b20.read(
@@ -47,12 +46,13 @@ function ds_start_read()
          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)
          print(ind,mac,res,temp,raw_temp)
-         ok, json = pcall(sjson.encode, {rom=mac, res=res, temp=temp, raw=raw_temp})
+         ok, json = pcall(sjson.encode, {rom=mac, res=res, temp=temp, raw=raw_temp, cnt=ds_read_counter})
          if ok then
             m:publish(MQTT_TOPIC, json, 0, 0, function(client) print("sent "..json.." to "..MQTT_TOPIC) end)
          else
             print("failed to encode!")
          end
+         ds_read_counter = ds_read_counter + 1
     end,{});
 end
 
@@ -76,4 +76,5 @@ function do_mqtt_connect()
   m:connect(MQTT_SERVER, handle_mqtt_connected, handle_mqtt_error)
 end
 
+ds18b20.setup(TEMP_PIN)
 do_mqtt_connect()