Update agent code and analytics scripts. Fixed bugs

application-manager-new
charitha 9 years ago
parent 8478f1ebf0
commit 3f533e2288

@ -17,16 +17,12 @@
~ under the License.
-->
<eventReceiver name="watertank_receiver" statistics="disable" trace="disable"
xmlns="http://wso2.org/carbon/eventreceiver">
<eventReceiver name="watertank_receiver" statistics="disable" trace="disable" xmlns="http://wso2.org/carbon/eventreceiver">
<from eventAdapterType="oauth-mqtt">
<property name="topic">carbon.super/watertank/+/data</property>
<property name="username">admin</property>
<property name="contentValidatorParams">
device_id_json_path:event.metaData.deviceId,device_id_topic_hierarchy_index:2
</property>
<property name="contentValidator">org.wso2.carbon.device.mgt.iot.input.adapter.mqtt.util.MQTTContentValidator
</property>
<property name="contentValidatorParams">device_id_json_path:event.metaData.deviceId,device_id_topic_hierarchy_index:2</property>
<property name="contentValidator">org.wso2.carbon.device.mgt.iot.input.adapter.mqtt.util.MQTTContentValidator</property>
<property name="contentTransformer">default</property>
<property name="dcrUrl">https://localhost:${carbon.https.port}/dynamic-client-web/register</property>
<property name="url">tcp://${mqtt.broker.host}:${mqtt.broker.port}</property>
@ -35,4 +31,3 @@
<mapping customMapping="disable" type="json"/>
<to streamName="org.wso2.iot.watertank" version="1.0.0"/>
</eventReceiver>

@ -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

Loading…
Cancel
Save