Pages: [1]   Go Down
Author Topic: Need help sending string across to Arduino from Processing as character array  (Read 1418 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there, I'm currently trying to send a string to Arduino from Processing. The thing I'm struggling with is receiving the string at the Arduino end and converting it to a character array. I've tried lots of different variations and can't seem to get it to work.

Currently, Processing is parsing an RSS feed (My Facebook Wall) and is retrieving the most recent post then sending by serial to Arduino. If anyone knows how to do this I would be extremely grateful as I've been trying for a few days now with no joy.

Processing code:

Code:
// feedParser.pde
//
// Reads RSS and Atom feeds. Requires ROME
// (https://rome.dev.java.net/)
// and JDOM (http://www.jdom.org/), just make
// a code folder and copy "jdom.jar" and "rome-*.jar"
// into it.
//
// Marius Watz - http://workshop.evolutionzone.com

import com.sun.syndication.feed.synd.*;
import com.sun.syndication.io.*;
import processing.serial.*;
Serial port;

FeedReader feed;
String feedurl;
String s;

void setup() {
  size(200, 200);
  port=new Serial(this,Serial.list()[0],9600);
}

void draw(){

  // load feed
  feedurl="http://fbrss.com/f/f86024c1b5594013ce6c72344701855b.xml";
  //println("Loading feed: "+feedurl);
  feed=new FeedReader(feedurl);

  // print feed data
   
    println(feed.entry[0]);
    delay(1000);
    println(s);
    port.write(s);
  for(int i=0; i< feed.numEntries; i++){
  //delay(500);
  //loop();
    //println(i+": "+feed.entry[i]);
  }
}

class FeedReader {
  SyndFeed feed;
  String url,description,title;
  int numEntries;
  FeedEntry entry[];

  public FeedReader(String _url) {
    url=_url;
    try {
      feed=new SyndFeedInput().build(new XmlReader(new URL(url)));
      description=feed.getDescription();
      title=feed.getTitle();

      java.util.List entrl=feed.getEntries();
      Object [] o=entrl.toArray();
      numEntries=o.length;

      entry=new FeedEntry[numEntries];
      for(int i=0; i< numEntries; i++) {
        entry[i]=new FeedEntry((SyndEntryImpl)o[i]);
        //println(i+": "+entry[i]);
      }
    }
    catch(Exception e) {
     // println("Exception in Feedreader: "+e.toString());
      e.printStackTrace();
    }
  }

}

class FeedEntry {
  Date pubdate;
  SyndEntryImpl entry;
  String author, contents, description, url, title;
  public String newline = System.getProperty("line.separator");

  public FeedEntry(SyndEntryImpl _entry) {
    try {
      entry=_entry;
      author=entry.getAuthor();
      Object [] o=entry.getContents().toArray();
      if(o.length>0) contents=((SyndContentImpl)o[0]).getValue();
      else contents="[No content.]";

      description=entry.getDescription().getValue();
      if(description.charAt(0)==
        System.getProperty("line.separator").charAt(0))
          description=description.substring(1);

      url=entry.getLink();
      title=entry.getTitle();
      pubdate=entry.getPublishedDate();
 
    }
    catch(Exception e) {
     // println("Exception in FeedEntry: "+e.toString());
      e.printStackTrace();
    }

  }

  public String toString() {
   // String s;

    s="Comment: "+title+newline+
     // "URL: "+url+newline+
      "Date: "+pubdate.toString()+newline;
      return s;
  }
}


Arduino Code:

Code:
char inData[20]; // Allocate some space for the string
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character
int ledPin = 13; // Set the pin to digital I/O 4

void setup(){
 pinMode(ledPin, OUTPUT);
 Serial.begin(9600);
 Serial.write("Power On");
}





char Comp(char* This){

 while(Serial.available() > 0) // Don't read unless
   // there you know there is data
 {
   if(index < 19) // One less than the size of the array
   {
     inChar = Serial.read(); // Read a character
     inData[index] = inChar; // Store it
     index++; // Increment where to write next
     inData[index] = '\0'; // Null terminate the string
   }
 }

 if(strcmp(inData,This)  == 0){
   for(int i=0;i<19;i++){
     inData[i]=0;
   }
   index=0;
   return(0);

 }
 else{
   return(1);


 }
}


void loop()
{
 if(Comp("light on")==0){
       digitalWrite(ledPin, HIGH); // turn the LED on
 }
 if(Comp("light off")==0){
       digitalWrite(ledPin, LOW); // turn the LED off
 }
delay(100); // Wait 100 milliseconds for next reading
}

The Arduino code is the original code I've been working from. I don't need any LED's to light up, I just need it to store the string so I can print the stored string using Roo Reynolds microprinter code.

So basically, can anyone help me to get my arduino to store a string from processing then print it using an adapted version of Roo Reynold's code.

Hopefully someone will be able to help. Thanks guys.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25761
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This
Code:
for(int i=0;i<19;i++){
     inData[i]=0;
   }
   index=0;
isn't necessary
Code:
index = 0;
inData[0] = '\0';
should do the trick.

What happens if your receiver misses the first letter of a string, because it was resetting?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't actually know what will happen as I'm pretty new to this. What I want is processing to check for new updates on my wall and when there is a new one I want it to be sent to Arduino. When Arduino receives a new string I want an LED to light and the new comment to print off. I think the code's almost there but just can't work out how to make Arduino print the stored string/character array.

Here's the printer code I need to adapt:
Code:

#include <SoftwareSerial.h>

#define rxPin 6
#define txPin 7

SoftwareSerial printer = SoftwareSerial(rxPin, txPin);

int sensorValue;
int power;
int power2;


const byte command = 0x1B;
const byte fullcut = 0x69;
const byte partialcut = 0x6D;
const byte doubleprint = 0x47;
const byte flipchars = 0x7B;
const byte rotatechars = 0x56;

void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  printer.begin(9600);
  Serial.begin(9600); //open the USB connection too

 

}

