Pc and Mcore with bluetooth module are paired. The port is COM9 outgoing in PC. However, when I send message Arduino does not get anything. I just disconnect arduino from pc and write simple code in Mcore that turn on LED when input message is 1. However still nothing. These are the codes in python and arduino side:
import time
import serial
port="COM9" #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'1') #These need to be bytes not unicode
bluetooth.close() #Otherwise the connection will remain open until a timeout
Arduino code:
char a;
void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
}
void loop()
{
if(Serial.available()){a = Serial.read();}
if (a == '1'){
digitalWrite(13, HIGH);
}
else{
digitalWrite(13, LOW);}
delay(100);
}
I have tried to install Pybluez for WIN10, however, there was error. Do you think that this is the reason?
Thanks in advance
2 Answers 2
Probably because you never called bluetooth.open()
.
What would be much easier to do is use pySerialTransfer and SerialTransfer.h to guarantee reliable and robust serial communication between Python and your Arduino.
pySerialTransfer is pip-installable and cross-platform compatible. SerialTransfer.h runs on the Arduino platform and can be installed through the Arduino IDE's Libraries Manager.
Both of these libraries have highly efficient and robust packetizing/parsing algorithms with easy to use APIs and come with examples.
kindly add a serial print before the if statement(if (a == '1')
) to see what value of a is recieved so we can debug it
else
part to debug