Go Down

Topic: Arduino + Wifly RN-XV + Processing (Read 2 times) previous topic - next topic

Rubeix

Hi everyone,

I have an Arduino UNO with a Wifly RN-XV sending values to my PC by wireless (telnet).
Now I want to get this values on Processing, but I don't know how to do it.
Is there some project like this??Someone can help me with this?

Thanks in advance

RĂºben Teixeira

xaral

I'm having the same problem Rubeix.
There's a lot of information/code on internet, but until now i couldn't put working on my project...

Rubeix

Which is the best and easiest way to do this connection?Via UDP or TCP/IP??

charalamposdoukas

I suggest you implement your own server in Processing and talk to it through the Arduino

check the following code:
Code: [Select]


import processing.net.*;
Server myServer;

void setup() {
  size(200, 200);
  // Starts a myServer on port 1234, change this to whatever suits you!
  myServer = new Server(this, 1234);
}

void draw() {
  // Get the next available client
  Client thisClient = myServer.available();
  // write the output of the client
  if (thisClient !=null) {
    String whatClientSaid = thisClient.readString();
    if (whatClientSaid != null) {
      println(thisClient.ip() + "t" + whatClientSaid);
    }
  }
}



and the Arduino code (for Ethernet example, easy to adopt):
Code: [Select]


#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);

// Enter the IP address of the server you're connecting to:
IPAddress server(192,168,1,1);

// Initialize the Ethernet client library
// with the IP address and port of the server
EthernetClient client;

void setup() {
  // start the Ethernet connection:
  Ethernet.begin(mac, ip);
  // start the serial library:
  Serial.begin(9600);
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 1234)) {
    Serial.println("connected");
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {
 
  //The following code will simply send characters for Terminal input to the server - use it as a test
  while (Serial.available() > 0) {
    char inChar = Serial.read();
    if (client.connected()) {
      client.print(inChar);
    }
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    // do nothing:
    while(true);
  }
}

www.buildinginternetofthings.com

Rubeix

#4
May 20, 2012, 02:44 am Last Edit: May 20, 2012, 02:47 am by Rubeix Reason: 1
Hello charalamposdoukas,

I have been trying to adapt the arduino code from Ethernet to Wifly. I did that looking at the diferences between Wifly_WebServer example from WiFly Shield code library alpha 2 (source:http://forum.sparkfun.com/viewtopic.php?f=32&t=25216) and WebServer example from Ethernet library.
The result is the following code, but it doesn´t run. It has an error that says: "No matching function for call to 'Client::Client()' ". Can you help me with this please?

Thanks in advance

Code: [Select]

#include "WiFly.h"
#include "Credentials.h"

Server server(80);

void setup() {
  // start the WiFly connection:
  WiFly.begin();
 
  if (!WiFly.join(ssid, passphrase)) {
    while (1) {
      // Hang on failure.
    }
  }

  Serial.begin(9600);
  Serial.print("IP: ");
  Serial.println(WiFly.ip());
 
  server.begin();

  // give the WiFly shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, 1234)) {
    Serial.println("connected");
  }
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop() {
  Client client = server.available();
 
  //The following code will simply send characters for Terminal input to the server - use it as a test
  while (Serial.available() > 0) {
    char inChar = Serial.read();
    if (client.connected()) {
      client.print(inChar);
    }
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    // do nothing:
    while(true);
  }
}


At the credentials sketch tab the code is:

Code: [Select]
#ifndef __CREDENTIALS_H__
#define __CREDENTIALS_H__

// Wifi parameters
char passphrase[] = "passphrase";//my passphrase
char ssid[] = "Wifly";

#endif



charalamposdoukas

Hi,

You have misplaced some of the code in your sketch, check the following code. Have not tested if it runs properly but I least it compiles :)

Code: [Select]

#include "WiFly.h"
#include "Credentials.h"

Server server(80);

void setup() {
  // start the WiFly connection:
  WiFly.begin();
 
  if (!WiFly.join(ssid, passphrase)) {
    while (1) {
      // Hang on failure.
    }
  }

  Serial.begin(9600);
  Serial.print("IP: ");
  Serial.println(WiFly.ip());
 
  server.begin();

  // give the WiFly shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
 
  //------rest shall be implemented inside loop()
 
  // if you get a connection, report back via serial:
  //if (client.connect(server, 1234)) {
  // 
  //  Serial.println("connected");
  //}
  //else {
  //  // if you didn't get a connection to the server:
  //  Serial.println("connection failed");
  //}
}

void loop() {
  Client client = server.available();
 
  //If client has been instantiated
  if (client) {
    //check for client connection
    while (client.connected()) {
      //if client has connected
      if (client.available()) {
        //The following code will simply send characters for Terminal input to the server - use it as a test
        while (Serial.available() > 0) {
          char inChar = Serial.read();
            if (client.connected()) {
              client.print(inChar);
            }
        }
      }
    }
  }
  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    // do nothing:
    while(true);
  }
}
www.buildinginternetofthings.com

charalamposdoukas

Sorry, previous post contains server code for the WiFly, but you need the Client code to connect to the Processing sketch (which is creating a server).

Code: [Select]


#include "WiFly.h"
#include "Credentials.h"

byte server[] = { 192,168,1,1 }; // Your PC IP

Client client(server, 1234);


void setup() {
 
  Serial.begin(9600);

  WiFly.begin();
 
  if (!WiFly.join(ssid, passphrase)) {
    Serial.println("Association failed.");
    while (1) {
      // Hang on failure.
    }
  } 

  Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    client.println("Hello World!\n");
    client.println();
  } else {
    Serial.println("connection failed");
  }
 
}

void loop() {
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
 
  while (Serial.available() > 0) {
          char inChar = Serial.read();
            if (client.connected()) {
              client.print(inChar);
            }
    }
 
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}


www.buildinginternetofthings.com

Go Up