Sending Serial Data to Arduino Using Python Script - python-3.x

I am facing a peculiar issue while writing characters to Arduino using python serial communication on macOS 10.14.
The Arduino is programmed to read a string, parse it and take PWM action to run a car.
Ardiuno's serial communication channel is configured to receive the strings in the format < A, B, C, D > where ABCD are numbers which denote car direction, speed, steering direction and steering position.
The problem is, when I send a string from the serial monitor or through the Python Development environment the string is received, parsed properly and command executed successfully.
However if I write a simple program in a file write.py and execute it from the command line, nothing happens.
import serial
ser = serial.Serial('/dev/cu.usbmodem14301', 9600)
data = '<1,150,0,0>'
ser.write(data.encode())
If I run this script from the macOS terminal using the command:
python write.py
nothing happens. What am I missing here?

A new USB connection with ser=serial.Serial('/dev/cu.usbmodem14301',9600) resets the Arduino. The data sent right after connection are lost because the Arduino boots.

It may be that the port is in text mode and will not send the data until a newline is sent:
data = '<1,150,0,0>\n'
ser.write(data.encode())
or flush() is called.
data = '<1,150,0,0>'
ser.write(data.encode())
ser.flush()

The most probably thing happening here is that the data is not being sent to the serial port.
There is a simple method to check this.
Connect the Arduino to your laptop (I suspect it to be a mac), and start the serial monitor on the Arduino IDE.
In the serial monitor type in <1,150,0,0> and press send.
The tx LED on the Arduino will blink. Now that you know how the pattern looks, repeat the same experiment with the Python code.
If the LED does not blink in the same manner you have a Serial port access issue, which you can fix using the instructions in the following link
Access USB serial ports using Python and pyserial
If not, I am stumped.

Related

I am trying to control all the outputs of an Arduino with pyserial

How to write a code that will generate a graphical interface and that will send the orders to the Arduino? So far, I have only managed to communicate with just pin 13 when I try with another pin nothing happens
I want to design a graphical interface in Python that will send commands and receive data from an Arduino in real time.
​from​ ​serial​ ​import​ ​*
​import​ ​tkinter
​serie​=​Serial​(​'COM4'​,​9600​)
​fenetre​ ​=​ ​tkinter​.​Tk​()
​fenetre​.​title​(​"Interface graphique Arduino"​)
​fenetre​.​geometry​(​"200x150"​)
​def​ ​ledOn​():
​   ​serie​.​write​(​b'7'​)
​def​ ​ledOff​():
​    ​serie​.​write​(​b'L'​)
​bouton1​=​tkinter​.​Button​(​fenetre​, ​text​=​"LED On"​, ​command​=​ledOn​ )
​bouton1​.​grid​(​column​=​1​, ​row​=​0​)
​bouton2​=​tkinter​.​Button​(​fenetre​, ​text​=​"LED Off"​, ​command​=​ledOff​)
​bouton2​.​grid​(​column​=​1​, ​row​=​1​)
​fenetre​.​mainloop​()
​serie​.​close​()

How to get the raw serial output from a serial device file without using pySerial?

I have an embedded development board connected to my Ubuntu host PC. And the board's serial port is exposed as /dev/ttyACM0 file.
I experiment with below naïve python code to read the raw serial output from the board.
with open("/dev/ttyACM0", "r") as sdf:
content = sdf.readline()
print(content)
I first let the board to output some string.
After a while, when I am sure the string output is finished, I run above script. It does print out the readable string. (BTW, it seems the serial output from the board seems to be cached somewhere.)
My questions are:
With tools like putty, I need to specify some parameters of the serial port, such as baud rate, etc. But I didn't specify anything in my script. How could it ever work?
If the serial port is outputting a stream of binary data endlessly, how to capture them lively?
Note: I don't want to use pySerial or other packages. I just want to do it from scratch.

Send cyclic command to serial port

