Sending data from processing to arduino via wifi shield or ethernet shield.

edited May 2014 in Arduino

Hello, i'm new to processing and arduino's stuff..First of all, excuse my english. With my project, i want to send to my arduino wifi shield 2 chars.They are the on and off commands that activate a relay.With the relay i'm turning on and off a desklamp. Although, i want the project to works, without the serial connection from my computer.And this is not happening.That's my problem. What i'm doing in the processing sketch is, making two rectangles and if i press the mouse inside the first one, i want the lamp to turn on.If i press the mouse inside the second one,i want the lamp to turn off. So, when i run my code, first i get the ip of my board and then, if i unplug the usb cable, and press the first rectangle the lamp is on indeed!
But, if i press the second one to turn off the relay, nothing happens. The same problem i have with the ethernet shield of arduino.I use exactly the same code in processing.After i get the ip address, unplug the usb connection and press the first rectangle, relay is on.But when i press the mouse to the second rectangle i'm getting a message like "socketexception connection reset". Obviously i'm doing something wrong with the "read" and "write" functions when a client is connected.Here is my code of processing..

import processing.serial.*;
import processing.net.*;



String myString ;
String host = " " ;
int lf = 10;    // Linefeed in ASCII
Serial myPort;  // The serial port
Client c;
PFont myFont;

void setup() {


  // Open the port you are using at the rate you want:
  myPort = new Serial(this, "COM3", 9600);

  myPort.clear();
  // Throw out the first reading, in case we started reading 

 myString = null;


}

void draw() {

      size(300,300);
      rect(25, 25, 50, 50);
      rect(80,80,50,50);
      fill(204, 102, 0);
      myFont = createFont("Amigo-20", 20);
      textFont(myFont);
      text("Lamp on",10,20);
      text("Lamp off",80,70);
      text("Click me!",100,180);

      while (myPort.available() > 0) {
       myString = myPort.readStringUntil(lf);
          if (myString != null) {

             host = myString.substring(0 , 11) ;
            println(host);

          }

      }
      c = new Client(this, host , 80); // Connect to server on port 80

      if((mouseX>=25&&mouseX<=75)&&(mouseY>=25&&mouseY<=75))
        {
              if(mousePressed==true)
                {
                  c.write('n');
                }
         }

       if((mouseX>=80&&mouseX<=130)&&(mouseY>=80&&mouseY<=130))
        {
             if(mousePressed==true)
               {
                c.write('f');
               }
         }
}

The next code is the wifi-arduino-code :

#include <SPI.h>
#include <WiFi.h>
#include <TinkerKit.h>

char ssid[] = "CYTA BACD";      //  your network SSID (name) 
char pass[] = "arxid...........";   // your network password
int keyIndex = 0;                 // your network key Index number (needed only for WEP)
 TKRelay relay(O5);
 TKLightSensor sensor(I0);
int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  Serial.begin(9600);      // initialize serial communication

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present"); 
    while(true);        // don't continue
  } 

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) { 
    //Serial.print("Attempting to connect to Network named: ");
    //Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  } 
  server.begin();                           // start the web server on port 80
  printWifiStatus();                        // you're connected now, so print out the status
}


void loop() {
  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,

    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then

        if (c == 'n') {                   
          relay.on();
        }

        if(c == 'f'){
          relay.off();
        }
      }
   }

    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

void printWifiStatus() {

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  //Serial.print("IP Address: ");
  Serial.print(ip);
  Serial.print(".");
  Serial.println();

}

And the next one is the ethernet-arduino-code:

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

byte mac[] = { 
  0x90, 0xA2, 0xDA, 0x0D, 0xBD, 0xDE };


EthernetServer server(80);

EthernetClient client;

//boolean alreadyConnected= false; //whether or not the client is connented

TKLightSensor sensor(I0);
TKRelay relay(O5);

void setup() {


  Serial.begin(9600);
  delay(1000);



  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // print your local IP address:

  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }
   Serial.println();

}


void loop() {
  // wait for a new client:
  EthernetClient client = server.available();

  if (client) {                             // if you get a client,
    //Serial.println("new client");           // print a message out the serial port

    while (client.connected()) {
       if (client.available()) {                                // loop while the client's connected
                                    // if there's bytes to read from the client,
        char c =client.read();             // read a byte, then
       // Serial.write(c); 

        if (c == 'n') {
           relay.on();
        }

        if(c == 'f'){
             relay.off();
        }
      }
    }
   client.stop();
 }
}

If anyone could help me, or anyone have an idea of what i'm doing wrong, i would be grateful!! Thank you anyway..

Sign In or Register to comment.