error with URL object
in
Programming Questions
•
9 months ago
Hey everyone, I'm new here so if this post belongs elsewhere, please let me know.
So my problem is that I'm following a guide from the "Getting Started With Arduino" book, but my code isn't working and when I download the sample code it has the same problem.
What I'm trying to do is pull an rss feed and parse it in processing, and then send some data about it to my arduino. However, the guide has me using a URL object that processing doesn't think exists. So here's my code(the error is occurring at the red underlined section):
- //lamp rss example(processing)
- //matvavna
- import processing.serial.*;
- String feed = "http://www.makezine.com/blog/index.xml";
- int interval = 10; // retrieve feed every sixty seconds
- int lastTime; // the last time we fetched the content
- int love = 0;
- int peace = 0;
- int arduino = 0;
- int light = 0; // light level measured by the lamp
- Serial port;
- color c;
- String cs;
- String buffer = ""; // accumulates characters coming from the arduino
- PFont font;
- void setup(){
- size(640, 480);
- frameRate(10);
- font = loadFont("CenturyGothic-32.vlw");
- fill(255);
- textFont(font, 32);
- String arduinoPort = Serial.list()[0];
- port = new Serial(this, arduinoPort, 9600); //connects to arduino
- lastTime = 0;
- fetchData();
- }
- void draw(){
- background(c);
- int n = (interval - ((millis()-lastTime)/1000));
- //build color for LEDs
- c = color(peace,love,arduino);
- cs = "#" + hex(c,6); //prepares a string to be sent to arduino
- text("Arduino Networked Lamp", 10, 40);
- text("reading data", 10, 100);
- text(feed, 10, 140);
- text("next update in " + n + " seconds", 10, 450);
- text("peace", 10, 200);
- text(" " + peace, 130, 200);
- rect(200, 212, peace, 28);
- text("love ", 10, 240);
- text(" " + love, 130, 280);
- rect(200, 212, love, 28);
- text("arduino ", 10, 280);
- text(" " + arduino, 130, 280);
- rect(200, 252, arduino, 28);
- //write color string to screen
- text("sending", 10, 380);
- text(cs, 200, 380);
- text("light level", 10, 320);
- rect(200, 352, light/10.23, 28);
- if( n <= 0){
- fetchData();
- lastTime = millis();
- }
- port.write(cs);//send sata to arduino
- if(port.available() > 0){ // check if there is anything writing
- int inByte = port.read(); // read one byte
- if(inByte != 10) {
- buffer = buffer + char(inByte);
- }
- else{
- //new line reached, time to process data
- if(buffer.length() > 1){
- //now we chop off the last character, it's a carriage return
- buffer = buffer.substring(0, buffer.length()-1);
- //turn buffer from string into number
- light = int(buffer);
- //reset buffer for next read cycle
- buffer = "";
- //need to clear data from arduino
- port.clear();
- }
- }
- }
- }
- void fetchData(){
- //these strings are used to parse the feed
- String data;
- String chunk;
- //zero the counters
- love = 0;
- peace = 0;
- arduino = 0;
- try{
- URL url = new URL(feed); // an object to represent the url
- // prepare a connection
- URLConnection conn = url.openConnection();
- conn.connect(); // connects to site
- //this bit connects the incoming stream to a buffered reader that reads the data one line at a time
- BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- //read each line from the feed
- while((data = in.realLine()) != null){
- StringTokenizer st = new StringTokenizer(data, "\" <>,.()[]"); // break down feed
- while(st.hasMoreTokens()) {
- //each chunk of data is made lowercase
- chunk = st.nextToken().toLowerCase();
- if(chunk.indexOf("love") >= 0 ) //found "love"?
- love++;
- if(chunk.indexOf("peace") >= 0 ) //found "peace"?
- peace++;
- if(chunk.indexOf("arduino") >= 0 ) //found "arduino"?
- arduino++;
- }
- }
- //set max # of references to 64
- if(peace > 64) peace = 64;
- if(love > 64) love = 64;
- if(arduino > 64) arduino = 64;
- //sets the max to 255
- peace = peace * 4;
- love = love * 4;
- arduino = arduino * 4;
- }
- catch(Exception ex){
- ex.printStackTrace();
- System.out.println("ERROR: " + ex.getMessage());
- }
- }
1