I am trying to run a shell script and capture the output. The sketch compiles and loads fine, but then does not output any serial text at all. I've modified the shell script command with a redirect to a log file, but that doesn't work either. It appears the process is never run. The output of the script is a single integer. Any ideas? I am basing this off the Arduino sample Process sketch.
#include <Process.h>
void setup() {
Bridge.begin(); // Initialize the Bridge
Serial.begin(9600); // Initialize the Serial
// Wait until a Serial Monitor is connected.
// while(!Serial);
}
void loop() {
Serial.println("Elevation");
Process p;
// Run the shell command to get the desired solar elevation (angle)
p.runShellCommand("/mnt/sdb1/20140225-2/elevation.sh 12.345 -12.78901 02/22/2014 02:42:00");
// do nothing until the process finishes, so you get the whole output:
while(p.running());
// Read command output. runShellCommand() should have passed a number
while (p.available()) {
int elevation = p.parseInt(); // look for an integer
Serial.println(elevation); // print the number as well
}
Serial.println("...");
delay(5000); // wait 5 seconds before you do it again
}
The funny thing is that I'm doing a Serial.println at the beginning of the loop, and that doesn't even run. I did as suggested as well, with no different results. I've also tried redirecting the shell command to a file like "/sdcard/shellscript.sh >> /sdcard/logs.txt" and the logs.txt file is never created, so I know the script is never run.
#include <Process.h>
void setup() {
Bridge.begin(); // Initialize the Bridge
Serial.begin(9600); // Initialize the Serial
// Wait until a Serial Monitor is connected.
// while(!Serial);
}
void loop() {
Serial.println("Elevation");
Process p;
// Run the shell command to get the desired solar elevation (angle)
p.runShellCommand("/mnt/sdb1/20140225-2/elevation.sh 12.345 -12.78901 02/22/2014 02:42:00 >> /mnt/sdb1/20140225-2/log.txt");
// do nothing until the process finishes, so you get the whole output:
while(p.running());
// Read command output. runShellCommand() should have passed a number
while (p.available()) {
// int elevation = p.parseInt(); // look for an integer
String elevation = p.readString();
Serial.println(elevation); // print the number as well
}
Serial.println("...");
delay(5000); // wait 5 seconds before you do it again
}
It turns out the sample on the Arduino website is wrong, so here's what I did to fix it- I added "#include <Bridge.h>;" and a "break;" statement. What is curious is that my bash script when called from a ssh session returns my numeric value, but when I call it from the Arduino sketch as shown below, it always returns a 0. I did a sample shell script to echo a number, and that works- so I suspect I am doing something with my script that is interfering with the shell script returning the value.
Piping the shell script results to a logfile also returns a 0 in the logfile, so I am puzzled as to what's going on here. It's a bit of uncharted territory, so I'll post updates as I figure them out- but any suggestions are certainly welcome!
// A big thank you to Arduino for this script to learn from: http://arduino.cc/en/Tutorial/ShellCommands#.Uw0qZPldXAk
#include <Process.h>
#include <Bridge.h>
void setup() {
Serial.println("Starting the serial port");
Serial.begin(9600); // Initialize the Serial
Serial.println("Starting the bridge");
Bridge.begin(); // Initialize the Bridge
}
void loop() {
Serial.println("Elevation");
Process p;
// Run the shell command to get the desired solar elevation (angle)
p.runShellCommand("/root/solar/solar.bash 31.123 -123.456 02/22/2014 02:42:00 -E ");
// do nothing until the process finishes, so you get the whole output:
while (p.running());
// Read command output. runShellCommand() should have passed a number
while (p.available()) {
int result = p.parseInt(); // look for an integer
int elevation = result;
Serial.println(elevation); // print the number as well
break;
}
Serial.println("...");
delay(5000); // wait 5 seconds before you do it again
}
All the parameters are getting passed in ok, and the "123" returns from the test script. It must be something about the script that's not outputting to the console normally.
I also found another bug- the serial port in the Adafruit IDE won't display anything while I have an Adafruit GPS hooked up to TX and RX (1 and 0) on the Yun.
You can't use the IDE's serial monitor if you're using pins 0 and 1 for other things on a regular Arduino either. Those are the default Serial interface pins.
WRT the script, I'm really not sure what to suggest next, except asking over in the official Arduino forums: http://forum.arduino.cc That's where the Yun's creators hang out, and they have a more detailed understanding of the Yun than I do.
Thank you- can SoftwareSerial be used to map the serial port to say, digital pins 5 and 3 for the GPS? I was under the impression there are some restrictions for the Leonardo and Yun in this matter.
Yeah, you can use SoftwareSerial to handle a UART connection through some other set of pins, then reserve 0 and 1 for communication with the IDE's serial port.
The restrictions on serial communication for the Yun mostly involve using pins 0 & 1.
I figured out the script issue- it was a path error in the script- errors result in the '0' result when using Process.h. It turns out that the GPS shield has to run on 0,1 because I am using all the other pins for other hardware. I converted the sample GPS script for the Leonardo to use with the Yun- it is included below. When I use pins 8,7 for serial, the console from the Linux side shows the GPS output just fine. When I comment out the SoftwareSerial and go with the hardware serial, the console on the Yun won't open.
// Test code for Adafruit GPS modules using MTK3329/MTK3339 driver
//
// This code shows how to listen to the GPS module in an interrupt
// which allows the program to have more 'freedom' - just parse
// when a new NMEA sentence is available! Then access data when
// desired.
//
// Tested and works great with the Adafruit Ultimate GPS module
// using MTK33x9 chipset
// ------> http://www.adafruit.com/products/746
// Pick one up today at the Adafruit electronics shop
// and help support open source hardware & software! -ada
//This code is intended for use with Arduino Leonardo and other ATmega32U4-based Arduinos
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <Console.h>
#include <Bridge.h>
// Connect the GPS Power pin to 5V
// Connect the GPS Ground pin to ground
// If using software serial (sketch example default):
// Connect the GPS TX (transmit) pin to Digital 8
// Connect the GPS RX (receive) pin to Digital 7
// If using hardware serial:
// Connect the GPS TX (transmit) pin to Arduino RX1 (Digital 0)
// Connect the GPS RX (receive) pin to matching TX1 (Digital 1)
// If using software serial, keep these lines enabled
// (you can change the pin numbers to match your wiring):
//SoftwareSerial mySerial(8, 7);
//Adafruit_GPS GPS(&mySerial);
// If using hardware serial, comment
// out the above two lines and enable these two lines instead:
Adafruit_GPS GPS(&Serial1);
HardwareSerial mySerial = Serial1;
// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO true
void setup()
{
// connect at 115200 so we can read the GPS fast enough and echo without dropping chars
// also spit it out
Bridge.begin();
Console.begin();
delay(5000);
Console.println("Adafruit GPS library basic test!");
// 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
GPS.begin(9600);
// uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
// uncomment this line to turn on only the "minimum recommended" data
//GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
// For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
// the parser doesn't care about other sentences at this time
// Set the update rate
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
// For the parsing code to work nicely and have time to sort thru the data, and
// print it out we don't suggest using anything higher than 1 Hz
delay(3000);
// Ask for firmware version
mySerial.println(PMTK_Q_RELEASE);
}
uint32_t timer = millis();
void loop() // run over and over again
{
char c = GPS.read();
// if you want to debug, this is a good time to do it!
if ((c) && (GPSECHO))
Serial.write(c);
// if a sentence is received, we can check the checksum, parse it...
if (GPS.newNMEAreceived()) {
// a tricky thing here is if we print the NMEA sentence, or data
// we end up not listening and catching other sentences!
// so be very wary if using OUTPUT_ALLDATA and trytng to print out data
//Console.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
// if millis() or timer wraps around, we'll just reset it
if (timer > millis()) timer = millis();
// approximately every 2 seconds or so, print out the current stats
if (millis() - timer > 2000) {
timer = millis(); // reset the timer
Console.print("\nTime: ");
Console.print(GPS.hour, DEC); Console.print(':');
Console.print(GPS.minute, DEC); Console.print(':');
Console.print(GPS.seconds, DEC); Console.print('.');
Console.println(GPS.milliseconds);
Console.print("Date: ");
Console.print(GPS.day, DEC); Console.print('/');
Console.print(GPS.month, DEC); Console.print("/20");
Console.println(GPS.year, DEC);
Console.print("Fix: "); Console.print((int)GPS.fix);
Console.print(" quality: "); Console.println((int)GPS.fixquality);
if (GPS.fix) {
Console.print("Location: ");
Console.print(GPS.latitude, 4); Console.print(GPS.lat);
Console.print(", ");
Console.print(GPS.longitude, 4); Console.println(GPS.lon);
Console.print("Speed (knots): "); Console.println(GPS.speed);
Console.print("Angle: "); Console.println(GPS.angle);
Console.print("Altitude: "); Console.println(GPS.altitude);
Console.print("Satellites: "); Console.println((int)GPS.satellites);
}
}
}
I'm glad to hear you're getting data from the GPS module to the Yun.
I'm stumped regarding the trouble with a hardware serial connection and the Yun console though.. you'll probably need to ask over in the Arduino forums about that (http://forum.arduino.cc/).