I have to execute some tests with a Bluetooth LE module.
For the BT Chip I have an evaluation board here, which I can connect via USB to serial port on my PC.
From the manual of the eval board I learned how to broadcast data from the BT chip using Tera Term. It is just a simple command like "SHW, 0018, AABBCCDD" I have to type in and the BT module will send this data.
Now I want to automate Tera Term, so that this command is executed every 100ms.
I did some research and I know that I have to use the Tera Term macro language (TTL?), but I'm really not into this program.
Can anybody help me out here with a code snippet or a link to the right explanation? I think, it should be pretty easy?
Additional info:
I connect to the eval board on USB Serial Port (COM7) with baud rate 115200.
This should do the job:
while 1
sendln 'SHW, 0018, AABBCCDD'
mpause 100
endwhile
Save it as eg test01.ttl and load it in teraterm like this:
Also, here you can find a description of TTL commands.
My opinion though, is that you should start using Python and PySerial to handle this kind of tasks.

Arduino Random Characters sent to Bluetooth TX during Code Upload

When I upload code to my Arduino while the TX and RX pins are connected to my HC-05 module, a bunch of random characters are sent to the TX buffer, and when I connect to a device, those characters are sent and mess up communication. Is there a way that I can clear this buffer after uploading the code? I've just been disconnecting the wires whenever I upload, but I'd like to find an easier way. Thanks!
Well, if you use a serial port to both send data and the program of course you will see it on the other side of the BT... Possible solutions:
disconnect the BT module every time you want to program the Arduino
shut down the other BT device (or just disconnect it) when you have to program the Arduino
shut down the HC-05 (or keep it in reset state) until the arduino says that it is communicating (so use a GPIO to control the reset pin or a transistor to power the BT on at the beginning of the program)
use a 3-state driver between the HC-05 and the Arduino serial ports (one driver for TX and one for RX) and activate its outputs at the beginning of the arduino program.
I don't like djUniversal's solution because you cannot control what the PC transmits; if, for instance, you decide to use the byte 0xAA to signal the start of the transmission then if the PC sends 0xAA the other device thinks that the Arduino is transmitting. Choosing longer bytes sequences helps, because the sequence becomes less probable, but.....
Moreover you have to send it at EVERY command, not just at the beginning, because you have to reset the arduino to program it (and so the other device is not aware of WHEN to stop considering the data).
The only other way around it is to send a header of maybe a couple of bytes each time to send a message. The other program can wait for these characters before it starts to take commands. Until those characters are read from the buffer you would just do a Serial.read() loop to get rid of the garbage.
Also, if garbage characters are going to screw up your program really badly you might want to think about creating some kind of crude checksum also to confirm the correct transmission.
Need help coding? Let me know.

Tell when Bluetooth module connects on Arduino

