无法从我的 arduino 传感器更新 Python (Tkinter) 中的传感器数据 [英] Cannot update sensor data in Python (Tkinter) from my arduino sensors

查看:43
本文介绍了无法从我的 arduino 传感器更新 Python (Tkinter) 中的传感器数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Python Tkinter 上的接口.我在从 arduino 向 Python 接口发送随机数之前进行了测试,它工作正常(它一直在更新),所以我认为从传感器发送数据时它会工作,但它没有.

I am learning about interfaces on Python Tkinter. I was testing before sending random numbers from my arduino to my Python interface, It worked (It updates all the time), so I thought it was going to work when sending data from a sensor, but it did not.

所以,这是一个测试代码,如果我按下按钮,我将不得不发送 3 个变量为 1,如果我不是,我将获得 2 个变量,数字为 2 和一个随机变量.

So, this is a test code, if I am pressing the button, I would have to send 3 variables with the number 1, and if I am not, I will get 2 variables with the number 2 and one random variable.

所以,我从来没有得到按下按钮时应该得到的值.我总是得到:随机数,2 和 2.

So, I never get the values that I should get when pressing the button. I am always getting: random number, 2 and 2.

阿杜诺:

void setup() {
  Serial.begin (9600);
  pinMode(i_presion, INPUT);
}

void loop() 
{
 if(digitalRead(i_presion) == HIGH ) 
  { 
  Serial.print(1);
  Serial.print(" ");
  Serial.print(1);
  Serial.print(" ");
  Serial.print(1);
  Serial.print(" "); 
  }
 else 
 { 
  Serial.print(random(3,8));
  Serial.print(" ");
  Serial.print(2);
  Serial.print(" ");
  Serial.print(2);
  Serial.print(" "); 
  }
}

蟒蛇:

import serial
import time
from Tkinter import *
root = Tk()
ser = serial.Serial("/dev/cu.usbmodem1411", 9600, timeout=1)

flagCharacter = 'k'

canvas = Canvas(root, width=1920, height=1080)
canvas.pack()

photo = PhotoImage(file= r"ANDREA-FIORI2.gif")
label=Label(root, image=photo)
photo = PhotoImage(file= r"ANDREA-FIORI2.gif")
canvas.pack(side='top', fill='both', expand='yes')
canvas.create_image(0, 0, image=photo, anchor='nw')



def sensores(planeado, producido, alertas):

    canvas.create_text(390, 430, text=planeado, fill="gray", font="Helvetica 100 bold",tag="T1")
    canvas.create_text(650, 430, text=producido, fill="gray", font="Helvetica 100 bold",tag="T2")
    canvas.create_text(900, 430, text=alertas, fill="gray", font="Helvetica 100 bold",tag="T3")

    #root.after(1000,sensores)

def borrar():
    canvas.delete("T1")
    canvas.delete("T2")
    canvas.delete("T3")

def do_update():
    ser.write(flagCharacter)
    borrar()
    allitems=ser.readline(6)
    x, y, z = allitems.split()
    sensores(x, y, z)
    root.after(1000, do_update)

do_update()
root.mainloop()

为什么它会随机更新函数而不是来自我的传感器的信息的任何想法?

Any ideas of why it is updating the function random, and not information from my sensors?

推荐答案

我很确定如果你改变这个

I'm pretty sure that if you change this

pinMode(i_presion, INPUT);

进入这个

pinMode(i_presion, INPUT_PULLUP);

它将开始工作......

It will start working....

原因是当按钮没有按下时你没有拉起引脚.

The reason is that you are not pulling up the pin when the button is not pressed.

或者,至少,这是导致...的最常见原因

Or, at least, this is the most common reason for the...

我从来没有得到按下按钮时应该得到的值.我总是得到:随机数,2 和 2

I never get the values that I should get when pressing the button. I am always getting: random number, 2 and 2

...问题.

这篇关于无法从我的 arduino 传感器更新 Python (Tkinter) 中的传感器数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