Pachube and WiShield

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

Re: Pachube and WiShield

Postby bj117 » Sat Jul 17, 2010 9:45 pm

Sorry - here it is...

Changes made to other files:

apps-conf.h in WiShield library:

//Here we include the header file for the application(s) we use in our project.
//#define APP_WEBSERVER
//#define APP_WEBCLIENT
//#define APP_SOCKAPP
//#define APP_UDPAPP
#define APP_WISERVER

g2100.c in WiShield library:

void zg_write_wep_key(U8* cmd_buf)
{
zg_wep_key_req_t* cmd = (zg_wep_key_req_t*)cmd_buf;

cmd->slot = 3; // WEP key slot
cmd->keyLen = 5; // Key length: 5 bytes (64-bit WEP); 13 bytes (128-bit WEP) - changed 13 to 5 here
cmd->defID = 0; // Default key ID: Key 0, 1, 2, 3
cmd->ssidLen = ssid_len;
.
.
.

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

#include <WiServer.h>
#include <MsTimer2.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,1,105};  // 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 = {"MYSSID"};    // 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 = {"12345678"};   // max 64 characters
// WEP 128-bit keys
prog_uchar wep_keys[] PROGMEM = {
/* replace key 0 with my 64 bit WEP key, also changed g2100.c WEP key length from 13 to 5 */
   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 TEMPERATURE_PIN 5
#define LIGHT_PIN       0

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


//MsTimer2 one minute timer ISR
void timerISR() {
   intTimer = true;
}

// 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)
//Your program will not want to use the hardcoded values below, rather it would read the
//sensor(s) and build the data string shown below.
void feedData()
{
   char buf[16];
   sprintf(buf, "%d,%d", temperature, light);
   WiServer.print(buf);
}

void setup()
{   
   temperature = 0;
   light = 0;

   //setup the analog pins as input, probly redundant
   pinMode(TEMPERATURE_PIN, INPUT);
   pinMode(LIGHT_PIN, INPUT);

   // 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);

   //set up global state data
   intTimer = false;

   //setup the timerISR to be called every minute
   MsTimer2::set(60000, timerISR); // 60000ms/1min period
   MsTimer2::start();
}


void loop()
{
   //--------------------------------------------------------------------------------------------
   //handle the timer overflow
   //--------------------------------------------------------------------------------------------
   if(true == intTimer) {
      //one minute has passed
      intTimer = false;
      temperature = convertTemp(analogRead(TEMPERATURE_PIN));
      light = 1024 - analogRead(LIGHT_PIN);
#ifdef DEBUG_PRINT
      Serial.print("===== intTimer fired: temperature ");
      Serial.print(temperature, DEC);
      Serial.print("F, light ");
      Serial.println(light, DEC);
#endif //DEBUG_PRINT
      postPachube.submit();   
   }

   // Run WiServer
   WiServer.server_task();

   delay(10);
}

/*
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;
}

/*
http://www.libelium.com/squidbee/index.php?title=Adding_a_light_sensor

(Ground) ---- (Light Sensor) -------|------- (1k-Resistor) ---- (+5v)
                                     |
                                Analog Pin 0
*/


Thanks.

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

Re: Pachube and WiShield

Postby GregEigsti » Sun Jul 18, 2010 8:21 am

Your code does not specifically show this but my guess is that you are somehow pulling in the Ethernet shield stuff in addition to the WiShield stuff. They are not compatible - that is the Ethernet shield and WiShield libraries are not plug and play with the other device. You mentioned using the "in_out" and the ethernet shield so I bring this up. If you look at what I am assuming to be the correct in_out sample (from Pachube) you will see a reference to the Client type at the top of the "ethernet_functions" file. Seems like that is getting pulled in maybe? I don't see any references to "Client" in your source. Is the "ethernet_functions" file part of your sketch?
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 » Sun Jul 18, 2010 8:47 am

That is a good deduction. Besides the code that I posted, I had the other two Pachube_in_out files (Ethernet functions and Pachube functions) in the same folder as this posted sketch. I was initially thinking I could use the Pachube_in_out example and just modify the way the ethernet connection is made (WiShield instead of Ethershield). I didn't remove them once I started working with your sketch. Removing the Pachube-related files from the folder with my modified version of your sketch let it compile successfully. Now I'll start to see if the posting is working correctly. Thanks a lot for your quick help.

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

Re: Pachube and WiShield

Postby GregEigsti » Sun Jul 18, 2010 9:30 am

I was initially thinking I could use the Pachube_in_out example and just modify the way the ethernet connection is made (WiShield instead of Ethershield)

An "Ethernet shield library" -> "WiShield library" translation layer would be nice - so anyone could use any sample ethernet shield sketch on a WiShield setup. Hrmmmm... The inclusion of "Client" (and ensuing error) was a dead giveaway ;)

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 bj117 » Sun Jul 18, 2010 3:34 pm

Following up on this, the sketch compiles and loads just fine. However, the WiShield doesn't seem to stay connected. I'm using WEP and have read it takes about 4 seconds for the led to come on, which is what I see. However, it goes off after about 13 seconds with Debug_Print undefined. While the light is on, I can ping the WiShield. If I click on the serial monitor during the 13 seconds the connection is active, the led will go out immediately, but then come on for a few more seconds before going out. If Debug_Print is defined, the led only comes on for a second. If I comment out the Wiserver.server.task() line in the loop section, the WiShield stays connected and I can see the temperature and light info posted to the Serial monitor with the DEBUG_PRINT command defined.

Any thoughts on why the WiShield seems to be disconnecting with DEBUG_PRINT defined (or undefined) and why the Wiserver.server.task seems to be connected with the loss of connection? Code is below:

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

#include <WiServer.h>
#include <MsTimer2.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,1,105};  // 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 = {"myssid"};    // 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 = {"12345678"};   // max 64 characters
// WEP 128-bit keys
prog_uchar wep_keys[] PROGMEM = {
   mywepkey,  // 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 TEMPERATURE_PIN 5
#define LIGHT_PIN       0

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


//MsTimer2 one minute timer ISR
void timerISR() {
   intTimer = true;
}

// 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)
//Your program will not want to use the hardcoded values below, rather it would read the
//sensor(s) and build the data string shown below.
void feedData()
{
   char buf[16];
   sprintf(buf, "%d,%d", temperature, light);
   WiServer.print(buf);
}

void setup()
{   
   temperature = 0;
   light = 0;

   //setup the analog pins as input, probly redundant
   pinMode(TEMPERATURE_PIN, INPUT);
   pinMode(LIGHT_PIN, INPUT);

   // 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);
   Serial.println("starting Serial output");
   //WiServer.enableVerboseMode(true);
#endif //DEBUG_PRINT

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

   //set up global state data
   intTimer = false;

   //setup the timerISR to be called every minute
   MsTimer2::set(60000, timerISR); // 60000ms/1min period
   MsTimer2::start();
}


void loop()
{
   //--------------------------------------------------------------------------------------------
   //handle the timer overflow
   //--------------------------------------------------------------------------------------------
   if(true == intTimer) {
      //one minute has passed
      intTimer = false;
      temperature = convertTemp(analogRead(TEMPERATURE_PIN));
      light = 1024 - analogRead(LIGHT_PIN);
#ifdef DEBUG_PRINT
      Serial.print("===== intTimer fired: temperature ");
      Serial.print(temperature, DEC);
      Serial.print("F, light ");
      Serial.println(light, DEC);
#endif //DEBUG_PRINT
      postPachube.submit();   
   }

   // Run WiServer
  //[color=#FF4040][color=#FF4000][/color]if I comment the next line out connection is not lost[/color]
   WiServer.server_task();

   delay(10);
}

/*
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;
}

/*
http://www.libelium.com/squidbee/index.php?title=Adding_a_light_sensor

(Ground) ---- (Light Sensor) -------|------- (1k-Resistor) ---- (+5v)
                                     |
                                Analog Pin 0
*/
bj117
 
Posts: 8
Joined: Tue Jul 06, 2010 11:16 pm

Re: Pachube and WiShield

Postby seacritter » Sun Jul 18, 2010 4:07 pm

try commenting out the color command...
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 bj117 » Sun Jul 18, 2010 4:56 pm

I tried to add the color command in the post, it isn't in the sketch I'm compiling.

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

Re: Pachube and WiShield

Postby GregEigsti » Tue Jul 20, 2010 10:33 am

I played with your sketch for longer than I had planned on and thought I was going crazy; could not get it to work reliably no way no how. So I loaded the SocketApp sketch and that worked for - good I'm not crazy ;)

I did not find anything definitive but you are missing the call to Serial.begin() in setup(). My guess is that in your editing of the original example code something critical was changed/removed.

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 bj117 » Tue Jul 20, 2010 12:17 pm

Greg - about missing the Serial.begin command in setup - the section below is cut from the code I posted above:

Code: Select all
void setup()
{   
   temperature = 0;
   light = 0;

   //setup the analog pins as input, probly redundant
   pinMode(TEMPERATURE_PIN, INPUT);
   pinMode(LIGHT_PIN, INPUT);

   // 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);
   Serial.println("starting Serial output");
   //WiServer.enableVerboseMode(true);
#endif //DEBUG_PRINT


The Serial.begin is in the ifdef block above. Were you saying I was missing this Serial.begin or should there be another somewhere?

Thanks for trying to sort this out, I'll start over with your original posted code and see if I have better luck. There are a few differences in that I'm using WEP and have to make the changes for my IP addresses, WEP key, feed #, etc, and changing the WEP key length in g2100.c. Just curious about what you saw when you tried the posted sketch?

I'll try the SocketApp to see if it works for me.

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

Re: Pachube and WiShield

Postby GregEigsti » Thu Jul 22, 2010 12:22 pm

about missing the Serial.begin command in setup - the section below is cut from the code I posted above...

Sorry, my error, must have gotten a bit snip happy :roll:
Just curious about what you saw when you tried the posted sketch?

Just random weird stuff as you described, working or not working once, etc. But the unmodified (except for wireless settings) socket app example worked. One thing I noticed is that the Windows telnet client (used telnet to test from the PC side) seemed to add the to issues; telnet in Mac OS seemed to be a better client.

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

PreviousNext

Return to Sketches and Applications

Who is online

Users browsing this forum: No registered users and 1 guest

cron