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

@ -1,9 +1,9 @@
trig = 5 --IO14 trig = 5 --IO14
echo = 7 --IO13 echo = 1 --IO5
relay = 0 --IO16 relay = 0 --IO16
is_relay_on = false is_relay_on = "false"
pulse_time = 0 pulse_time = 0
water_level = 0 water_level = -1
relay_on = 10 relay_on = 10
relay_off = 100 relay_off = 100
tank_height = 120 tank_height = 120
@ -22,7 +22,7 @@ function read_config()
if (file.open("config") ~= nil) then if (file.open("config") ~= nil) then
local result = string.sub(file.readline(), 1, -2) -- to remove newline character local result = string.sub(file.readline(), 1, -2) -- to remove newline character
file.close() file.close()
local v1, v2, v3 = result:match("([^,]+),([^,]+)") local v1, v2, v3 = result:match("([^,]+),([^,]+),([^,]+)")
relay_on = tonumber(v1) relay_on = tonumber(v1)
relay_off = tonumber(v2) relay_off = tonumber(v2)
tank_height = tonumber(v3) tank_height = tonumber(v3)
@ -39,7 +39,7 @@ gpio.mode(echo, gpio.INT)
read_config() read_config()
gpio.trig(echo, "both", function(level) gpio.trig(echo, "both", function(level)
local du = tmr.now() - pulse_time du = tmr.now() - pulse_time
if (level == 1) then if (level == 1) then
pulse_time = tmr.now() pulse_time = tmr.now()
else else
@ -47,34 +47,28 @@ gpio.trig(echo, "both", function(level)
water_level = tank_height - (du / 40); water_level = tank_height - (du / 40);
if (water_level < relay_on) then if (water_level < relay_on) then
gpio.write(relay, gpio.HIGH) gpio.write(relay, gpio.HIGH)
is_relay_on = true is_relay_on = "true"
elseif (water_level > relay_off) then elseif (water_level > relay_off) then
gpio.write(relay, gpio.LOW) gpio.write(relay, gpio.LOW)
is_relay_on = false is_relay_on = "false"
end end
print("Water Level: " .. water_level .. " cm")
collectgarbage()
end end
end) end)
tmr.alarm(0, 5000, 1, function() tmr.alarm(0, 5000, 1, function()
collectgarbage()
gpio.write(trig, gpio.HIGH) gpio.write(trig, gpio.HIGH)
tmr.delay(10) tmr.delay(10)
gpio.write(trig, gpio.LOW) gpio.write(trig, gpio.LOW)
end) if client_connected then
if (water_level > -1) then
tmr.alarm(0, 10000, 1, function()
if t == nil then
print("Error reading from DHTxx")
else
if (client_connected) then
local payload = "{event:{metaData:{owner:\"${DEVICE_OWNER}\",deviceId:\"${DEVICE_ID}\"},payloadData:{relay:" .. is_relay_on .. ", waterlevel:" .. water_level .. "}}}" 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) 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) end)
else
connectMQTTClient()
end end
else
connectMQTTClient()
end end
end) end)
@ -85,30 +79,30 @@ function connectMQTTClient()
else else
print("Client IP: " .. ip) print("Client IP: " .. ip)
print("Trying to connect MQTT client") print("Trying to connect MQTT client")
m:connect("${MQTT_EP}", $ { MQTT_PORT } , 0 , function (client) m:connect("${MQTT_EP}", ${MQTT_PORT}, 0, function(client)
client_connected = true client_connected = true
print("MQTT client connected") print("MQTT client connected")
subscribeToMQTTQueue() subscribeToMQTTQueue()
end) end)
end end
end end
function subscribeToMQTTQueue() function subscribeToMQTTQueue()
m: subscribe("carbon.super/watertank/${DEVICE_ID}/command", 0, function(client, topic, message) m:subscribe("carbon.super/watertank/${DEVICE_ID}/command", 0, function(client, topic, message)
print("Subscribed to MQTT Queue") print("Subscribed to MQTT Queue")
end) end)
m: on("message", function(client, topic, message) m:on("message", function(client, topic, message)
print("MQTT message received") print("MQTT message received")
print(message) print(message)
local v1, v2, v3 = message: match("([^,]+),([^,]+)") local v1, v2, v3 = message:match("([^,]+),([^,]+),([^,]+)")
relay_on = tonumber(v1) relay_on = tonumber(v1)
relay_off = tonumber(v2) relay_off = tonumber(v2)
tank_height = tonumber(v3) tank_height = tonumber(v3)
print("Received configs:".. relay_on.. ",".. relay_off.. ",".. tank_height) print("Received configs:" .. relay_on .. "," .. relay_off .. "," .. tank_height)
save_config(); save_config();
end) end)
m: on("offline", function(client) m:on("offline", function(client)
print("Disconnected") print("Disconnected")
client_connected = false client_connected = false
end) end)
end end

Loading…
Cancel
Save