I am having a bit of a bizarre problem that I can now reliably replicate. I have an Arduino sketch that reads from the serial port, and based on the input, turns on/off or changes the color of the attached LED strip. The Arduino is attached to a Raspberry Pi 3, from which I send data on its serial port to the Arduino via /dev/Arduino, 9600baud.
When the Raspberry Pi boots, it doesn't matter how long I wait, the Arduino does not receive the message. I use the following snippet to write to the Pi's serial port. Each message is only 1 byte (a number, but represented as an ASCII character).
import serial
with serial.Serial('/dev/Arduino', 9600, timeout=1) as port:
port.write(str(1).encode())
I have discovered that once I open a monitor to the serial port using "screen", I can issue commands to the Arduino and it lights the LED strip correctly. Everything works well until I reboot the Raspberry Pi.
It seems like some buffer needs to flushed or something and I have tried using port.flush() both before and after port.write() and it hasn't worked.
What are some things I can try?
A simplified version of my sketch is below:
#include "Wire.h"
#include "Adafruit_NeoPixel.h"
Adafruit_NeoPixel strip = Adafruit_NeoPixel(N_LEDS, PIN, NEO_RGBW + NEO_KHZ800);
void setup() {
Serial.begin(9600);
strip.begin();
loop();
}
void loop() {
char incomingByte;
while(true) { // strip.show() is run within each LED function
if (Serial.available() > 0) {
incomingByte = Serial.read();
switch(incomingByte) {
case '0':
// Machine ready.
glow_white();
break;
case '1':
// Machine badness.
glow_red();
break;
case '2':
// Printing
rainbow();
break;
case '3':
// Machine done.
glow_green();
break;
case '4':
// Machine off
led_off();
break;
default:
glow_white();
break;
}
}
wait();
}
}
static void wait() {
delay(10);
}