0
I want to send the temperature value in OPCUA to the Mysql database, in this case the value of temp_var_node, my code is as follows:
import sys
sys.path.insert(0, "..")
import time
import datetime
import subprocess
import logging
from opcua import Server
import BMP085
import mysql.connector as mysql
##### Sensor Data of BMP180 ######################
bmp = BMP085.BMP085()
temperature =bmp.read_temperature()
altitude = bmp.read_altitude()
pressure = bmp.read_pressure()
################################################
if __name__ == "__main__":
logging.basicConfig(level = logging.WARNING)
# get Objects node, this is where we should put our nodes
# setup our server
server = Server()
server.set_endpoint("opc.tcp://192.168.1.10:4840/freeopcua/server/")
# setup our own namespace, not really necessary but should as spec
uri = "http://192.168.1.10:4840/freeopcua/server/"
idx = server.register_namespace(uri)
# get Objects node, this is where we should put our nodes
objects = server.get_objects_node()
# populating our address space
################################### Object ####################################################
sensordata = objects.add_object(idx, "Sensor One BMP180")
############################# Variables of Object #############################################
temp_var_node= sensordata.add_variable(idx, "Temperature Sensor One", temperature)
print("the temp_var_node is:", temp_var_node)
altit_var_node = sensordata.add_variable(idx, "Altitude Sensor One", altitude)
print("the altit_var_node is:", altit_var_node)
press_var_node = sensordata.add_variable(idx, "Pressure Sensor One", pressure)
print("the pressure_var_node is:", press_var_node)
#################### Set MyVariable to be writable by clients #################################
temp_var_node.set_writable()# Set MyVariable to be writable by clients
altit_var_node.set_writable()
press_var_node.set_writable()
######################### Sending data values of variables to the database ####################################
db = mysql.connect(host = '192.168.1.15',port = 3306,user = 'root',password = 'admin',database = 'OPCUA')
cursor = db.cursor()
delete = "DROP TABLE sensorBMP180_raspberrypi2"
cursor.execute(delete)
sql = """CREATE TABLE sensorBMP180_raspberrypi2 (
temperature DOUBLE,
altitude DOUBLE,
pressure INT)"""
cursor.execute(sql)
# starting de server OPCUA!
server.start()
try:
while True:
############## Values on UaExpert ###########################################
temp_var_node.set_value(bmp.read_temperature())
time.sleep(2)
altit_var_node.set_value(bmp.read_altitude())
time.sleep(2)
press_var_node.set_value(bmp.read_pressure())
time.sleep(2)
################## Values to DataBase #########################################
temperature = bmp.read_temperature()
pressure = bmp.read_pressure()
altitude = bmp.read_altitude()
time.sleep(2)
cursor.execute("""INSERT INTO sensorBMP180_raspberrypi2 VALUES (%s, %s, %s)""",(temperature,pressure,altitude))
db.commit()
finally:
#close connection, remove subcsriptions, etc
server.stop()
db.close()
#################################################################################
When on the line
cursor.execute("""INSERT INTO sensorBMP180_raspberrypi2 VALUES (%s, %s, %s)""",(temperature,pressure,altitude))
toggle to temp_var_node, press_var_node and altit_var_node where these are node values gives me this error:
############################# code to get values of Node variables temp = temp_var_node.get_value()
print("the temp_var_node is:", temp)
time.sleep(1)
altit = altit_var_node.get_value()
print("the altit_var_node is:", altit)
time.sleep(1)
press = press_var_node.get_value()
print("the pressure_var_node is:", press)
time.sleep(1)
cursor.execute("""INSERT INTO sensorBMP180_raspberrypi1 VALUES (%s, %s, %s)""",(temp, altit, press))
db.commit()




What kind of
temperature, pressureandaltitude?– Tuxpilgrim
temperature is double, Pressure is int and altitude is double
– Sergio Nunes
On your table
altitudeisINT...– Tuxpilgrim
And by error, the value you’re playing on the variables is of the type
node, tries to explicitly convert when inserting into the database and tells me the result.– Tuxpilgrim
Something like that:
cursor.execute("""INSERT INTO sensorBMP180_raspberrypi2 VALUES (%s, %s, %s)""",(float(temperature),int(pressure),int(altitude)))– Tuxpilgrim
I did so to get the values of the nodes (see in the above question the code I did)
– Sergio Nunes
Take a look at the answer, I don’t think you understand the comment ;)
– Tuxpilgrim
as it is in your comment gave the image above, and how I can convert the variables of Node ex. temp_var_node?
– Sergio Nunes
Let’s go continue this discussion in chat.
– Tuxpilgrim