Loading...
Logo
Processing Forum
kaihansen79's Profile
7 Posts
20 Responses
0 Followers

Activity Trend

Last 30 days
Show:
Private Message
    I've been basing both programs off of the SerialCallResponse example in Arduino 1.0 ( http://arduino.cc/en/Tutorial/SerialCallResponse ) to get them to talk.  Perhaps it was never meant for something like this though.  

    In a nutshell, I want to first send 15 Arduino sensor values to Processing; then write 6 Processing values to the Arduino; lastly, I was looking to return those last 6 values to Processing to confirm that they were recorded.

    Any advice or corrections would be greatly appreciated.  I've been struggling with this for a bit now.  At any rate here are the println responses generated through my debugging efforts:

    1. Stable Library
      =========================================
      Native lib Version = RXTX-2.1-7
      Java lib Version   = RXTX-2.1-7
      firstContact == false
      inByte == 'A'
      secondContact == false
      thirdContact == false
      didn't receive sensor data
      goes on like this for a while
      didn't receive sensor data
      secondContact == false
      thirdContact == false
      138                                       //confirmation of the first value in the array received
      serialCount == 15; 'Z' has been given to arduino; it should begin getData() now
      secondContact == false
      thirdContact == false
      firstContact == false
      inByte == 'A'
      secondContact == false
      thirdContact == false
      didn't receive sensor data
      goes on like this for a while
      thirdContact == false
      didn't receive sensor data
      secondContact == false
      inByte == 'B' + 'B'
      thirdContact == false
      didn't receive sensor data
      secondContact == true
      weather and preferences and 'Y' have been sent to arduino
      thirdContact == false
      didn't receive sensor data
      secondContact == false
      goes on like this for a while
      thirdContact == false
      didn't receive sensor data
      secondContact == false
      inByte == 'B' + 'B'
      thirdContact == false
      didn't receive sensor data
      secondContact == true
      weather and preferences and 'Y' have been sent to arduino  
      thirdContact == false
      didn't receive sensor data
      secondContact == false
      goes on like this for a while
      didn't receive sensor data
      secondContact == false
      thirdContact == false
      didn't receive sensor data
      secondContact == false
      thirdContact == false
      131
      serialCount == 15; 'Z' has been given to arduino; it should begin getData() now
      secondContact == false
      thirdContact == false
      firstContact == false
      goes on like this for a while
      thirdContact == false
      firstContact == false
      secondContact == false
      inByte == 'B' + 'B'
      thirdContact == false
      firstContact == false
      secondContact == true
      weather and preferences and 'Y' have been sent to arduino
      thirdContact == false
      firstContact == false
      secondContact == false
      goes on like this for a while
      secondContact == false
      thirdContact == false
      firstContact == false
      secondContact == false
      inByte == 'B' + 'B'
      thirdContact == false
      firstContact == false
      secondContact == true
      weather and preferences and 'Y' have been sent to arduino  //it's odd that it's skipping
      thirdContact == false
      firstContact == false
      secondContact == false
      thirdContact == false
      firstContact == false
      secondContact == false
      inByte == 'B' + 'B'
      thirdContact == false
      firstContact == false
      secondContact == true
      weather and preferences and 'Y' have been sent to arduino
      thirdContact == false
      firstContact == false
      secondContact == false
      inByte == 'B' + 'B'
      thirdContact == false
      firstContact == false
      secondContact == true
      weather and preferences and 'Y' have been sent to arduino
      thirdContact == false
      firstContact == false
      secondContact == false
      goes on like this for a while
      thirdContact == false
      firstContact == false
      secondContact == false
      inByte == 'B' + 'B'
      thirdContact == false
      firstContact == false
      secondContact == true
      weather and preferences and 'Y' have been sent to arduino
      thirdContact == false
      firstContact == false
      keeps going until I kill the program
    Code for both Arduino and Processing classes are here (password = Adaptive):   http://robofacades.wordpress.com/code-hosting/acm11-facadeagent14/ 

    Here is my processing code:

    1. int[] serialInArray = new int[15];    // Where we'll put what we receive 
    2. int[] serialInArray3 = new int[6];
    3. int serialCount = 0;                 // A count of how many bytes we receive
    4. boolean firstContact = false;        // Whether we've heard from the microcontroller
    5. boolean secondContact = false;  
    6. boolean thirdContact = false;  

    7. void serialEvent(Serial comPort4){
    8.   
    9.   int inByte = comPort4.read();
    10.   
    11.   if (firstContact == false) {
    12.     delay(25);
    13.     println("firstContact == false");
    14.     if (inByte == 'A') { 
    15.       comPort4.clear();          // clear the serial port buffer
    16.       firstContact = true;     // you've had first contact from the microcontroller
    17.       comPort4.write('A');       // ask for more
    18.       delay(25);
    19.       println("inByte == 'A'");
    20.     }
    21.   } 
    22.    
    23.   else if (firstContact = true){
    24.     delay(25);
    25.     // Add the latest byte from the serial port to array:
    26.     serialInArray[serialCount] = inByte;
    27.     serialCount++;
    28.     if (serialCount > 14 ) {      //reference to arduino values
    29.     //println("serialCount > 14");
    30.       intLightSens1 = serialInArray[0];             
    31.       intLightSens2 = serialInArray[1];             //intLightSens1, intLightSens2, intLightSens3, avgInteriorLight, extLightSens1, extLightSens2, extLightSens3, extLightSens4, extLightSens5, solarPosition, actuatorPercent   
    32.       intLightSens3 = serialInArray[2];
    33.       avgInteriorLight = serialInArray[3];          //temp_c_indoor 11, temp_f_indoor 12, dewpoint_f_indoor 13, relative_humidity_indoor 14
    34.       extLightSens1 = serialInArray[4];
    35.       extLightSens2 = serialInArray[5];
    36.       extLightSens3 = serialInArray[6];
    37.       extLightSens4 = serialInArray[7];
    38.       extLightSens5 = serialInArray[8];
    39.       solarPosition = serialInArray[9];
    40.       actuatorPercent = serialInArray[10];
    41.       temp_c_indoor = serialInArray[11];
    42.       temp_f_indoor = serialInArray[12];
    43.       dewpoint_f_indoor = serialInArray[13];
    44.       relative_humidity_indoor = serialInArray[14];

    45.       //comPort4.clear(); 
    46.       delay (50);
    47.       comPort4.write('Z');
    48.       firstContact = false;  //reset boolean
    49.       serialCount = 0; 
    50.       println(intLightSens1);
    51.       println("serialCount == 15; 'Z' has been given to arduino; it should begin getData() now");
    52.     }
    53.     else{
    54.       println("didn't receive sensor data");
    55.     }
    56.   }

    57.   if (secondContact == false) {
    58.     delay(25);
    59.     println("secondContact == false");
    60.     //comPort4.clear(); 
    61.     if (inByte == 'B' + 'B') { 
    62.       comPort4.clear();          // clear the serial port buffer
    63.       secondContact = true;     // you've had second contact from the microcontroller
    64.       comPort4.write('B' + 'B');       // ask for more
    65.       delay(25);
    66.       println("inByte == 'B' + 'B'");
    67.     }
    68.   }

    69.   else if (secondContact = true) {
    70.     delay(25);
    71.     comPort4.write(temp_f);
    72.     comPort4.write(relative_humidity);
    73.     comPort4.write(dewpoint_f);
    74.     comPort4.write(desiredInteriorLight);
    75.     comPort4.write(desiredIndoorHumidity);
    76.     comPort4.write(desiredIndoorTemp_f);
    77.   
    78.     delay(25);
    79.     //comPort4.clear();
    80.     secondContact = false;  //reset boolean
    81.     comPort4.write('Y');
    82.     serialCount = 0;
    83.     println("secondContact == true");
    84.     println("weather and preferences and 'Y' have been sent to arduino");
    85.   }

    86.   if (thirdContact == false) {
    87.     println("thirdContact == false");
    88.     if (inByte == 'C' + 'C' + 'C') { 
    89.       comPort4.clear();          // clear the serial port buffer
    90.       thirdContact = true;     // third contact from the microcontroller
    91.       comPort4.write('C' + 'C' + 'C');       // ask for more
    92.       delay(25);
    93.       println("inByte == 'C' + 'C' + 'C'");
    94.     }
    95.   } 
    96.     
    97.   else if (thirdContact == true) {
    98.     delay(25);
    99.     println("thirdContact == true");
    100.     serialInArray3[serialCount] = inByte;
    101.     serialCount++;
    102.     if (serialCount > 5 ) { 
    103.       println (serialInArray3[0]);
    104.       println (serialInArray3[1]);
    105.       println (serialInArray3[2]);
    106.       println (serialInArray3[3]);
    107.       println (serialInArray3[4]);
    108.       println (serialInArray3[5]);
    109.       println("serialCount == 6; 'X' was sent and loop should restart");
    110.       
    111.       //comPort4.clear();
    112.       delay (50);
    113.       comPort4.write('X'); 
    114.       serialCount = 0;   // Reset serialCount 
    115.       thirdContact = false; //reset boolean
    116.     }
    117.     else{
    118.       println("didn't receive serialInArray3 from arduino");
    119.     }
    120.   }
    121. }
    And the Arduino code:

    1. //int inByte = 0;
    2. //boolean secondContact = false;

    3. //int serialInArray[6];    // Where we'll put what we receive
    4. //int serialCount = 0;                 // A count of how many bytes we receive

    5. void logData() {
    6.   Serial.read();
    7.   establishContact();  // send a byte to establish contact until receiver responds 
    8.   if (Serial.available() > 0) {
    9.     //int writeSensorArray[] = {photoCell1, photoCell2, photoCell3, avgInteriorLight, photoCell4, photoCell5, photoCell6, photoCell7, photoCell8, solarPosition, actuatorPercent, temp_c_indoor, temp_f_indoor, relative_humidity_indoor, dewpoint_f_indoor};                     
    10.     
    11.     Serial.write(photoCell1);
    12.     Serial.write(photoCell2);
    13.     Serial.write(photoCell3);
    14.     Serial.write(avgInteriorLight);
    15.     Serial.write(photoCell4);
    16.     Serial.write(photoCell5);
    17.     Serial.write(photoCell6);
    18.     Serial.write(photoCell7);
    19.     Serial.write(photoCell8);
    20.     Serial.write(solarPosition);
    21.     Serial.write(actuatorPercent);
    22.     Serial.write(temp_c_indoor);
    23.     Serial.write(temp_f_indoor);
    24.     Serial.write(relative_humidity_indoor);  
    25.     Serial.write(dewpoint_f_indoor);
    26.   }
    27.   
    28.   if (Serial.read() == 'Z'){
    29.     confirmReceipt();
    30.     getData();
    31.   }
    32.  
    33.   if (Serial.read() == 'Y'){
    34.     confirmReceipt2();
    35.     delay(50);
    36.     confirmGetData();
    37.   }
    38.  
    39.   if (Serial.read() == 'X'){
    40.     Serial.end();
    41.   } 
    42. }

    43. void getData(){
    44.   
    45.   int serialInArray[6] = {Serial.read()};
    46.     
    47.   temp_f = serialInArray[0];
    48.   relative_humidity = serialInArray[1];
    49.   dewpoint_f = serialInArray[2];
    50.   desiredInteriorLight = serialInArray[3];
    51.   desiredIndoorHumidity = serialInArray[4];
    52.   desiredIndoorTemp_f = serialInArray[5];
    53. }

    54. void confirmGetData() {
    55.   Serial.write(temp_f);  
    56.   Serial.write(relative_humidity);  
    57.   Serial.write(dewpoint_f);  
    58.   Serial.write(desiredInteriorLight);  
    59.   Serial.write(desiredIndoorHumidity);  
    60.   Serial.write(desiredIndoorTemp_f);  
    61. }

    62. void establishContact() {
    63.   while (Serial.available() <= 0) {
    64.     Serial.print('A');   // send a capital A
    65.     delay(300);
    66.   }
    67. }
    68.   
    69. void confirmReceipt() {
    70.   while (Serial.available() <= 1) {
    71.     Serial.write('B' + 'B');   // send a capital B
    72.     delay(300);
    73.   }
    74. }

    75. void confirmReceipt2() {
    76.   while (Serial.available() <= 2) {
    77.     Serial.print('C' + 'C' + 'C');   // send a capital C
    78.     delay(300);
    79.   }
    80. }

    In the PostgreSQL example for the SQLibrary ( http://bezier.de/processing/libs/sql/ ), it says that the example only works if the DB is on the local network.  I can't seem to find anything in the documentation that suggests a way to differentiate the code to facilitate a remote-connection away from the local network.

    Does anyone know if this can be done, or how?

    PostgreSQL Example:
    1. // updated fjenett 20081129

    2. import de.bezier.data.sql.*;    


    3. PostgreSQL pgsql;


    4. void setup()
    5. {
    6.     size( 100, 100 );
    7.     // this example assumes that you are running the 
    8.     // postgresql server locally (on "localhost").
    9.     //
    10.     // replace with your own postgresql-account.
    11.     //
    12.     String user     = "fjenett";
    13.     String pass     = "fjenett";
    14.     // name of the database to use
    15.     //
    16.     String database = "test";
    17.     
    18.     // connect to database on "localhost"
    19.     //
    20.     pgsql = new PostgreSQL( this, "localhost", database, user, pass );
    21.     
    22.     // connected?
    23.     if ( pgsql.connect() )
    24.     {
    25.         // query the number of entries in table "weather"
    26.         pgsql.query( "SELECT COUNT(*) FROM weather" );
    27.         
    28.         // results found?
    29.         if ( pgsql.next() )
    30.         {
    31.             // nice, then let's report them back
    32.             println( "number of rows in table weather: " + pgsql.getInt(1) );
    33.         }
    34.         
    35.         // now let's query for last 10 entries in "weather"
    36.         pgsql.query( "SELECT * FROM weather LIMIT 10" );
    37.         
    38.         // anything found?
    39.         while( pgsql.next() )
    40.         {
    41.             // splendid, here's what we've found ..
    42.             println( pgsql.getString("city") );
    43.         }
    44.     }
    45.     else
    46.     {
    47.         // yay, connection failed !
    48.     }
    49. }
    Hello,

    I'm using Processing to read sensor values from an Arduino via com port.  Everything seems to work fine for a couple of hours or so, but eventually one of the last values in the array String[] readList = splitTokens(getRawArduino); comes back null and causes a java.lang.ArrayIndexOutOfBoundsException at 11, 12, 13 or 14.  Usually it happens at 11 though.  The following variables are the source of the null values, and are all coming from the SHT15 temp/humidity sensor:
    •  temp_c_indoor = float(readList[11]);             
    •  temp_f_indoor = float(readList[12]);             
    •  dewpoint_f_indoor = float(readList[13]);         
    •  relative_humidity_indoor = float(readList[14]);

    Once in a while the sensor will return 0.0000 causing the null in processing.  I'm trying to implement a try() and catch() function to deal with this, but I'm not understanding how it's set up and I'm hoping somebody can point out what I'm doing wrong.

    1. void draw(){ 
    2.   
    3.    if (lastQueryTime + 600000 > millis() && lastQueryTime < 100){
    4.       getXML();
    5.       doQueryExecuteDB();
    6.       lastQueryTime = millis();  
    7.    }  
    8.   
    9.    if (arduinoCom + 120000 < millis()){  
    10.      try{
    11.       nullException = readList.readArduino();
    12.      }
    13.      catch (ArrayIndexOutOfBoundsException) {
    14.        nullException = null;
    15.      }
    16.      if (nullException == null){
    17.        noLoop();
    18.      }
    19.      else{
    20.        readArduino();
    21.      }
    22.       writeArduino(); 
    23.       doWriteArduinoDB(); 
    24.       arduinoCom = millis();
    25.    }
    26.    
    27.    if (lastQueryTime + 600000 < millis()) {
    28.       getXML();
    29.       doQueryExecuteDB();
    30.       lastQueryTime = millis(); 
    31.    }   
    32. }

    1. void readArduino(){

    2.  //get the sensor and actuator data from the serial port
    3.  if (comPort4.available() > 0) 
    4.  {
    5.  getRawArduino = comPort4.readString();
    6.  
    7.  String[] readList = splitTokens(getRawArduino);
    8. //convert strings to floats
    9.  intLightSens1 = int(readList[0]);      // intLightSens1
    10.  intLightSens2 = int(readList[1]);      // intLightSens2
    11.  intLightSens3 = int(readList[2]);      // intLightSens3
    12.  
    13.  avgInteriorLight = int(readList[3]);   // avgInteriorLight

    14.  extLightSens1 = int(readList[4]);      // extLightSens1
    15.  extLightSens2 = int(readList[5]);      // extLightSens2
    16.  extLightSens3 = int(readList[6]);      // extLightSens3
    17.  extLightSens4 = int(readList[7]);      // extLightSens4
    18.  extLightSens5 = int(readList[8]);      // extLightSens5
    19.  
    20.  solarPosition = int(readList[9]);      // solarPosition
    21.  actuatorPercent = int(readList[10]);   // actuatorPercent

    22.  temp_c_indoor = float(readList[11]);             // temp_c_indoor
    23.  temp_f_indoor = float(readList[12]);             // temp_f_indoor
    24.  dewpoint_f_indoor = float(readList[13]);         // dewpoint_f_indoor
    25.  relative_humidity_indoor = float(readList[14]);  // relative_humidity_indoor
    26.  }
    27.  
    28.  else{
    29.    println ("Not connected to Arduino");
    30.   }
    31. }
    Hello,

    Is there a way to project updated values into a SQL command string?  I'm accessing web-based XML data every ten minutes and want to write it to my PostgreSQL DB. 

    This would be the example command to write to DB:
    pgsql.execute(INSERT INTO table (column1, column2, etc) VALUES (value1, value2, ect));

    For these variables:
    String station_location_xml;
    String observation_time_xml;
    String temp_f_xml;
    String temp_c_xml;
    String relative_humidity_xml;
    String wind_degrees_xml;
    String wind_dir_xml;
    String wind_mph_xml;
    String wind_gust_mph_xml;
    String dewpoint_f_xml;
    String dewpoint_c_xml;

    I want to write into the DB, which could look like:
    pgsql.execute(   "INSERT INTO adaptive_weather_longterm (station_location, observation_time, temp_f, temp_c, relative_humidity, wind_dir, wind_mph, wind_gust_mph, dewpoint_f, dewpoint_c) VALUES ('" + station_location_xml + "', '" + observation_time_xml + "', '" + temp_f_xml + "', '" + temp_c_xml + "', '" + relative_humidity_xml + "', '" + wind_dir_xml + "', '" + wind_mph_xml + "', '" + wind_gust_mph_xml + "', '" + dewpoint_f_xml + "', '" + dewpoint_c_xml + "')");

    Except, the library requires a straight up string here for "VALUES".  

    I've tried coding:
    String sqlCommand = new String ("INSERT INTO adaptive_weather_longterm (station_location, observation_time, temp_f, temp_c, relative_humidity, wind_dir, wind_mph, wind_gust_mph, dewpoint_f, dewpoint_c) VALUES ('" + station_location_xml + "', '" + observation_time_xml + "', '" + temp_f_xml + "', '" + temp_c_xml + "', '" + relative_humidity_xml + "', '" + wind_dir_xml + "', '" + wind_mph_xml + "', '" + wind_gust_mph_xml + "', '" + dewpoint_f_xml + "', '" + dewpoint_c_xml + "')");
      
    return (sqlCommand);

    Then calling the variable:
    pgsql.execute(sqlCommand);

    However, I'm met with " Exception in thread "Animation Thread" java.lang.NullPointerException".

    This is a problem since this data is not static.  How can I get the dynamic values into this string???

    Is there a new method I can add to the source?

    Thanks in advance!


    I would like to write data to my PostgreSQL DB using SQLibrary.

    First off, is it capable of doing that?  I've had success querying the DB using this library, but there doesn't seem to be much documentation on writing to a DB using it.  I've tried using pgsql.execute but get this error: 

    SQL.query(): java.sql.SQLException.
    org.postgresql.util.PSQLException: ERROR: syntax error at end of input
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1608)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1343)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:194)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:451)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:336)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:328)
    at de.bezier.data.sql.SQL.execute(Unknown Source)
    at dbQuery_XMLFeed.manipulateDB(dbQuery_XMLFeed.java:234)
    at dbQuery_XMLFeed.draw(dbQuery_XMLFeed.java:56)
    at processing.core.PApplet.handleDraw(Unknown Source)
    at processing.core.PApplet.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:662)

    The fact that it says "SQL.query():" leads me to think that this library only queries.

    If it is possible, could someone provide an example?

    Thanks in advance.

    void manipulateDB(){
      //query write start
        size( 300, 300 );
        //  assumes that you are running the 
        // postgresql server locally (on "localhost").
        //
        // replace with your own postgresql-account.
        //
        String user     = "user name";
        String pass     = "password";
        // name of the database to use
        //
        String database = "db name";
        
        // connect to database on "localhost"
        //
        pgsql = new PostgreSQL( this, "db address", database, user, pass );
        // connected?
        if ( pgsql.connect() )
        {
            pgsql.execute( "INSERT INTO adaptive_weather_longterm (station_location, observation_time, temp_f, temp_c, relative_humidity, wind_dir, wind_mph, wind_gust_mph, dewpoint_f, dewpoint_c) VALUES (station_location_xml, observation_time_xml, temp_f_xml, temp_c_xml, relative_humidity_xml, wind_dir_xml, wind_mph_xml, wind_gust_mph_xml, dewpoint_f_xml, dewpoint_c_xml");
        }
        else
        {
            //connection failed !
        }
        //query write end
    }

    Hello,

    I'm trying to parse XML weather data from wunderground.com using XMLElement, and it's looking like wunderground's API requires the use of JSON too.

    Can anyone shed some light on how to make these come together?

    The code below is junk, but I hope that it can give some idea of what I'm trying to accomplish.

    import org.json.*;

    XMLElement xml;

    void setup() {
      size(600, 600);
      xmlElement xmlFeed = new XMLElement(this, "http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=KILCHICA30");                                              
      int numSites = xmlFeed.getChildCount();      
      for (int i = 0; i < numSites; i++) {    
        XMLElement current_observations = xml.getChild(i);
        XMLElement[] kids = entry.getChildren();
        
        //int id = kid.getInt("1");      //second node      
        float current_observation = int(kids[0].getContent()); 
        string termp_f = string(kids[1].getContent());
        //String url = kid.getString("http://api.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=KILCHICA30"); 
        //String site = kid.getContent();
        println(id + " : " + url + " : " + site); 
        println(current_observation);
        println(termp_f);
        
      }
    }