Pages: [1]   Go Down
Author Topic: Arduino WiShield 1.0 and Processing for Robot Control  (Read 1148 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Greetings All,

I just tested the SocketApp for WiShield just fine.  I'd like to now replace the provided C example with some processing code.  I tried this:

Code:


import processing.net.*;
Client myClient;
int dataIn;
 
void setup() {
  size(200, 200);
  // Connect to the local machine at port 5204.
  // This example will not run if you haven't
  // previously started a server on this port
  myClient = new Client(this, "192.168.5.89", 1000);
}
 
void draw() {
  if (myClient.available() > 0) {
    dataIn = myClient.read();
  }
  background(dataIn);
}

Figuring it should at least put in the initial what's is your name string.  To no avail.

Here is the SocketApp code main file:

Code:
/*
 * Socket App
 *
 * A simple socket application example using the WiShield 1.0
 */

#include <WiShield.h>

#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,5,89}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,5,254}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"ssid"}; // max 32 bytes

unsigned char security_type = 3; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"password"}; // max 64 characters

// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;
//---------------------------------------------------------------------------

void setup()
{
WiFi.init();
}

void loop()
{
WiFi.run();
}

And the support file:

Code:
/******************************************************************************

  Filename: socketapp.c
  Description: Simple socket programming example for the WiShield 1.0

 ******************************************************************************

  TCP/IP stack and driver for the WiShield 1.0 wireless devices

  Copyright(c) 2009 Async Labs Inc. All rights reserved.

  This program is free software; you can redistribute it and/or modify it
  under the terms of version 2 of the GNU General Public License as
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 59
  Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  Contact Information:
  <asynclabs@asynclabs.com>

   Author               Date        Comment
  ---------------------------------------------------------------
   AsyncLabs 06/06/2009 Initial version

 *****************************************************************************/

/*
 * This is a short example of how to write uIP applications using
 * protosockets.
 */

/*
 * We define the application state (struct socket_app_state) in the
 * socketapp.h file, so we need to include it here. We also include
 * uip.h (since this cannot be included in socketapp.h) and
 * <string.h>, since we use the memcpy() function in the code.
 */
#include "socketapp.h"
#include "uip.h"
#include <string.h>

/*
 * Declaration of the protosocket function that handles the connection
 * (defined at the end of the code).
 */
static int handle_connection(struct socket_app_state *s);
/*---------------------------------------------------------------------------*/
/*
 * The initialization function. We must explicitly call this function
 * from the system initialization code, some time after uip_init() is
 * called.
 */
void socket_app_init(void)
{
  /* We start to listen for connections on TCP port 1000. */
  uip_listen(HTONS(1000));
}
/*---------------------------------------------------------------------------*/
/*
 * In socketapp.h we have defined the UIP_APPCALL macro to
 * socket_app_appcall so that this function is uIP's application
 * function. This function is called whenever an uIP event occurs
 * (e.g. when a new connection is established, new data arrives, sent
 * data is acknowledged, data needs to be retransmitted, etc.).
 */
void socket_app_appcall(void)
{
  /*
   * The uip_conn structure has a field called "appstate" that holds
   * the application state of the connection. We make a pointer to
   * this to access it easier.
   */
  struct socket_app_state *s = &(uip_conn->appstate);

  /*
   * If a new connection was just established, we should initialize
   * the protosocket in our applications' state structure.
   */
  if(uip_connected()) {
    PSOCK_INIT(&s->p, s->inputbuffer, sizeof(s->inputbuffer));
  }

  /*
   * Finally, we run the protosocket function that actually handles
   * the communication. We pass it a pointer to the application state
   * of the current connection.
   */
  handle_connection(s);
}
/*---------------------------------------------------------------------------*/
/*
 * This is the protosocket function that handles the communication. A
 * protosocket function must always return an int, but must never
 * explicitly return - all return statements are hidden in the PSOCK
 * macros.
 */
