theDuke540:
I have a sneaking suspicion this isn't the entire code... If you want good help, you need to help us out and not leave anything out that might help us help you.
Haha, you are exactly right... I do believe the issue lies with how the Bridge/Process libraries - and/or my code - cannot handle a Linino restart. Essentially I have been looking for a way to check if the Bridge is functional before running a Process on it. Here is my full code. Thanks to all in advance for your time and suggestions!
#include <Bridge.h>
#include <Process.h>
#include <Console.h>
#include <DHT22.h>
#define DHTPIN 2
// how often to run the pin read (in milliseconds)
const unsigned long RUN_INTERVAL_MILLIS = 60000;
const unsigned long AVE_INTERVAL = 1000;
// the last time we ran the pin read (initialized to 60 seconds ago,
// so the pins are read immediately when we start up)
unsigned long lastRun = 0;//= (unsigned long)- 60000;
unsigned long lastAveRun = (unsigned long)- 1000;
// variables for setting up time
Process date; // process used to get the date
String timeString;
DHT22 DHT1(DHTPIN);
const int ledPin = 13;
//a0 info
const int a0Pin = 0;
//i.e. 1023 arduino input = 300 ug reading = 5 V
const float a0scale = 300; //reading equal to full scale of 1023 units (e.g. 5 V with standard ref.)
const float a1scale = 1;
const float a2scale = 1;
int a0Inc = 0;
float a0Num = 0; //a0
float temp1; //a1
float humi1; //a2
int pAvail;
void setup() {
pinMode(ledPin, OUTPUT);
delay(3000);
Process p;
pAvail = p.available();
Bridge.begin();
Console.begin();
// run an initial date process. Should return:
if (!date.running()) {
date.begin("date");
date.addParameter("+%D +%T");
date.run();
Console.println(date.readString());
}
for(int x = 0; x < 50; x++){
digitalWrite(ledPin, !digitalRead(ledPin));
delay(50);
}
digitalWrite(ledPin, LOW);
}//end setup
void loop() {
// get the number of milliseconds this sketch has been running
unsigned long now = millis();
//if (Bridge.){ Console.println("Bridge running");}
//else {Console.println("Bridge not running");}
//build average values
if (now - lastAveRun >= AVE_INTERVAL) {
lastAveRun = now;
a0Num += analogRead(a0Pin);
a0Inc ++;
digitalWrite(ledPin, !digitalRead(ledPin));
}
// run again if it's been RUN_INTERVAL_MILLIS milliseconds since we last ran
if (now - lastRun >= RUN_INTERVAL_MILLIS) {
lastRun = now;
Console.print("p.avail: ");
Console.println(pAvail);
//need to check bridge here
Bridge.begin();
if (!date.running()) { // get the date and time
date.begin("date");
date.addParameter("+%D +%T");
date.run();
}//end if
// put the date and time into a string variable
while (date.available()>0) {
timeString = date.readString();
}//end while
//need to check bridge here
String latestDate = getLatestDate();
Console.println("Latest: " + latestDate);
Console.println("Current: " + intDate(timeString));
getDHT();
if (a0Inc > 0) {
float a0Ave = a0Num/a0Inc;
a0Num = 0;
a0Inc = 0;
if (intDate(timeString) > latestDate) {
digitalWrite(ledPin, HIGH);
Console.print(timeString);
Console.print("a0ave: ");
Console.println(a0ave);
runSqlQuery(timeString,a0ave,a0scale,temp1,a1scale,humi1,a2scale);
for(int x = 0; x < 40; x++){
digitalWrite(ledPin, !digitalRead(ledPin));
delay(25);
}
} //end if: date check
else {
//current date is older than latest date - do not write to DB
for (int x = 0; x < 5; x++){
digitalWrite(ledPin, !digitalRead(ledPin));
delay(200);
}//end for
}//end else
} //end if
else {
}//end else dustInc !> 0
}//end if: query every 60 seconds
}// end loop
// function to run the appending of the data to the database
unsigned int runSqlQuery(String date, float a0, float a0scale, float a1, float a1scale, float a2, float a2scale){
Process p;
String cmd = "sqlite3 ";
String paramstring1 = "-line ";
// set the path and name of database
String paramstring2 ="/mnt/sda1/arduino/www/data.db ";
// insert a row with time and sensor data
String paramstring3 ="'INSERT INTO raw (\"date\",\"a0\",\"a0scale\",\"a1\",\"a1scale\",\"a2\",\"a2scale\") VALUES (\""+date+"\","+a0+","+a0scale+","+a1+","+a1scale+","+a2+","+a2scale+");'";
// get the error code
String paramstring4 =" ; echo $?";
Console.println(p.available());
Console.println("runSqlQuery");
Bridge.begin();
p.runShellCommand(cmd + paramstring1 + paramstring2 + paramstring3+ paramstring4);
Console.println(p.available());
// Read process output with stream method
while (p.available()>0) {
char c = p.read();
Console.println(p.available());
Console.print(c);
}
Console.flush();
}
String getLatestDate() {
Process p;
String cmd = "sqlite3 ";
String par1 = "-line ";
//db path
String par2 = "/mnt/sda1/arduino/www/data.db ";
String par3 = "'SELECT date FROM raw ORDER BY rowid DESC LIMIT 1;'";
String par4 = " ;";// echo $?";
Console.println("getLatestDate");
//need to check bridge here
Bridge.begin();
p.runShellCommand(cmd + par1 + par2 + par3 + par4);
//do nothing while cmd running
while (p.running());
//read shell output
String output = "";
int i = 0;
while (p.available()>0){
// String result = p.parse();
i ++;
char c = p.read();
if (i > 8 && i < 27) { //start with 9th character, which should be start of date
output = output + c;
}
}
//Console.println(intDate(output));
//Console.flush();
return intDate(output);
}
String intDate(String date) {
String year = date.substring(6,8); //' + 'output.charAt(7)');
String month = date.substring(0,2); //String(output.charAt(0) + output.charAt(1));
String day = date.substring(3,5); //String(output.charAt(3) + output.charAt(4));
String hour = date.substring(10,12); //String(output.charAt(10) + output.charAt(11));
String minute = date.substring(13,15); //String(output.charAt(13) + output.charAt(14));
String second = date.substring(16,18); //String(output.charAt(16) + output.charAt(17));
String fullDate = year + month + day + hour + minute + second;
return fullDate;
}
void getDHT(){
DHT22_ERROR_t errorCode;
errorCode = DHT1.readData();
switch(errorCode){
case DHT_ERROR_NONE:
temp1 = DHT1.getTemperatureC();
humi1 = DHT1.getHumidity();
}
}