Problems with pachube update script

Discussions related to the WiServer add-on code for WiShield. WiServer is a friendly, easy-to-use front end for webserver and webclient usages.

Moderator: shard7

Re: Problems with pachube update script

Postby sandeen » Fri Oct 01, 2010 3:17 pm

Hm most of those non-wiserver examples are pretty thin.

I don't suppose you have a uip sample somewhere that does an http PUT? ;)

-Eric

(p.s. slacklab seems down atm)
sandeen
 
Posts: 12
Joined: Mon Sep 20, 2010 6:58 am

Re: Problems with pachube update script

Postby GregEigsti » Fri Oct 01, 2010 6:46 pm

Thanks for the slacklab warning - mysql seems to be crashing a lot lately :(

I don't have a uIP sample that puts/posts - but it should not be too difficult ;)

  • Build up a buffer with the basic body of the put/post
  • In your loop rely upon a timer, etc., to send a put/post every n minutes/hours/days/etc (or every time some event occurs).
  • Gather data from your sensors, etc., and put it into your put/post buffer (sprintf() works nicely for this)
  • Send the put/post data to pachube

If I get some time I'll look at what is necessary to get this going. Do me a favor and refresh me with what your goals for this project are.

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: Problems with pachube update script

Postby sandeen » Fri Oct 01, 2010 8:26 pm

Yep the goal is to push 3 ints to pachube once per minute ;) (I'm doing whole-house power measurement, including solar, so I push solar, house, and net utility in/out)

In pseudocode, it's:

Code: Select all
// init wishield variables etc

void loop()
{

start = millis();
for (int i = 0; i < NUMBER_OF_CTS; i++)
{
   for (int n = 0; n < numberOfSamples; n++)
   {
       // sample, filter, accumulate voltage and CT current readings
   } // End of V/I samples loop

  // frob the power values a little more

} // End of CTs loop

// Check if it's time to send an external update of averages
if (millis() >= updateTime) {

   // Do another update PACHUBE_UPDATE_INTERVAL from now
   updateTime += PACHUBE_UPDATE_INTERVAL;
   // Get average power info since last external update

   // today for the wiserver I do:

   response_done = 0;
   // This uses a PUT to send the 3 values up to pachube
   postPachube.submit();
   // We know we have net traffic now; give server_task() some concentrated love
   do {
     WiServer.server_task();
     delay(10);
   } while (!response_done);

}


I can post the whole sketch if you're interested; my problem just seems to lie with the wiserver flaking out, and if lower level interfaces are more reliable, I'll try that again.

Right now my only network interaction is that push to pachube.

Thanks,
-Eric
sandeen
 
Posts: 12
Joined: Mon Sep 20, 2010 6:58 am

Re: Problems with pachube update script

Postby GregEigsti » Sat Oct 02, 2010 9:38 am

Ok, that should be pretty easy to do. Among other things I log power usage to Pachube (Phase 1, 2 and total) - however I have a Win32 server in the middle. The server polls the Arduino/WiShield, logs the data to MySQL, then sends it on to Pachube. Should be "trivial" to port that code to uIP - its standard BSD socket code which can be mapped to uIP functions. If anything it will serve as a nice template for what needs to be done on the WiShield. Gimme a bit and I'll post that hunk o' code.

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: Problems with pachube update script

Postby sandeen » Sat Oct 02, 2010 10:02 am

Thanks Greg. So what level of API do you use with your wishield that it stays online?

FWIW I have a couple perl scripts to pull data back off pchube and send it to google PowerMeter if you're interested; it's pretty slick :)
sandeen
 
Posts: 12
Joined: Mon Sep 20, 2010 6:58 am

Re: Problems with pachube update script

Postby sandeen » Sat Oct 02, 2010 10:07 am

Oh you have it in mysql, even easier, one script goes from mysql straight to powermeter...
sandeen
 
Posts: 12
Joined: Mon Sep 20, 2010 6:58 am

Re: Problems with pachube update script

Postby GregEigsti » Sat Oct 02, 2010 10:17 am

I play at the uIP level - its closest to BSD sockets (where I am comfy) and allows the most flexibility.

Now this is Win32 sockets code so it won't translate directly to uIP but it may help... It should at least show how to do a Pachube PUT (how to build up a PUT packet). Gahhh, watch out for the memory leaks on failure :oops: Hmmm, also looks like I am hardcoding the content length in the PUT packet to 1 - this should probably be made dynamic and correct... Yeah... looks like this code could use a re-visit ;)

Code: Select all
#define PACKETSIZE 1024

void someOtherFunc()
{
   ...
   //post the data to Pachube
   sendPUTPachube("173.203.98.29", 80, "www.pachube.com", NULL, 2557, phase1, phase2);
   ...
}

bool sendPUTPachube(char *ipAddr, int port, char* hostName, char* URL, int feed, float phase1, float phase2)
{
   int retval;
   struct sockaddr_in server;
   struct hostent *hp;
   int  conn_socket;
   
   char *pSendBuf = new char[PACKETSIZE];
   if(!pSendBuf) {
      printf("could not allocate packet memory.\n");
      return false;
   }
   
   char *pRecvBuf = new char[PACKETSIZE];
   if(!pRecvBuf) {
      printf("could not allocate packet memory.\n");
      return false;
   }
   
   hp = gethostbyname(ipAddr);   
   if(!hp) {
      printf("Client cannot resolve address [%s].\n", ipAddr);
      return false;
   }
   
   // Copy the resolved information into the sockaddr_in structure
   memset(&server, 0, sizeof(server));
   memcpy(&(server.sin_addr), hp->h_addr, hp->h_length);
   server.sin_family = hp->h_addrtype;
   server.sin_port = htons(port);
   
   conn_socket = socket(AF_INET, SOCK_STREAM ,0); // Open a socket
   if (conn_socket < 0) {
      printf("Error Opening socket\n");
      return false;
   }
   
   //printf("Client connecting to: %s.\n", hp->h_name);
   
   if(-1 == connect(conn_socket, (struct sockaddr*)&server, sizeof(server))) {
      printf("connect(conn_socket) failed.\n");
      return false;
   }
   
   // fill the packet with 'data'
   memset(pSendBuf, '0', PACKETSIZE * sizeof(char));
   _snprintf_s(pSendBuf, PACKETSIZE, PACKETSIZE, "PUT /api/%d.csv HTTP/1.1\nHost: www.pachube.com\nX-PachubeApiKey: <YOUR_PACHUBE_KEY>\nContent-Length: 1\nConnection: close\n\n%f,%f,%f\n", feed, phase1, phase2, phase1 + phase2);
   //printf("%s\n", pSendBuf);
   
   retval = send(conn_socket, pSendBuf, strlen(pSendBuf), 0);
   if (-1 == retval) {
      printf("send(conn_socket) failed.\n");
      return false;
   }
   
   retval = recv(conn_socket, pRecvBuf, PACKETSIZE,0 );
   if (-1 == retval) {
      printf("recv(conn_socket) failed.\n");
      //shutdown(conn_socket, SHUT_RDWR);
      closesocket(conn_socket);
      return false;
   }
   
   /*
   printf("\n======================================================================\n");
   if(NULL != strstr(pRecvBuf, "HTTP/1.1 200 OK"))
      printf("Received OK\n");
   else
      printf("Did not receive OK\n");
   printf("\n======================================================================\n");
   printf("sendPUTPachube Received:\n");
   printf("%s\n", pRecvBuf);
   printf("\n======================================================================\n");
   */

   //shutdown(conn_socket, SHUT_RDWR);
   closesocket(conn_socket);
   
   delete pSendBuf;
   delete pRecvBuf;
   
   return true;
}
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: Problems with pachube update script

Postby sandeen » Tue Oct 19, 2010 10:17 pm

I'm trying things at a slightly lower level, using WebClient (non-wiserver) example as a guide, I'll post back if it stays online longer. Here's hoping!

(I tried switching to a wired ethernet shield, but -that- dropped my 5V analog ref, which threw all my readings out of calibration, argh).
sandeen
 
Posts: 12
Joined: Mon Sep 20, 2010 6:58 am

Re: Problems with pachube update script

Postby sandeen » Sun Oct 24, 2010 2:16 pm

Well, using it at the WebClient interface level certainly seems better... Still won't stay up/connected more than a couple of days. Grumble. Ok, maybe down to uip level next....
sandeen
 
Posts: 12
Joined: Mon Sep 20, 2010 6:58 am

Previous

Return to WiServer

Who is online

Users browsing this forum: No registered users and 1 guest