Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f22ed82280
Fetching contributors…

Cannot retrieve contributors at this time

file 364 lines (248 sloc) 8.614 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
/* ==============================
 * This code, which assumes you're using the official Arduino Ethernet shield,
 * updates a Pachube feed with your analog-in values and grabs values from a Pachube
 * feed - basically it enables you to have both "local" and "remote" sensors.
 *
 * Tested with Arduino 14
 *
 * Pachube is www.pachube.com - connect, tag and share real time sensor data
 * code by usman (www.haque.co.uk), may 2009
 * copy, distribute, whatever, as you like.
 *
 * v1.1 - added User-Agent & fixed HTTP parser for new Pachube headers
 * and check millis() for when it wraps around
 *
 * =============================== */



#include <Ethernet.h>
#include <string.h>
#include <SPI.h> // Add this through the menu -- Sketch | Import Library | SPI

#undef int() // needed by arduino 0011 to allow use of stdio
#include <stdio.h> // for function sprintf


// replace this with YOUR Pachube feed ID:
#define SHARE_FEED_ID 34225

// this is the ID of the remote Pachube feed that you want to connect to
#define REMOTE_FEED_ID 000

// ensure remoteSensor array is big enough to fit remote data streams
#define REMOTE_FEED_DATASTREAMS 0

// if the connection is good wait 10 seconds before updating again
#define UPDATE_INTERVAL 10000

// if connection fails/resets wait 10 seconds before trying again
#define RESET_INTERVAL 10000


// replace the Xs with your Pachube API key:
#define PACHUBE_API_KEY "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"


// replace the Xs with the MAC address of your Ethernet Shield:
byte mac[] = { 0xXX, 0xXX, 0xXX, 0xXX, 0xXX, 0xXX };


// provide an IP address for your Ethernet shield. The IP address below _might_ work:
byte ip[] = { 192, 168, 1, 110 };


byte subnet[] = {255, 255, 254,0};

// replace this with the internet IP address of your router:
byte gateway[]={127,0,0,1 };

// This should be the IP address for Pachube.com. Check Pachube to see if it has changed.
byte remoteServer[] = { 173,203,98,29 };


float remoteSensor[REMOTE_FEED_DATASTREAMS];


int geiger_input = 2;
long count = 0;
long countPerMinute = 0;
long timePrevious = 0;
long timePreviousMeassure = 0;
long time = 0;
long countPrevious = 0;
float radiationValue = 0.0;

//This is the conversion factor for the SBM-20 rediation detection tube
#define CONV_FACTOR 0.0057


Client localClient(remoteServer, 80);
unsigned int interval;


char buff[64];
int pointer = 0;

char pachube_data[70];


boolean found_status_200 = false;
boolean found_session_id = false;
boolean found_CSV = false;
char *found;
unsigned int successes = 0;
unsigned int failures = 0;
boolean ready_to_update = true;
boolean reading_pachube = false;


boolean request_pause = false;
boolean found_content = false;


unsigned long last_connect;


int content_length;



void setup()
{
  Serial.begin(57600);
  setupEthernet();
  pinMode(geiger_input, INPUT);
  digitalWrite(geiger_input,HIGH);

  attachInterrupt(0,countPulse,FALLING);
}


void loop()
{
  if (millis()-timePreviousMeassure > 10000)
  {

        countPerMinute = count * 6;
        radiationValue = countPerMinute * CONV_FACTOR;
        timePreviousMeassure = millis();
        Serial.print("cpm = ");
        Serial.print(countPerMinute,DEC);
        Serial.print(" - ");
        Serial.print("uSv/h = ");
        Serial.println(radiationValue,4);

   count = 0;


  }


  pachube_in_out();




}




void countPulse()
{
  detachInterrupt(0);
  count++;
  digitalWrite(13,HIGH);
  while(digitalRead(2)==0){}
  digitalWrite(13,LOW);
  attachInterrupt(0,countPulse,FALLING);
}





void setupEthernet(){
  resetEthernetShield();
  Client remoteClient(255);
  delay(500);
  interval = UPDATE_INTERVAL;
  Serial.println("setup complete");
}


void clean_buffer() {
  pointer = 0;
  memset(buff,0,sizeof(buff));
}


void resetEthernetShield(){
  Serial.println("reset ethernet");
        Ethernet.begin(mac, ip, gateway, subnet);
}




