python - 在带有 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
解决方案
不确定同时使用 socket 和 pyserial 来达到什么目的。据我所知,蓝牙套接字在 Windows 下甚至不可用,而且从外观上看,您的 Python 是在没有蓝牙支持的情况下构建的。
将您的 Windows 10 PC 与 HC05 模块配对。然后在 Windows 蓝牙设置中设置传出 COM 端口。(蓝牙和其他设备 -> 更多蓝牙选项)
然后使用pyserial发送数据。
我建议您一步一步按照任何适用于 Windows 10 的 Arduino 蓝牙教程进行操作。
推荐阅读
- android - 如何在android中播放m3u文件?
- api - 从 openshift 调用 API 的 IP/主机名白名单方式
- javascript - 修复了滚动时的侧边栏,然后在向上滚动时恢复正常
- scala - Spark reduce 功能导致“错误类型不匹配”
- java - Java - 强制/警告不推荐使用的注释没有链接(或不推荐使用的链接)
- php - 不删除 codeigniter 中的 index.php
- matlab - 使用二分法找到第一类贝塞尔函数 (J0(x)) 的第 n 个根
- javascript - Javascript/NodeJS:在 forEach 内完成 .find 之前的 html 渲染
- go - 在 go 中使用 sleep 时执行哪个 goroutine?
- ios - 如何在 iOS 中去除 mapbox 的蓝色和灰色注解