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

asked Feb 25, 2020 at 12:18
5
  • first I would remove the else part to debug 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. Commented Mar 4, 2020 at 10:54
  • I have tried many combinations including your suggestion. 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. 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.

answered Apr 7, 2020 at 5:05
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

answered Mar 7, 2020 at 9:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.