Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "EtherShield.h"
- #define MY_MAC_ADDRESS {0x88,0x88,0x88,0x88,0x88,0x88} // must be uniquely defined for all Nanodes, e.g. just change the last number
- #define USE_DHCP // comment out this line to use static network parameters
- #define PACHUBE_API_KEY "DSrZGziy2fmMYSECRETKEYMYSECRETKEYHKxqTFOYOs" // change this to your API key
- #define HTTPFEEDPATH "/v2/feeds/35969" // change this to th relative URL of your feed
- #ifndef USE_DHCP // then you need to supply static network parameters, only if you are not using DHCP
- #define MY_IP_ADDRESS {192,168, 1, 25}
- #define MY_NET_MASK {255,255,255, 0}
- #define MY_GATEWAY {192,168, 1, 1}
- #define MY_DNS_SERVER {192,168, 1, 1}
- #endif
- int sensorValue1 = 0;
- // change the template to be consistent with your datastreams: see http://api.pachube.com/v2/
- #define FEED_POST_MAX_LENGTH 256
- static char feedTemplate[] = "{\"version\":\"1.0.0\",\"datastreams\":[{\"id\":\"temp\", \"current_value\":\"%d\"},]}";
- static char feedPost[FEED_POST_MAX_LENGTH] = {0};
- uint8_t fillOutTemplateWithSensorValues(uint8_t temp){
- snprintf(feedPost, FEED_POST_MAX_LENGTH, feedTemplate, temp); // this simply populates the current_value filed with temp
- return (1);
- }
- static uint8_t mymac[6] = MY_MAC_ADDRESS;
- static uint8_t websrvip[4] = { 0,0,0,0 };
- static uint8_t dhcpsvrip[4] = { 0,0,0,0 };
- static uint8_t myip[4] = { 0,0,0,0 };
- static uint8_t mynetmask[4] = { 0,0,0,0 };
- static uint8_t gwip[4] = { 0,0,0,0 };
- static uint8_t dnsip[4] = { 0,0,0,0 };
- // global string buffer for hostname message:
- #define FEEDHOSTNAME "api.pachube.com\r\nX-PachubeApiKey: " PACHUBE_API_KEY
- #define FEEDWEBSERVER_VHOST "api.pachube.com"
- static char hoststr[150] = FEEDWEBSERVER_VHOST;
- #define BUFFER_SIZE 550
- static uint8_t buf[BUFFER_SIZE+1];
- EtherShield es=EtherShield();
- void setup(){
- Serial.begin(2400);
- Serial.println("phillips321.co.uk Room Monitor");
- // Initialise SPI interface
- es.ES_enc28j60SpiInit();
- // initialize ENC28J60
- es.ES_enc28j60Init(mymac, 8);
- #ifdef USE_DHCP
- acquireIPAddress();
- #endif
- printNetworkParameters();
- //init the ethernet/ip layer:
- es.ES_init_ip_arp_udp_tcp(mymac,myip, 80);
- // init the web client:
- es.ES_client_set_gwip(gwip); // e.g internal IP of dsl router
- es.ES_dnslkup_set_dnsip(dnsip); // generally same IP as router
- Serial.println("Awaiting Client Gateway");
- while(es.ES_client_waiting_gw()){
- int plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
- es.ES_packetloop_icmp_tcp(buf,plen);
- }
- Serial.println("Client Gateway Complete, Resolving Host");
- resolveHost(hoststr, websrvip);
- Serial.print("Resolved host: ");
- Serial.print(hoststr);
- Serial.print(" to IP: ");
- printIP(websrvip);
- Serial.println();
- es.ES_client_set_wwwip(websrvip);
- }
- void loop(){
- int plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
- es.ES_packetloop_icmp_tcp(buf,plen);
- float temp = (analogRead(0) * .004882814) ;
- temp = ((temp - 0.5) * 100) ;
- Serial.println(temp);
- delay(500);
- if(fillOutTemplateWithSensorValues(temp)){
- es.ES_client_http_post(PSTR(HTTPFEEDPATH),PSTR(FEEDWEBSERVER_VHOST),PSTR(FEEDHOSTNAME), PSTR("PUT "), feedPost, &browserresult_callback );
- }
- }
- #ifdef USE_DHCP
- void acquireIPAddress(){
- uint16_t dat_p;
- long lastDhcpRequest = millis();
- uint8_t dhcpState = 0;
- Serial.println("Sending initial DHCP Discover");
- es.ES_dhcp_start( buf, mymac, myip, mynetmask,gwip, dnsip, dhcpsvrip );
- while(1) {
- // handle ping and wait for a tcp packet
- int plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
- dat_p=es.ES_packetloop_icmp_tcp(buf,plen);
- // dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));
- if(dat_p==0) {
- int retstat = es.ES_check_for_dhcp_answer( buf, plen);
- dhcpState = es.ES_dhcp_state();
- // we are idle here
- if( dhcpState != DHCP_STATE_OK ) {
- if (millis() > (lastDhcpRequest + 10000L) ){
- lastDhcpRequest = millis();
- // send dhcp
- Serial.println("Sending DHCP Discover");
- es.ES_dhcp_start( buf, mymac, myip, mynetmask,gwip, dnsip, dhcpsvrip );
- }
- }
- else {
- return;
- }
- }
- }
- }
- #endif
- // hostName is an input parameter, ipAddress is an outputParame
- void resolveHost(char *hostName, uint8_t *ipAddress){
- es.ES_dnslkup_request(buf, (uint8_t*)hostName );
- while(1){
- int plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
- es.ES_packetloop_icmp_tcp(buf,plen);
- if(es.ES_udp_client_check_for_dns_answer(buf, plen)) {
- uint8_t *websrvipptr = es.ES_dnslkup_getip();
- for(int on=0; on <4; on++ ) {
- ipAddress[on] = *websrvipptr++;
- }
- return;
- }
- }
- }
- // Output a ip address from buffer from startByte
- void printIP( uint8_t *buf ) {
- for( int i = 0; i < 4; i++ ) {
- Serial.print( buf[i], DEC );
- if( i<3 )
- Serial.print( "." );
- }
- }
- void printNetworkParameters(){
- Serial.print( "My IP: " );printIP( myip );Serial.println();
- Serial.print( "Netmask: " ); printIP( mynetmask );Serial.println();
- Serial.print( "DNS IP: " );printIP( dnsip );Serial.println();
- Serial.print( "GW IP: " );printIP( gwip );Serial.println();
- }
- void browserresult_callback(uint8_t statuscode,uint16_t datapos){
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement