(to make this actually work you'll need to fill in a real pachube api key, feed id, SSID, and AP password of course, and fix up local IP addresses)
- Code: Select all
#include <WiServer.h>
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
#define PACHUBE_API_KEY "<MY PACHUBE API KEY>"
#define PACHUBE_FEED_ID "<MY FEED ID>"
#define PACHUBE_UPDATE_INTERVAL (30L * 1000L)
// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {10,0,0,82}; // 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 = {"MY_SSID"}; // 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 = {"my_password"}; // 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
};
// setup the wireless mode
// infrastructure - connect to AP
unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------
// Function that prints data from the server
// prints data --received--
void printData(char* data, int len) {
unsigned long printtime = millis();
Serial.print(printtime);
Serial.println(": got data.");
}
//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 commas (no spaces)
// Sends data -out-
//
void feedData() {
char buf[32];
unsigned long feedtime = millis();
Serial.print(feedtime);
Serial.println(": sent data.");
sprintf(buf, "%d,%d,%d", 0, 0, 0);
WiServer.print(buf);
}
// IP Address for api.pachube.com
uint8 ip[] = {173,203,98,29};
char hostName[] = "www.pachube.com\nX-PachubeApiKey: " PACHUBE_API_KEY "\nConnection: close";
char url[] = "/api/" PACHUBE_FEED_ID ".csv?_method=put";
// A request that POSTS data to Pachube
POSTrequest postPachube(ip, 80, hostName, url, feedData);
unsigned long updateTime = 0; // next time to post data
void setup()
{
Serial.begin(57600);
// 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)
WiServer.enableVerboseMode(true);
// Have the printData function called when data is returned by the server
postPachube.setReturnFunc(printData);
// First update 30s from now
updateTime = millis() + PACHUBE_UPDATE_INTERVAL;
}
void loop()
{
delay(1000);
Serial.println("loop");
if (millis() >= updateTime) {
unsigned long timeNow = millis();
Serial.print(timeNow);
Serial.print(" >= ");
Serial.print(updateTime);
Serial.print(": Updating pachube; ");
// Do another update 30s from now
updateTime += PACHUBE_UPDATE_INTERVAL;
Serial.print("updateTime now ");
Serial.println(updateTime);
postPachube.submit();
}
// Run WiServer
WiServer.server_task();
delay(10);
}
The problem is, things start off just fine; I see on the serial port:
- Code: Select all
64869 >= 94506: Updating pachube; updateTime now 94506
loop
loop
loop
loop
loop
loop
loop
loop
loop
loop
loop
loop
loop
loop
loop
loop
loop
loop
loop
Connected to www.pachube.com
X-PachubeApiKey: XXXX
Connection: close
84136: sent data.
TX 269 bytes
RX 0 bytes from www.pachube.com
X-PachubeApiKey: XXX
Connection: close
loop
loop
loop
loop
RX 164 bytes from www.pachube.com
X-PachubeApiKey: XXX
Connection: close
88240: got data.
loop
RX 164 bytes from www.pachube.com
X-PachubeApiKey: XXX
Connection: close
89280: got data.
loop
loop
loop
loop
loop
loop
loop
95360 >= 124506: Updating pachube; updateTime now 124506
RX 39 bytes from www.pachube.com
X-PachubeApiKey: XXX
Connection: close
95393: got data.
Ended connection with www.pachube.com
X-PachubeApiKey: XXX
Connection: close
95421: got data.
... i.e. it does seem to be sending and receiving requests, but eventually this stops; all I see is:
- Code: Select all
875286 >= 904506: Updating pachube; updateTime now 904506
loop
...
904650 >= 934506: Updating pachube; updateTime now 934506
loop
...
935028 >= 964506: Updating pachube; updateTime now 964506
loop
...
and I never get any debug message indicating I've hit the feedData() function.
Any idea what I'm doing wrong, or how to debug this?
(I have a hunch that I'm sending a new request before the old one is complete..?)
Thanks,
-Eric