I have written a python script for MQTT subscriber which works perfectly fine locally.
Python Subscriber script
import paho.mqtt.client as mqtt
import os,json
filePath = "logs.csv"
def initiateFile():
if (os.path.exists(filePath) == False):
fileObj = open(filePath, "w")
fileObj.write("Label,x,y,z\n")
def readFile():
data = open(filePath,'r').read()
return data
def decodeJson(jsonString):
jsonObject = json.loads(jsonString)
label = jsonObject.keys()[0]
x = jsonObject[label]['x']
y = jsonObject[label]['y']
z = jsonObject[label]['z']
return label.encode("utf-8")+","+x+","+y+","+z;
def writeInFile(newData):
oldData = readFile()
fileObj = open(filePath, "w")
fileObj.write(oldData+newData+"\n")
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc)+" "+str(client))
client.subscribe("sensors/test")
initiateFile()
def on_message(client, userdata, msg):
print msg.payload
writeInFile(decodeJson(msg.payload))
def on_disconnect(client, userdata, rc):
print("Disconnect, reason: " + str(rc))
print("Disconnect, reason: " + str(client))
client = mqtt.Client()
client.username_pw_set(username, password)
client.connect(broker,port)
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()
client.on_disconnect = on_disconnect
But When I tried to deploy it on google cloud, I got "ImportError: No module named paho.mqtt.client" error.
Then I tried following solutions but got error
1.) Declare paho-mqtt library in app.yaml
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
libraries:
- name: paho-mqtt
version: "1.3.0"
***ERROR: (gcloud.app.deploy) An error occurred while parsing file: [C:\Users\uni5p_000\Desktop\RMIT_Studies\Sem_1\Cloud_Computing\Practical\GOOGLE\python-docs-samples\appengine\standard\hello_world\app.yaml]
the library "paho-mqtt" is not supported
in "C:\Users\uni5p_000\Desktop\RMIT_Studies\Sem_1\Cloud_Computing\Practical\GOOGLE\python-docs-samples\appengine\standard\hello_world\app.yaml", line 11, column 19***
2.) pip install paho-mqtt on Cloud shell
***OSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/paho_mqtt-1.3.0.dist-info'***
How to proceed now?