0% found this document useful (0 votes)
8 views1 page

Python Device Connection and Logging

The document contains Python code for establishing a database connection using MySQL and managing a device connection with the pytuya library. It defines a Network class and a Device class that handles device connections and logs data to a database. The Device class includes methods for connecting to a device and logging power measurement data into a specified database table.

Uploaded by

tandondevyansh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

Python Device Connection and Logging

The document contains Python code for establishing a database connection using MySQL and managing a device connection with the pytuya library. It defines a Network class and a Device class that handles device connections and logs data to a database. The Device class includes methods for connecting to a device and logging power measurement data into a specified database table.

Uploaded by

tandondevyansh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import mysql.

connector
import pytuya;
import time;

def establish_database_connection(username="root", paswd="raspberry", db_name=


"exampledb"):
mydb = [Link](host = "localhost",
user = username,
password = paswd, database = db_name)
mycursor = [Link]()

return mydb, mycursor

class Network:
def __init__(self):
return

class Device:

def __init__(self,device_Id,device_Ip,device_Key='12345678'):
self.device_Id=device_Id
self.device_Ip=device_Ip
self.device_Key=device_Key
[Link] = None

def establish_device_connection(pass):
try:
[Link]=
[Link](self.device_Id,self.device_Ip,self.device_Key)

except:
print("Device Connection Failed. Please try again.")
return None

def data_logging(self, db, cursor):


cmd = "INSERT INTO powerMeasurement1(power) VALUES (%s)"
data = [Link]()
val = (int(data1['dps']['5']),)
[Link](cmd,val)
[Link]()

You might also like