Advertisement
phillips321

posting to pachube

Sep 21st, 2011
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.48 KB | None | 0 0
  1. #include "EtherShield.h"
  2.  
  3. #define MY_MAC_ADDRESS {0x88,0x88,0x88,0x88,0x88,0x88}               // must be uniquely defined for all Nanodes, e.g. just change the last number
  4. #define USE_DHCP                                                     // comment out this line to use static network parameters
  5. #define PACHUBE_API_KEY "DSrZGziy2fmMYSECRETKEYMYSECRETKEYHKxqTFOYOs" // change this to your API key
  6. #define HTTPFEEDPATH "/v2/feeds/35969"                               // change this to th relative URL of your feed
  7.  
  8. #ifndef USE_DHCP // then you need to supply static network parameters, only if you are not using DHCP
  9.   #define MY_IP_ADDRESS {192,168,  1, 25}
  10.   #define MY_NET_MASK   {255,255,255,  0}
  11.   #define MY_GATEWAY    {192,168,  1,  1}
  12.   #define MY_DNS_SERVER {192,168,  1,  1}
  13. #endif
  14. int sensorValue1 = 0;
  15. // change the template to be consistent with your datastreams: see http://api.pachube.com/v2/
  16. #define FEED_POST_MAX_LENGTH 256
  17. static char feedTemplate[] = "{\"version\":\"1.0.0\",\"datastreams\":[{\"id\":\"temp\", \"current_value\":\"%d\"},]}";
  18. static char feedPost[FEED_POST_MAX_LENGTH] = {0};
  19. uint8_t fillOutTemplateWithSensorValues(uint8_t temp){  
  20.   snprintf(feedPost, FEED_POST_MAX_LENGTH, feedTemplate, temp); // this simply populates the current_value filed with temp
  21.   return (1);
  22. }
  23.  
  24. static uint8_t mymac[6] = MY_MAC_ADDRESS;
  25. static uint8_t websrvip[4]  = { 0,0,0,0 };
  26. static uint8_t dhcpsvrip[4] = { 0,0,0,0 };    
  27. static uint8_t myip[4]      = { 0,0,0,0 };
  28. static uint8_t mynetmask[4] = { 0,0,0,0 };
  29. static uint8_t gwip[4]      = { 0,0,0,0 };
  30. static uint8_t dnsip[4]     = { 0,0,0,0 };
  31.  
  32.  
  33. // global string buffer for hostname message:
  34. #define FEEDHOSTNAME "api.pachube.com\r\nX-PachubeApiKey: " PACHUBE_API_KEY
  35. #define FEEDWEBSERVER_VHOST "api.pachube.com"
  36. static char hoststr[150] = FEEDWEBSERVER_VHOST;
  37.  
  38. #define BUFFER_SIZE 550
  39. static uint8_t buf[BUFFER_SIZE+1];
  40.  
  41. EtherShield es=EtherShield();
  42.  
  43. void setup(){
  44.   Serial.begin(2400);
  45.   Serial.println("phillips321.co.uk Room Monitor");
  46.  
  47.   // Initialise SPI interface
  48.   es.ES_enc28j60SpiInit();
  49.  
  50.   // initialize ENC28J60
  51.   es.ES_enc28j60Init(mymac, 8);
  52.  
  53. #ifdef USE_DHCP
  54.   acquireIPAddress();
  55. #endif
  56.  
  57.   printNetworkParameters();
  58.  
  59.   //init the ethernet/ip layer:
  60.   es.ES_init_ip_arp_udp_tcp(mymac,myip, 80);
  61.  
  62.   // init the web client:
  63.   es.ES_client_set_gwip(gwip);  // e.g internal IP of dsl router
  64.   es.ES_dnslkup_set_dnsip(dnsip); // generally same IP as router
  65.  
  66.   Serial.println("Awaiting Client Gateway");
  67.   while(es.ES_client_waiting_gw()){
  68.     int plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
  69.     es.ES_packetloop_icmp_tcp(buf,plen);    
  70.   }
  71.   Serial.println("Client Gateway Complete, Resolving Host");
  72.  
  73.   resolveHost(hoststr, websrvip);
  74.   Serial.print("Resolved host: ");
  75.   Serial.print(hoststr);
  76.   Serial.print(" to IP: ");
  77.   printIP(websrvip);
  78.   Serial.println();
  79.  
  80.   es.ES_client_set_wwwip(websrvip);
  81. }
  82.  
  83. void loop(){
  84.   int plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
  85.   es.ES_packetloop_icmp_tcp(buf,plen);
  86.  
  87.   float temp = (analogRead(0) * .004882814) ;
  88.   temp = ((temp - 0.5) * 100) ;
  89.   Serial.println(temp);
  90.   delay(500);
  91.  
  92.   if(fillOutTemplateWithSensorValues(temp)){
  93.     es.ES_client_http_post(PSTR(HTTPFEEDPATH),PSTR(FEEDWEBSERVER_VHOST),PSTR(FEEDHOSTNAME), PSTR("PUT "), feedPost, &browserresult_callback );    
  94.   }
  95.  
  96. }
  97.  
  98. #ifdef USE_DHCP
  99. void acquireIPAddress(){
  100.   uint16_t dat_p;
  101.   long lastDhcpRequest = millis();
  102.   uint8_t dhcpState = 0;
  103.   Serial.println("Sending initial DHCP Discover");
  104.   es.ES_dhcp_start( buf, mymac, myip, mynetmask,gwip, dnsip, dhcpsvrip );
  105.  
  106.   while(1) {
  107.     // handle ping and wait for a tcp packet
  108.     int plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
  109.  
  110.     dat_p=es.ES_packetloop_icmp_tcp(buf,plen);
  111.     //    dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
  112.     if(dat_p==0) {
  113.       int retstat = es.ES_check_for_dhcp_answer( buf, plen);
  114.       dhcpState = es.ES_dhcp_state();
  115.       // we are idle here
  116.       if( dhcpState != DHCP_STATE_OK ) {
  117.         if (millis() > (lastDhcpRequest + 10000L) ){
  118.           lastDhcpRequest = millis();
  119.           // send dhcp
  120.           Serial.println("Sending DHCP Discover");
  121.           es.ES_dhcp_start( buf, mymac, myip, mynetmask,gwip, dnsip, dhcpsvrip );
  122.         }
  123.       }
  124.       else {
  125.         return;        
  126.       }
  127.     }
  128.   }  
  129. }
  130. #endif
  131.  
  132. // hostName is an input parameter, ipAddress is an outputParame
  133. void resolveHost(char *hostName, uint8_t *ipAddress){
  134.   es.ES_dnslkup_request(buf, (uint8_t*)hostName );
  135.   while(1){
  136.     int plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
  137.     es.ES_packetloop_icmp_tcp(buf,plen);  
  138.     if(es.ES_udp_client_check_for_dns_answer(buf, plen)) {
  139.       uint8_t *websrvipptr = es.ES_dnslkup_getip();
  140.       for(int on=0; on <4; on++ ) {
  141.         ipAddress[on] = *websrvipptr++;
  142.       }    
  143.       return;
  144.     }    
  145.   }
  146. }  
  147.  
  148.  
  149. // Output a ip address from buffer from startByte
  150. void printIP( uint8_t *buf ) {
  151.   for( int i = 0; i < 4; i++ ) {
  152.     Serial.print( buf[i], DEC );
  153.     if( i<3 )
  154.       Serial.print( "." );
  155.   }
  156. }
  157.  
  158. void printNetworkParameters(){
  159.   Serial.print( "My IP:   " );printIP( myip );Serial.println();
  160.   Serial.print( "Netmask: " ); printIP( mynetmask );Serial.println();
  161.   Serial.print( "DNS IP:  " );printIP( dnsip );Serial.println();
  162.   Serial.print( "GW IP:   " );printIP( gwip );Serial.println();  
  163. }
  164.  
  165. void browserresult_callback(uint8_t statuscode,uint16_t datapos){
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement