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:
// 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:
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.