Hi,
I'm getting values from Pachube using a WiShield and using an RGB LED to reflect the values. Upon starting up I get the values from Pachube , but can never seem to get the values a second time.
I have a loop where it calls Pachube every 20 seconds for 4 tries if this doesn't work then it switches to waiting another minute, then goes back to the 20 seconds for 4 tries. Basically I want it to call Pachube until it gets a value, then wait a long time like half an hour before calling again.
I think I have a memory issue because sometimes the Arduino goes wonky and resets itself and I never get any more data. How do I clean out my memory or reset it to 0? How do I get values from Pachube a second time?
/*
* Thanks to the following people who made this project possible
Miguel Sanchez for the Arduino mood light code
Greg Eigsti & Voyager from the Asynclabs boards.
This project uses 2 Arduino boards and 2 Wishields
*/
#include <WiShield.h>
#include <WiServer.h>
#include <MsTimer2.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
#include <stdio.h>
char field [ 64 ];
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,2}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,1}; // router or gateway IP address
byte subnet_mask[] = {255,255,255,0}; // your subnet mask for the local network
const prog_char ssid[] PROGMEM = {"Your Network Name"}; // 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 = {"12345668"}; // max 64 characters
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
};
// WEP 128-bit keys
// sample HEX keys
// 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;
//---------------------------------------------------------------------------
//global state data
boolean intTimer;
boolean gotData;
int temp1;
int temp2;
int sun;
int x=0;
int statey =0;
int resetShield=0;
unsigned int timercount = 0;
unsigned int successes = 0;
// unsigned int failures = 0;
int val; // use to read a byte from the serial
int state=0;
int i=0;
boolean inValidPacket;
char recvData[400]=" ";
// IP Address for Pachube.com
uint8 ip[] = {209,40,205,190};
char hostName[] = "www.pachube.com\nX-PachubeApiKey: "Your key here"\nConnection: close";
char url[] = "/api/29.57.csv?_method=put";
GETrequest getPachube(ip, 80, "www.pachube.com", "/api/2957.csv HTTP/1.1\nHost: pachube.com\nX-PachubeApiKey: c8a045ad5501e612c71319394c5aa77241172ed0ee66f071558d9660d8b54aa0\nConnection: Close\n");
int r=0;
int g=0;
int b=0;
//MsTimer2 one minute timer ISR
void timerISR() {
intTimer = true;
}
void printGetData(char* data, int len) {
//if we are not in the middle of a packet AND we find the OK token
if(false == inValidPacket && NULL != strstr(data, "HTTP/1.1 200 OK")) {
//mark that we are in a valid packet
inValidPacket = true;
//temporary for debugging purposes
Serial.println("Data reception started");
}
else {
//temporary for debugging purposes
// Serial.println("How did we get here?");
}
//if we were in a valid packet AND we get the ending 0 == len call
if(true == inValidPacket && 0 == len) {
//mark that we are no longer in a valid packet
inValidPacket = false;
//grab out the values with something like sscanf()
// Need to point sscanf to the right place
//char *data; //data from the WiShield - set to something like the "re-hydrated" string above
//char *begin;
//char *end;
const char *ptr = recvData;
int n;
while ( sscanf(ptr, "%64[^\n]%n", field, &n) == 1 )
{
printf("field = \"%s\"\n", field);
Serial.print ("Field = ");
Serial.println(field);
if(strlen(field) <= 15 && (strlen(field) > 4))
{
scan2(); //parse out last bit of data
}
delay(10);
ptr += n; /* advance the pointer by the number of characters read */
if ( *ptr != '\n' )
{
break; /* didn't find an expected delimiter, done? */
}
while ( *ptr == '\n' )
{
++ptr; /* skip the delimiter */
}
}
//temporary for debugging purposes
Serial.println("Data reception ended");
Serial.println("Data string = ");
Serial.println(recvData);
Serial.println(" ");
Serial.println("Values from sscanf");
Serial.print("Temp 1 ");
Serial.println(temp1);
Serial.print("Temp 2 = ");
Serial.println(temp2);
Serial.print("Sun = ");
Serial.println(sun);
//reset the received data buffer to all 0s - clean up...memset 400
memset(recvData, 0, 400 * sizeof(char));
}
else {
//just being anal... probably not needed
memset(recvData, 0, 400 * sizeof(char));
//stash away the last bit of data received
memcpy(recvData, data, len);
//temporary for debugging purposes
Serial.println("Received data:");
// Serial.println(data);
}
}
void setup() {
Serial.begin(57600);
Serial.println("Connecting...");
// WiFi.init();
rgb(5,255,25);// write values
// gotData=false;
WiServer.enableVerboseMode(false);
// Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
WiServer.init(NULL);
//init global data; add to, not replace, what is in your setup() function
inValidPacket = false;
memset(recvData, 0, 400 * sizeof(char));
// Have the processData function called when data is returned by the server
getPachube.setReturnFunc(printGetData);
//set up global state data
intTimer = false;
//setup the timerISR to be called every xxxxx ms
MsTimer2::set(20000, timerISR); // 60000ms/1min period
MsTimer2::start();
Serial.println("Connect Timer 20 sec");
}
void loop()
{
if(statey==1)
{
colorchange();
}
//--------------------------------------------------------------------------------------------
//handle the timer overflow
//--------------------------------------------------------------------------------------------
if(true == intTimer) {
//Time has has passed
intTimer = false;
Serial.println();
Serial.println("== Timer fired ==");
MsTimer2::set(20000, timerISR); // 60000ms/1min period
MsTimer2::start();
Serial.print("timer 20 sec");
Serial.print("\nTotal calls= ");
Serial.println(++timercount);
getPachube.submit();
if (getPachube.isActive()) {
Serial.print("\nSuccess calls= ");
Serial.println(++successes);
}
}
if(successes >=4)
{
Serial.println("Timer 1 min");
successes=0;
MsTimer2::set(60000, timerISR); // 60000ms/1min period
MsTimer2::start();
Serial.println("Reset Connection");
WiFi.init();
gotData=false;
}
if(gotData==true)
{
Serial.println("Timer 1 min");
successes=0;
MsTimer2::set(60000, timerISR); // 60000ms/1min period
MsTimer2::start();
Serial.println("Reset Connection");
WiFi.init();
gotData=false;
}
// Run WiServer
WiServer.server_task();
delay(10);
}
void scan2()
{
Serial.println("Parsing..");
sscanf(field, "%d,%d,%d", &temp1, &temp2, &sun);
Serial.print("Variable 1-3 = ");
Serial.println(temp1);
// Serial.print("Variable 2 = ");
Serial.println(temp2);
// Serial.print("Variable 3 = ");
Serial.println(sun);
gotData=true;
statey=1;
Serial.println("Time 1 min ");
MsTimer2::set(60000, timerISR); // 60000ms/1min period
MsTimer2::start();
// memset(field, 0, 100 * sizeof(char));
if (temp1<=32)
{
b=temp1;
r=(temp2*2+25);
g=sun;
}
else if (temp1<=60)
{
b=temp1*2;
r=temp2*2;
g=sun+10;
}
else
{
b=temp1*2+25;
r=temp2-25;
g=sun+75;
successes=0;
}
}
void colorchange()
{
int color,i,value,time=25; // how long does it take each step
color=random(1,4); // which color to dim now
value=random(temp1,256); // new value of that color
/* Serial.print("Value = ");
Serial.println(value);
Serial.print ("R = ");
Serial.println(r);
Serial.print ("B = ");
Serial.println(b); */
switch(color) {
case 1:
if(r>value) for(i=r;i>value;i--) {
rgb(i,g,b);
delay(time);
}