elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Security Series.XSS. [Cross Site Scripting]


+  Foro de elhacker.net
|-+  Programación
| |-+  Scripting
| | |-+  Gui para Arduino en Python
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Gui para Arduino en Python  (Leído 3,761 veces)
hacelo

Desconectado Desconectado

Mensajes: 130



Ver Perfil
Gui para Arduino en Python
« en: 8 Enero 2023, 17:38 pm »

Hola, estoy intentando hacer una Gui para Arduino en Python, pero me he topado con el siguiente problema.

Quiero actualizar cada segundo el valor de una etiqueta volcándole el valor de lectura de un potenciómetro y no soy capaz de hacerlo.

El potenciómetro sí que lo leo pero no consigo actualizar el valor de la etiqueta y al ejecutar el código  me arroja el siguiente mensaje NameError ‘valorPot’ is not defined en la linea 129

Agradecería cualquier tipo de ayuda para solventar este contratiempo teniendo en cuenta que soy muy novato en esto de la programación…. :)

Un saludo y muchas gracias.





Código
  1. # -*- coding: utf-8 -*-
  2.  
  3. # Form implementation generated from reading ui file 'guiPruebaFirm2.ui'
  4. #
  5. # Created by: PyQt5 UI code generator 5.15.7
  6. #
  7. # WARNING: Any manual changes made to this file will be lost when pyuic5 is
  8. # run again.  Do not edit this file unless you know what you are doing.
  9.  
  10.  
  11. from PyQt5 import QtCore, QtGui, QtWidgets
  12. import sys
  13. from PyQt5 import QtCore, QtGui, QtWidgets
  14. from PyQt5.uic import loadUi
  15. from PyQt5.QtWidgets import QWidget, QApplication, QMainWindow, QVBoxLayout
  16. from PyQt5.QtCore import *
  17. import threading
  18. from threading import Thread
  19. import pyfirmata
  20. import time
  21.  
  22.  
  23. # -------- Configuración de Arduino
  24. board = pyfirmata.Arduino('COM4')
  25. iter8 = pyfirmata.util.Iterator(board)
  26. iter8.start()
  27.  
  28. led1 = board.get_pin('d:2:o')
  29. pot = board.get_pin('a:0:i')
  30.  
  31.  
  32.  
  33. class Ui_MainWindow(object):
  34.    def setupUi(self, MainWindow):
  35.        MainWindow.setObjectName("MainWindow")
  36.        MainWindow.resize(506, 289)
  37.        self.centralwidget = QtWidgets.QWidget(MainWindow)
  38.        self.centralwidget.setObjectName("centralwidget")
  39.        self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
  40.        self.horizontalLayout.setContentsMargins(0, 0, 0, -1)
  41.        self.horizontalLayout.setObjectName("horizontalLayout")
  42.        self.widget = QtWidgets.QWidget(self.centralwidget)
  43.        self.widget.setMinimumSize(QtCore.QSize(100, 100))
  44.        self.widget.setMaximumSize(QtCore.QSize(15777215, 16777215))
  45.        self.widget.setStyleSheet("background-color: rgb(255, 255, 127);\n"
  46. "background-color: rgb(0, 0, 127);")
  47.        self.widget.setObjectName("widget")
  48.        self.valor1 = QtWidgets.QLabel(self.widget)
  49.        self.valor1.setGeometry(QtCore.QRect(350, 50, 51, 18))
  50.        self.valor1.setStyleSheet("color: rgb(255, 255, 255);\n"
  51. "font: 87 10pt \"Arial Black\";\n"
  52. "")
  53.        self.valor1.setObjectName("valor1")
  54.        self.label1 = QtWidgets.QLabel(self.widget)
  55.        self.label1.setGeometry(QtCore.QRect(50, 50, 47, 18))
  56.        self.label1.setStyleSheet("color: rgb(255, 255, 255);\n"
  57. "font: 87 10pt \"Arial Black\";")
  58.        self.label1.setObjectName("label1")
  59.        self.radioButton1 = QtWidgets.QRadioButton(self.widget)
  60.        self.radioButton1.setGeometry(QtCore.QRect(50, 150, 66, 60))
  61.        self.radioButton1.setStyleSheet("QRadioButton::indicator {\n"
  62. "  width:                  50px;\n"
  63. "  height:                 50px;\n"
  64. "  border-radius:          30px;\n"
  65. "}\n"
  66. "\n"
  67. "QRadioButton::indicator:checked {\n"
  68. "  background-color:       #00ff00;  \n"
  69. "  border:                 5px solid black;\n"
  70. "}\n"
  71. "\n"
  72. "QRadioButton::indicator:unchecked {\n"
  73. "    background-color:       red;\n"
  74. "    border:                 5px solid black;\n"
  75. "}\n"
  76. "   ")
  77.        self.radioButton1.setText("")
  78.        self.radioButton1.setObjectName("radioButton1")
  79.        self.label2 = QtWidgets.QLabel(self.widget)
  80.        self.label2.setGeometry(QtCore.QRect(50, 126, 66, 18))
  81.        self.label2.setStyleSheet("color: rgb(255, 255, 255);\n"
  82. "font: 87 10pt \"Arial Black\";")
  83.        self.label2.setAlignment(QtCore.Qt.AlignCenter)
  84.        self.label2.setObjectName("label2")
  85.        self.label3 = QtWidgets.QLabel(self.widget)
  86.        self.label3.setGeometry(QtCore.QRect(199, 125, 84, 18))
  87.        self.label3.setStyleSheet("color: rgb(255, 255, 255);\n"
  88. "font: 87 10pt \"Arial Black\";")
  89.        self.label3.setAlignment(QtCore.Qt.AlignCenter)
  90.        self.label3.setObjectName("label3")
  91.        self.layoutWidget = QtWidgets.QWidget(self.widget)
  92.        self.layoutWidget.setGeometry(QtCore.QRect(300, 300, 86, 44))
  93.        self.layoutWidget.setObjectName("layoutWidget")
  94.        self.verticalLayout = QtWidgets.QVBoxLayout(self.layoutWidget)
  95.        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
  96.        self.verticalLayout.setObjectName("verticalLayout")
  97.        self.valorPot = QtWidgets.QLabel(self.widget)
  98.        self.valorPot.setGeometry(QtCore.QRect(200, 150, 84, 42))
  99.        self.valorPot.setStyleSheet("color: rgb(255, 255, 255);\n"
  100. "font: 87 10pt \"Arial Black\";\n"
  101. "")
  102.        self.valorPot.setAlignment(QtCore.Qt.AlignCenter)
  103.        self.valorPot.setObjectName("valorPot")
  104.        self.horizontalLayout.addWidget(self.widget)
  105.        MainWindow.setCentralWidget(self.centralwidget)
  106.        self.statusbar = QtWidgets.QStatusBar(MainWindow)
  107.        self.statusbar.setObjectName("statusbar")
  108.        MainWindow.setStatusBar(self.statusbar)
  109.  
  110.        self.retranslateUi(MainWindow)
  111.        QtCore.QMetaObject.connectSlotsByName(MainWindow)
  112.  
  113.    def retranslateUi(self, MainWindow):
  114.        _translate = QtCore.QCoreApplication.translate
  115.        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
  116.        self.label2.setText(_translate("MainWindow", "LED "))
  117.        self.label3.setText(_translate("MainWindow", "VALOR POT."))
  118.        self.valorPot.setText(_translate("MainWindow", "0.00"))
  119.  
  120.        self.radioButton1.toggled.connect(self.led1)
  121.  
  122.    def leer_datos():
  123.        global datos
  124.        while True:
  125.            time.sleep(1)
  126.            datos = str (pot.read())
  127.            print (datos)
  128.  
  129.            valorPot.setText(datos)
  130.  
  131.  
  132.    thread = threading.Thread(target=leer_datos)    
  133.    thread.start()
  134.  
  135.  
  136.    def led1(self):
  137.        if self.radioButton1.isChecked()==True:
  138.            led1.write(1)
  139.  
  140.            print("ON")
  141.  
  142.        else:
  143.            led1.write(0)
  144.  
  145.  
  146.  
  147. if __name__ == "__main__":
  148.    import sys
  149.    app = QtWidgets.QApplication(sys.argv)
  150.    MainWindow = QtWidgets.QMainWindow()
  151.    ui = Ui_MainWindow()
  152.    ui.setupUi(MainWindow)
  153.    MainWindow.show()
  154.    sys.exit(app.exec_())
  155.  


