0: # Push data only if there had been a successful temperature read
diff --git a/features/iot-plugins-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/src/main/resources/agent/src/deviceConfig.properties b/features/iot-plugins-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/src/main/resources/agent/src/deviceConfig.properties
deleted file mode 100644
index 489af7042..000000000
--- a/features/iot-plugins-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/src/main/resources/agent/src/deviceConfig.properties
+++ /dev/null
@@ -1,33 +0,0 @@
-#
-# Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
-#
-# Licensed under the Apache License, Version 2.0 (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# http://www.apache.org/licenses/LICENSE-2.0
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-#
-[Device-Configurations]
-server-name=${SERVER_NAME}
-owner=${DEVICE_OWNER}
-deviceId=${DEVICE_ID}
-device-name=${DEVICE_NAME}
-controller-context=/raspberrypi/controller
-https-ep=${HTTPS_EP}
-http-ep=${HTTP_EP}
-apim-ep=${APIM_EP}
-mqtt-ep=${MQTT_EP}
-xmpp-ep=${XMPP_EP}
-auth-method=token
-auth-token=${DEVICE_TOKEN}
-refresh-token=${DEVICE_REFRESH_TOKEN}
-push-interval=15
-
-
diff --git a/features/iot-plugins-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/src/main/resources/agent/src/iotUtils.py b/features/iot-plugins-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/src/main/resources/agent/src/iotUtils.py
index 1419f91d6..6ad552114 100644
--- a/features/iot-plugins-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/src/main/resources/agent/src/iotUtils.py
+++ b/features/iot-plugins-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/src/main/resources/agent/src/iotUtils.py
@@ -58,6 +58,8 @@ MQTT_EP = configParser.get('Device-Configurations', 'mqtt-ep')
XMPP_EP = configParser.get('Device-Configurations', 'xmpp-ep')
AUTH_TOKEN = configParser.get('Device-Configurations', 'auth-token')
CONTROLLER_CONTEXT = configParser.get('Device-Configurations', 'controller-context')
+MQTT_SUB_TOPIC = configParser.get('Device-Configurations', 'mqtt-sub-topic').format(owner = DEVICE_OWNER, deviceId = DEVICE_ID)
+MQTT_PUB_TOPIC = configParser.get('Device-Configurations', 'mqtt-pub-topic').format(owner = DEVICE_OWNER, deviceId = DEVICE_ID)
DEVICE_INFO = '{"owner":"' + DEVICE_OWNER + '","deviceId":"' + DEVICE_ID + '","reply":'
HTTPS_EP = configParser.get('Device-Configurations', 'https-ep')
HTTP_EP = configParser.get('Device-Configurations', 'http-ep')
diff --git a/features/iot-plugins-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/src/main/resources/agent/src/mqttConnector.py b/features/iot-plugins-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/src/main/resources/agent/src/mqttConnector.py
new file mode 100644
index 000000000..2a76045fa
--- /dev/null
+++ b/features/iot-plugins-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.feature/src/main/resources/agent/src/mqttConnector.py
@@ -0,0 +1,118 @@
+#!/usr/bin/env python
+
+"""
+/**
+* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+*
+* WSO2 Inc. licenses this file to you under the Apache License,
+* Version 2.0 (the "License"); you may not use this file except
+* in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied. See the License for the
+* specific language governing permissions and limitations
+* under the License.
+**/
+"""
+
+import time
+import iotUtils
+import paho.mqtt.client as mqtt
+
+
+# The callback for when the client receives a CONNACK response from the server.
+def on_connect(client, userdata, flags, rc):
+ print("MQTT_LISTENER: Connected with result code " + str(rc))
+
+ # Subscribing in on_connect() means that if we lose the connection and
+ # reconnect then subscriptions will be renewed.
+ print ("MQTT_LISTENER: Subscribing with topic " + TOPIC)
+ client.subscribe(TOPIC)
+
+
+
+# The callback for when a PUBLISH message is received from the server.
+def on_message(client, userdata, msg):
+ print( "MQTT_LISTENER: " + msg.topic + " " + str(msg.payload) )
+
+ request = str(msg.payload)
+
+ resource = request.split(":")[0].upper()
+ state = request.split(":")[1].upper()
+
+ print "MQTT_LISTENER: Resource- " + resource
+
+ if resource == "TEMP":
+ pass
+ #request.send_response(200)
+ #request.send_header("Content-type", "text/plain")
+ #request.end_headers()
+ #request.wfile.write(LAST_TEMP)
+ # return
+
+ elif resource == "BULB":
+ iotUtils.switchBulb(state)
+
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+# The Main method of the server script
+# This method is invoked from RaspberryStats.py on a new thread
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+def main():
+
+ MQTT_ENDPOINT = iotUtils.MQTT_EP.split(":")
+ MQTT_IP = MQTT_ENDPOINT[1].replace('//','')
+ MQTT_PORT = int(MQTT_ENDPOINT[2])
+
+ DEV_OWNER = iotUtils.DEVICE_OWNER
+ DEV_ID = iotUtils.DEVICE_ID
+
+ global TOPIC
+ TOPIC = "wso2/" + DEV_OWNER + "/raspberrypi/" + DEV_ID
+
+ print ("MQTT_LISTENER: MQTT_ENDPOINT is " + str(MQTT_ENDPOINT))
+ print ("MQTT_LISTENER: MQTT_TOPIC is " + TOPIC)
+
+ mqttClient = mqtt.Client()
+ mqttClient.on_connect = on_connect
+ mqttClient.on_message = on_message
+ mqttClient.username_pw_set(iotUtils.AUTH_TOKEN, password = "")
+
+ mqttClient.connect(MQTT_IP, MQTT_PORT, 60)
+ print "MQTT_LISTENER: " + time.asctime(), "Connected to MQTT Broker - %s:%s" % (MQTT_IP, MQTT_PORT)
+
+ # Blocking call that processes network traffic, dispatches callbacks and
+ # handles reconnecting.
+ # Other loop*() functions are available that give a threaded interface and a
+ # manual interface.
+ mqttClient.loop_forever()
+
+ # while True:
+ # try:
+ # mqttClient.connect(MQTT_IP, MQTT_PORT, 60)
+ # print "MQTT_LISTENER: " + time.asctime(), "Connected to MQTT Broker - %s:%s" % (MQTT_IP, MQTT_PORT)
+ #
+ # # Blocking call that processes network traffic, dispatches callbacks and
+ # # handles reconnecting.
+ # # Other loop*() functions are available that give a threaded interface and a
+ # # manual interface.
+ # mqttClient.loop_forever()
+ #
+ # except (KeyboardInterrupt, Exception) as e:
+ # print "MQTT_LISTENER: Exception in MQTTServerThread (either KeyboardInterrupt or Other)"
+ # print ("MQTT_LISTENER: " + str(e))
+ #
+ # mqttClient.disconnect()
+ # print "MQTT_LISTENER: " + time.asctime(), "Connection to Broker closed - %s:%s" % (MQTT_IP, MQTT_PORT)
+ # print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
+ # pass
+
+
+if __name__ == '__main__':
+ iotUtils.setUpGPIOPins()
+ main()
+