So I wrote a simple serial testing script between the arduino and python. Arduino code:
Code: Select all
String inputString = "";
int led = 13;
boolean stringComplete;
void setup() {
Serial.begin(9600);
inputString.reserve(200);
pinMode(led, OUTPUT);
stringComplete = false;
}
void loop() {
if (stringComplete) {
Serial.println(inputString);
stringComplete = false;
if(inputString == "on") {
digitalWrite(led, HIGH);
Serial.println("woo light");
}
if(inputString == "off") {
digitalWrite(led, LOW);
Serial.println("no light");
}
inputString = "";
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if (inChar == '\n') {
stringComplete = true;
break;
}
inputString += inChar;
}
}Code: Select all
#!/usr/bin/python
import serial
import sys
if len(sys.argv) == 2:
s = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
s.write(sys.argv[1] + "\n")
print sys.argv[1]
Rebooted.
Ran the python program and it didn't turn on the led, so I opened the arduino ide and reuploaded -> same problem.
Then I opened the serial monitor and tried to see what the output was and it started working again, and throughout several successive tests it never worked until I opened the serial monitor after a reboot. I could shutdown the monitor and it would keep working until the next reboot as well.
So my question is: how can I get it to just work correctly on a reboot without having to open the Serial Monitor? I remember something from years ago about a logic level converter working correctly for that instead of USB, but I don't know the pin configuration for rPi3 for one anyway and I'd prefer to use usb. Anyone know how to solve this?