首页 > 解决方案 > 在带有 jupyter notebook 的 windows 10 计算机上使用 python 将一个字节的数据发送到 hc 05 arduino 蓝牙模块

问题描述

我必须构建可以根据代码结果移动的汽车,因此必须通过蓝牙(使用蓝牙模块 Hc -05)发送信息字节 unsing python(当前使用 jupyter notebook,python 3)。我已经尝试了几天没有运气。这是我一直在使用的代码。使用套接字库

import socket

serverMACAddress = '00:00:00:00:00:00'
port = 4
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.connect((serverMACAddress,port))
s.send(bytes("A",'UT
AttributeError: module 'socket' has no attribute 'AF_BLUETOOTH'F-8'))
s.close()

以及序列和时间库

import serial
import time


port="COM4" #This will be different for various devices,COM port.
bluetooth=serial.Serial(port, 9600)#Start communications with the bluetooth unit
bluetooth.flushInput() #This gives the bluetooth a little kick
bluetooth.write(b"A")#These need to be bytes not unicode
bluetooth.close() #Otherwise the connection will remain open until a timeout 

这是arduino的代码

#include <SoftwareSerial.h>
SoftwareSerial miBT(10,11);

void setup() {
  Serial.begin(9600);
  miBT.begin(38400);

  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
}

void loop(){
  if (miBT.available()>0) {

    byte input=miBT.read();

    if(input == 'B'){          //FORWARD ---- 
        digitalWrite(6, HIGH);
        digitalWrite(5, LOW);
        digitalWrite(4, LOW);
        digitalWrite(3, HIGH);
    }

    if(input == 'A'){          //BACKWARD --- 
        digitalWrite(6, LOW);
        digitalWrite(5, HIGH);
        digitalWrite(4, HIGH);
        digitalWrite(3, LOW);
    }

    if(input == 'C'){          //LEFT --- 
        digitalWrite(6, LOW);
        digitalWrite(5, HIGH);
        digitalWrite(4, LOW);
        digitalWrite(3, LOW);
    }

    if(input == 'D'){          //RIGHT --- 
        digitalWrite(5, LOW);
        digitalWrite(4, HIGH);
        digitalWrite(3, LOW);
    }

    if(input == 'E'){          //STOP --- 
        digitalWrite(6, LOW);
        digitalWrite(5, LOW);
        digitalWrite(4, LOW);
        digitalWrite(3, LOW);
    }
  }
}

尝试了几天,我没有很多编码技巧。

因为 hc05 使用“COM4”或发送和“COM5”接收

HC-05             Arduino UNO
-----             -----------
RX       -->      Pin 11
TX       -->      Pin 10
+5v      -->      +5v
GND      -->      GND 

标签: pythonarduinobluetoothjupyter-notebook

解决方案


不确定同时使用 socket 和 pyserial 来达到什么目的。据我所知,蓝牙套接字在 Windows 下甚至不可用,而且从外观上看,您的 Python 是在没有蓝牙支持的情况下构建的。

将您的 Windows 10 PC 与 HC05 模块配对。然后在 Windows 蓝牙设置中设置传出 COM 端口。(蓝牙和其他设备 -> 更多蓝牙选项)

然后使用pyserial发送数据。

我建议您一步一步按照任何适用于 Windows 10 的 Arduino 蓝牙教程进行操作。


推荐阅读