Pachube and WiShield

Discussion about any of the included sketches (i.e. WebServer), or user-generated applications.

Re: Pachube and WiShield

Postby seacritter » Wed Jul 14, 2010 6:55 am

Declare your variables in the beginning, then set them in setup...

declare them here:
Code: Select all
//non WiShield defines
    //#define DEBUG_PRINT
    #define IR_PIN    2
    #define LED_PIN   13
   
    long energy;
    long power;
    int lastRead;
    long counter;
    long lastUpdate;

    // IP Address for Pachube.com
    uint8 ip[] = {173,203,98,29};
    char hostName[] = "www.pachube.com\nX-PachubeApiKey:XXXXXXX\nConnection: close";
    char url[] = "/api/feeds/XXX.csv?_method=put";
    // A request that POSTS data to Pachube
    POSTrequest postPachube(ip, 80, hostName, url, feedData);


Then set them here:
Code: Select all
    void setup()
    {   
       lastRead = HIGH; // The last reading (off)
       lastUpdate = 0; // The last time we sent an update
       counter = 0;    // The current counter
       energy = 0;     // The energy used in the current period (Wh)
       power = 0;      // Rate of power used in current period (W)

       //setup the analog pins as input, probly redundant
       pinMode(IR_PIN, INPUT);
       pinMode(LED_PIN, OUTPUT);
Thanks,
Capn Scott
http://smallweatherstation.com
User avatar
seacritter
 
Posts: 45
Joined: Fri May 28, 2010 10:42 am
Location: Today? Near Philly...

Re: Pachube and WiShield

Postby bpmccain » Wed Jul 14, 2010 7:22 am

Thanks Capn Scott. I've tried that and it's reduced the compile error to:

error: 'POSTrequest' does not name a type In function 'void setup()':
In function 'void loop()':

It's probably something very simple, but I'm new to this and not the strongest programmer (slowly improving, but don't get much time to do it as a hobby).

Code: Select all
/*
* A simple sketch that uses WiServer to PUT (via POST) to Pachube
*/

#include <WiServer.h>
#include <math.h>

//Wireless configuration defines ----------------------------------------
#define WIRELESS_MODE_INFRA   1
#define WIRELESS_MODE_ADHOC   2

//Wireless configuration parameters ----------------------------------------
unsigned char local_ip[]       = {192,168,0,200};  // IP address of WiShield
unsigned char gateway_ip[]     = {192,168,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 = {"XXXX"};    // 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 = {"XXXX"};   // max 64 characters
// WEP 128-bit keys
prog_uchar wep_keys[] PROGMEM = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  // 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 ----------------------------------------

//non WiShield defines
//#define DEBUG_PRINT
#define IR_PIN    2
#define LED_PIN   13
int lastRead;    // The last reading (off)
long lastUpdate; // The last time we sent an update
long counter;    // The current counter
long energy;     // The energy used in the current period (Wh)
long power;      // Rate of power used in current period (W)

// IP Address for Pachube.com 
uint8 ip[] = {173,203,98,29};
char hostName[] = "www.pachube.com\nX-PachubeApiKey:XXXX\nConnection: close";
char url[] = "/api/feeds/XXX.csv?_method=put";
// A request that POSTS data to Pachube
POSTrequest postPachube(ip, 80, hostName, url, feedData);

//body data function, provides data for the POST command in comma separated values (CSV)
//currently POSTs one value but more can be added by separating with comma (no spaces)
void feedData()
{
   char buf[16];
   sprintf(buf, "%d,%d", energy, power);
   WiServer.print(buf);
}

// Function that prints data from the server
void printData(char* data, int len) {
   // Print the data returned by the server
   // Note that the data is not null-terminated, may be broken up into smaller packets, and
   // includes the HTTP header.
#ifdef DEBUG_PRINT
   while (len-- > 0) {
      Serial.print(*(data++));
   }
#endif //DEBUG_PRINT
}

void setup()
{   
   //setup the analog pins as input, probly redundant
   pinMode(IR_PIN, INPUT);
   pinMode(LED_PIN, OUTPUT);
   lastRead = HIGH; // The last reading (off)
   lastUpdate = 0; // The last time we sent an update
   counter = 0;    // The current counter
   energy = 0;     // The energy used in the current period (Wh)
   power = 0;      // Rate of power used in current period (W)

   // Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
   WiServer.init(NULL);

   // Enable Serial output and ask WiServer to generate log messages (optional)
   #ifdef DEBUG_PRINT
      Serial.begin(9600);
      //WiServer.enableVerboseMode(true);
   #endif //DEBUG_PRINT

   // Have the processData function called when data is returned by the server
   postPachube.setReturnFunc(printData);

}

void loop()
{
   int r = digitalRead(2);

   if (r != lastRead)
   {
     lastRead = r;
     if (r == LOW) // A blink has been detected.
     {
        counter++;
        digitalWrite(13,  HIGH);
     }
     else
     {
        digitalWrite(13,  LOW);
     }
   }

   if (millis() > lastUpdate + 5000 || millis() < lastUpdate)
   {
      // Send data out the serial port.
      lastUpdate = millis();
      energy=counter;
      power=counter*3600/5;
      #ifdef DEBUG_PRINT
         Serial.print("===== LastUpdate fired: energy ");
         Serial.print(energy, DEC);
         Serial.print("Wh, power ");
         Serial.println(light, DEC);
         Serial.print("W");
      #endif //DEBUG_PRINT
      postPachube.submit();
      counter=0;
      energy=0;
      power=0;
   }

   // Run WiServer
   WiServer.server_task();
   delay(10);
}
bpmccain
 
Posts: 7
Joined: Tue Jul 13, 2010 2:31 pm

Re: Pachube and WiShield

Postby seacritter » Wed Jul 14, 2010 7:30 am

With this code, I get a "Done Compiling" and a "Binary sketch size: 16748 bytes (of a 30720 byte maximum)"

This is the code:
Code: Select all
    /*
    * A simple sketch that uses WiServer to PUT (via POST) to Pachube
    */

    #include <WiServer.h>
    #include <math.h>

    //Wireless configuration defines ----------------------------------------
    #define WIRELESS_MODE_INFRA   1
    #define WIRELESS_MODE_ADHOC   2

    //Wireless configuration parameters ----------------------------------------
    unsigned char local_ip[]       = {192,168,0,200};  // IP address of WiShield
    unsigned char gateway_ip[]     = {192,168,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 = {"XXXX"};    // 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 = {"XXXX"};   // max 64 characters
    // WEP 128-bit keys
    prog_uchar wep_keys[] PROGMEM = {
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  // 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 ----------------------------------------

    //non WiShield defines
    //#define DEBUG_PRINT
    #define IR_PIN    2
    #define LED_PIN   13
   
    long energy;
    long power;
    int lastRead;
    long counter;
    long lastUpdate;

    // IP Address for Pachube.com
    uint8 ip[] = {173,203,98,29};
    char hostName[] = "www.pachube.com\nX-PachubeApiKey:XXXXXXX\nConnection: close";
    char url[] = "/api/feeds/XXX.csv?_method=put";
    // A request that POSTS data to Pachube
    POSTrequest postPachube(ip, 80, hostName, url, feedData);


    // Function that prints data from the server
    void printData(char* data, int len) {
       // Print the data returned by the server
       // Note that the data is not null-terminated, may be broken up into smaller packets, and
       // includes the HTTP header.
    #ifdef DEBUG_PRINT
       while (len-- > 0) {
          Serial.print(*(data++));
       }
    #endif //DEBUG_PRINT
    }

    //body data function, provides data for the POST command in comma separated values (CSV)
    //currently POSTs one value but more can be added by separating with comma (no spaces)
    void feedData()
    {
       char buf[16];
       sprintf(buf, "%d,%d", energy, power);
       WiServer.print(buf);
    }

    void setup()
    {   
       lastRead = HIGH; // The last reading (off)
       lastUpdate = 0; // The last time we sent an update
       counter = 0;    // The current counter
       energy = 0;     // The energy used in the current period (Wh)
       power = 0;      // Rate of power used in current period (W)

       //setup the analog pins as input, probly redundant
       pinMode(IR_PIN, INPUT);
       pinMode(LED_PIN, OUTPUT);

       // Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
       WiServer.init(NULL);

       // Enable Serial output and ask WiServer to generate log messages (optional)
       #ifdef DEBUG_PRINT
          Serial.begin(9600);
          //WiServer.enableVerboseMode(true);
       #endif //DEBUG_PRINT

       // Have the processData function called when data is returned by the server
       postPachube.setReturnFunc(printData);

    }

    void loop()
    {
       int r = digitalRead(2);

       if (r != lastRead)
       {
         lastRead = r;
         if (r == LOW) // A blink has been detected.
         {
            counter++;
            digitalWrite(13,  HIGH);
         }
         else
         {
            digitalWrite(13,  LOW);
         }
       }

       if (millis() > lastUpdate + 5000 || millis() < lastUpdate)
       {
          // Send data out the serial port.
          lastUpdate = millis();
          energy=counter;
          power=counter*3600/5;
          #ifdef DEBUG_PRINT
             Serial.print("===== LastUpdate fired: energy ");
             Serial.print(tenergy, DEC);
             Serial.print("Wh, power ");
             Serial.println(light, DEC);
             Serial.print("W");
          #endif //DEBUG_PRINT
          postPachube.submit();
          counter=0;
          energy=0;
          power=0;
       }

       // Run WiServer
       WiServer.server_task();
       delay(10);
    }


Are your libraries in the right places?
Thanks,
Capn Scott
http://smallweatherstation.com
User avatar
seacritter
 
Posts: 45
Joined: Fri May 28, 2010 10:42 am
Location: Today? Near Philly...

Re: Pachube and WiShield

Postby bpmccain » Wed Jul 14, 2010 7:51 am

I went back through all the posts and realized I had missed Greg's statement of:

The WiShield header apps-conf.h - make sure that #define APP_WISERVER is uncommented (and the others are commented)

I hadn't done this. Changed it and it works now. Thanks for the help. It was helpful to find out that the code itself wasn't wrong and had to be in the libraries somewhere. Thanks.
bpmccain
 
Posts: 7
Joined: Tue Jul 13, 2010 2:31 pm

Re: Pachube and WiShield

Postby GregEigsti » Wed Jul 14, 2010 8:52 am

Go take a look at the wiki section for the sample apps; you need to modify apps-conf.h (and in some case uip_conf.h) to set what kind of WiFi app you are building.

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: Pachube and WiShield

Postby bpmccain » Thu Jul 15, 2010 7:46 am

Thanks Greg.

After getting all of that working, I am having some infuriating problems with sprintf.

I am trying to send two variables to Pachube, but it seems whichever one I place after the comma gets sent as 0 (even though I can verify through Serial.print that it is not 0 at the time that it is written to the buffer

This appears to be the only problem I have left, as everything is writing to Pachube at the moment. I appreciate all the help. Thanks.

Brian

Code: Select all
/*
* A simple sketch that uses WiServer to PUT (via POST) to Pachube
*/

#include <WiServer.h>
#include <math.h>

//Wireless configuration defines ----------------------------------------
#define WIRELESS_MODE_INFRA   1
#define WIRELESS_MODE_ADHOC   2

//Wireless configuration parameters ----------------------------------------
unsigned char local_ip[]       = {192,168,0,200};  // IP address of WiShield
unsigned char gateway_ip[]     = {192,168,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 = {"XXXX"};    // 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 = {"XXXXX"};   // max 64 characters
// WEP 128-bit keys
prog_uchar wep_keys[] PROGMEM = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  // 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 ----------------------------------------

//non WiShield defines
#define DEBUG_PRINT
#define IR_PIN    2
#define LED_PIN   13

int lastRead;// The last reading (off)
long lastUpdate; // The last time we sent an update
long counter;    // The current counter
long energy;     // The energy used in the current period (Wh)
long power;      // Rate of power used in current period (W)

// IP Address for Pachube.com 
uint8 ip[] = {173,203,98,29};
char hostName[] = "www.pachube.com\nX-PachubeApiKey:XXXX\nConnection: close";
char url[] = "/api/feeds/XXXX.csv?_method=put";
// A request that POSTS data to Pachube
POSTrequest postPachube(ip, 80, hostName, url, feedData);

//body data function, provides data for the POST command in comma separated values (CSV)
//currently POSTs one value but more can be added by separating with comma (no spaces)
void feedData()
{
   char buf[16];
   Serial.println(energy,DEC);
   Serial.println(power,DEC);
   sprintf(buf,"%d,%d",energy,power);
   Serial.println(buf);
   WiServer.print(buf);
   energy=0;
   power=0;
}

// Function that prints data from the server
void printData(char* data, int len) {
   // Print the data returned by the server
   // Note that the data is not null-terminated, may be broken up into smaller packets, and
   // includes the HTTP header.
//#ifdef DEBUG_PRINT
//   while (len-- > 0) {
//      Serial.print(*(data++));
//   }
//#endif //DEBUG_PRINT
}

void setup()
{   
   //setup the analog pins as input, probly redundant
   pinMode(IR_PIN, INPUT);
   pinMode(LED_PIN, OUTPUT);
   lastRead = HIGH; // The last reading (off)
   lastUpdate = 0; // The last time we sent an update
   counter = 10;    // The current counter
   energy = 0;     // The energy used in the current period (Wh)
   power = 0;      // Rate of power used in current period (W)

   // Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
   WiServer.init(NULL);

   // Enable Serial output and ask WiServer to generate log messages (optional)
   #ifdef DEBUG_PRINT
      Serial.begin(9600);
      //WiServer.enableVerboseMode(true);
   #endif //DEBUG_PRINT

   // Have the processData function called when data is returned by the server
   postPachube.setReturnFunc(printData);

}

void loop()
{
   int r = digitalRead(2);
   if (r != lastRead)
   {
     lastRead = r;
     if (r == LOW) // A blink has been detected.
     {
        counter++;
        digitalWrite(13,  HIGH);
     }
     else
     {
        digitalWrite(13,  LOW);
     }
   }

   if (millis() > lastUpdate + 9999 || millis() < lastUpdate)
   {
   
     // Send data out the serial port.
      lastUpdate = millis();
      energy=counter;
      power=counter*3600/10;
      #ifdef DEBUG_PRINT
         Serial.println(counter, DEC);
         Serial.println(energy, DEC);
         Serial.println(power, DEC);
         Serial.println(lastUpdate, DEC);
      #endif //DEBUG_PRINT
      postPachube.submit();
      counter=10;
   }

   // Run WiServer
   WiServer.server_task();
}
bpmccain
 
Posts: 7
Joined: Tue Jul 13, 2010 2:31 pm

Re: Pachube and WiShield

Postby seacritter » Thu Jul 15, 2010 8:23 am

sprintf isn't as full functioned as in normal versions of libc. Being that this is a device, the code needs to be a bit smaller. I've found that sprintf is a bit picky about variables. It really likes char and int .... If you can convert to those, you'll stay pretty safe...
Thanks,
Capn Scott
http://smallweatherstation.com
User avatar
seacritter
 
Posts: 45
Joined: Fri May 28, 2010 10:42 am
Location: Today? Near Philly...

Re: Pachube and WiShield

Postby GregEigsti » Thu Jul 15, 2010 10:42 am

Don't know if this is a problem or not (with your limited use of the pins) but digital pins 2 and 13 are used by the WiShield (by default) and you really should leave them alone for the best results ;)

Code: Select all
#define IR_PIN    2
#define LED_PIN   13
...
   pinMode(IR_PIN, INPUT);
   pinMode(LED_PIN, OUTPUT);
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: Pachube and WiShield

Postby bj117 » Sat Jul 17, 2010 1:48 pm

Hi - trying to get the wishield and pachube sketch from viewtopic.php?f=15&t=86&start=10#p431 to work. I'm getting the error 'Client does not name a type'. I can get the pachbe in_out to work with a regular ethernet shield, and the WiShield webserver example works. Any thoughts on what to change to fix this client error message?

Thanks.
bj117
 
Posts: 8
Joined: Tue Jul 06, 2010 11:16 pm

Re: Pachube and WiShield

Postby GregEigsti » Sat Jul 17, 2010 5:08 pm

Please post your code.
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

PreviousNext

Return to Sketches and Applications

Who is online

Users browsing this forum: No registered users and 1 guest

cron