static int handle_connection(struct socket_app_state *s)
{
  PSOCK_BEGIN(&s->p);

  PSOCK_SEND_STR(&s->p, "Hello. What is you name?\n");
  PSOCK_READTO(&s->p, '\n');
  PSOCK_SEND_STR(&s->p, "Hello ");
  PSOCK_SEND_STR(&s->p, s->inputbuffer);
  memset(s->inputbuffer, 0x00, sizeof(s->inputbuffer));
  PSOCK_CLOSE(&s->p);

  PSOCK_END(&s->p);
}
/*---------------------------------------------------------------------------*/

It would seem to me, that it would at least output the "What is your name?" part.  I understand I have not input set up to respond to the server's query.
« Last Edit: August 08, 2011, 10:51:32 pm by DanK42 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The SocketApp code is for the Arduino with shield attached. If you want to run that code on the PC, in Processing, you will need to attach the WiShield to the PC. Post a schematic of how you have done that, and we'll help you with the (off-topic) processing of getting Processing to use the shield.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the quick response.  However, actually no...I'm pretty sure my topic is fine (WiShield + Arduino -> PC w/Processing)...the processing code is on the computer, the SocketApp is on the Arduino as well as the WiShield.  And the initial bit of code is the processing code I'm trying to use to replace the sample C application provided with the WiShield SocketApp exmaple.  Here is the original C app that was included with the SocketApp example.  

Code:
// client.c
// A stream socket client demo
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>

#define PORT 1000    // the port WiShield is listening on

#define MAXDATASIZE 100    // max number of bytes we can get at once from WiShield

int main()
{
    int sockfd, numbytes;
    char buf[MAXDATASIZE];
    struct sockaddr_in srv;
    unsigned char buf_len;

    memset(&srv, 0, sizeof(srv));

    // setup IP address of WiShield
    srv.sin_family = AF_INET;
    srv.sin_port = htons(PORT);
    inet_pton(AF_INET, "192.168.1.2", &srv.sin_addr);

    // setup socket and connect
    if ((sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
        perror("socket");
        exit(0);
    }

    if (connect(sockfd, (const void*)&srv, sizeof(srv)) == -1) {
        close(sockfd);
        perror("connect");
        exit(0);
    }

    printf("connected to WiShield\n");

    // Wait for welcome message from WiShield
    if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
        perror("recv");
        exit(1);
    }
    buf[numbytes] = '\0';
    printf("%s", buf);

    // Get name from user
    scanf("%s", buf);
    buf_len = strlen(buf);
    buf[buf_len] = '\n';

    // Send user response
    if (send(sockfd, buf, buf_len+1, 0) == -1) {
        perror("send");
        close(sockfd);
        exit(0);
    }

    // We loop here until we receive the entire response
    // indicated by a newline character
    do {
        if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
            perror("recv");
            exit(1);
        }
        buf[numbytes] = '\0';
        printf("%s", buf);

        // check for newline character
        if (buf[numbytes-1] != '\n') {
         continue;
        }
        else {
         break;
        }
    } while(1);

    // close socket
    close(sockfd);

    return 0;
}
« Last Edit: August 09, 2011, 11:05:52 am by DanK42 » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

SOLVED

Turns out I was just about their, here is my new Processing code, that excepts just the initial output from the WiShield SocketApp code.  The result you should get is:

"Hello. What is you name?"  (I did not bother going any further so don't bother trying to type your name at it.  Also it prints in the Console Window, where you get error messages rather than the normal output window, sorry quick and dirty for now.  I'll come back with the finished product.)

Code:
import processing.net.*;
Client myClient;
//int dataIn;
String data;
 
void setup() {
  size(200, 200);
  myClient = new Client(this, "192.168.8.89", 1000);
}
 
void draw() {
  if (myClient.available() > 0) {
    //dataIn = myClient.read();
    data = myClient.readString();
    println(data);
  }
  //background(dataIn);
}
« Last Edit: August 09, 2011, 11:44:43 am by DanK42 » Logged

Pages: [1]   Go Up
Jump to: