- 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