Arduino + Wishield + Servos

Hi,

Anyone have any experience with this combination?..

Essentially I want the Arduino to act like a webserver with the help of the Wishield. With this I want to create a website with buttons to control the servos. Thing is the Wishield and the Arduino seem to work but when I add the Servos to the Arduino using the Wishield extender pins and upload Servo code to the Arduino the Wishield stops working. I am using a breadboard for all the wiring.

I have concluded that this might be due to the fact that I am uploading Servo and Webserver code seperately to the Arduino. Am I correct in saying that this renders one these components i.e. the servos or the Wishield to not work as the previous code on the Arduino gets deleted/replaced?. Is it possible to merge Servo and Webserver code into the same sketch and upload it?

P.S. I have tried merging the two codes and it didn't work for me. I believe this is due to me being a complete newbie at programming.

Any help would be greatly appreciated.

Am I correct in saying that this renders one these components i.e. the servos or the Wishield to not work as the previous code on the Arduino gets deleted/replaced?.

Yes, this is what happens.

Is it possible to merge Servo and Webserver code into the same sketch and upload it?

Yes, it is.

If you post your two sketches, some suggestions can be given on how to merge them.

Hi,

Thanks for the reply… .


Here is the first sketch. It’s pretty simple sketch to test the servos.


// Bot builder BoxBot demo code. Random movements 

#include <Servo.h> // Included to make the servos turn 
Servo leftServo; // Telling the above library we have this servo 
Servo rightServo; // Telling the above library we have this servo
void setup() 
{ 
  leftServo.attach(5); // Enables the use of this servo  
  rightServo.attach(6); // Enables the use of this servo 
} 

void loop() 
{ 
  int choice = random(2); // Declare a random 1 or 2 number  
  int duration = random(2000); // Declare a random number between 1 and 2000

  goForward(); // Use the goForward code below  
  delay(duration); // Use a delay that turns the servos for the random duration in milliseconds 

// After the above delay has finished let's turn the robot  
  if (choice == 1) // If the random number is 1 then go right  
    { 
      goLeft(); // Use the goLeft code below  
    } 
    
    else // If the random number is not 1 it must be 2 so...  
      { 
        goRight(); // Use the goRight code below  
      } 
    
  delay(duration); // Use a delay that turns the servos for the random duration in milliseconds  
} 

void goForward() // This makes the bot go forward by turning a servo: 
{ 

  leftServo.write(110); // One way  
  rightServo.write(50); // And the other way 
} 

void goRight() // This makes the bot turn by making the servos: 
{ 
  leftServo.write(50); // Turn the same way lower than 90 (center) 
  rightServo.write(50); // Turn the same way lower than 90 (center) 
} 

void goLeft() // This makes the bot turn by making the servos: 
{ 
  leftServo.write(110); // Turn the same way higher than 90 (center) 
  rightServo.write(110); // Turn the same way higher than 90 (center) 
}

Here is the webserver code… it’s the sample from the AsyncLabs wiki… There are two sketches in one file for some reason… This makes it difficult to combine the servo code as I have no clue which file I should modify. Also I get variable errors when I attempt this.
Not much modification done to it yet… I am hoping I can create a User Interface using HTML and JavaScript, I am assuming all the code will be written inside the PROGMEM =?.


Web Server.pde

/*
 * Web Server
 *
 * A simple web server 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,1,200};      // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,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 = {"BeBox"};            // max 32 bytes

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

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"14CCAACB88"};      // 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();
}

// This is the webpage that is served up by the webserver
const prog_char webpage[] PROGMEM = {"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<center><h1>Hello World!! I am WiShield</h1><form method=\"get\" action=\"0\">Toggle LED:<input type=\"submit\" name=\"0\" value=\"LED1\"></input></form></center>"};

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

Webserver.c

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

  Filename:            webserver.h
  Description:      Webserver app 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                  05/29/2009      Initial version

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

#include "uip.h"
#include <string.h>
#include "webserver.h"
#include "config.h"

static int handle_connection(struct webserver_state *s);

void webserver_init(void)
{
      uip_listen(HTONS(80));
}

void webserver_appcall(void)
{
      struct webserver_state *s = &(uip_conn->appstate);

      if(uip_connected()) {
            PSOCK_INIT(&s->p, s->inputbuf, sizeof(s->inputbuf));
      }

      handle_connection(s);
}

#define ISO_nl      0x0a
#define ISO_space   0x20
#define ISO_slash   0x2f

const char http_get[5] = {0x47, 0x45, 0x54, 0x20, };      /* "GET " */

unsigned short fill_buf(void* blk)
{
      unsigned short webpage_len;

      webpage_len = (strlen_P(webpage)>uip_mss())?uip_mss():strlen_P(webpage);

      memcpy_P(uip_appdata, webpage, webpage_len);
      return webpage_len;
}

static int handle_connection(struct webserver_state *s)
{
      PSOCK_BEGIN(&s->p);

      // the incoming GET request will have the following format:
      // GET / HTTP/1.1 ....
      // we have to parse this string to determine the resource being requested
      // if the requested resource is not the root webpage ('/') then,
      // GET /<resource name> HTTP/1.1 ....
      // we should parse the specific resource and react appropriately

      // read incoming data until we read a space character
      PSOCK_READTO(&s->p, ISO_space);

      // parse the data to determine if it was a GET request
      if(strncmp(s->inputbuf, http_get, 4) != 0) {
            PSOCK_CLOSE_EXIT(&s->p);
      }

      // continue reading until the next space character
      PSOCK_READTO(&s->p, ISO_space);

      // determine the requested resource
      // in this case, we check if the request was for the '/' root page
      // AKA index.html
      if(s->inputbuf[0] != ISO_slash) {
            // request for unknown webpage, close and exit
            PSOCK_CLOSE_EXIT(&s->p);
      }

      if(s->inputbuf[1] != ISO_space) {
            // request for unavailable resource
            // not supported, modify to add support for additional resources
            PSOCK_CLOSE_EXIT(&s->p);
      }

      //PSOCK_SEND_STR(&s->p, "HTTP/1.1 200 OK\r\n");
      //PSOCK_SEND_STR(&s->p, "Content-Type: text/html\r\n");
      //PSOCK_SEND_STR(&s->p, "\r\n");
      //PSOCK_SEND_STR(&s->p, "Hello World, I am WiShield");
      //PSOCK_SEND_STR(&s->p, "<center><h1>Hello World!! I am WiShield</h1></center>");
      PSOCK_GENERATOR_SEND(&s->p, fill_buf, 0);
      PSOCK_CLOSE(&s->p);
      PSOCK_END(&s->p);
}

Thanks.

bump