From 3f533e22885e2e5b7e12721311820822cb026007 Mon Sep 17 00:00:00 2001 From: charitha Date: Wed, 29 Jun 2016 20:23:18 +0530 Subject: [PATCH] Update agent code and analytics scripts. Fixed bugs --- .../watertank_receiver/watertank_receiver.xml | 11 +-- .../src/main/resources/agent/water-tank.lua | 78 +++++++++---------- 2 files changed, 39 insertions(+), 50 deletions(-) diff --git a/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_receiver/watertank_receiver.xml b/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_receiver/watertank_receiver.xml index df75155b..3338a1c3 100644 --- a/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_receiver/watertank_receiver.xml +++ b/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_receiver/watertank_receiver.xml @@ -17,16 +17,12 @@ ~ under the License. --> - + carbon.super/watertank/+/data admin - - device_id_json_path:event.metaData.deviceId,device_id_topic_hierarchy_index:2 - - org.wso2.carbon.device.mgt.iot.input.adapter.mqtt.util.MQTTContentValidator - + device_id_json_path:event.metaData.deviceId,device_id_topic_hierarchy_index:2 + org.wso2.carbon.device.mgt.iot.input.adapter.mqtt.util.MQTTContentValidator default https://localhost:${carbon.https.port}/dynamic-client-web/register tcp://${mqtt.broker.host}:${mqtt.broker.port} @@ -35,4 +31,3 @@ - diff --git a/modules/samples/watertank/feature/feature/src/main/resources/agent/water-tank.lua b/modules/samples/watertank/feature/feature/src/main/resources/agent/water-tank.lua index 7ee9f38b..56256eca 100644 --- a/modules/samples/watertank/feature/feature/src/main/resources/agent/water-tank.lua +++ b/modules/samples/watertank/feature/feature/src/main/resources/agent/water-tank.lua @@ -1,9 +1,9 @@ trig = 5 --IO14 -echo = 7 --IO13 +echo = 1 --IO5 relay = 0 --IO16 -is_relay_on = false +is_relay_on = "false" pulse_time = 0 -water_level = 0 +water_level = -1 relay_on = 10 relay_off = 100 tank_height = 120 @@ -22,7 +22,7 @@ function read_config() if (file.open("config") ~= nil) then local result = string.sub(file.readline(), 1, -2) -- to remove newline character file.close() - local v1, v2, v3 = result:match("([^,]+),([^,]+)") + local v1, v2, v3 = result:match("([^,]+),([^,]+),([^,]+)") relay_on = tonumber(v1) relay_off = tonumber(v2) tank_height = tonumber(v3) @@ -39,7 +39,7 @@ gpio.mode(echo, gpio.INT) read_config() gpio.trig(echo, "both", function(level) - local du = tmr.now() - pulse_time + du = tmr.now() - pulse_time if (level == 1) then pulse_time = tmr.now() else @@ -47,34 +47,28 @@ gpio.trig(echo, "both", function(level) water_level = tank_height - (du / 40); if (water_level < relay_on) then gpio.write(relay, gpio.HIGH) - is_relay_on = true + is_relay_on = "true" elseif (water_level > relay_off) then gpio.write(relay, gpio.LOW) - is_relay_on = false + is_relay_on = "false" end - print("Water Level: " .. water_level .. " cm") - collectgarbage() end end) tmr.alarm(0, 5000, 1, function() + collectgarbage() gpio.write(trig, gpio.HIGH) tmr.delay(10) gpio.write(trig, gpio.LOW) -end) - -tmr.alarm(0, 10000, 1, function() - if t == nil then - print("Error reading from DHTxx") - else - if (client_connected) then + if client_connected then + if (water_level > -1) then local payload = "{event:{metaData:{owner:\"${DEVICE_OWNER}\",deviceId:\"${DEVICE_ID}\"},payloadData:{relay:" .. is_relay_on .. ", waterlevel:" .. water_level .. "}}}" m:publish("carbon.super/watertank/${DEVICE_ID}/data", payload, 0, 0, function(client) - print("Published> Water Level: " .. water_level .. "cm Relay: " .. is_relay_on .. "%") + print("Published> Water Level: " .. water_level .. "cm Relay: " .. is_relay_on) end) - else - connectMQTTClient() end + else + connectMQTTClient() end end) @@ -85,30 +79,30 @@ function connectMQTTClient() else print("Client IP: " .. ip) print("Trying to connect MQTT client") - m:connect("${MQTT_EP}", $ { MQTT_PORT } , 0 , function (client) - client_connected = true - print("MQTT client connected") - subscribeToMQTTQueue() - end) -end + m:connect("${MQTT_EP}", ${MQTT_PORT}, 0, function(client) + client_connected = true + print("MQTT client connected") + subscribeToMQTTQueue() + end) + end end function subscribeToMQTTQueue() -m: subscribe("carbon.super/watertank/${DEVICE_ID}/command", 0, function(client, topic, message) -print("Subscribed to MQTT Queue") -end) -m: on("message", function(client, topic, message) -print("MQTT message received") -print(message) -local v1, v2, v3 = message: match("([^,]+),([^,]+)") -relay_on = tonumber(v1) -relay_off = tonumber(v2) -tank_height = tonumber(v3) -print("Received configs:".. relay_on.. ",".. relay_off.. ",".. tank_height) -save_config(); -end) -m: on("offline", function(client) -print("Disconnected") -client_connected = false -end) + m:subscribe("carbon.super/watertank/${DEVICE_ID}/command", 0, function(client, topic, message) + print("Subscribed to MQTT Queue") + end) + m:on("message", function(client, topic, message) + print("MQTT message received") + print(message) + local v1, v2, v3 = message:match("([^,]+),([^,]+),([^,]+)") + relay_on = tonumber(v1) + relay_off = tonumber(v2) + tank_height = tonumber(v3) + print("Received configs:" .. relay_on .. "," .. relay_off .. "," .. tank_height) + save_config(); + end) + m:on("offline", function(client) + print("Disconnected") + client_connected = false + end) end