I'm working on a project with Android and Arduino and am trying to figure out how on the Arduino side to tell if the Bluetooth is connected or not.
I'm using one of these Bluetooth Modules to connect. I know I can send a command through Android, but I'm trying to have an action happen automatically when they connect and not have to run a background application on the Android if possible.
Using the module supplied and nothing else you cannot: notice the module has four connectors:
Power (Vcc)
Ground
Tx (send)
Rx (receive)
Given this interface the only way to determine whether the bluetooth module is paired is to send something to the paired device and have it respond in such as way that your Arduino knows that it is connected. For instance, if your Android program always responds with "Hi there!" when it receives a string "Hello?", then by seingin "Hello?" your Arduino will know that it is paired with your Android phone/tablet. Your Arduino could poll (send the interrogation string) every minute (or every five seconds) to see if it is paired with your device.
There is a better way, but it will require some soldering on your part. If your module is HC-03/HC-05-based, then the PIO9 pin is the "paired indicator LED" (see datasheet here). You could connect that pin to an Arduino input pin and read the level: reading digital 1 will indicate that the device is paired, while reading digital 0 will indicate that it is not. It is possible, though not certain, that the pin on your module labeled STATE is exactly this kind of a pin, i.e. it indicates the paired status. Unfortunately. this pin it isn't connected to the header, so you'll have to solder a wire to the correctponding pad to connect it to your Arduino. You should test it first by connecting a multimeter in voltage mode to that pad and measure the potential between that pad and ground in paired and non-paired state. If this is the pin that responds to the paired state then you are golden. It might be that it indicates power (like the HC-03/05 PIO8 whilc blinks when on). If it turns out that the STATE pin is not the pairing status, then you should request a datasheet from your supplier, and use that to find the status LED connection: one is likely to exist. Once you found the correct pad, verify its function using the voltmeter again. Then solder a wire to that connection and read it from your Arduino.
IMPORTANT: Make sure that your Arduino never puts out a digital 1 on the Arduino pin connected to the bluetooth module status pin: these bluetooth modules run on 3.3V, and connecting any unprotected pins to 5V will be damaging. The Vcc and Txd pins are voltage shifted in the module you bought, but the LED/Status lines are likely not to be. So if the Arduino pin connected to "status" on your Bluetooth module is configured as output and you digitalWrite(HIGH) to it, you will likely damage the Bluetooth module.
Unfortuntaely, the HC-05 will switch states when paired, but won't output a 1 until it's actually connected to something.
For instance, I can unpair my phone from the HC-05, pair again, and then the LED will change state, but the output of the STATE pin is still 0. If I open up an app, and connect to the device manually then the LED, and STATE pin will change state. The LED will periodically blink twice, and the STATE pin outputs a 1 to the Arduino.
If you would like to read the the value of the STATE pin, connect a wire to any of the inputs to the arduino, and code Serial.println(digitalRead(inputPin)); inputPin being the wire to the input of the Arduino.
I've been fighting this thing for months, and have yet to find a way to make this thing automatically connect to my phone. It won't even allow for me to connect to it from my phone to the HC-05 unless I download an app onto my Android. It's possible to bind the HC-05 to a certain address, but even this did not work for me. I want to mess with the "AT+CLASS" command, but the documentation behind the instruction has hindered me thus far.
From the HC-05 datasheet we see that the connection status depends on the output from PI09. Apparently sending "AT+BIND?" to the module will return the status of PI08 & PI09 in the form,
"+ POLAR=PI08,PI09" however this makes no sense to me because in order to get this you must enter AT mode and entering AT mode will disrupt the paired connection, hence it will always send PI09 marked as "not connected".
THUS in order to see if the connection is still live from the arduinos POV I can only see 2 feasible ways:
Program arduino to, every so often, send a "hello?" and if it doesn't receive the expected "Hi back" response, then it is to assume that it isn't connected.
Connect PI09 to an arduino input pin and read it's value whenever you want to check if the connection is live or not
AT+STATE? will return the current state of the connection. Yes you will need to enter at mode, that is done by bringing up pin 11 HIGH on the HC05 module. It does require soldering but it's kinda worth it. It then allows full AT control of the device, then set it LOW to return it to normal working mode.
Another option, which I don't fully understand, is the AT+MPIO? command, which returns the state of all the pins in some strange masked format I don't understand yet.
I use the first option above so that I can terminal (Bluetooth) from my phone to the HC05 and switch on a led/relay etc (ie bring up pin 2 to HIGH) on the HC05. This required entering AT mode (pin 11 HIGH), sending the command AT+PIO=2,1 and then setting pin 11 to LOW to return to normal working mode.
Note: I noticed I had to put a 200ms delay in between high and AT and LOW commands. Angela's solution is nice - I use a cheap XBEE Bluetooth module (HC-05 Bluetooth Bee Master & Slave Module with Bluetooth XBee for Arduino uk2015) 2 units(HC05/6) for 5Stg which are laid out in XBEE format - handy for the 3.3v.

Resources