Ok, I'm an idiot, I got this thread confused with another. Sigh. I'll try to give your code a look.
Greg
/*Post Pachube & Get Pachube
*
* Thanks to:
* GregEigsti and ChiefRobot for writing the get, post, and parse code. 5/28/2010
* Shard7 for ConnectionFix 11/12/10
*
*Gets and Posts to Pachube. 11/14/10
*/
#include <WiServer.h>
//Wireless configuration defines ----------------------------------------
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
//Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,108}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,254}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"2WIRE814"}; // max 32 bytes
unsigned char security_type = 1; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"YOUR WPA PASSPHRASE"}; // max 64 characters
// WEP 64-bit keys
//Change the length on line 337 in the g2100.c file: cmd->keyLen = 5; // Key length: 5 bytes (64-bit WEP); 13 bytes (128-bit WEP)
prog_uchar wep_keys[] PROGMEM = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, // Key 0 Shortened the keys to match my router settings.
0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
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
//--------------------------------------------------------------
//Pachube
//--------------------------------------------------------------
// IP Address for Pachube.com
uint8 ip[] = {173,203,98,29};
char hostName[] = "www.pachube.com\nX-PachubeApiKey: XXXX\nConnection: close";
char url[] = "/api/XXXX.csv?_method=put";
// A request that POSTS data to Pachube
POSTrequest postPachube(ip, 80, hostName, url, feedData);
// Change the url to get data from a different feed. (This would be set to my feed number to get data from me.)
char hostName2[] = "www.pachube.com";
char url2[] = "/api/feeds/XXXX.csv";
GETrequest getPachube(ip, 80, hostName, url2);
// End of Pachube setup.
//--------------------------------------------------------------
// Global constants:
//--------------------------------------------------------------
#define DEBUG_PRINT
#define DEBUG_WIFI_CONNECTION
#define SENSOR2 1
#define SENSOR1 0
int LEDpins[] = {3,4,5}; //right to left
// Global variables:
//--------------------------------------------------------------
boolean inValidPacket;
char recvData[256];
unsigned long updateTime = 30000;
unsigned long lastMillis = 0;
// Sensor Data Variables:
//--------------------------------------------------------------
int post_number;
int sensor1_val;
int sensor2_val;
//Incoming Data Variables: Use an appopriate number of variables for your CSV values.
//--------------------------------------------------------------
int var1;
int var2;
int var3;
//definitions for lamp
//--------------------------------------------------------------
/*uint8 ip2[] = {173,201,242,1};
// IP Address for Owenschoppe.com
char hostName3[] = "www.owenschoppe.com";
char url3[] = "XXXX";
// A request that POSTS data to server
GETrequest getAlarm(ip2, 80, hostName3, url3);*/
int LampPin = 6; //lamp
int newAlarm = 0;
int Alarm = 0;
int DimmerPin = 3;
boolean Lamp = false;
#define BUTTON1 8
int Button = 0;
/*int hours;
int minutes;
int seconds;
int currentTime;
int timeON = 475; //in minutes from midnight
int timeOFF = 600;
boolean timeOut = true;*/
// 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()
{
#ifdef DEBUG_PRINT
Serial.println("feedData");
#endif
char buf[16];
//To use more variable just follow the pattern.
sprintf(buf, "%d,%d,%d", post_number, sensor1_val, sensor2_val);
WiServer.print(buf);
}
// Function that prints data from the server during POSTrequests
//--------------------------------------------------------------
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.
while (len-- > 0) {
Serial.print(*(data++));
}
}
// Function that prints data from the server during GETrequests
// Parsing the data takes three stages:
// 1. Data is received in chunks. The last chunk is stored in recvData.
// 2. recvData is split into individual lines.
// 3. The last line, which always contains the csv data, is split into individual values.
//--------------------------------------------------------------
//--------------------------------------------------------------
/*void printGetAlarm(char* data, int len) {
#ifdef DEBUG_PRINT
Serial.println("printGetAlarm");
#endif
//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;
#ifdef DEBUG_PRINT
//temporary for debugging purposes
Serial.println("Data reception has started");
//Serial.println(len);
#endif
}
//if we were in a valid packet AND we get the ending 0 == len call
//This is executed once after receiving the last chunck of data.
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()
parseAlarm(recvData);
#ifdef DEBUG_PRINT
//temporary for debugging purposes
Serial.println("Data reception has ended");
//Serial.println(data);
#endif
}
else {
//insures that the data in recvData is clean and doesn't have leftovers from before
memset(recvData, 0, 256 * sizeof(char));
//stash away the last bit of data received
memcpy(recvData, data, len);
#ifdef DEBUG_PRINT //temporary for debugging purposes
Serial.println("-------- Received a chunk of Alarm data: --------");
Serial.println(recvData);
//inString = recvData;
//Serial.println(data);
//Serial.println("Last chunk of data:");
//Serial.print("Length: ");
//Serial.println(len);
Serial.println("-------- End of Chunk --------");
#endif //DEBUG_PRINT
}
}
//--------------------------------------------------------------
void parseAlarm(char* Data) {
#ifdef DEBUG_PRINT
Serial.println("parseAlarm");
#endif
const char *ptr = Data;
char field [ 64 ];
int n;
int x;
//steps through the recvData saving the last line in 'field' and the number of characters read in 'n'.
while ( sscanf(ptr, "%64[^\n]%n", field, &n) == 1 )
{
#ifdef DEBUG_PRINT
//printf("field = \"%s\"\n", field);
//Serial.print ("Field = ");
//Serial.println(field);
#endif //DEBUG_PRINT
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
}
}
//Store each comma separated value (CSV) in the associated variable
//To use more variable just follow the pattern.
//sscanf looks for comma separated integers (%d , %d). For longer numbers or floats use a different data type.
//Don't forget to change the data type of your variables to match.
//sscanf(field, "%d,%d" , &var1, &var2 );
sscanf(field, "%d" , &newAlarm);
//#ifdef DEBUG_PRINT
Serial.print ("newAlarm = "); //print results
Serial.println(newAlarm);
//#endif //DEBUG_PRINT
}*/
//-------------------------------------------------------------
void printGetData(char* data, int len) {
#ifdef DEBUG_PRINT
Serial.println("printGetData");
#endif
//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;
#ifdef DEBUG_PRINT
//temporary for debugging purposes
Serial.println("Data reception has started");
//Serial.println(len);
#endif
}
//if we were in a valid packet AND we get the ending 0 == len call
//This is executed once after receiving the last chunck of data.
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()
parseData(recvData);
#ifdef DEBUG_PRINT
//temporary for debugging purposes
Serial.println("Data reception has ended");
//Serial.println(data);
#endif
}
else {
//insures that the data in recvData is clean and doesn't have leftovers from before
memset(recvData, 0, 256 * sizeof(char));
//stash away the last bit of data received
memcpy(recvData, data, len);
#ifdef DEBUG_PRINT //temporary for debugging purposes
Serial.println("-------- Received a chunk of data: --------");
Serial.println(recvData);
//inString = recvData;
//Serial.println(data);
//Serial.println("Last chunk of data:");
//Serial.print("Length: ");
//Serial.println(len);
Serial.println("-------- End of Chunk --------");
#endif //DEBUG_PRINT
}
}
//Function that parses the last packet of data received.
//--------------------------------------------------------------
void parseData(char* Data) {
const char *ptr = Data;
char field [ 64 ];
int n;
int x;
//steps through the recvData saving the last line in 'field' and the number of characters read in 'n'.
while ( sscanf(ptr, "%64[^\n]%n", field, &n) == 1 )
{
#ifdef DEBUG_PRINT
//printf("field = \"%s\"\n", field);
//Serial.print ("Field = ");
//Serial.println(field);
#endif //DEBUG_PRINT
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 */
}
}
//Store each comma separated value (CSV) in the associated variable
//To use more variable just follow the pattern.
//sscanf looks for comma separated integers (%d , %d). For longer numbers or floats use a different data type.
//Don't forget to change the data type of your variables to match.
//sscanf(field, "%d,%d" , &var1, &var2 );
sscanf(field, "%d,%d,%d," , &var1, &var2, &var3 );
#ifdef DEBUG_PRINT
Serial.print ("Var1 = "); //print results
Serial.println(var1);
Serial.print ("Var2 = ");
Serial.println(var2);
Serial.print ("Var3 = ");
Serial.println(var3);
#endif //DEBUG_PRINT
}
//Setup function
//--------------------------------------------------------------
void setup() {
//initialize sensor variables
post_number = 0;
sensor1_val = 0;
sensor2_val = 0;
//setup the analog pins as input, probly redundant
pinMode(SENSOR2, INPUT);
pinMode(SENSOR1, INPUT);
for (int i = 0; i < 3; i++) {
pinMode(LEDpins[i], OUTPUT);
}
pinMode(LampPin, OUTPUT);
pinMode(DimmerPin, OUTPUT);
pinMode(BUTTON1, INPUT);
// Enable Serial output.
#ifdef DEBUG_PRINT
Serial.begin(57600);
#endif //DEBUG_PRINT
// Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
#ifdef DEBUG_WIFI_CONNECTION
//Serial.println("***WiShield Starting***");
//WiServer.enableVerboseMode(true); //Ask WiServer to generate log messages (optional).
#endif
// Have the printData function called when data is returned by the server during POSTrequests
postPachube.setReturnFunc(printData);
// Have the printGetData function called when data is returned by the server during GETrequests
getPachube.setReturnFunc(printGetData);
//getAlarm.setReturnFunc(printGetAlarm);
//set up global state data
inValidPacket = false;
memset(recvData, 0, 256 * sizeof(char));
//intTimer = false;
//cycle = 0;
//setup the timerISR to be called every minute
//timer.setInterval(30000, timerISR); // 60000ms/1min period //DON't USE AN INTERVAL LESS THAN THIS. CAUSES IRRATIC SERVER CALLS. PACHUBE WILL RESPOND ONCE EVERY 3 MIN.
//MsTimer2::start();
}
//Main loop function.
//--------------------------------------------------------------
void loop() {
if(millis() - lastMillis > updateTime) {
//time inverval has passed
lastMillis = millis();
//For now just increment the variables. In the future connect them to actual sensors.
post_number++;
sensor1_val = convertTemp(analogRead(SENSOR1));
sensor2_val = analogRead(SENSOR2);
//Button = analogRead(BUTTON1);
#ifdef DEBUG_PRINT
Serial.println("===== Timer Fired =====");
Serial.print("Post Number: ");
Serial.print(post_number, DEC);
Serial.print(" Temp: ");
Serial.print(sensor1_val, DEC);
Serial.print("F, Pressure: ");
Serial.print(sensor2_val, DEC);
Serial.print(", Intesity: ");
Serial.println(newAlarm);
#endif //DEBUG_PRINT
//submit tasks for WiServer
postPachube.submit();
getPachube.submit();
}
WiServer.server_task();
delay(10);
}
//Function for displaying an integery value as a scale of LEDs.
//--------------------------------------------------------------
/*void displayLED(int bright) {
#ifdef DEBUG_SENSOR
Serial.print("Bright: ");
Serial.println(bright, DEC);
#endif //DEBUG_PRINT
//clear the LED display
for (int i = 0; i < 3; i++) {
digitalWrite(LEDpins[i], LOW);
}
//add each LED as appropriate
if(bright > 18) {
digitalWrite(LEDpins[0], HIGH);
}
if(bright > 20) {
digitalWrite(LEDpins[1], HIGH);
}
if(bright > 22) {
digitalWrite(LEDpins[2], HIGH);
}
}
//Function for turning on the light at the appropriate time.
//--------------------------------------------------------------
void displayLight(int newAlarm, int Button) {
#ifdef DEBUG_PRINT
Serial.println("*** Light Logic ***");
#endif
//do this all the time. Writes the intensity from the server to the dimmer pin. Expects a value from 0-255
analogWrite(DimmerPin, newAlarm);
//switch alarm and include logic for a local kill button
if (Alarm == 0 && newAlarm > 0) {
if (Lamp == 0) { //checks that the local switch didn't already turn on the lamp
Lamp = !Lamp;
#ifdef DEBUG_PRINT
Serial.println("***Light ON***");
#endif
digitalWrite(LampPin, HIGH);
}
Alarm = newAlarm;
}
if (Alarm > 0 && newAlarm == 0) {
if (Lamp > 0) { //checks that the local switch didn't already kill the lamp
Lamp = !Lamp;
#ifdef DEBUG_PRINT
Serial.println("***Light OFF***");
#endif
digitalWrite(LampPin, LOW);
}
Alarm = newAlarm;
}
if (Button > 100) {
//button is not currently implemented, but this would be the place
Lamp = !Lamp;
//fade function
if(Lamp == 0) {
digitalWrite(LampPin, LOW);
#ifdef DEBUG_PRINT
Serial.println("***Light OFF***");
#endif
}
else {
digitalWrite(LampPin, HIGH);
#ifdef DEBUG_PRINT
Serial.println("***Light ON***");
#endif
}
}
}*/
/*
http://www.arduino.cc/playground/ComponentLib/Thermistor2
(Ground) ---- (10k-Resistor) -------|------- (Thermistor) ---- (+5v)
|
Analog Pin 0
*/
int convertTemp(int value)
{
double temp;
temp = log(((10240000 / value) - 10000));
temp = 1 / (0.001129148 + (0.000234125 * temp) + (0.0000000876741 * temp * temp * temp));
temp = temp - 273.15; // Convert Kelvin to Celcius
temp = (temp * 9.0) / 5.0 + 32.0; // Convert Celcius to Fahrenheit
return (int)temp;
}
/*
WiShield Pin Use:
D2 - Interupt (optionally can switch to D8)
D7 - SS for flash
D9 - LED (optional)
D10 - SS
D11 - Out
D12 - In
D13 - CLK
A0-SENSOR1(Temp)
A1-SENSOR2(Pressure)
D3-LED
D4-LED
D5-LED
D6-Lamp
D8-Button(not in use)
*/
/*Post Pachube & Get OwenSchoppe.com
*
* Thanks to:
* GregEigsti and ChiefRobot for writing the get, post, and parse code. 5/28/2010
* Shard7 for ConnectionFix 11/12/10
*
*POSTS to Pachube and GETS from owenschoppe.com.
*/
#include <WiServer.h>
//Wireless configuration defines ----------------------------------------
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
//Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,108}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,254}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"2WIRE814"}; // max 32 bytes
unsigned char security_type = 1; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"YOUR WPA PASSPHRASE"}; // max 64 characters
// WEP 64-bit keys
//Change the length on line 337 in the g2100.c file: cmd->keyLen = 5; // Key length: 5 bytes (64-bit WEP); 13 bytes (128-bit WEP)
prog_uchar wep_keys[] PROGMEM = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, // Key 0 Shortened the keys to match my router settings.
0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
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
//--------------------------------------------------------------
//Pachube
//--------------------------------------------------------------
// IP Address for Pachube.com
uint8 ip[] = {173,203,98,29};
char hostName[] = "www.pachube.com\nX-PachubeApiKey: XXXX\nConnection: close";
char url[] = "/api/XXXX.csv?_method=put";
// A request that POSTS data to Pachube
POSTrequest postPachube(ip, 80, hostName, url, feedData);
// Change the url to get data from a different feed. (This would be set to my feed number to get data from me.)
char hostName2[] = "www.pachube.com";
char url2[] = "/api/feeds/XXXX.csv";
GETrequest getPachube(ip, 80, hostName, url2);
// End of Pachube setup.
//--------------------------------------------------------------
// Global constants:
//--------------------------------------------------------------
#define DEBUG_PRINT
#define DEBUG_WIFI_CONNECTION
#define SENSOR2 1
#define SENSOR1 0
int LEDpins[] = {3,4,5}; //right to left
// Global variables:
//--------------------------------------------------------------
boolean inValidPacket;
char recvData[256];
unsigned long updateTime = 30000;
unsigned long lastMillis = 0;
// Sensor Data Variables:
//--------------------------------------------------------------
int post_number;
int sensor1_val;
int sensor2_val;
//Incoming Data Variables: Use an appopriate number of variables for your CSV values.
//--------------------------------------------------------------
int var1;
int var2;
int var3;
//definitions for lamp
//--------------------------------------------------------------
uint8 ip2[] = {173,201,242,1};
// IP Address for Owenschoppe.com
char hostName3[] = "www.owenschoppe.com";
char url3[] = "XXXX";
// A request that POSTS data to server
GETrequest getAlarm(ip2, 80, hostName3, url3);
int LampPin = 6; //lamp
int newAlarm = 0;
int Alarm = 0;
int DimmerPin = 3;
boolean Lamp = false;
#define BUTTON1 8
int Button = 0;
/*int hours;
int minutes;
int seconds;
int currentTime;
int timeON = 475; //in minutes from midnight
int timeOFF = 600;
boolean timeOut = true;*/
// 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()
{
#ifdef DEBUG_PRINT
Serial.println("feedData");
#endif
char buf[16];
//To use more variable just follow the pattern.
sprintf(buf, "%d,%d,%d", post_number, sensor1_val, sensor2_val);
WiServer.print(buf);
}
// Function that prints data from the server during POSTrequests
//--------------------------------------------------------------
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.
while (len-- > 0) {
Serial.print(*(data++));
}
}
// Function that prints data from the server during GETrequests
// Parsing the data takes three stages:
// 1. Data is received in chunks. The last chunk is stored in recvData.
// 2. recvData is split into individual lines.
// 3. The last line, which always contains the csv data, is split into individual values.
//--------------------------------------------------------------
//--------------------------------------------------------------
void printGetAlarm(char* data, int len) {
#ifdef DEBUG_PRINT
Serial.println("printGetAlarm");
#endif
//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;
#ifdef DEBUG_PRINT
//temporary for debugging purposes
Serial.println("Data reception has started");
//Serial.println(len);
#endif
}
//if we were in a valid packet AND we get the ending 0 == len call
//This is executed once after receiving the last chunck of data.
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()
parseAlarm(recvData);
#ifdef DEBUG_PRINT
//temporary for debugging purposes
Serial.println("Data reception has ended");
//Serial.println(data);
#endif
}
else {
//insures that the data in recvData is clean and doesn't have leftovers from before
memset(recvData, 0, 256 * sizeof(char));
//stash away the last bit of data received
memcpy(recvData, data, len);
#ifdef DEBUG_PRINT //temporary for debugging purposes
Serial.println("-------- Received a chunk of Alarm data: --------");
Serial.println(recvData);
//inString = recvData;
//Serial.println(data);
//Serial.println("Last chunk of data:");
//Serial.print("Length: ");
//Serial.println(len);
Serial.println("-------- End of Chunk --------");
#endif //DEBUG_PRINT
}
}
//--------------------------------------------------------------
void parseAlarm(char* Data) {
#ifdef DEBUG_PRINT
Serial.println("parseAlarm");
#endif
const char *ptr = Data;
char field [ 64 ];
int n;
int x;
//steps through the recvData saving the last line in 'field' and the number of characters read in 'n'.
while ( sscanf(ptr, "%64[^\n]%n", field, &n) == 1 )
{
#ifdef DEBUG_PRINT
//printf("field = \"%s\"\n", field);
//Serial.print ("Field = ");
//Serial.println(field);
#endif //DEBUG_PRINT
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
}
}
//Store each comma separated value (CSV) in the associated variable
//To use more variable just follow the pattern.
//sscanf looks for comma separated integers (%d , %d). For longer numbers or floats use a different data type.
//Don't forget to change the data type of your variables to match.
//sscanf(field, "%d,%d" , &var1, &var2 );
sscanf(field, "%d" , &newAlarm);
//#ifdef DEBUG_PRINT
Serial.print ("newAlarm = "); //print results
Serial.println(newAlarm);
//#endif //DEBUG_PRINT
}
/*void printGetData(char* data, int len) {
#ifdef DEBUG_PRINT
Serial.println("printGetData");
#endif
//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;
#ifdef DEBUG_PRINT
//temporary for debugging purposes
Serial.println("Data reception has started");
//Serial.println(len);
#endif
}
//if we were in a valid packet AND we get the ending 0 == len call
//This is executed once after receiving the last chunck of data.
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()
parseData(recvData);
#ifdef DEBUG_PRINT
//temporary for debugging purposes
Serial.println("Data reception has ended");
//Serial.println(data);
#endif
}
else {
//insures that the data in recvData is clean and doesn't have leftovers from before
memset(recvData, 0, 256 * sizeof(char));
//stash away the last bit of data received
memcpy(recvData, data, len);
#ifdef DEBUG_PRINT //temporary for debugging purposes
Serial.println("-------- Received a chunk of Pachube data: --------");
Serial.println(recvData);
//inString = recvData;
//Serial.println(data);
//Serial.println("Last chunk of data:");
//Serial.print("Length: ");
//Serial.println(len);
Serial.println("-------- End of Chunk --------");
#endif //DEBUG_PRINT
}
}
//Function that parses the last packet of data received.
//--------------------------------------------------------------
void parseData(char* Data) {
#ifdef DEBUG_PRINT
Serial.println("parseData");
#endif
const char *ptr = Data;
char field [ 64 ];
int n;
int x;
//steps through the recvData saving the last line in 'field' and the number of characters read in 'n'.
while ( sscanf(ptr, "%64[^\n]%n", field, &n) == 1 )
{
#ifdef DEBUG_PRINT
//printf("field = \"%s\"\n", field);
//Serial.print ("Field = ");
//Serial.println(field);
#endif //DEBUG_PRINT
ptr += n;
if ( *ptr != '\n' )
{
break;
}
while ( *ptr == '\n' )
{
++ptr;
}
}
//Store each comma separated value (CSV) in the associated variable
//To use more variable just follow the pattern.
//sscanf looks for comma separated integers (%d , %d). For longer numbers or floats use a different data type.
//Don't forget to change the data type of your variables to match.
//sscanf(field, "%d,%d" , &var1, &var2 );
sscanf(field, "%d,%d,%d," , &var1, &var2, &var3 );
#ifdef DEBUG_PRINT
Serial.print ("Var1 = "); //print results
Serial.println(var1);
Serial.print ("Var2 = ");
Serial.println(var2);
Serial.print ("Var3 = ");
Serial.println(var3);
#endif //DEBUG_PRINT
}*/
//Setup function
//--------------------------------------------------------------
void setup() {
//initialize sensor variables
post_number = 0;
sensor1_val = 0;
sensor2_val = 0;
//setup the analog pins as input, probly redundant
pinMode(SENSOR2, INPUT);
pinMode(SENSOR1, INPUT);
for (int i = 0; i < 3; i++) {
pinMode(LEDpins[i], OUTPUT);
}
pinMode(LampPin, OUTPUT);
pinMode(DimmerPin, OUTPUT);
pinMode(BUTTON1, INPUT);
// Enable Serial output.
#ifdef DEBUG_PRINT
Serial.begin(57600);
#endif //DEBUG_PRINT
// Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
#ifdef DEBUG_WIFI_CONNECTION
//Serial.println("***WiShield Starting***");
//WiServer.enableVerboseMode(true); //Ask WiServer to generate log messages (optional).
#endif
// Have the printData function called when data is returned by the server during POSTrequests
postPachube.setReturnFunc(printData);
// Have the printGetData function called when data is returned by the server during GETrequests
//getPachube.setReturnFunc(printGetData);
getAlarm.setReturnFunc(printGetAlarm);
//set up global state data
inValidPacket = false;
memset(recvData, 0, 256 * sizeof(char));
//intTimer = false;
//cycle = 0;
//setup the timerISR to be called every minute
//timer.setInterval(30000, timerISR); // 60000ms/1min period //DON't USE AN INTERVAL LESS THAN THIS. CAUSES IRRATIC SERVER CALLS. PACHUBE WILL RESPOND ONCE EVERY 3 MIN.
//MsTimer2::start();
}
//Main loop function.
//--------------------------------------------------------------
void loop() {
if(millis() - lastMillis > updateTime) {
//time inverval has passed
lastMillis = millis();
//For now just increment the variables. In the future connect them to actual sensors.
post_number++;
sensor1_val = convertTemp(analogRead(SENSOR1));
sensor2_val = analogRead(SENSOR2);
//Button = analogRead(BUTTON1);
#ifdef DEBUG_PRINT
Serial.println("===== Timer Fired =====");
Serial.print("Post Number: ");
Serial.print(post_number, DEC);
Serial.print(" Temp: ");
Serial.print(sensor1_val, DEC);
Serial.print("F, Pressure: ");
Serial.print(sensor2_val, DEC);
Serial.print(", Intesity: ");
Serial.println(newAlarm);
#endif //DEBUG_PRINT
//submit tasks for WiServer
postPachube.submit();
//getPachube.submit();
getAlarm.submit();
}
WiServer.server_task();
delay(10);
}
//Function for displaying an integery value as a scale of LEDs.
//--------------------------------------------------------------
/*void displayLED(int bright) {
#ifdef DEBUG_SENSOR
Serial.print("Bright: ");
Serial.println(bright, DEC);
#endif //DEBUG_PRINT
//clear the LED display
for (int i = 0; i < 3; i++) {
digitalWrite(LEDpins[i], LOW);
}
//add each LED as appropriate
if(bright > 18) {
digitalWrite(LEDpins[0], HIGH);
}
if(bright > 20) {
digitalWrite(LEDpins[1], HIGH);
}
if(bright > 22) {
digitalWrite(LEDpins[2], HIGH);
}
}
//Function for turning on the light at the appropriate time.
//--------------------------------------------------------------
void displayLight(int newAlarm, int Button) {
#ifdef DEBUG_PRINT
Serial.println("*** Light Logic ***");
#endif
//do this all the time. Writes the intensity from the server to the dimmer pin. Expects a value from 0-255
analogWrite(DimmerPin, newAlarm);
//switch alarm and include logic for a local kill button
if (Alarm == 0 && newAlarm > 0) {
if (Lamp == 0) { //checks that the local switch didn't already turn on the lamp
Lamp = !Lamp;
#ifdef DEBUG_PRINT
Serial.println("***Light ON***");
#endif
digitalWrite(LampPin, HIGH);
}
Alarm = newAlarm;
}
if (Alarm > 0 && newAlarm == 0) {
if (Lamp > 0) { //checks that the local switch didn't already kill the lamp
Lamp = !Lamp;
#ifdef DEBUG_PRINT
Serial.println("***Light OFF***");
#endif
digitalWrite(LampPin, LOW);
}
Alarm = newAlarm;
}
if (Button > 100) {
//button is not currently implemented, but this would be the place
Lamp = !Lamp;
//fade function
if(Lamp == 0) {
digitalWrite(LampPin, LOW);
#ifdef DEBUG_PRINT
Serial.println("***Light OFF***");
#endif
}
else {
digitalWrite(LampPin, HIGH);
#ifdef DEBUG_PRINT
Serial.println("***Light ON***");
#endif
}
}
}*/
/*
http://www.arduino.cc/playground/ComponentLib/Thermistor2
(Ground) ---- (10k-Resistor) -------|------- (Thermistor) ---- (+5v)
|
Analog Pin 0
*/
int convertTemp(int value)
{
double temp;
temp = log(((10240000 / value) - 10000));
temp = 1 / (0.001129148 + (0.000234125 * temp) + (0.0000000876741 * temp * temp * temp));
temp = temp - 273.15; // Convert Kelvin to Celcius
temp = (temp * 9.0) / 5.0 + 32.0; // Convert Celcius to Fahrenheit
return (int)temp;
}
/*
WiShield Pin Use:
D2 - Interupt (optionally can switch to D8)
D7 - SS for flash
D9 - LED (optional)
D10 - SS
D11 - Out
D12 - In
D13 - CLK
A0-SENSOR1(Temp)
A1-SENSOR2(Pressure)
D3-LED
D4-LED
D5-LED
D6-Lamp
D8-Button(not in use)
*/
/*Get Pachube & Get Owenschoppe.com
*
* Thanks to:
* GregEigsti and ChiefRobot for writing the get, post, and parse code. 5/28/2010
* Shard7 for ConnectionFix 11/12/10
*
*Gets from both Pachube and Owenschoppe.com but can't post.
*/
#include <WiServer.h>
//Wireless configuration defines ----------------------------------------
#define WIRELESS_MODE_INFRA 1
#define WIRELESS_MODE_ADHOC 2
//Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,108}; // IP address of WiShield
unsigned char gateway_ip[] = {192,168,1,254}; // router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0}; // subnet mask for the local network
const prog_char ssid[] PROGMEM = {"2WIRE814"}; // max 32 bytes
unsigned char security_type = 1; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2
// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"YOUR WPA PASSPHRASE"}; // max 64 characters
// WEP 64-bit keys
//Change the length on line 337 in the g2100.c file: cmd->keyLen = 5; // Key length: 5 bytes (64-bit WEP); 13 bytes (128-bit WEP)
prog_uchar wep_keys[] PROGMEM = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, // Key 0 Shortened the keys to match my router settings.
0x00, 0x00, 0x00, 0x00, 0x00, // Key 1
0x00, 0x00, 0x00, 0x00, 0x00, // Key 2
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
//--------------------------------------------------------------
//Pachube
//--------------------------------------------------------------
// IP Address for Pachube.com
uint8 ip[] = {173,203,98,29};
char hostName[] = "www.pachube.com\nX-PachubeApiKey: XXXX\nConnection: close";
char url[] = "/api/XXXX.csv?_method=put";
// A request that POSTS data to Pachube
POSTrequest postPachube(ip, 80, hostName, url, feedData);
// Change the url to get data from a different feed. (This would be set to my feed number to get data from me.)
char hostName2[] = "www.pachube.com";
char url2[] = "/api/feeds/XXXX.csv";
GETrequest getPachube(ip, 80, hostName, url2);
// End of Pachube setup.
//--------------------------------------------------------------
// Global constants:
//--------------------------------------------------------------
#define DEBUG_PRINT
#define DEBUG_WIFI_CONNECTION
#define SENSOR2 1
#define SENSOR1 0
int LEDpins[] = {3,4,5}; //right to left
// Global variables:
//--------------------------------------------------------------
boolean inValidPacket;
char recvData[256];
unsigned long updateTime = 30000;
unsigned long lastMillis = 0;
// Sensor Data Variables:
//--------------------------------------------------------------
int post_number;
int sensor1_val;
int sensor2_val;
//Incoming Data Variables: Use an appopriate number of variables for your CSV values.
//--------------------------------------------------------------
int var1;
int var2;
int var3;
//definitions for lamp
//--------------------------------------------------------------
uint8 ip2[] = {173,201,242,1};
// IP Address for Owenschoppe.com
char hostName3[] = "www.owenschoppe.com";
char url3[] = "XXXX";
// A request that POSTS data to server
GETrequest getAlarm(ip2, 80, hostName3, url3);
int LampPin = 6; //lamp
int newAlarm = 0;
int Alarm = 0;
int DimmerPin = 3;
boolean Lamp = false;
#define BUTTON1 8
int Button = 0;
//int hours;
//int minutes;
//int seconds;
//int currentTime;
//int timeON = 475; //in minutes from midnight
//int timeOFF = 600;
//boolean timeOut = true;
/*void timerISR() {
intTimer = true;
}*/
// 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()
{
#ifdef DEBUG_PRINT
Serial.println("feedData");
#endif
char buf[16];
//To use more variable just follow the pattern.
sprintf(buf, "%d,%d,%d", post_number, sensor1_val, sensor2_val);
WiServer.print(buf);
}
// Function that prints data from the server during POSTrequests
//--------------------------------------------------------------
/*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.
while (len-- > 0) {
Serial.print(*(data++));
}
}*/
// Function that prints data from the server during GETrequests
// Parsing the data takes three stages:
// 1. Data is received in chunks. The last chunk is stored in recvData.
// 2. recvData is split into individual lines.
// 3. The last line, which always contains the csv data, is split into individual values.
//--------------------------------------------------------------
void printGetAlarm(char* data, int len) {
#ifdef DEBUG_PRINT
Serial.println("printGetAlarm");
#endif
//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;
#ifdef DEBUG_PRINT
//temporary for debugging purposes
Serial.println("Data reception has started");
//Serial.println(len);
#endif
}
//if we were in a valid packet AND we get the ending 0 == len call
//This is executed once after receiving the last chunck of data.
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()
parseAlarm(recvData);
#ifdef DEBUG_PRINT
//temporary for debugging purposes
Serial.println("Data reception has ended");
//Serial.println(data);
#endif
}
else {
//insures that the data in recvData is clean and doesn't have leftovers from before
memset(recvData, 0, 256 * sizeof(char));
//stash away the last bit of data received
memcpy(recvData, data, len);
#ifdef DEBUG_PRINT //temporary for debugging purposes
Serial.println("-------- Received alarm data: --------");
Serial.println(recvData);
//inString = recvData;
//Serial.println(data);
//Serial.println("Last chunk of data:");
//Serial.print("Length: ");
//Serial.println(len);
Serial.println("-------- End of Chunk --------");
#endif //DEBUG_PRINT
}
}
//--------------------------------------------------------------
void parseAlarm(char* Data) {
#ifdef DEBUG_PRINT
Serial.println("parseAlarm");
#endif
const char *ptr = Data;
char field [ 64 ];
int n;
int x;
//steps through the recvData saving the last line in 'field' and the number of characters read in 'n'.
while ( sscanf(ptr, "%64[^\n]%n", field, &n) == 1 )
{
#ifdef DEBUG_PRINT
//printf("field = \"%s\"\n", field);
//Serial.print ("Field = ");
//Serial.println(field);
#endif //DEBUG_PRINT
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 */
}
}
//Store each comma separated value (CSV) in the associated variable
//To use more variable just follow the pattern.
//sscanf looks for comma separated integers (%d , %d). For longer numbers or floats use a different data type.
//Don't forget to change the data type of your variables to match.
//sscanf(field, "%d,%d" , &var1, &var2 );
sscanf(field, "%d" , &newAlarm);
//#ifdef DEBUG_PRINT
Serial.print ("newAlarm = "); //print results
Serial.println(newAlarm);
//#endif //DEBUG_PRINT
}
//--------------------------------------------------------------
void printGetData(char* data, int len) {
#ifdef DEBUG_PRINT
Serial.println("printGetData");
#endif
//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;
#ifdef DEBUG_PRINT
//temporary for debugging purposes
Serial.println("Data reception has started");
//Serial.println(len);
#endif
}
//if we were in a valid packet AND we get the ending 0 == len call
//This is executed once after receiving the last chunck of data.
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()
parseData(recvData);
#ifdef DEBUG_PRINT
//temporary for debugging purposes
Serial.println("Data reception has ended");
//Serial.println(data);
#endif
}
else {
//insures that the data in recvData is clean and doesn't have leftovers from before
memset(recvData, 0, 256 * sizeof(char));
//stash away the last bit of data received
memcpy(recvData, data, len);
#ifdef DEBUG_PRINT //temporary for debugging purposes
Serial.println("-------- Received Pachube data: --------");
Serial.println(recvData);
//inString = recvData;
//Serial.println(data);
//Serial.println("Last chunk of data:");
//Serial.print("Length: ");
//Serial.println(len);
Serial.println("-------- End of Chunk --------");
#endif //DEBUG_PRINT
}
}
//Function that parses the last packet of data received.
//--------------------------------------------------------------
void parseData(char* Data) {
#ifdef DEBUG_PRINT
Serial.println("parseData");
#endif
const char *ptr = Data;
char field [ 64 ];
int n;
int x;
//steps through the recvData saving the last line in 'field' and the number of characters read in 'n'.
while ( sscanf(ptr, "%64[^\n]%n", field, &n) == 1 )
{
#ifdef DEBUG_PRINT
//printf("field = \"%s\"\n", field);
//Serial.print ("Field = ");
//Serial.println(field);
#endif //DEBUG_PRINT
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
}
}
//Store each comma separated value (CSV) in the associated variable
//To use more variable just follow the pattern.
//sscanf looks for comma separated integers (%d , %d). For longer numbers or floats use a different data type.
//Don't forget to change the data type of your variables to match.
//sscanf(field, "%d,%d" , &var1, &var2 );
sscanf(field, "%d,%d,%d," , &var1, &var2, &var3 );
#ifdef DEBUG_PRINT
Serial.print ("Var1 = "); //print results
Serial.println(var1);
Serial.print ("Var2 = ");
Serial.println(var2);
Serial.print ("Var3 = ");
Serial.println(var3);
#endif //DEBUG_PRINT
}
//Setup function
//--------------------------------------------------------------
void setup() {
//initialize sensor variables
post_number = 0;
sensor1_val = 0;
sensor2_val = 0;
//setup the analog pins as input, probly redundant
pinMode(SENSOR2, INPUT);
pinMode(SENSOR1, INPUT);
for (int i = 0; i < 3; i++) {
pinMode(LEDpins[i], OUTPUT);
}
pinMode(LampPin, OUTPUT);
pinMode(DimmerPin, OUTPUT);
pinMode(BUTTON1, INPUT);
// Enable Serial output.
#ifdef DEBUG_PRINT
Serial.begin(57600);
#endif //DEBUG_PRINT
// Initialize WiServer (we'll pass NULL for the page serving function since we don't need to serve web pages)
#ifdef DEBUG_WIFI_CONNECTION
//Serial.println("***WiShield Starting***");
//WiServer.enableVerboseMode(true); //Ask WiServer to generate log messages (optional).
#endif
// Have the printData function called when data is returned by the server during POSTrequests
//postPachube.setReturnFunc(printData);
// Have the printGetData function called when data is returned by the server during GETrequests
getPachube.setReturnFunc(printGetData);
getAlarm.setReturnFunc(printGetAlarm);
//set up global state data
inValidPacket = false;
memset(recvData, 0, 256 * sizeof(char));
//intTimer = false;
//cycle = 0;
//setup the timerISR to be called every minute
//MsTimer2::set(30000, timerISR); // 60000ms/1min period //DON't USE AN INTERVAL LESS THAN THIS. CAUSES IRRATIC SERVER CALLS. PACHUBE WILL RESPOND ONCE EVERY 3 MIN.
//MsTimer2::start();
}
//Main loop function.
//--------------------------------------------------------------
void loop() {
if(millis() - lastMillis > updateTime) {
//time inverval has passed
lastMillis = millis();
//For now just increment the variables. In the future connect them to actual sensors.
post_number++;
//sensor1_val = convertTemp(analogRead(SENSOR1));
//sensor2_val = analogRead(SENSOR2);
//Button = analogRead(BUTTON1);
#ifdef DEBUG_PRINT
Serial.println("*** Timer Fired ***");
Serial.print("Post Number: ");
Serial.print(post_number, DEC);
Serial.print(" Temp: ");
Serial.print(sensor1_val, DEC);
Serial.print("F, Pressure: ");
Serial.print(sensor2_val, DEC);
Serial.print(", Intesity: ");
Serial.println(newAlarm);
#endif //DEBUG_PRINT
//Display the data received from Pachube.
//displayLED(var2);
//Use this for testing the sensor.
//sensor1_val = 1024 - analogRead(SENSOR1);
//displayLED(sensor1_val);
//displayLight(newAlarm, Button);
/*switch (cycle) {
case 0 :
if( !getPachube.isActive() && !getAlarm.isActive() ) {
Serial.println("Get Pachube");
getPachube.submit();
cycle++;
}
break;
case 1:
if( !getPachube.isActive() && !getAlarm.isActive() ) {
Serial.println("Get Alarm");
getAlarm.submit();
cycle = 0;
}
break;
}*/
//postPachube.submit();
getPachube.submit();
getAlarm.submit();
}
WiServer.server_task();
delay(10);
}
//Function for displaying an integery value as a scale of LEDs.
//--------------------------------------------------------------
/*void displayLED(int bright) {
#ifdef DEBUG_SENSOR
Serial.print("Bright: ");
Serial.println(bright, DEC);
#endif //DEBUG_PRINT
//clear the LED display
for (int i = 0; i < 3; i++) {
digitalWrite(LEDpins[i], LOW);
}
//add each LED as appropriate
if(bright > 18) {
digitalWrite(LEDpins[0], HIGH);
}
if(bright > 20) {
digitalWrite(LEDpins[1], HIGH);
}
if(bright > 22) {
digitalWrite(LEDpins[2], HIGH);
}
}*/
//Function for turning on the light at the appropriate time.
//--------------------------------------------------------------
void displayLight(int newAlarm, int Button) {
#ifdef DEBUG_PRINT
Serial.println("*** Light Logic ***");
#endif
//do this all the time. Writes the intensity from the server to the dimmer pin. Expects a value from 0-255
analogWrite(DimmerPin, newAlarm);
//switch alarm and include logic for a local kill button
if (Alarm == 0 && newAlarm > 0) {
if (Lamp == 0) { //checks that the local switch didn't already turn on the lamp
Lamp = !Lamp;
#ifdef DEBUG_PRINT
Serial.println("***Light ON***");
#endif
digitalWrite(LampPin, HIGH);
}
Alarm = newAlarm;
}
if (Alarm > 0 && newAlarm == 0) {
if (Lamp > 0) { //checks that the local switch didn't already kill the lamp
Lamp = !Lamp;
#ifdef DEBUG_PRINT
Serial.println("***Light OFF***");
#endif
digitalWrite(LampPin, LOW);
}
Alarm = newAlarm;
}
if (Button > 100) {
//button is not currently implemented, but this would be the place
Lamp = !Lamp;
//fade function
if(Lamp == 0) {
digitalWrite(LampPin, LOW);
#ifdef DEBUG_PRINT
Serial.println("***Light OFF***");
#endif
}
else {
digitalWrite(LampPin, HIGH);
#ifdef DEBUG_PRINT
Serial.println("***Light ON***");
#endif
}
}
}
/*
http://www.arduino.cc/playground/ComponentLib/Thermistor2
(Ground) ---- (10k-Resistor) -------|------- (Thermistor) ---- (+5v)
|
Analog Pin 0
*/
int convertTemp(int value)
{
double temp;
temp = log(((10240000 / value) - 10000));
temp = 1 / (0.001129148 + (0.000234125 * temp) + (0.0000000876741 * temp * temp * temp));
temp = temp - 273.15; // Convert Kelvin to Celcius
temp = (temp * 9.0) / 5.0 + 32.0; // Convert Celcius to Fahrenheit
return (int)temp;
}
/*
WiShield Pin Use:
D2 - Interupt (optionally can switch to D8)
D7 - SS for flash
D9 - LED (optional)
D10 - SS
D11 - Out
D12 - In
D13 - CLK
A0-SENSOR1(Temp)
A1-SENSOR2(Pressure)
D3-Fader
D4-LED
D5-LED
D6-Lamp
D8-Button(not in use)
*/
Return to Sketches and Applications
Users browsing this forum: No registered users and 2 guests