void pachube_in_out()
{


  if (millis() < last_connect) last_connect = millis();


  if (request_pause){
        if ((millis() - last_connect) > interval){
          ready_to_update = true;
          reading_pachube = false;
          request_pause = false;
          found_status_200 = false;
          found_session_id = false;
          found_CSV = false;


          //Serial.print("Ready to connect: ");
          //Serial.println(millis());
        }
  }


  if (ready_to_update){
        Serial.println("Connecting...");
        if (localClient.connect()) {


          // here we assign comma-separated values to 'data', which will update Pachube datastreams
          // we use all the analog-in values, but could of course use anything else millis(), digital
          // inputs, etc. . i also like to keep track of successful and failed connection
          // attempts, sometimes useful for determining whether there are major problems.


          sprintf(pachube_data,"%d,%d", countPerMinute, radiationValue);
          content_length = strlen(pachube_data);
          Serial.println("We're connected to Pachube!");


          localClient.print("PUT /api/");
          localClient.print(SHARE_FEED_ID);
          localClient.print(".csv HTTP/1.1\nHost: pachube.com\nX-PachubeApiKey: ");
          localClient.print(PACHUBE_API_KEY);


          localClient.print("\nUser-Agent: Arduino (Pachube In Out v1.1)");
          localClient.print("\nContent-Type: text/csv\nContent-Length: ");
          localClient.print(content_length);
          localClient.print("\nConnection: close\n\n");
          localClient.print(pachube_data);
          localClient.print("\n");

          Serial.print("We've sent ");
          Serial.print(pachube_data);
          Serial.println("to Pachube.");


          ready_to_update = false;
          reading_pachube = true;
          request_pause = false;
          interval = UPDATE_INTERVAL;


          // Serial.print("finished PUT: ");
          // Serial.println(millis());


        }
        else {
          Serial.print("connection failed!");
          Serial.print(++failures);
          found_status_200 = false;
          found_session_id = false;
          found_CSV = false;
          ready_to_update = false;
          reading_pachube = false;
          request_pause = true;
          last_connect = millis();
          interval = RESET_INTERVAL;
          setupEthernet();
        }
  }


  while (reading_pachube){
        while (localClient.available()) {
          checkForResponse();
        }


        if (!localClient.connected()) {
          disconnect_pachube();
        }
  }
}


void disconnect_pachube(){
  Serial.println("disconnecting.\n=====\n\n");
  localClient.stop();
  ready_to_update = false;
  reading_pachube = false;
  request_pause = true;
  last_connect = millis();
  found_content = false;
  resetEthernetShield();
}




void checkForResponse(){
  char c = localClient.read();
  //Serial.print(c);
  buff[pointer] = c;
  if (pointer < 64) pointer++;
  if (c == '\n') {
        found = strstr(buff, "200 OK");
        if (found != 0){
          found_status_200 = true;
          //Serial.println("Status 200");
        }
        buff[pointer]=0;
        found_content = true;
        clean_buffer();
  }


  if ((found_session_id) && (!found_CSV)){
        found = strstr(buff, "HTTP/1.1");
        if (found != 0){
          char csvLine[strlen(buff)-9];
          strncpy (csvLine,buff,strlen(buff)-9);


          //Serial.println("This is the retrieved CSV:");
          //Serial.println("---");
          //Serial.println(csvLine);
          //Serial.println("---");
          Serial.println("\n--- updated: ");
          Serial.println(pachube_data);
          Serial.println("\n--- retrieved: ");
          char delims[] = ",";
          char *result = NULL;
          char * ptr;
          result = strtok_r( buff, delims, &ptr );
          int counter = 0;
          while( result != NULL ) {
            remoteSensor[counter++] = atof(result);
            result = strtok_r( NULL, delims, &ptr );
          }
          for (int i = 0; i < REMOTE_FEED_DATASTREAMS; i++){
            Serial.print( (int)remoteSensor[i]); // because we can't print floats
            Serial.print("\t");
          }


          found_CSV = true;


          Serial.print("\nsuccessful updates=");
          Serial.println(++successes);


        }
  }


  if (found_status_200){
        found = strstr(buff, "_id=");
        if (found != 0){
          clean_buffer();
          found_session_id = true;
        }
  }
}
Something went wrong with that request. Please try again.