« Última modificación: 8 Enero 2023, 17:39 pm por hacelo » En línea

hacelo

Desconectado Desconectado

Mensajes: 130



Ver Perfil
Re: Gui para Arduino en Python
« Respuesta #1 en: 15 Febrero 2023, 19:41 pm »

Ya he conseguido que se actualice el texto de la etiqueta volcándole el valor de la lectura de la entrada  analógica.
Aporto la parte del código que he tenido que modificar  por si a alguien le interesa.
Un saludo.


Código
  1.    def retranslateUi(self, MainWindow):
  2.        _translate = QtCore.QCoreApplication.translate
  3.        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
  4.        self.label2.setText(_translate("MainWindow", "LED "))
  5.        self.label3.setText(_translate("MainWindow", "VALOR POT."))
  6.        self.valorPot.setText(_translate("MainWindow", "0.00"))
  7.  
  8.        self.radioButton1.toggled.connect(self.led1)
  9.  
  10.        def leer_datos():
  11.            global datos
  12.            while True:
  13.                time.sleep(1)
  14.                datos = str (pot.read())
  15.                print (datos)
  16.  
  17.                self.valorPot.setText(datos)
  18.  
  19.  
  20.        thread = threading.Thread(target=leer_datos)    
  21.        thread.start()
  22.  


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines