Z-wave-Mosquitto Broker-Dashboard connection.

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
tanimadinmiben
Posts: 6
Joined: 07 Jul 2019 11:20

Z-wave-Mosquitto Broker-Dashboard connection.

Post by tanimadinmiben »

Hello,
I have been trying to send datas to my dashboard. I use mosquitto with websockets. My aim is to listen sensor datas, and send it to the sqlite database, and retrieve datas on my dashboard. I can retrieve data directly from broker to mydashboard but I want to store them to visualize arriving times etc.

Here is my code for listening datas:

Code: Select all

#!/usr/bin/python

import paho.mqtt.client as mqttClient
import time
from store_Sensor_Data_to_DB import sensor_Data_Handler
def on_connect(client, userdata, flags, rc):

    if rc == 0:

        print("Connected to broker")

        global Connected                #Use global variable
        Connected = True                #Signal connection

    else:

        print("Connection failed")

def on_message(client, userdata, message):
        print "MQTT Data Received..."
        print "MQTT Topic: " + message.topic
        print "Data: " + message.payload
        sensor_Data_Handler(message.topic, message.payload)

Connected = False   #global variable for the state of the connection

broker_address= "192.168.137.159"  #Broker address
port = 1883                         #Broker port
user = ""                    #Connection username
password = ""            #Connection password

client = mqttClient.Client("Python")               #create new instance
client.username_pw_set(user, password=password)    #set username and password
client.on_connect= on_connect                      #attach function to callback
client.on_message= on_message                      #attach function to callback
client.connect(broker_address,port,60) #connect
client.subscribe("home/front/door") #subscribe
client.loop_fore
and to send to database but it seems it doesn't work.

Code: Select all

#!/usr/bin/env python
import json
import sqlite3

# SQLite DB Name
DB_Name =  "smarthome.db"

#===============================================================
# Database Manager Class

class DatabaseManager():
	def __init__(self):
		self.conn = sqlite3.connect(DB_Name)
		self.conn.execute('pragma foreign_keys = on')
		self.conn.commit()
		self.cur = self.conn.cursor()
		
	def add_del_update_db_record(self, sql_query, args=()):
		self.cur.execute(sql_query, args)
		self.conn.commit()
		return

	def __del__(self):
		self.cur.close()
		self.conn.close()

#===============================================================
# Functions to push Sensor Data into Database

# Function to save Dor to DB Table
def Door_Data_Handler(jsonData):
	#Parse Data 
	json_Dict = json.loads(jsonData)
	arrivalTime = json_Dict['Date']
	doorName = json_Dict['doorName']
    doorSituation = json_Dict['doorSituation']
	
	#Push into DB Table
	dbObj = DatabaseManager()
	dbObj.add_del_update_db_record("insert into myhome (arrivalTime, doorName, doorSituation) values (?,?,?)",[arrivalTime, doorName, doorSituation])
	del dbObj
	print "Inserted Door Data into Database."
	print ""


# Master Function to Select DB Funtion based on MQTT Topic

def sensor_Data_Handler(Topic, jsonData):
	if Topic == "home/front/door":
		Door_Data_Handler(jsonData)
	

Post Reply