0

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

5
  • first I would remove the else part to debug
    – Juraj
    Commented Mar 4, 2020 at 5:45
  • I also increased delay much like 3 seconds, still no response. From the led of bluetooth module it is felt that it receives something, but either format is problem or maybe it is not working properly.
    – Pasha
    Commented Mar 4, 2020 at 10:54
  • I have tried many combinations including your suggestion.
    – Pasha
    Commented Mar 4, 2020 at 11:33
  • I can't add the comment that's why add an answer. Check this link and you will get your result. github.com/huberf/Computer-to-Arduino-Bluetooth Commented Mar 5, 2020 at 10:33
  • Thank you all for answers, but the reason was very simple: The baud rate of Mbot bluetooth was 115200 and hence it could not get proper data.
    – Pasha
    Commented Apr 14, 2020 at 19:28

2 Answers 2

1

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.