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

查看:211
本文介绍了无法从我的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.

所以,这是一个考验code,如果我pressing按钮,我将不得不派出3个变量的编号为1,如果我不是,我会得到两个变量的数量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.

所以,我从来没有当pressing按钮,我应该得到的值。我总是得到:随机数,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.

Arduino的:

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(" "); 
  }
}

的Python:

Python:

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?

推荐答案

我是pretty相信如果你改变这种

I'm pretty sure that if you change this

pinMode(i_presion, INPUT);

进入这个

pinMode(i_presion, INPUT_PULLUP);

这将开始工作......

It will start working....

原因是,当按钮是不是pressed你是不是拉起脚。

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...

我从来没有当pressing按钮,我应该得到的值。我总是得到:随机数,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天全站免登陆