void print(char text[]) {
  printer.print(text);
}

void println(char text[]) {
  printer.println(text);
}


void feed() {
  printer.println("");
  printer.println("");
  printer.println("");
}

void cut() {
  printer.print(command, BYTE);
  printer.print(fullcut, BYTE);
}

void partialCut() {
  printer.print(command, BYTE);
  printer.print(partialcut, BYTE);
}

void setDoublePrintOn() {
  setDoublePrint(0x01);
}

void setDoublePrintOff() {
  setDoublePrint(0x00);
}

void setDoublePrint(byte mode) {
  printer.print(command, BYTE);
  printer.print(doubleprint, BYTE);
  printer.print(mode, BYTE);
}

void loop() {
  power = digitalRead(2);
  power2 = digitalRead(3);
 
  if (power == HIGH){ 
 
  delay(2000);
 
  feed();
  println("");
  println("Steve Murray - Just a week and MAKE will begin..");
  println("24/01/2011 22:44:59");
  println("");
  cut();
  delay(1000);
  }
 
 
   
}

Hopefully someone on here will know. Basically I am making a facebook wall box. When someone posts on your wall the lid lights up, then when you open the box the latest post on your wall prints off via the till printer. Sounds simple but it's turning out to be quite hard. Thanks for your help and any input is appreciated.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25761
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to decide how long the longest string is likely to be, and how it is delimited.
The way the Arduino code you posted works is it keeps buffering until it finds a string match, then resets the buffer.
You're not matching strings, so you need to decide when a string has finished before returning.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the help but as I'm new to this type of code I don't really understand what you mean by that. Do you know of a way to convert the string to a character array without defining a length? Or another way to read the string from processing? I can't find an answer anywhere.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25761
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Do you know of a way to convert the string to a character array without defining a length?
No, because the memory on an Arduino is very limited.
If you know if the string is going to be terminated with a carriage-return, it may make things easier.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks very much. Do you know how I could terminate the string with a carriage return? I know I may sound really stupid but I don't really know what you mean.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

port.write("EndWithCR\n");
Add a carriage return/line feed with the \n character.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks. So would this work then?
Code:
void draw(){

  // load feed
  feedurl="http://fbrss.com/f/f86024c1b5594013ce6c72344701855b.xml";
  //println("Loading feed: "+feedurl);
  feed=new FeedReader(feedurl);

  // print feed data
   
    println(feed.entry[0]);
    delay(1000);
    println(s + "\r");
    port.write(s + "\r");
  for(int i=0; i< feed.numEntries; i++){
  //delay(500);
  //loop();
    //println(i+": "+feed.entry[i]);
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So would this work then?
Why not try it, and answer your own question?

Why are you using \r instead of \n?

Where is s defined/valued?
Logged

Pages: [1]   Go Up
Jump to: