Hey,
My arduino program keeps crashing after a few loops of running and I'm trying to diagnose why it's crashing.
Is there a way to track and print out the hardware usage(RAM, other stuff I don't know about) after each loop?
Thanks!
Hey,
My arduino program keeps crashing after a few loops of running and I'm trying to diagnose why it's crashing.
Is there a way to track and print out the hardware usage(RAM, other stuff I don't know about) after each loop?
Thanks!
It's more likely to be a software problem. Post your code (between code tags) so that we can see what may be causing it.
Is there a way to track and print out the hardware usage(RAM, other stuff I don't know about) after each loop?
There are ways to track SRAM usage.
http://playground.arduino.cc/Code/AvailableMemory
Keep in mind that "after each loop" is the wrong time to be measuring things like memory usage, as that is partly dependent on function calls, etc.
Keep in mind, also, that the simple act of measuring things is going to affect what you are measuring.
Henry's response is spot on, though.
Ah, that would definitely make sense. I'll post the code later tonight.
Thanks for the heads up.
Here's the sketch code:
#include <Bridge.h>
#include <Process.h>
#define ledPin 7
long salesDifference = 0;
long saleCount = 0;
long saleNumber = 0;
long lastSaleCount = 0;
void setup()
{
Bridge.begin();
pinMode(ledPin,OUTPUT);
pinMode(13,OUTPUT);
}
void loop()
{
runScript();
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);
}
void runScript()
{
Process reading;
reading.begin("python");
reading.addParameter("mike.py");
reading.run();
String output1;
while(reading.available())
{
output1 += (char)reading.read();
}
String saleCount = String(output1);
saleNumber = stringToLong(saleCount);
salesDifference = saleNumber - lastSaleCount;
lastSaleCount = saleNumber;
if(salesDifference > 0 && salesDifference != lastSaleCount)
{
digitalWrite(ledPin,HIGH);
delay(1000);
digitalWrite(ledPin,LOW);
delay(1000);
}
}
long stringToLong(String s)
{
char arr[12];
s.toCharArray(arr, sizeof(arr));
return atol(arr);
}
Here's the python script on the linino side:
import urllib2
import ast
r = urllib2.urlopen('https://openapi.etsy.com/v2/users/mjmostachetti/profile?fields=transaction_sold_count&api_key=1pmmjgt3j4nz5ollhzz2hvib')
a = r.read()
y = ast.literal_eval(a)
b = y['results'][0]['transaction_buy_count']
print b
String saleCount = String(output1);
It makes no sense to pass a String to the String constructor, so that you can get a String.
long stringToLong(String s)
{
char arr[12];
s.toCharArray(arr, sizeof(arr));
return atol(arr);
}
The String class has a (misnamed) toInt() function that actually returns a long.
Personally, I'd get rid of the String class altogether.
So I removed the code you recommended, but now when I do:
void runScript()
{
Process reading;
reading.begin("python");
reading.addParameter("mike.py");
reading.run();
String output1;
while(reading.available())
{
output1 += (char)reading.read();
}
Serial.println(output1);
Nothing prints out. I did add this to the setup and #include:
#include <Bridge.h>
#include <Process.h>
#include <Serial.h>
#define ledPin 7
long salesDifference = 0;
long saleCount = 0;
long saleNumber = 0;
long lastSaleCount = 0;
void setup()
{
Bridge.begin();
pinMode(ledPin,OUTPUT);
pinMode(13,OUTPUT);
Serial.begin(9600);
while(!Serial);
Serial.println("ready");
}
reading.run();
String output1;
while(reading.available())
How long does it take the script to get started, and start returning some data? More than a few nanoseconds, I'm guessing.
Not sure on the exact time. But it runs through the loop fairly quickly(~2 sec) per loop until it crashes. Do you know why it's not outputting the number?
But it runs through the loop fairly quickly(~2 sec) per loop until it crashes. Do you know why it's not outputting the number?
Most of the time is spent setting up to run the process. NO time is spent waiting for the process to start returning data. Whatever it does return (nothing) is appended to the String, and the String is printed. Then, loop() starts over. It is no surprise to me that nothing is printed.
while(reading.available())
This code will stop consuming the process output as soon as all available output has been consumed. Since the Arduino will probably be capable of reading input far sooner and far faster that the process will output it, this loop is likely to terminate immediately, leaving the process orphaned. I'm not familiar with using the bridge but I expect you'll find that you need to keep consuming input as long as the process is running, in order to receive all the output from the process.
Hmm, thanks for the great explanation!
So you're saying the output from my python script is coming out to slowly, such that my sketch thinks it has consumed all the output from it and exits the while loop(probably before it even starts)? I'm guessing a delay function would fix this issue, allowing the python script to finish outputting the data and then proceed to the while loop.
I thought all code went top to bottom(i.e. top processes finish before the ones below begin).
Is that correct?
I thought all code went top to bottom(i.e. top processes finish before the ones below begin).
Is that correct?
Depends on the function. Some functions trigger asynchronous processing (as Process::run() does).
So the process starts, but before it generates any output, you have determined that there is nothing going to some from the process, (because nothing has in the few nanoseconds after you start the process), so you move on, ignoring any output that the process may generate later.
Interesting. I guess that makes sense, since the linino side is separate from the arduino side. I should have put that together,
Slowly getting there. I'll try the delay on the arduino side while the python script is running and let you know if that fixes the problem. Thanks again for the help explaining the code!
Adding a delay of 10 seconds doesn't seem to help the problem.
Process reading;
reading.begin("./etsy.php");
reading.run();
delay(10000);
String output1;
while(reading.available())
{
output1 += (char)reading.read();
}
I'm guessing I still don't understand why it can't read out the output from the script.
Was the problem that the script wasn't done before I'm asking if it's available and reading from it?
mjmostachetti:
Was the problem that the script wasn't done before I'm asking if it's available and reading from it?
Yes, that is exactly the problem. I suggest you need to know whether the script is still running. I don't know enough about the bridge to tell you how to do that, but if you have the capability to launch processes then it makes sense that you would also have the capability to find the status of those processes. Hopefully, if you an find some examples of starting processes they will include showing you how to find the status of the process afterwards.