Need help getting this fairly simple code working

Discussions related to the WiServer add-on code for WiShield. WiServer is a friendly, easy-to-use front end for webserver and webclient usages.

Moderator: shard7

Need help getting this fairly simple code working

Postby Dan9019 » Mon Aug 16, 2010 4:31 pm

Hey, I'm trying to do a spin on the much-seen ambient light type project for my first arduino project. I want to set up a yellow jacket to pull a couple pieces of weather data from a server on my network, and then display colors with RGB leds accordingly. Right, so I have the pulling and parsing mostly figured out with the help of a piece of code I found on this forum, but whenever I try to add in some code structure to actually translated that data to light, my code stops working. Here is it:

Code: Select all
// A sketch that downloads heat index and weather condition from local server
// and displays it using RGB LEDS.


#include <WiServer.h>


#define WIRELESS_MODE_INFRA   1
#define WIRELESS_MODE_ADHOC   2

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {10,0,0,7};   // IP address of WiShield
unsigned char gateway_ip[] = {10,0,0,1};   // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};   // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"XXXXXXX"};      // 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 = {"XXXXXX"};   // 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;
// End of wireless configuration parameters ----------------------------------------


//Variables
int redLed = 3;
int greenLed = 6;
int blueLed = 5;

char Temp[4];
char Rain[2];
int t;
int r;
unsigned long updateTime = 0;

// IP Address for local server
uint8 ip[] = {10,0,0,9};

// A request that gets the latest data from server
GETrequest getWeather(ip, 80, "10,0,0,9", "/html/weatherData.txt");


void setup() {
 
  pinMode(redLed, OUTPUT);
  pinMode(blueLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
 
Serial.begin(9600);
   
  WiServer.init(NULL);
 
  WiServer.enableVerboseMode(false);
 
  // Have the processData function called when data is returned by the server
  getWeather.setReturnFunc(processData);

}


// --- Function that process data from a .submit request
void processData(char* data, int len) {
      int myPointer=0, foundit=0;
      if (len > 0) { // if len of packet greater than 0 then do following
        memset(data, ' ', sizeof(data));  // initialize to empty
        for (int ix=0; ix < len; ix++) { // count number of characters from the packet to read
          char theChar=data[ix]; // read 1 character
          if ((theChar=='|') || (foundit!=0)) { // found the pointer
            if((foundit!=0) && (theChar!='\n') && (theChar!='\t')) { // If foundit already set to true, and char isn't junk, then add char to wiDateTime
              data[myPointer] = theChar; // add qualified char to wiDateTime
              myPointer++; // increment wiDateTime pointer
            }
           foundit=1; // terminator was found so foundit = true
          }
        } // if the pointer wasn't found in this packet, then it'll do the same to the subsequent packets, until the pointer is found
        if (foundit!=0) {
          // The terminator was found, now split the string
          sscanf (data,"%[^','], %1[^',']", Temp, Rain);
       
                r = atoi(Rain);
                t = atoi(Temp);
                Serial.println(t);
                Serial.println(r);
        }
      } 
    }   
       



void loop() {
 
   
   while(millis() < updateTime)
     {
         while( t >= 85 && r == 0)
           {
               analogWrite(redLed, 255);
               analogWrite(greenLed, 0);
               analogWrite(blueLed, 50);
               
               if(millis() >= updateTime)
                 {
                   Serial.println("break");
                   break;                   
                 } 
           }
       
           while( t < 85 && r == 0)
           {
               analogWrite(redLed, 50);
               analogWrite(greenLed, 255);
               analogWrite(blueLed, 0);
               
               if(millis() >= updateTime)
                 {
                   Serial.println("break");
                   break;
                 } 
           }
     }
     
     
     if(millis() >= updateTime)
    {
       getWeather.submit();
       updateTime += 60000;
       Serial.println("submitted");
    }


   WiServer.server_task(); 


}


Serial output from above code:
Code: Select all
submitted
break
submitted
break
submitted
break
...


As you can see, my t and r values are never getting printed as they should be when the processData function gets done doing its thing. The strange thing is, if i take out everything from the loop() expect for the updating if statement and the WiServer.server_task(), the processData function works as it should, and my r and t values get printed correctly to serial every minute. Why are my LED control structures messing that up?

Thanks for any help
Dan9019
 
Posts: 3
Joined: Sat Jul 31, 2010 2:49 am

Re: Need help getting this fairly simple code working

Postby GregEigsti » Mon Aug 16, 2010 4:54 pm

A quick guess without really understanding your code ;)
It looks like your code in the loop() function is causing too much time to pass between calls to WiServer.server_task(). This function must be called often/quickly to give the WiShield/YellowJacket processor time to stay on the network and do network related housekeeping. It looks like you will need to rearrange your code so that WiServer.server_task() is called more often (e.g. many times a second).

Greg
Check out the wiki!
uIP Stack Docs
Compatible Access Point List
WiShield user contrib branch - DNS, DHCP, AP Scanning, bug fixes, etc.
SlackLab.org - My geek projects blog.
User avatar
GregEigsti
 
Posts: 1067
Joined: Sun Aug 02, 2009 5:23 pm
Location: Sammamish WA USA (near Seattle)
  • Website

Re: Need help getting this fairly simple code working

Postby Dan9019 » Mon Aug 16, 2010 6:15 pm

Thanks! That was exactly the kind of knowledge of how WiServer works that I needed.
Dan9019
 
Posts: 3
Joined: Sat Jul 31, 2010 2:49 am


Return to WiServer

Who is online

Users browsing this forum: Exabot [Bot] and 1 guest