send a byte of data by bluetooth to arduino using python. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: send a byte of data by bluetooth to arduino using python. (/thread-22890.html) |
send a byte of data by bluetooth to arduino using python. - x1cygnus - Dec-01-2019 I have to construct car that can move based on the result the code, so have to send byte of information by bluetooth(using the bluetooth module Hc -05) using python(currently using jupyter notebook, python 3). I've been trying for days with no luck. this are codes I've been using. try with the socket library 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()And the serial and time library 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 this is the 'C' code for the 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); } } }trying for days, i dont have a lot of coding skill. for the hc05 is using the "COM4" or sending and "COM5" for recieving HC-05 Arduino UNO ----- ----------- RX --> Pin 11 TX --> Pin 10 +5v --> +5v GND --> GND RE: send a byte of data by bluetooth to arduino using python. - VerlaHiltz - Mar-13-2020 Hi....It says zero is returned if no valid number is received in the timeout period. So it is not showing that your blue tooth is working. In your Arduino code there is no need to convert incoming bytes into a string before printing it out. Forget the parsInt at the moment and just print the raw data you get from the Bluetooth. |