Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***************************************************************************/
- /* */
- /* This sketch gets the data from the coupled Paraimpu actuator. */
- /* Thanks to Roberto Musanti */
- /* */
- /* http://paraimpu.crs4.it */
- /* */
- /* */
- /* counter = counts the readings */
- /* url = actuator address */
- /* metro = tenths of a second of delay between readings */
- /* */
- /* This sketch is released under GPL v3. For more details, please read */
- /* http://www.gnu.org/licenses/gpl-3.0.txt */
- /***************************************************************************/
- import processing.net.*;
- int counter;
- int metro;
- int module;
- String url;
- String data;
- ArrayList<String> words;
- char temp;
- String tmpstr;
- void setup() {
- counter = 0;
- url = "http://paraimpu.crs4.it/use?token=0895060a-fa3e-4da4-acf0-6d3411754bcb";
- metro = 10;
- data = "";
- words = new ArrayList();
- module = floor(millis() / 100);
- }
- void draw() {
- data = polling(url, metro);
- /******************************************************************/
- /* add your code to process the read values and enjoy yourself! */
- /******************************************************************/
- //lets extract the strings between " and " in the json
- words=parse_string(data);
- //now that we have words, we elaborate them
- elaborate_words(words);
- //we reset data containing json and start all over, also reset words
- cleanAll();
- }
- //code for resetting stuff at every draw
- void cleanAll(){
- data="";
- words.clear();
- }
- //code for elaborating words
- void elaborate_words(ArrayList<String> words){
- //print only the string you want
- String content="";
- String author="";
- if(words.size() >0){
- for (int i=0; i< words.size();i++){
- //println(words.get(i));
- if((words.get(i)).equals("text")){
- content=words.get(++i);
- }
- if((words.get(i)).equals("user")){
- author=words.get(++i);
- }
- }
- println(author);
- println(content);
- println("");
- }
- }
- //code for extractin string from the json string
- ArrayList<String> parse_string(String data){
- ArrayList<String> working =new ArrayList();
- for(int i=0; i< data.length();i++){
- temp=data.charAt(i);
- if (temp=='"') {
- String tempPhrase="";
- i++;
- while((temp=data.charAt(i)) != '"'){
- tempPhrase=tempPhrase+temp;
- i++;
- }
- i++;
- //println(tempPhrase);
- working.add(tempPhrase);
- }
- }
- return working;
- }
- /******************************************************************************/
- /* Polling Paraimpu URL. Until a new data is get, the last read is returned. */
- /******************************************************************************/
- String polling(String url, int metro) {
- String value = "";
- if(floor(millis() / 100) == module) {
- module = module + metro;
- counter++;
- //println("tick: " + counter + " - tenths of a second: " + floor(millis() / 100));
- String lines[] = loadStrings(url);
- if(lines != null && lines.length > 0){
- for (int i = 0; i < lines.length; i++) {
- value = value + lines[i];
- // println("New data - " + value);
- }
- }
- }
- if (value.length() < 1){
- value = data;
- }
- return value;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement