Python Forum
send a byte of data by bluetooth to arduino using python.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
send a byte of data by bluetooth to arduino using python.
#1
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 

t
his 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
Reply


Messages In This Thread
send a byte of data by bluetooth to arduino using python. - by x1cygnus - Dec-01-2019, 11:07 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  sending arduino data to python and use a parity check 10_Lucas 6 3,449 Aug-03-2019, 10:37 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020