Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python Arduino:在';str';和';int';_Python 3.x_Arduino - Fatal编程技术网

Python 3.x Python Arduino:在';str';和';int';

Python 3.x Python Arduino:在';str';和';int';,python-3.x,arduino,Python 3.x,Arduino,我想用Arduino向我的手机发送信息 我正在使用Twilio的API、Python和Arduino IDE。所以我使用两个按钮,如果我按下按钮1,信息是“打开灯”,如果我按下按钮2,信息是“关闭灯” 但python有一个问题: TypeError:'>'在'str'和'int'实例之间不受支持。 我在下面的代码中隐藏了我的帐户SID、令牌和号码 阿杜伊诺代码 const int buttonPin1 = 6; // the number of the pushbutton pin

我想用Arduino向我的手机发送信息

我正在使用Twilio的API、Python和Arduino IDE。所以我使用两个按钮,如果我按下按钮1,信息是“打开灯”,如果我按下按钮2,信息是“关闭灯”

但python有一个问题:

TypeError:'>'在'str'和'int'实例之间不受支持。

我在下面的代码中隐藏了我的帐户SID、令牌和号码

阿杜伊诺代码


const int buttonPin1 = 6;     // the number of the pushbutton pin

const int buttonPin2 = 7;

int buttonstate1 = 0;

int buttonstate2 = 0;

void setup() {

 Serial.begin(9600);

 pinMode(buttonPin1, INPUT);

 pinMode(buttonPin2, INPUT);

 pinMode(LED_BUILTIN, OUTPUT);

}

void loop() {

  buttonstate1 = digitalRead(buttonPin1);

  buttonstate2 = digitalRead(buttonPin2);

  if (buttonstate1 == HIGH) {

    Serial.println("a");

    digitalWrite(LED_BUILTIN, HIGH);

    delay(1000);
 } else {
    // turn LED off:

digitalWrite(LED_BUILTIN, LOW);

}
  if (buttonstate2 == HIGH) {

    Serial.println("b");

    digitalWrite(LED_BUILTIN, HIGH);

    delay(1000);
  }
else {

    digitalWrite(LED_BUILTIN, LOW);

  }
Python代码


import time

import serial

from twilio.rest import Client

arduinoserial = serial.Serial('COM3', 9600)

time.sleep(2)

account_sid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

auth_token  = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

client = Client(account_sid, auth_token)

while True:

        if arduinoserial.read('a'):

            print(arduinoserial.readline().str())

            messageTosend="TURN ON LAMP"

            message = client.messages.create(

                                body=messageTosend,

                                from_='+14xxxxxxx',

                                to='+14xxxxxxxx'
                        )
        if arduinoserial.read(int('b')):

            print(arduinoserial.readline().str())

            messageTosend = "TURN OFF LAMP"

            message = client.messages.create(

                body=messageTosend,

                from_='+14xxxxxxxxxxxx',

                to='+14xxxxxxxxxxxxxxx'
            )

time.sleep(1)

print(message.sid)

arduinoserial.close()

你的两个if语句就是问题所在

下面是serial.read()的工作原理:

下面是您需要如何调整代码:(如果您只想这样做一次,请去掉while循环)

这行代码从缓冲区读取一行,将其转换为字符串,然后将其与另一个字符(“a”或“b”)进行比较。如果字符串和字符相等,则执行If语句的主体


希望这有帮助。:]

请正确格式化您的代码
arduinoserial.read() #reads one byte from buffer
arduinoserial.read(10) #reads ten bytes from buffer

arduinoserial.read('a') #makes no sense
while true:
    arduino_input = arduinoserial.readline() #reads a full line of text (also waits till the arduino sent a line of text)

    if arduino_input == 'a': #compares text to 'a'
        [do your stuff here]

    if arduino_input == 'b': #compares text to 'b'
        [do your stuff here]