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();
}